Skip to content
Snippets Groups Projects
Verified Commit 117901ce authored by Louis's avatar Louis :fire:
Browse files

Print statement in interpreter

parent a9de8dbf
No related branches found
No related tags found
No related merge requests found
Pipeline #495 passed with stages
in 55 seconds
use crate::parse::ast::{ use crate::parse::ast::{
BinaryOp, DeclareFunction, ExpressionList, Program, UnaryOp, ValueExpression, VoidExpression, BinaryOp, DeclareFunction, ExpressionList, Print, Program, UnaryOp, ValueExpression,
VoidExpression,
}; };
use crate::runtime::executor::Visitor; use crate::runtime::executor::Visitor;
use crate::runtime::value::{ForgeValue, UnsupportedOperation}; use crate::runtime::value::{ForgeValue, UnsupportedOperation};
...@@ -110,7 +111,19 @@ impl Visitor for SimpleExecutor { ...@@ -110,7 +111,19 @@ impl Visitor for SimpleExecutor {
&mut self, &mut self,
expression: &VoidExpression, expression: &VoidExpression,
) -> Result<Self::Output, Self::Error> { ) -> Result<Self::Output, Self::Error> {
Err(RuntimeError::Unsupported("Void Expression")) match expression {
VoidExpression::ConditionLoop(_) => {
return Err(RuntimeError::Unsupported("ConditionLoop"))
}
VoidExpression::Import(_) => return Err(RuntimeError::Unsupported("Import")),
VoidExpression::Export(_) => return Err(RuntimeError::Unsupported("Export")),
VoidExpression::Print(Print { expr }) => {
let value = self.evaluate_value_expression(expr.as_ref())?;
println!("{}", value);
}
}
Ok(ForgeValue::Null)
} }
fn evaluate_program(&mut self, program: &Program) -> Result<Self::Output, Self::Error> { fn evaluate_program(&mut self, program: &Program) -> Result<Self::Output, Self::Error> {
...@@ -154,4 +167,11 @@ mod interpreter_test { ...@@ -154,4 +167,11 @@ mod interpreter_test {
let mut vm = SimpleExecutor::default(); let mut vm = SimpleExecutor::default();
assert_eq!(vm.evaluate_program(&add_numbers), Ok(ForgeValue::from(0))); assert_eq!(vm.evaluate_program(&add_numbers), Ok(ForgeValue::from(0)));
} }
#[test]
fn print() {
let print_4 = parse_program("print 2 + 2").expect("Failed to parse");
let mut vm = SimpleExecutor::default();
assert_eq!(vm.evaluate_program(&print_4), Ok(ForgeValue::Null));
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment