Add test for interpreter

This commit is contained in:
Daniel M 2022-01-02 22:02:31 +01:00
parent 39dd5e81f2
commit 7a69efc240

View File

@ -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);
}
}