Skip to content

Commit

Permalink
Update token_literal implementation for nodes in array.rs, `boolean…
Browse files Browse the repository at this point in the history
….rs`, `call.rs`, `function.rs`, `hash.rs`, `if_expression.rs`, `index.rs`, `infix.rs`, `integer.rs`, `mod.rs`, `prefix.rs`, `string.rs`, `block.rs`, `expression.rs`, `let_statement.rs`, `mod.rs`, `return_statement.rs`, `array.rs`, `boolean.rs`, `built_in_function.rs`, `function.rs`, `hash.rs`, `integer.rs`, `quote.rs`, `mod.rs`, `null.rs`,`return_value.rs`, and `string.rs` files.
  • Loading branch information
DaviRain-Su committed Oct 23, 2023
1 parent a52454a commit 9adbfa0
Show file tree
Hide file tree
Showing 30 changed files with 57 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/ast/expression/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ impl ArrayLiteral {
}

impl NodeInterface for ArrayLiteral {
fn token_literal(&self) -> String {
self.token.literal().into()
fn token_literal(&self) -> &str {
self.token.literal()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ast/expression/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl Display for Boolean {
}

impl NodeInterface for Boolean {
fn token_literal(&self) -> String {
self.token.literal().into()
fn token_literal(&self) -> &str {
self.token.literal()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ast/expression/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl Display for Call {
}

impl NodeInterface for Call {
fn token_literal(&self) -> String {
self.token.literal().into()
fn token_literal(&self) -> &str {
self.token.literal()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ast/expression/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Display for FunctionLiteral {
}

impl NodeInterface for FunctionLiteral {
fn token_literal(&self) -> String {
fn token_literal(&self) -> &str {
self.token.literal().into()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ast/expression/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ impl Display for HashLiteral {
}

impl NodeInterface for HashLiteral {
fn token_literal(&self) -> String {
self.token.literal().into()
fn token_literal(&self) -> &str {
self.token.literal()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ast/expression/if_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ impl Display for If {
}

impl NodeInterface for If {
fn token_literal(&self) -> String {
self.token.literal().into()
fn token_literal(&self) -> &str {
self.token.literal()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ast/expression/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl Index {
}

impl NodeInterface for Index {
fn token_literal(&self) -> String {
self.token.literal().into()
fn token_literal(&self) -> &str {
self.token.literal()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ast/expression/infix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ impl Display for Infix {
}

impl NodeInterface for Infix {
fn token_literal(&self) -> String {
self.token.literal().into()
fn token_literal(&self) -> &str {
self.token.literal()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ast/expression/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ impl Display for IntegerLiteral {
}

impl NodeInterface for IntegerLiteral {
fn token_literal(&self) -> String {
format!("{}", self.value)
fn token_literal(&self) -> &str {
"integer_literal"
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ast/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Display for Expression {
}

impl NodeInterface for Expression {
fn token_literal(&self) -> String {
fn token_literal(&self) -> &str {
match self {
Self::Prefix(pre_exp) => pre_exp.token_literal(),
Self::Infix(infix_exp) => infix_exp.token_literal(),
Expand Down
2 changes: 1 addition & 1 deletion src/ast/expression/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Display for Prefix {
}

impl NodeInterface for Prefix {
fn token_literal(&self) -> String {
fn token_literal(&self) -> &str {
self.right.token_literal()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ast/expression/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl Display for StringLiteral {
}

impl NodeInterface for StringLiteral {
fn token_literal(&self) -> String {
self.token.literal().into()
fn token_literal(&self) -> &str {
self.token.literal()
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Display for Node {
pub trait NodeInterface: Debug + Display {
/// 必须提供 TokenLiteral()方法,该方法返回与其
/// 关联的词法单元的字面量
fn token_literal(&self) -> String;
fn token_literal(&self) -> &str;
}

/// 这个 Program 节点将成为语法分析器生成的每个 AST 的根节点。每个有效的
Expand All @@ -105,9 +105,9 @@ impl Program {
Self::default()
}

pub fn token_literal(&self) -> String {
pub fn token_literal(&self) -> &str {
if self.statements.is_empty() {
String::new()
""
} else {
self.statements
.first()
Expand All @@ -125,7 +125,7 @@ impl Program {
}

pub fn eval_program(&self, env: &mut Environment) -> anyhow::Result<Object> {
trace!("[eval_program] program is ({})", self);
trace!("[eval_program] program is ({self})");
let null = crate::object::null::Null;
let mut result: Object = null.into();

Expand All @@ -135,7 +135,7 @@ impl Program {

match result {
Object::ReturnValue(value) => {
trace!("[eval_statement] ReturnValue is ({:?})", value);
trace!("[eval_statement] ReturnValue is ({value:?})");
return Ok(value.value().clone());
}
_ => continue,
Expand All @@ -147,7 +147,7 @@ impl Program {
}

impl NodeInterface for Program {
fn token_literal(&self) -> String {
fn token_literal(&self) -> &str {
self.token_literal()
}
}
Expand Down Expand Up @@ -189,8 +189,8 @@ impl From<Boolean> for Identifier {
}

impl NodeInterface for Identifier {
fn token_literal(&self) -> String {
self.token.literal().into()
fn token_literal(&self) -> &str {
self.token.literal()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ast/statement/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Display for BlockStatement {
}

impl NodeInterface for BlockStatement {
fn token_literal(&self) -> String {
fn token_literal(&self) -> &str {
self.token.literal().into()
}
}
2 changes: 1 addition & 1 deletion src/ast/statement/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Display for ExpressionStatement {
}

impl NodeInterface for ExpressionStatement {
fn token_literal(&self) -> String {
fn token_literal(&self) -> &str {
self.expression.token_literal()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ast/statement/let_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Default for LetStatement {
}

impl NodeInterface for LetStatement {
fn token_literal(&self) -> String {
fn token_literal(&self) -> &str {
self.token.literal().into()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ast/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum Statement {
}

impl NodeInterface for Statement {
fn token_literal(&self) -> String {
fn token_literal(&self) -> &str {
match self {
Self::Expression(exp_s) => exp_s.token_literal(),
Self::Let(let_s) => let_s.token_literal(),
Expand Down
2 changes: 1 addition & 1 deletion src/ast/statement/return_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Display for ReturnStatement {
}

impl NodeInterface for ReturnStatement {
fn token_literal(&self) -> String {
fn token_literal(&self) -> &str {
self.token.literal().into()
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/evaluator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ impl Node {
Statement::Let(let_statement) => {
let val_node = Node::from(*let_statement.value.clone());
let val = val_node.eval(env)?;

env.store(let_statement.name.value.clone(), val);

Ok(Null.into())
}
Statement::Return(return_statement) => {
Expand All @@ -73,7 +71,6 @@ impl Node {
let left = left_node.eval(env)?;
let right_node = Node::from(infix.right().clone());
let right = right_node.eval(env)?;

left.eval_infix_expression(infix.operator(), right)
}
Expression::IntegerLiteral(integer) => Ok(Integer::new(integer.value()).into()),
Expand All @@ -85,11 +82,10 @@ impl Node {
Expression::FunctionLiteral(function) => {
let params = function.parameters().clone();
let body = function.body().clone();

Ok(Function::new(params, body, env.clone()).into())
}
Expression::Call(call_exp) => {
if call_exp.function().token_literal() == *"quote" {
if call_exp.function().token_literal() == "quote" {
return Node::from(call_exp.arguments()[0].clone()).quote();
}
let call_exp_node = Node::from(call_exp.function().clone());
Expand Down
2 changes: 1 addition & 1 deletion src/object/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Array {
}

impl NodeInterface for Array {
fn token_literal(&self) -> String {
fn token_literal(&self) -> &str {
ARRAY.into()
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/object/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ impl Display for Boolean {
}

impl NodeInterface for Boolean {
fn token_literal(&self) -> String {
self.value.to_string()
fn token_literal(&self) -> &str {
if self.value {
"true"
} else {
"false"
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/object/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ impl ObjectInterface for Builtin {
}

impl NodeInterface for Builtin {
fn token_literal(&self) -> String {
BUILD_FUNC.to_string()
fn token_literal(&self) -> &str {
BUILD_FUNC
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/object/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ impl ObjectInterface for Function {
}

impl NodeInterface for Function {
fn token_literal(&self) -> String {
format!("{self}")
fn token_literal(&self) -> &str {
"function"
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/object/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl ObjectInterface for Hash {
}

impl NodeInterface for Hash {
fn token_literal(&self) -> String {
fn token_literal(&self) -> &str {
HASH.into()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/object/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl Display for Integer {
}

impl NodeInterface for Integer {
fn token_literal(&self) -> String {
self.value.to_string()
fn token_literal(&self) -> &str {
"integer"
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/object/macro/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Display for Quote {
}

impl NodeInterface for Quote {
fn token_literal(&self) -> String {
fn token_literal(&self) -> &str {
QUOTE.into()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl Display for Object {
}

impl NodeInterface for Object {
fn token_literal(&self) -> String {
fn token_literal(&self) -> &str {
match self {
Self::Boolean(value) => value.token_literal(),
Self::Integer(value) => value.token_literal(),
Expand Down
4 changes: 2 additions & 2 deletions src/object/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ impl Display for Null {
}

impl NodeInterface for Null {
fn token_literal(&self) -> String {
format!("{self}")
fn token_literal(&self) -> &str {
NULL
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/object/return_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl Display for ReturnValue {
}

impl NodeInterface for ReturnValue {
fn token_literal(&self) -> String {
format!("{}", self.value)
fn token_literal(&self) -> &str {
self.value.token_literal()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/object/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl ObjectInterface for StringObj {
}

impl NodeInterface for StringObj {
fn token_literal(&self) -> String {
self.value.clone()
fn token_literal(&self) -> &str {
&self.value
}
}

Expand Down

0 comments on commit 9adbfa0

Please sign in to comment.