Skip to content

Commit

Permalink
update mirror
Browse files Browse the repository at this point in the history
  • Loading branch information
DaviRain-Su committed May 31, 2024
1 parent 4827c18 commit 56c870c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 55 deletions.
27 changes: 2 additions & 25 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,29 @@ use crate::object::environment::Environment;

use crate::object::Object;
use crate::token::Token;
use derive_more::From;
use std::fmt::{Debug, Display, Formatter};

#[derive(Debug, Clone, PartialOrd, PartialEq, Eq, Ord, Hash)]
#[derive(Debug, Clone, PartialOrd, PartialEq, Eq, Ord, Hash, From)]
pub enum Node {
Program(Program),
Expression(Expression),
Statement(Statement), // expression statement, return statement, let statement
Object(Object),
}

impl From<Program> for Node {
fn from(value: Program) -> Self {
Self::Program(value)
}
}

impl From<Expression> for Node {
fn from(value: Expression) -> Self {
Self::Expression(value)
}
}

impl From<&Expression> for Node {
fn from(value: &Expression) -> Self {
Self::Expression(value.clone())
}
}

impl From<Statement> for Node {
fn from(value: Statement) -> Self {
Self::Statement(value)
}
}

impl From<&Statement> for Node {
fn from(value: &Statement) -> Self {
Self::Statement(value.clone())
}
}

impl From<Object> for Node {
fn from(value: Object) -> Self {
Self::Object(value)
}
}

impl From<&Object> for Node {
fn from(value: &Object) -> Self {
Self::Object(value.clone())
Expand Down
30 changes: 3 additions & 27 deletions src/ast/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ pub mod expression;
pub mod let_statement;
pub mod return_statement;

use super::Node;
use crate::ast::statement::block::BlockStatement;
use crate::ast::statement::expression::ExpressionStatement;
use crate::ast::statement::let_statement::LetStatement;
use crate::ast::statement::return_statement::ReturnStatement;
use crate::ast::NodeInterface;
use derive_more::From;
use std::fmt::{Debug, Display, Formatter};

use super::Node;

#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, From)]
pub enum Statement {
Expression(ExpressionStatement),
Let(LetStatement),
Expand Down Expand Up @@ -42,48 +42,24 @@ impl Display for Statement {
}
}

impl From<ExpressionStatement> for Statement {
fn from(value: ExpressionStatement) -> Self {
Self::Expression(value)
}
}

impl From<ExpressionStatement> for Node {
fn from(value: ExpressionStatement) -> Self {
Self::Statement(Statement::Expression(value))
}
}

impl From<LetStatement> for Statement {
fn from(value: LetStatement) -> Self {
Self::Let(value)
}
}

impl From<LetStatement> for Node {
fn from(value: LetStatement) -> Self {
Self::Statement(Statement::Let(value))
}
}

impl From<ReturnStatement> for Statement {
fn from(value: ReturnStatement) -> Self {
Self::Return(value)
}
}

impl From<ReturnStatement> for Node {
fn from(value: ReturnStatement) -> Self {
Self::Statement(Statement::Return(value))
}
}

impl From<BlockStatement> for Statement {
fn from(value: BlockStatement) -> Self {
Self::BlockStatement(value)
}
}

impl From<BlockStatement> for Node {
fn from(value: BlockStatement) -> Self {
Self::Statement(Statement::BlockStatement(value))
Expand Down
4 changes: 2 additions & 2 deletions src/object/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ impl Display for Environment {
for (key, value) in self.store.iter() {
writeln!(f, "{}: {}", key, value)?;
}
writeln!(f, "")?;
writeln!(f,)?;
if self.outer.is_some() {
writeln!(f, "{}", self.outer.as_ref().unwrap())
} else {
writeln!(f, "")
writeln!(f,)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl<'a> Parser<'a> {
let mut stmt = ReturnStatement::new(self.current_token.clone());
self.next_token()?;
// add equal expression
stmt.update_expression(self.parse_expression(LOWEST)?.into());
stmt.update_expression(self.parse_expression(LOWEST)?);
while !self.cur_token_is(TokenType::SEMICOLON) {
self.next_token()?;
}
Expand Down

0 comments on commit 56c870c

Please sign in to comment.