schala/schala-lang/language/src/tree_walk_eval/test.rs

66 lines
1.3 KiB
Rust
Raw Normal View History

2021-10-24 21:54:08 -07:00
#![cfg(test)]
use crate::symbol_table::SymbolTable;
use crate::tree_walk_eval::State;
fn evaluate_input(input: &str) -> Result<String, String> {
let ast = crate::util::quick_ast(input);
let mut symbol_table = SymbolTable::new();
symbol_table.process_ast(&ast).unwrap();
let reduced_ir = crate::reduced_ir::reduce(&ast, &symbol_table);
2021-10-24 23:05:47 -07:00
reduced_ir.debug(&symbol_table);
2021-10-24 21:54:08 -07:00
let mut state = State::new();
let mut outputs = state.evaluate(reduced_ir, true);
outputs.pop().unwrap()
}
fn eval_assert(input: &str, expected: &str) {
assert_eq!(evaluate_input(input), Ok(expected.to_string()));
}
#[test]
fn test_basic_eval() {
eval_assert("1 + 2", "3");
2021-10-24 22:55:12 -07:00
eval_assert("let mut a = 1; a = 2", "()");
2021-10-24 23:05:47 -07:00
eval_assert("let mut a = 1; a = a + 2; a", "3");
2021-10-24 21:54:08 -07:00
}
2021-10-24 22:44:52 -07:00
#[test]
fn op_eval() {
eval_assert("- 13", "-13");
eval_assert("10 - 2", "8");
}
#[test]
fn function_eval() {
eval_assert("fn oi(x) { x + 1 }; oi(4)", "5");
eval_assert("fn oi(x) { x + 1 }; oi(1+2)", "4");
}
#[test]
fn scopes() {
/*
let scope_ok = r#"
let a = 20
fn haha() {
let a = 10
a
}
haha()
"#;
eval_assert(scope_ok, "10");
let scope_ok = r#"
let a = 20
fn queque() {
let a = 10
a
}
a
"#;
eval_assert(scope_ok, "20");
*/
}