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

550 lines
9.9 KiB
Rust
Raw Normal View History

2021-10-24 21:54:08 -07:00
#![cfg(test)]
2021-10-30 19:46:40 -07:00
use pretty_assertions::assert_eq;
2021-10-30 22:45:08 -07:00
use test_case::test_case;
2021-10-24 21:54:08 -07:00
2021-10-27 10:23:51 -07:00
use crate::{
symbol_table::SymbolTable,
tree_walk_eval::{evaluator::Evaluator, State},
type_inference::TypeContext,
};
2021-10-24 21:54:08 -07:00
fn evaluate_input(input: &str) -> Result<String, String> {
let ast = crate::util::quick_ast(input);
let mut symbol_table = SymbolTable::new();
2021-10-27 15:39:09 -07:00
let mut type_context = TypeContext::new();
2021-10-27 10:23:51 -07:00
2021-10-27 15:39:09 -07:00
symbol_table.process_ast(&ast, &mut type_context).unwrap();
2021-10-27 10:23:51 -07:00
2021-10-29 22:03:34 -07:00
let reduced_ir = crate::reduced_ir::reduce(&ast, &symbol_table, &type_context);
reduced_ir.debug(&symbol_table);
println!("========");
symbol_table.debug();
2021-10-27 10:23:51 -07:00
let mut state = State::new();
2021-11-01 01:42:04 -07:00
let mut evaluator = Evaluator::new(&mut state, &type_context);
2021-10-27 10:23:51 -07:00
let mut outputs = evaluator.evaluate(reduced_ir, true);
outputs.pop().unwrap()
2021-10-24 21:54:08 -07:00
}
fn eval_assert(input: &str, expected: &str) {
assert_eq!(evaluate_input(input), Ok(expected.to_string()));
}
2021-10-29 22:03:34 -07:00
fn eval_assert_failure(input: &str, expected: &str) {
assert_eq!(evaluate_input(input), Err(expected.to_string()));
}
2021-10-24 21:54:08 -07:00
#[test]
fn test_basic_eval() {
eval_assert("1 + 2", "3");
eval_assert("let mut a = 1; a = 2", "()");
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() {
2021-11-14 00:34:21 -08:00
eval_assert("-13", "-13");
eval_assert("10 - 2", "8");
2021-10-24 22:44:52 -07:00
}
#[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");
2021-10-24 22:44:52 -07:00
}
#[test]
fn scopes() {
let scope_ok = r#"
2021-10-24 22:44:52 -07:00
let a = 20
fn haha() {
let something = 38
2021-10-24 22:44:52 -07:00
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");
}
2021-11-02 18:07:08 -07:00
#[test]
fn eval_scopes_2() {
eval_assert(
r#"
fn trad() {
let a = 10
fn jinner() {
let b = 20
b
}
a + jinner()
}
trad()"#,
"30",
);
2021-11-20 22:55:11 -08:00
//TODO this shouldn't depend on details of id assignment
2021-11-02 18:07:08 -07:00
let err =
2021-11-20 22:55:11 -08:00
"No symbol found for name: QualifiedName { id: Id { idx: 22, t: PhantomData }, components: [\"a\"] }";
2021-11-02 18:07:08 -07:00
eval_assert_failure(
r#"
fn trad() {
let a = 10
fn inner() {
let b = 20
a + b
}
inner()
}
trad()
"#,
err,
);
}
2021-10-25 16:53:25 -07:00
#[test]
fn adt_output_1() {
let source = r#"
type Option<T> = Some(T) | None
let a = Option::None
let b = Option::Some(10)
2021-10-25 19:57:06 -07:00
(b, a)
2021-10-25 16:53:25 -07:00
"#;
2021-10-25 19:57:06 -07:00
eval_assert(source, "(Some(10), None)");
2021-10-25 16:53:25 -07:00
}
2021-10-29 22:03:34 -07:00
#[test]
fn adt_output_2() {
let source = r#"
type Gobble = Unknown | Rufus { a: Int, torrid: Nat }
let b = Gobble::Rufus { a: 3, torrid: 99 }
b
"#;
eval_assert(source, "Rufus { a: 3, torrid: 99 }");
let source = r#"
type Gobble = Unknown | Rufus { a: Int, torrid: Nat }
let b = Gobble::Rufus { torrid: 3, a: 84 }
b
"#;
eval_assert(source, "Rufus { a: 84, torrid: 3 }");
let source = r#"
type Gobble = Unknown | Rufus { a: Int, torrid: Nat }
let b = Gobble::Rufus { a: 84 }
b
"#;
eval_assert_failure(source, "Field torrid not specified for record Gobble::Rufus");
}
2021-10-25 20:26:53 -07:00
#[test]
fn basic_if_statement() {
let source = r#"
2021-10-25 20:26:53 -07:00
let a = 10
let b = 10
if a == b then { 69 } else { 420 }
"#;
eval_assert(source, "69");
}
2021-10-25 23:01:32 -07:00
#[test]
2021-10-25 23:26:03 -07:00
fn basic_patterns_1() {
let source = r#"
2021-10-25 23:01:32 -07:00
let x = 10
let a = if x is 10 then { 255 } else { 256 }
let b = if 23 is 99 then { 255 } else { 256 }
let c = if true is false then { 9 } else { 10 }
let d = if "xxx" is "yyy" then { 20 } else { 30 }
(a, b, c, d)
"#;
eval_assert(source, "(255, 256, 10, 30)");
}
2021-10-25 23:34:17 -07:00
#[test_case("sanchez", "1")]
#[test_case("mouri", "2")]
#[test_case("hella", "3")]
#[test_case("cyrus", "4")]
fn basic_patterns_2(input: &str, expected: &str) {
let mut source = format!(r#"let x = "{}""#, input);
source.push_str(
r#"
2021-10-25 23:26:03 -07:00
if x {
2021-10-25 23:34:17 -07:00
is "sanchez" then 1
is "mouri" then 2
is "hella" then 3
is _ then 4
2021-10-25 23:26:03 -07:00
}
"#,
);
2021-10-25 23:34:17 -07:00
eval_assert(&source, expected);
2021-10-25 23:26:03 -07:00
}
2021-10-26 00:39:24 -07:00
#[test_case(r#"(45, "panda", false, 2.2)"#, r#""yes""#)]
#[test_case(r#"(99, "panda", false, -2.45)"#, r#""maybe""#)]
fn tuple_patterns(input: &str, expected: &str) {
let mut source = format!("let x = {}", input);
source.push_str(
r#"
2021-10-26 00:39:24 -07:00
if x {
is (45, "pablo", _, 28.4) then "no"
is (_, "panda", _, 2.2) then "yes"
is _ then "maybe"
}"#,
);
2021-10-26 00:39:24 -07:00
eval_assert(&source, expected);
}
2021-11-01 13:46:38 -07:00
#[test]
fn record_patterns_1() {
2021-11-01 13:46:38 -07:00
let source = r#"
type Ara = Kueh { a: Int, b: String } | Morbuk
let alpha = Ara::Kueh { a: 10, b: "sanchez" }
if alpha {
is Ara::Kueh { a, b } then (b, a)
is _ then ("nooo", 8888)
}"#;
eval_assert(source, r#"("sanchez", 10)"#);
}
2021-11-01 13:46:38 -07:00
#[test]
fn record_patterns_2() {
2021-11-01 13:46:38 -07:00
let source = r#"
type Ara = Kueh { a: Int, b: String } | Morbuk
let alpha = Ara::Kueh { a: 10, b: "sanchez" }
if alpha {
is Ara::Kueh { a, b: le_value } then (le_value, (a*2))
is _ then ("nooo", 8888)
}"#;
eval_assert(source, r#"("sanchez", 20)"#);
}
2021-11-01 15:56:20 -07:00
#[test]
fn record_patterns_3() {
2021-11-01 15:56:20 -07:00
let source = r#"
type Vstsavlobs = { tkveni: Int, b: Ia }
type Ia = { sitqva: Int, ghmerts: String }
let b = Vstsavlobs { tkveni: 3, b: Ia::Ia { sitqva: 5, ghmerts: "ooo" } }
2021-11-01 15:56:20 -07:00
if b {
is Vstsavlobs::Vstsavlobs { tkveni: _, b: Ia::Ia { sitqva, ghmerts } } then sitqva
is _ then 5000
}"#;
eval_assert(source, "5");
2021-11-01 13:46:38 -07:00
}
2021-10-25 15:01:03 -07:00
#[test]
fn if_is_patterns() {
let source = r#"
2021-10-25 15:01:03 -07:00
type Option<T> = Some(T) | None
2021-10-26 11:37:43 -07:00
let q = "a string"
2021-10-25 15:01:03 -07:00
let x = Option::Some(9); if x is Option::Some(q) then { q } else { 0 }"#;
eval_assert(source, "9");
2021-10-25 15:01:03 -07:00
let source = r#"
2021-10-25 15:01:03 -07:00
type Option<T> = Some(T) | None
2021-10-26 11:37:43 -07:00
let q = "a string"
let outer = 2
let x = Option::None; if x is Option::Some(q) then { q } else { -2 + outer }"#;
eval_assert(source, "0");
2021-10-25 20:26:53 -07:00
}
2021-10-25 15:01:03 -07:00
2021-10-26 13:02:40 -07:00
#[test]
fn full_if_matching() {
let source = r#"
type Option<T> = Some(T) | None
let a = Option::None
2021-11-14 00:34:21 -08:00
if a { is Option::None then 4; is Option::Some(x) then x }
2021-10-26 13:02:40 -07:00
"#;
eval_assert(source, "4");
let source = r#"
type Option<T> = Some(T) | None
let sara = Option::Some(99)
2021-11-14 00:34:21 -08:00
if sara { is Option::None then 1 + 3; is Option::Some(x) then x }
2021-10-26 13:02:40 -07:00
"#;
eval_assert(source, "99");
let source = r#"
let a = 10
2021-11-14 00:34:21 -08:00
if a { is 10 then "x"; is 4 then "y" }
2021-10-26 13:02:40 -07:00
"#;
eval_assert(source, "\"x\"");
let source = r#"
let a = 10
2021-11-14 00:34:21 -08:00
if a { is 15 then "x"; is 10 then "y" }
2021-10-26 13:02:40 -07:00
"#;
eval_assert(source, "\"y\"");
}
2021-10-26 13:05:42 -07:00
//TODO - I can probably cut down some of these
#[test]
fn string_pattern() {
let source = r#"
let a = "foo"
2021-11-14 00:34:21 -08:00
if a { is "foo" then "x"; is _ then "y" }
2021-10-26 13:05:42 -07:00
"#;
eval_assert(source, "\"x\"");
}
#[test]
fn boolean_pattern() {
let source = r#"
2021-10-26 13:05:42 -07:00
let a = true
if a {
2021-11-14 00:34:21 -08:00
is true then "x"
2021-10-26 13:05:42 -07:00
is false then "y"
}
"#;
eval_assert(source, "\"x\"");
}
#[test]
fn boolean_pattern_2() {
let source = r#"
2021-10-26 13:05:42 -07:00
let a = false
2021-11-14 00:34:21 -08:00
if a { is true then "x"; is false then "y" }
2021-10-26 13:05:42 -07:00
"#;
eval_assert(source, "\"y\"");
}
#[test]
fn ignore_pattern() {
let source = r#"
2021-10-26 13:05:42 -07:00
type Option<T> = Some(T) | None
if Option::Some(10) {
is _ then "hella"
}
"#;
eval_assert(source, "\"hella\"");
}
#[test]
fn tuple_pattern() {
let source = r#"
2021-10-26 13:05:42 -07:00
if (1, 2) {
2021-11-14 00:34:21 -08:00
is (1, x) then x;
2021-10-26 13:05:42 -07:00
is _ then 99
}
"#;
eval_assert(source, "2");
}
#[test]
fn tuple_pattern_2() {
let source = r#"
2021-10-26 13:05:42 -07:00
if (1, 2) {
2021-11-14 00:34:21 -08:00
is (10, x) then x
2021-10-26 13:05:42 -07:00
is (y, x) then x + y
}
"#;
eval_assert(source, "3");
}
#[test]
fn tuple_pattern_3() {
let source = r#"
2021-10-26 13:05:42 -07:00
if (1, 5) {
2021-11-14 00:34:21 -08:00
is (10, x) then x
2021-10-26 13:05:42 -07:00
is (1, x) then x
}
"#;
eval_assert(source, "5");
}
#[test]
fn tuple_pattern_4() {
let source = r#"
2021-10-26 13:05:42 -07:00
if (1, 5) {
2021-11-14 00:34:21 -08:00
is (10, x) then x
is (1, x) then x
2021-10-26 13:05:42 -07:00
}
"#;
eval_assert(source, "5");
}
2021-10-26 13:06:11 -07:00
#[test]
fn prim_obj_pattern() {
let source = r#"
2021-10-26 13:06:11 -07:00
type Stuff = Mulch(Nat) | Jugs(Nat, String) | Mardok
let a = Stuff::Mulch(20)
let b = Stuff::Jugs(1, "haha")
let c = Stuff::Mardok
let x = if a {
2021-11-14 00:34:21 -08:00
is Stuff::Mulch(20) then "x"
2021-10-26 13:06:11 -07:00
is _ then "ERR"
}
let y = if b {
2021-11-14 00:34:21 -08:00
is Stuff::Mulch(n) then "ERR"
is Stuff::Jugs(2, _) then "ERR"
is Stuff::Jugs(1, s) then s
is _ then "ERR"
2021-10-26 13:06:11 -07:00
}
let z = if c {
2021-11-14 00:34:21 -08:00
is Stuff::Jugs(_, _) then "ERR"
is Stuff::Mardok then "NIGH"
is _ then "ERR"
2021-10-26 13:06:11 -07:00
}
(x, y, z)
"#;
eval_assert(source, r#"("x", "haha", "NIGH")"#);
}
#[test]
fn basic_lambda_evaluation_1() {
let source = r#"
let q = \(x, y) { x * y }
let x = q(5, 2)
let y = \(m, n, o) { m + n + o }(1,2,3)
(x, y)
"#;
eval_assert(source, r"(10, 6)");
}
#[test]
fn basic_lambda_evaluation_2() {
let source = r#"
fn milta() {
\(x) { x + 33 }
}
milta()(10)
"#;
eval_assert(source, "43");
}
2021-10-25 15:01:03 -07:00
#[test]
fn import_all() {
let source = r#"
2021-10-25 15:01:03 -07:00
type Option<T> = Some(T) | None
import Option::*
let x = Some(9); if x is Some(q) then { q } else { 0 }"#;
eval_assert(source, "9");
2021-10-25 15:01:03 -07:00
}
2021-10-30 22:45:08 -07:00
#[test]
fn accessors() {
let source = r#"
2021-10-31 02:30:38 -07:00
type Klewos = { a: Int, b: String }
2021-10-30 22:45:08 -07:00
let value = Klewos::Klewos { a: 50, b: "nah" }
(value.a, value.b)
"#;
eval_assert(source, r#"(50, "nah")"#);
}
2021-11-01 01:42:04 -07:00
#[test]
fn early_return() {
let source = r#"
fn chnurmek(a: Int): Int {
if a == 5 then {
return 9999;
}
return (a + 2);
}
(chnurmek(5), chnurmek(0))
2021-11-01 11:16:42 -07:00
"#;
2021-11-01 01:42:04 -07:00
eval_assert(source, r#"(9999, 2)"#);
2021-11-01 11:16:42 -07:00
let source = r#"
fn marbuk(a: Int, b: Int): (Int, Int) {
if a == 5 then {
if b == 6 then {
return (50, 50);
}
return (a, b + 1)
}
(a * 100, b * 100)
}
let x = marbuk(1, 1)
let y = marbuk(5, 1)
let z = marbuk(5, 6)
(x, y, z)
"#;
eval_assert(source, "((100, 100), (5, 2), (50, 50))");
2021-11-01 12:35:25 -07:00
}
#[test]
fn loops() {
let source = r#"
let mut a = 0
let mut count = 0
2021-11-01 12:40:41 -07:00
while a != 5 {
2021-11-01 12:35:25 -07:00
a = a + 1
count = count + 100
}
2021-11-01 11:16:42 -07:00
2021-11-01 12:35:25 -07:00
count
"#;
eval_assert(source, "500");
2021-11-01 01:42:04 -07:00
}
2021-11-02 01:16:08 -07:00
#[test]
fn loops_2() {
let source = r#"
let mut a = 0
let mut acc = 0
while a < 10 {
acc = acc + 1
a = a + 1
// Without this continue, the output would be 20
if a == 5 then {
continue
}
acc = acc + 1
}
acc"#;
eval_assert(source, "19");
}
2021-11-02 21:19:29 -07:00
#[test]
fn list_literals() {
eval_assert(
r#"
let a = [7, 8, 9]
a
"#,
"[7, 8, 9]",
);
2021-11-03 00:01:12 -07:00
eval_assert(
r#"
let a = [7, 8, 9]
fn foo() { return 2 }
(a[0], a[foo()])
"#,
"(7, 9)",
);
2021-11-02 21:19:29 -07:00
}