From 7a69efc240426b40aad1d836256f5e70f122cdb6 Mon Sep 17 00:00:00 2001 From: Daniel M Date: Sun, 2 Jan 2022 22:02:31 +0100 Subject: [PATCH] Add test for interpreter --- src/interpreter.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/interpreter.rs b/src/interpreter.rs index 19c49c0..e67c1ad 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -1,6 +1,6 @@ use crate::parser::{Ast, BinOpType}; -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq, Clone)] pub enum Value { I64(i64), } @@ -40,3 +40,32 @@ impl Interpreter { } } } + +#[cfg(test)] +mod test { + use crate::parser::{Ast, BinOpType}; + use super::{Interpreter, Value}; + + #[test] + fn test_interpreter_expr() { + // Expression: 1 + 2 * 3 + 4 + // With precedence: (1 + (2 * 3)) + 4 + let ast = Ast::BinOp( + BinOpType::Add, + Ast::BinOp( + BinOpType::Add, + Ast::I64(1).into(), + Ast::BinOp(BinOpType::Mul, Ast::I64(2).into(), Ast::I64(3).into()).into(), + ) + .into(), + Ast::I64(4).into(), + ); + + let expected = Value::I64(11); + + let mut interpreter = Interpreter::new(); + let actual = interpreter.resolve_expr(ast); + + assert_eq!(expected, actual); + } +}