diff --git a/src/ast.rs b/src/ast.rs index 7f98b0f..c900815 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -97,7 +97,7 @@ pub struct Loop { /// This is executed after each loop to advance the condition variables pub advancement: Option, /// The loop body that is executed each loop - pub body: Ast, + pub body: BlockScope, } #[derive(Debug, PartialEq, Eq, Clone)] @@ -105,9 +105,9 @@ pub struct If { /// The condition pub condition: Expression, /// The body that is executed when condition is true - pub body_true: Ast, + pub body_true: BlockScope, /// The if body that is executed when the condition is false - pub body_false: Ast, + pub body_false: BlockScope, } #[derive(Debug, PartialEq, Eq, Clone)] @@ -118,10 +118,7 @@ pub enum Statement { Print(Expression), } -#[derive(Debug, PartialEq, Eq, Clone, Default)] -pub struct Ast { - pub prog: Vec, -} +pub type BlockScope = Vec; impl BinOpType { /// Get the precedence for a binary operator. Higher value means the OP is stronger binding. diff --git a/src/interpreter.rs b/src/interpreter.rs index 5ab17c8..595f498 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -1,7 +1,7 @@ use std::{fmt::Display, rc::Rc}; use crate::{ - ast::{Ast, BinOpType, Expression, If, Statement, UnOpType}, + ast::{BlockScope, BinOpType, Expression, If, Statement, UnOpType}, lexer::lex, parser::parse, }; @@ -63,9 +63,9 @@ impl Interpreter { self.run(&ast); } - pub fn run(&mut self, prog: &Ast) { + pub fn run(&mut self, prog: &BlockScope) { let vartable_len = self.vartable.len(); - for stmt in &prog.prog { + for stmt in prog { match stmt { Statement::Expr(expr) => { self.resolve_expr(expr); diff --git a/src/parser.rs b/src/parser.rs index f0f56b4..75775d2 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,7 +4,7 @@ use crate::ast::*; use crate::token::Token; /// Parse the given tokens into an abstract syntax tree -pub fn parse, A: IntoIterator>(tokens: A) -> Ast { +pub fn parse, A: IntoIterator>(tokens: A) -> BlockScope { let mut parser = Parser::new(tokens); parser.parse() } @@ -22,7 +22,7 @@ impl> Parser { /// Parse tokens into an abstract syntax tree. This will continuously parse statements until /// encountering end-of-file or a block end '}' . - fn parse(&mut self) -> Ast { + fn parse(&mut self) -> BlockScope { let mut prog = Vec::new(); loop { @@ -37,7 +37,7 @@ impl> Parser { } } - Ast { prog } + prog } /// Parse a single statement from the tokens. @@ -92,7 +92,7 @@ impl> Parser { panic!("Error lexing if: Expected '}}'") } - let mut body_false = Ast::default(); + let mut body_false = BlockScope::default(); if matches!(self.peek(), Token::Else) { self.next(); @@ -249,7 +249,7 @@ impl> Parser { mod tests { use super::{parse, BinOpType, Expression}; use crate::{ - parser::{Ast, Statement}, + parser::Statement, token::Token, }; @@ -284,9 +284,7 @@ mod tests { Expression::I64(4).into(), )); - let expected = Ast { - prog: vec![expected], - }; + let expected = vec![expected]; let actual = parse(tokens); assert_eq!(expected, actual);