2024-11-12 20:40:51 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2024-11-14 19:16:33 +00:00
|
|
|
use talc_lang::{compiler::compile, optimize::optimize, parser, value::Value, vm::Vm};
|
2024-11-12 20:40:51 +00:00
|
|
|
|
|
|
|
fn assert_eval(src: &str, value: Value) {
|
|
|
|
let mut ex = parser::parse(src).expect(&format!("failed to parse expression"));
|
|
|
|
optimize(&mut ex);
|
|
|
|
let f = compile(&ex, None).expect("failed to compile expression");
|
|
|
|
let f = Rc::new(f);
|
|
|
|
let mut vm = Vm::new(16, Vec::new());
|
|
|
|
let res = vm
|
|
|
|
.run_function(f.clone(), vec![f.into()])
|
|
|
|
.expect("vm produced an exception");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
res.get_type().name(),
|
|
|
|
value.get_type().name(),
|
|
|
|
"result and target types differ"
|
|
|
|
);
|
|
|
|
assert_eq!(res, value, "result and target differ");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn scope() {
|
|
|
|
assert_eval(
|
|
|
|
"
|
2024-12-21 05:55:45 +00:00
|
|
|
var x = 7
|
|
|
|
var y = 10
|
|
|
|
do
|
|
|
|
var x = 200
|
|
|
|
y = 100
|
|
|
|
end
|
|
|
|
x + y
|
|
|
|
",
|
|
|
|
Value::from(7 + 100),
|
2024-11-12 20:40:51 +00:00
|
|
|
);
|
|
|
|
assert_eval(
|
|
|
|
"
|
2024-12-21 05:55:45 +00:00
|
|
|
var cond = true
|
|
|
|
var z = 2
|
|
|
|
if cond then var z = 5 else var z = 6 end
|
|
|
|
z
|
|
|
|
",
|
|
|
|
Value::from(2),
|
2024-11-12 20:40:51 +00:00
|
|
|
);
|
|
|
|
assert_eval(
|
|
|
|
"
|
2024-12-21 05:55:45 +00:00
|
|
|
var i = 55
|
|
|
|
var j = 66
|
|
|
|
for i in 0..10 do
|
|
|
|
j = i
|
|
|
|
end
|
|
|
|
i + j
|
|
|
|
",
|
|
|
|
Value::from(55 + 9),
|
2024-11-12 20:40:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn forloop() {
|
|
|
|
assert_eval(
|
|
|
|
"
|
2024-12-21 05:55:45 +00:00
|
|
|
sum = 0
|
|
|
|
for i in 0..5 do
|
|
|
|
for j in 0..10 do
|
|
|
|
sum += i*j
|
|
|
|
end
|
|
|
|
end
|
|
|
|
sum
|
|
|
|
",
|
|
|
|
Value::from(45 * 10),
|
2024-11-12 20:40:51 +00:00
|
|
|
);
|
|
|
|
assert_eval(
|
|
|
|
"
|
2024-12-21 05:55:45 +00:00
|
|
|
map = {a=3, b=2, c=4, d=7}
|
|
|
|
prod = 1
|
|
|
|
for k in map do
|
|
|
|
prod *= map[k]
|
|
|
|
end
|
|
|
|
prod
|
|
|
|
",
|
|
|
|
Value::from(3 * 2 * 4 * 7),
|
2024-11-12 20:40:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn closures() {
|
|
|
|
assert_eval(
|
|
|
|
"
|
2024-12-21 05:55:45 +00:00
|
|
|
var x = 2
|
|
|
|
next = \\->do x = x * 2 + 1 end
|
|
|
|
next() + next() + next()
|
|
|
|
",
|
|
|
|
Value::from(5 + 11 + 23),
|
2024-11-12 20:40:51 +00:00
|
|
|
);
|
|
|
|
assert_eval(
|
|
|
|
"
|
2024-12-21 05:55:45 +00:00
|
|
|
var x = 0
|
|
|
|
fn outer(n) do
|
|
|
|
fn inner() do
|
|
|
|
x += n
|
|
|
|
n += 1
|
|
|
|
x
|
|
|
|
end
|
|
|
|
end
|
2024-11-12 20:40:51 +00:00
|
|
|
|
2024-12-21 05:55:45 +00:00
|
|
|
var f = outer(2)
|
|
|
|
var g = outer(6)
|
|
|
|
f() + f() + g() + g() + f()
|
|
|
|
",
|
|
|
|
Value::from(2 + 5 + 11 + 18 + 22),
|
2024-11-12 20:40:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tailcall() {
|
|
|
|
assert_eval(
|
|
|
|
"
|
2024-12-21 05:55:45 +00:00
|
|
|
fn test(n, a) do
|
|
|
|
if n <= 0 then
|
|
|
|
a
|
|
|
|
else
|
|
|
|
self(n-1, n+a)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
test(24, 0)
|
|
|
|
",
|
|
|
|
Value::from(300),
|
2024-11-12 20:40:51 +00:00
|
|
|
) // = 24 + 23 + ... + 1
|
|
|
|
}
|