2024-08-10 23:10:03 +02:00
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
use haku::{
|
2024-08-27 20:43:14 +02:00
|
|
|
ast::{dump::dump, Ast},
|
2024-08-10 23:10:03 +02:00
|
|
|
bytecode::{Chunk, Defs},
|
|
|
|
compiler::{compile_expr, Compiler, Source},
|
2024-08-27 20:43:14 +02:00
|
|
|
lexer::{lex, Lexer},
|
|
|
|
parser::{self, Parser, ParserLimits},
|
|
|
|
source::SourceCode,
|
2024-08-10 23:10:03 +02:00
|
|
|
system::System,
|
2024-08-27 20:43:14 +02:00
|
|
|
token::Lexis,
|
2024-09-01 09:35:26 +02:00
|
|
|
value::{Closure, Ref, RefId, Value},
|
2024-08-10 23:10:03 +02:00
|
|
|
vm::{Vm, VmLimits},
|
|
|
|
};
|
|
|
|
|
|
|
|
fn eval(code: &str) -> Result<Value, Box<dyn Error>> {
|
|
|
|
let mut system = System::new(1);
|
|
|
|
|
2024-08-22 20:27:25 +02:00
|
|
|
let code = SourceCode::unlimited_len(code);
|
2024-08-27 20:43:14 +02:00
|
|
|
|
|
|
|
let mut lexer = Lexer::new(Lexis::new(1024), code);
|
|
|
|
lex(&mut lexer)?;
|
|
|
|
|
|
|
|
let mut ast = Ast::new(1024);
|
|
|
|
let mut parser = Parser::new(&lexer.lexis, &ParserLimits { max_events: 1024 });
|
|
|
|
parser::toplevel(&mut parser);
|
|
|
|
let (root, mut parser_diagnostics) = parser.into_ast(&mut ast)?;
|
|
|
|
println!("{}", dump(&ast, root, Some(code)));
|
2024-08-10 23:10:03 +02:00
|
|
|
let src = Source {
|
|
|
|
code,
|
|
|
|
ast: &ast,
|
|
|
|
system: &system,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut defs = Defs::new(256);
|
|
|
|
let mut chunk = Chunk::new(65536).unwrap();
|
|
|
|
let mut compiler = Compiler::new(&mut defs, &mut chunk);
|
|
|
|
compile_expr(&mut compiler, &src, root)?;
|
2024-08-27 20:43:14 +02:00
|
|
|
let closure_spec = compiler.closure_spec();
|
2024-08-10 23:10:03 +02:00
|
|
|
let defs = compiler.defs;
|
|
|
|
|
2024-08-27 20:43:14 +02:00
|
|
|
let mut diagnostics = lexer.diagnostics;
|
|
|
|
diagnostics.append(&mut parser_diagnostics);
|
|
|
|
diagnostics.append(&mut compiler.diagnostics);
|
|
|
|
|
|
|
|
for diagnostic in &diagnostics {
|
2024-08-10 23:10:03 +02:00
|
|
|
println!(
|
2024-08-27 20:43:14 +02:00
|
|
|
"{}..{} {:?}: {}",
|
|
|
|
diagnostic.span().start,
|
|
|
|
diagnostic.span().end,
|
|
|
|
diagnostic.span().slice(code),
|
|
|
|
diagnostic.message()
|
2024-08-10 23:10:03 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:43:14 +02:00
|
|
|
if !diagnostics.is_empty() {
|
|
|
|
panic!("diagnostics were emitted")
|
2024-08-10 23:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let limits = VmLimits {
|
2024-08-27 20:43:14 +02:00
|
|
|
stack_capacity: 1024,
|
2024-08-10 23:10:03 +02:00
|
|
|
call_stack_capacity: 256,
|
|
|
|
ref_capacity: 256,
|
|
|
|
fuel: 32768,
|
2024-08-20 23:00:39 +02:00
|
|
|
memory: 1024,
|
2024-08-10 23:10:03 +02:00
|
|
|
};
|
|
|
|
let mut vm = Vm::new(defs, &limits);
|
|
|
|
let chunk_id = system.add_chunk(chunk)?;
|
|
|
|
println!("bytecode: {:?}", system.chunk(chunk_id));
|
2024-08-27 20:43:14 +02:00
|
|
|
println!("closure spec: {closure_spec:?}");
|
2024-08-10 23:10:03 +02:00
|
|
|
|
2024-08-27 20:43:14 +02:00
|
|
|
let closure = vm.create_ref(Ref::Closure(Closure::chunk(chunk_id, closure_spec)))?;
|
2024-08-10 23:10:03 +02:00
|
|
|
let result = vm.run(&system, closure)?;
|
|
|
|
|
|
|
|
println!("used fuel: {}", limits.fuel - vm.remaining_fuel());
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[track_caller]
|
|
|
|
fn expect_number(code: &str, number: f32, epsilon: f32) {
|
|
|
|
match eval(code) {
|
|
|
|
Ok(Value::Number(n)) => assert!((n - number).abs() < epsilon, "expected {number}, got {n}"),
|
|
|
|
other => panic!("expected ok/numeric result, got {other:?}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn literal_nil() {
|
|
|
|
assert_eq!(eval("()").unwrap(), Value::Nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn literal_number() {
|
|
|
|
expect_number("123", 123.0, 0.0001);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn literal_bool() {
|
2024-08-27 20:43:14 +02:00
|
|
|
assert_eq!(eval("False").unwrap(), Value::False);
|
|
|
|
assert_eq!(eval("True").unwrap(), Value::True);
|
2024-08-10 23:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_nil() {
|
2024-08-27 20:43:14 +02:00
|
|
|
assert_eq!(
|
|
|
|
eval(r#" \_ -> () "#).unwrap(),
|
|
|
|
Value::Ref(RefId::from_u32(1))
|
|
|
|
);
|
2024-08-10 23:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_nil_call() {
|
2024-08-27 20:43:14 +02:00
|
|
|
assert_eq!(eval(r#"(\_ -> ()) ()"#).unwrap(), Value::Nil);
|
2024-08-10 23:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_arithmetic() {
|
2024-08-27 20:43:14 +02:00
|
|
|
expect_number(r#"(\x -> x + 2) 2"#, 4.0, 0.0001);
|
2024-08-10 23:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_let() {
|
2024-08-27 20:43:14 +02:00
|
|
|
expect_number(r#"(\addTwo -> addTwo 2) \x -> x + 2"#, 4.0, 0.0001);
|
2024-08-10 23:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_closure() {
|
2024-08-27 20:43:14 +02:00
|
|
|
expect_number(r#"((\x -> \y -> x + y) 2) 2"#, 4.0, 0.0001);
|
2024-08-10 23:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_literal() {
|
2024-08-27 20:43:14 +02:00
|
|
|
expect_number("if (1) 1 else 2", 1.0, 0.0001);
|
|
|
|
expect_number("if (()) 1 else 2", 2.0, 0.0001);
|
|
|
|
expect_number("if (False) 1 else 2", 2.0, 0.0001);
|
|
|
|
expect_number("if (True) 1 else 2", 1.0, 0.0001);
|
2024-08-10 23:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_simple() {
|
|
|
|
let code = r#"
|
2024-08-27 20:43:14 +02:00
|
|
|
x = 1
|
|
|
|
y = 2
|
|
|
|
x + y
|
2024-08-10 23:10:03 +02:00
|
|
|
"#;
|
|
|
|
expect_number(code, 3.0, 0.0001);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_fib_recursive() {
|
|
|
|
let code = r#"
|
2024-08-27 20:43:14 +02:00
|
|
|
fib = \n ->
|
|
|
|
if (n < 2)
|
|
|
|
n
|
|
|
|
else
|
|
|
|
fib (n - 1) + fib (n - 2)
|
|
|
|
|
|
|
|
fib 10
|
2024-08-10 23:10:03 +02:00
|
|
|
"#;
|
|
|
|
expect_number(code, 55.0, 0.0001);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_mutually_recursive() {
|
|
|
|
let code = r#"
|
2024-08-27 20:43:14 +02:00
|
|
|
f = \x ->
|
|
|
|
if (x < 10)
|
|
|
|
g (x + 1)
|
|
|
|
else
|
|
|
|
x
|
|
|
|
|
|
|
|
g = \x ->
|
|
|
|
if (x < 10)
|
|
|
|
f (x * 2)
|
|
|
|
else
|
|
|
|
x
|
|
|
|
|
|
|
|
f 0
|
2024-08-10 23:10:03 +02:00
|
|
|
"#;
|
|
|
|
expect_number(code, 14.0, 0.0001);
|
|
|
|
}
|
|
|
|
|
2024-08-22 17:50:44 +02:00
|
|
|
#[test]
|
|
|
|
fn def_botsbuildbots() {
|
2024-08-27 20:43:14 +02:00
|
|
|
let code = r#"
|
|
|
|
botsbuildbots = \_ -> botsbuildbots ()
|
|
|
|
botsbuildbots ()
|
|
|
|
"#;
|
|
|
|
if let Err(error) = eval(code) {
|
2024-08-22 17:50:44 +02:00
|
|
|
assert_eq!(
|
|
|
|
error.to_string(),
|
|
|
|
"Exception {\n message: \"too much recursion\",\n}"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
panic!("error expected");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-10 23:10:03 +02:00
|
|
|
#[test]
|
|
|
|
fn let_single() {
|
|
|
|
let code = r#"
|
2024-08-27 20:43:14 +02:00
|
|
|
let x = 1
|
|
|
|
x + 1
|
2024-08-10 23:10:03 +02:00
|
|
|
"#;
|
|
|
|
expect_number(code, 2.0, 0.0001);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_many() {
|
|
|
|
let code = r#"
|
2024-08-27 20:43:14 +02:00
|
|
|
let x = 1
|
|
|
|
let y = 2
|
|
|
|
x + y
|
2024-08-10 23:10:03 +02:00
|
|
|
"#;
|
|
|
|
expect_number(code, 3.0, 0.0001);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_sequence() {
|
|
|
|
let code = r#"
|
2024-08-27 20:43:14 +02:00
|
|
|
let x = 1
|
|
|
|
let y = x + 1
|
|
|
|
x + y
|
2024-08-10 23:10:03 +02:00
|
|
|
"#;
|
|
|
|
expect_number(code, 3.0, 0.0001);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_subexpr() {
|
|
|
|
let code = r#"
|
2024-08-27 20:43:14 +02:00
|
|
|
(let x = 1
|
|
|
|
let y = 2
|
|
|
|
x * y) + 2
|
2024-08-10 23:10:03 +02:00
|
|
|
"#;
|
2024-08-27 20:43:14 +02:00
|
|
|
expect_number(code, 4.0, 0.0001);
|
2024-08-10 23:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-08-27 20:43:14 +02:00
|
|
|
fn let_subexpr_two() {
|
2024-08-10 23:10:03 +02:00
|
|
|
let code = r#"
|
2024-08-27 20:43:14 +02:00
|
|
|
(let x = 1
|
|
|
|
2) +
|
|
|
|
(let x = 1
|
|
|
|
x)
|
2024-08-10 23:10:03 +02:00
|
|
|
"#;
|
2024-08-27 20:43:14 +02:00
|
|
|
expect_number(code, 3.0, 0.0001);
|
2024-08-10 23:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_subexpr_many() {
|
|
|
|
let code = r#"
|
2024-08-27 20:43:14 +02:00
|
|
|
(let x = 1
|
|
|
|
let y = 2
|
|
|
|
x * y) +
|
|
|
|
(let x = 1
|
|
|
|
2) +
|
|
|
|
(let x = 1
|
|
|
|
x)
|
2024-08-10 23:10:03 +02:00
|
|
|
"#;
|
2024-08-27 20:43:14 +02:00
|
|
|
expect_number(code, 5.0, 0.0001);
|
2024-08-10 23:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn system_arithmetic() {
|
2024-08-27 20:43:14 +02:00
|
|
|
expect_number("1 + 2 + 3 + 4", 10.0, 0.0001);
|
|
|
|
expect_number("(2 * 1) + 1 + (6 / 2) + (10 - 3)", 13.0, 0.0001);
|
2024-08-10 23:10:03 +02:00
|
|
|
}
|
2024-09-02 21:35:52 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_78() {
|
|
|
|
let code = r#"
|
|
|
|
f = \_ ->
|
|
|
|
let x = 1
|
|
|
|
x + x
|
|
|
|
|
|
|
|
[
|
|
|
|
f ()
|
|
|
|
f ()
|
|
|
|
]
|
|
|
|
"#;
|
|
|
|
assert_eq!(eval(code).unwrap(), Value::Ref(RefId::from_u32(2)))
|
|
|
|
}
|