From 117901cec8cdf5b19d895139f31f7f4300e1cce1 Mon Sep 17 00:00:00 2001 From: Louis Capitanchik <contact@louiscap.co> Date: Tue, 16 May 2023 17:32:58 +0100 Subject: [PATCH] Print statement in interpreter --- .../src/runtime/executor/simple.rs | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/forge-script-lang/src/runtime/executor/simple.rs b/forge-script-lang/src/runtime/executor/simple.rs index 6dbd2c3..13cad9a 100644 --- a/forge-script-lang/src/runtime/executor/simple.rs +++ b/forge-script-lang/src/runtime/executor/simple.rs @@ -1,5 +1,6 @@ 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::value::{ForgeValue, UnsupportedOperation}; @@ -110,7 +111,19 @@ impl Visitor for SimpleExecutor { &mut self, expression: &VoidExpression, ) -> 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> { @@ -154,4 +167,11 @@ mod interpreter_test { let mut vm = SimpleExecutor::default(); 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)); + } } -- GitLab