Skip to content

Commit

Permalink
Don't pass unnecessary Tokens in AST nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
poteto committed Jul 5, 2021
1 parent 33e000b commit 3c62fd2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 47 deletions.
41 changes: 21 additions & 20 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ impl fmt::Display for Program {
#[derive(Clone, Debug, PartialEq)]
pub enum Statement {
Expression(Expression),
Let(Token, Identifier, Expression),
Return(Token, Expression),
Block(Token, Rc<Vec<Statement>>),
Let(Identifier, Expression),
Return(Expression),
Block(Rc<Vec<Statement>>),
}

impl fmt::Display for Statement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Statement::Expression(expression) => expression.fmt(f),
Statement::Let(token, name, value) => write!(
Statement::Let(name, value) => write!(
f,
"{token} {name} = {value};",
token = token,
token = Token::Let,
name = name,
value = value,
),
Statement::Return(token, value) => {
write!(f, "{token} {value};", token = token, value = value)
Statement::Return(value) => {
write!(f, "{token} {value};", token = Token::Return, value = value)
}
Statement::Block(_, statements) => {
Statement::Block(statements) => {
statements
.iter()
.map(|statement| statement.fmt(f))
Expand All @@ -68,27 +68,28 @@ pub enum Expression {
Identifier(Identifier),
Integer(IntegerSize),
String(SymbolU32),
Boolean(Token, bool),
Boolean(bool),

Prefix(Token, Box<Expression>),
Prefix(
Token, // Operator
Box<Expression>, // Right
),
Infix(
Token,
Token, // Operator
Box<Expression>, // Left
Box<Expression>, // Right
),

If(
Token,
Box<Expression>, // Condition
Option<Box<Statement>>, // Consequence
Option<Box<Statement>>, // Alternative
),
Function(
Token,
Rc<Vec<Identifier>>, // Parameters
Rc<Statement>, // Body
),
Call(
Token,
Box<Expression>, // Function
Vec<Expression>, // Arguments
),
Expand All @@ -102,7 +103,7 @@ impl fmt::Display for Expression {
Expression::String(string_key) => {
write!(f, "String({:?})", string_key.to_usize())
}
Expression::Boolean(_, value) => value.fmt(f),
Expression::Boolean(value) => value.fmt(f),
Expression::Prefix(token, right) => {
write!(f, "({operator}{right})", operator = token, right = right)
}
Expand All @@ -113,12 +114,12 @@ impl fmt::Display for Expression {
operator = operator,
right = right
),
Expression::If(token, condition, consequence, alternative) => {
Expression::If(condition, consequence, alternative) => {
if let (condition, Some(consequence)) = (condition, consequence) {
write!(
f,
"{token} {cond} {cons}",
token = token,
token = Token::If,
cond = condition,
cons = consequence
)?;
Expand All @@ -128,18 +129,18 @@ impl fmt::Display for Expression {
}
Ok(())
}
Expression::Function(token, parameters, body) => write!(
Expression::Function(parameters, body) => write!(
f,
"{token}({params}) {{{body}}}",
token = token,
token = Token::Function,
params = parameters
.iter()
.map(|ident| ident.to_string())
.collect::<Vec<String>>()
.join(", "),
body = body
),
Expression::Call(_, function, arguments) => write!(
Expression::Call(function, arguments) => write!(
f,
"{func}({args})",
func = function,
Expand Down
16 changes: 8 additions & 8 deletions src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ impl Interpreter {

fn eval_statement(&mut self, statement: &Statement) -> EvalResult {
match statement {
Statement::Let(_, name, value) => {
Statement::Let(name, value) => {
let value = self.eval_expression(value)?;
self.env.borrow_mut().set(&name.0, Rc::clone(&value));
Ok(value)
}
Statement::Return(_, value) => {
Statement::Return(value) => {
let value = self.eval_expression(value)?;
Ok(Rc::new(IR::ReturnValue(Rc::clone(&value))))
}
Statement::Expression(expression) => self.eval_expression(expression),
Statement::Block(_, statements) => self.eval_program(statements),
Statement::Block(statements) => self.eval_program(statements),
}
}

fn eval_block_statement(&mut self, block_statement: &Statement) -> EvalResult {
let mut result = Rc::new(IR::Nothing);
if let Statement::Block(_, statements) = block_statement {
if let Statement::Block(statements) = block_statement {
for statement in statements.iter() {
let value = self.eval_statement(statement)?;
match &*value {
Expand All @@ -96,7 +96,7 @@ impl Interpreter {
}
}
Expression::Integer(value) => Ok(Rc::new(IR::Integer(*value))),
Expression::Boolean(_, value) => Ok(self.get_interned_bool(value)),
Expression::Boolean(value) => Ok(self.get_interned_bool(value)),
Expression::Prefix(operator, right) => {
let right = self.eval_expression(&right)?;
self.eval_prefix_expression(operator, right)
Expand All @@ -106,15 +106,15 @@ impl Interpreter {
let right = self.eval_expression(right)?;
self.eval_infix_expression(operator, left, right)
}
Expression::If(_, condition, consequence, alternative) => {
Expression::If(condition, consequence, alternative) => {
self.eval_if_expression(condition, consequence, alternative)
}
Expression::Function(_, parameters, body) => Ok(Rc::new(IR::Function(
Expression::Function(parameters, body) => Ok(Rc::new(IR::Function(
Rc::clone(parameters),
Rc::clone(body),
Rc::clone(&self.env),
))),
Expression::Call(_, function, arguments) => {
Expression::Call(function, arguments) => {
let function = self.eval_expression(function)?;
let evaluated_args = arguments
.iter()
Expand Down
28 changes: 9 additions & 19 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ impl<'a> Parser<'a> {
self.expect_peek(Token::Assign)?;
self.next_token();
let statement = Statement::Let(
Token::Let,
Identifier(ident),
self.parse_expression(Precedence::Lowest)?,
);
Expand All @@ -114,8 +113,7 @@ impl<'a> Parser<'a> {

fn parse_return_statement(&mut self) -> Result<Statement, ParserError> {
self.next_token();
let statement =
Statement::Return(Token::Return, self.parse_expression(Precedence::Lowest)?);
let statement = Statement::Return(self.parse_expression(Precedence::Lowest)?);
if self.peek_token == Token::Semicolon {
self.next_token();
}
Expand Down Expand Up @@ -143,7 +141,7 @@ impl<'a> Parser<'a> {
}
self.next_token();
}
Ok(Statement::Block(Token::Lbrace, Rc::new(statements)))
Ok(Statement::Block(Rc::new(statements)))
}

fn parse_expression(&mut self, precedence: Precedence) -> Result<Expression, ParserError> {
Expand Down Expand Up @@ -185,7 +183,6 @@ impl<'a> Parser<'a> {
}
}
Ok(Expression::If(
Token::If,
Box::new(condition),
consequence,
alternative,
Expand All @@ -200,11 +197,7 @@ impl<'a> Parser<'a> {
let parameters = self.parse_function_parameters()?;
self.expect_peek(Token::Lbrace)?;
let body = Rc::new(self.parse_block_statement()?);
Ok(Expression::Function(
Token::Function,
Rc::new(parameters),
body,
))
Ok(Expression::Function(Rc::new(parameters), body))
}

fn parse_function_parameters(&mut self) -> Result<Vec<Identifier>, ParserError> {
Expand Down Expand Up @@ -235,9 +228,7 @@ impl<'a> Parser<'a> {
Ok(Expression::Identifier(Identifier(*identifier_key)))
}
Token::Integer(i) => Ok(Expression::Integer(*i)),
Token::Boolean(value) => {
Ok(Expression::Boolean(Token::Boolean(*value), *value == true))
}
Token::Boolean(value) => Ok(Expression::Boolean(*value == true)),
Token::Bang | Token::Minus => self.parse_prefix_expression(),
Token::Lparen => self.parse_grouped_expression(),
Token::If => self.parse_if_expression(),
Expand Down Expand Up @@ -265,28 +256,27 @@ impl<'a> Parser<'a> {
}

fn parse_prefix_expression(&mut self) -> Result<Expression, ParserError> {
let token = self.curr_token.clone();
let operator = self.curr_token.clone();
self.next_token();
Ok(Expression::Prefix(
token,
operator,
Box::new(self.parse_expression(Precedence::Prefix)?),
))
}

fn parse_infix_expression(&mut self, left: Expression) -> Result<Expression, ParserError> {
let token = self.curr_token.clone();
let precedence = precedence_for(&token);
let operator = self.curr_token.clone();
let precedence = precedence_for(&operator);
self.next_token();
Ok(Expression::Infix(
token,
operator,
Box::new(left),
Box::new(self.parse_expression(precedence)?),
))
}

fn parse_call_expression(&mut self, left: Expression) -> Result<Expression, ParserError> {
Ok(Expression::Call(
Token::Lparen,
Box::new(left),
self.parse_call_arguments()?,
))
Expand Down

0 comments on commit 3c62fd2

Please sign in to comment.