diff --git a/src/evaluator.rs b/src/evaluator.rs index 7a9fab0..8d7e0c3 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -1,8 +1,11 @@ use crate::ast::{AstNode, Expression, Statement}; +use std::collections::HashMap; use crate::token::Token; #[derive(Default)] -pub struct Evaluator {} +pub struct Evaluator { + context: HashMap +} #[derive(Debug, Clone, PartialEq)] pub enum Object { @@ -14,10 +17,12 @@ pub enum Object { impl Evaluator { pub fn new() -> Self { - Evaluator::default() + Evaluator { + context: HashMap::new() + } } - pub fn eval(&self, node: AstNode) -> Object { + pub fn eval(&mut self, node: AstNode) -> Object { match node { AstNode::Program { statements } => self.eval_program(statements), AstNode::Statement(statement) => match statement { @@ -25,7 +30,16 @@ impl Evaluator { let result_object = self.eval(AstNode::Expression(value)); Object::Return(Box::new(result_object)) } - Statement::LetStatement { .. } => todo!(), + Statement::LetStatement { name, value, .. } => { + let let_name = match name { + Expression::Identifier { token: Token::Identifier(str) } => str, + _ => panic!() + }; + + let result_object = self.eval(AstNode::Expression(value)); + self.context.insert(let_name.clone(), result_object.clone()); + result_object + }, }, AstNode::Expression(expression) => match expression { Expression::Int { @@ -62,13 +76,16 @@ impl Evaluator { } Object::Null + }, + Expression::Identifier { token: Token::Identifier(let_name) } => { + self.context.get(&let_name).expect("ERROR: Could not find identifer").clone() } _ => todo!(), }, } } - fn eval_program(&self, statements: Vec) -> Object { + fn eval_program(&mut self, statements: Vec) -> Object { let mut result = Object::Null; for statement in statements { @@ -81,7 +98,7 @@ impl Evaluator { result } - fn eval_block_statement(&self, statements: Vec) -> Object { + fn eval_block_statement(&mut self, statements: Vec) -> Object { let mut result = Object::Null; for statement in statements { diff --git a/src/main.rs b/src/main.rs index 67b4655..34a8afc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use kl_rs::{evaluator::Evaluator, lexer::Lexer, parser::Parser, token::Token}; fn main() { let stdin = std::io::stdin(); let mut handle = stdin.lock(); + let mut evaluator = Evaluator::new(); loop { let mut input = String::new(); @@ -58,7 +59,6 @@ fn main() { println!(); println!("EVALUATED: "); - let evaluator = Evaluator::new(); let object = evaluator.eval(program); println!("{}", object.inspect()); }