diff --git a/src/interpreter.rs b/src/interpreter.rs index d5e3ce0..8d470c2 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -42,6 +42,43 @@ pub fn evaluate(err_hdl: &mut ErrorHandler, ast: &dyn Interpretable) -> Option Result { + for stmt in self.0.iter() { + stmt.interprete()?; + } + Ok(Value::Nil) + } +} + +/* ------------------------------- * + * INTERPRETER FOR STATEMENT NODES * + * ------------------------------- */ + +impl Interpretable for ast::StmtNode { + fn interprete(&self) -> Result { + match self { + ast::StmtNode::Expression(expr) => expr.interprete(), + ast::StmtNode::Print(expr) => { + let value = expr.interprete()?; + let output = match value { + Value::Nil => String::from("nil"), + Value::Boolean(true) => String::from("true"), + Value::Boolean(false) => String::from("false"), + Value::Number(n) => n.to_string(), + Value::String(s) => s, + }; + println!("{}", output); + Ok(Value::Nil) + } + } + } +} + /* -------------------------------- * * INTERPRETER FOR EXPRESSION NODES * * -------------------------------- */ diff --git a/src/parser.rs b/src/parser.rs index db19aec..f461752 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -75,9 +75,11 @@ impl Parser { fn parse_statement(&mut self) -> Result { if self.expect(&[TokenType::Print]).is_some() { let expression = self.parse_expression()?; + self.consume(&TokenType::Semicolon, "expected ';' after value")?; Ok(ast::StmtNode::Print(expression)) } else { let expression = self.parse_expression()?; + self.consume(&TokenType::Semicolon, "expected ';' after expression")?; Ok(ast::StmtNode::Expression(expression)) } }