#![cfg(all(feature = "compile", feature = "verify"))]
extern crate alloc;
use keleusma::Arena;
use keleusma::bytecode::Value;
use keleusma::compiler::compile;
use keleusma::lexer::tokenize;
use keleusma::parser::parse;
use keleusma::vm::{DEFAULT_ARENA_CAPACITY, Vm, VmState};
fn run_word(src: &str) -> i64 {
let m = compile(&parse(&tokenize(src).expect("lex")).expect("parse")).expect("compile");
let arena = Arena::with_capacity(DEFAULT_ARENA_CAPACITY);
let v = {
let mut vm = Vm::new(m, &arena).expect("verify");
match vm.call(&[]).expect("call") {
VmState::Finished(v) => v,
other => panic!("expected finished, got {:?}", other),
}
};
match v {
Value::Int(n) => n,
other => panic!("expected Int, got {:?}", other),
}
}
#[test]
fn generic_tuple_returning_function_specialized_twice() {
let src = "\
fn dup<T>(x: T) -> (T, T) { (x, x) }\n\
fn main() -> Word { \
let p = dup(7); \
let q = dup(3 as Byte); \
p.0 + p.1 + (q.0 as Word) + (q.1 as Word) }";
assert_eq!(run_word(src), 20);
}
#[test]
fn non_generic_composite_still_correct() {
let src = "\
struct P { x: Word, y: Word }\n\
fn main() -> Word { let p = P { x: 4, y: 5 }; p.x + p.y }";
assert_eq!(run_word(src), 9);
}