#![cfg(all(feature = "compile", feature = "verify"))]
extern crate alloc;
use keleusma::compiler::compile;
use keleusma::lexer::tokenize;
use keleusma::parser::parse;
use keleusma::vm::{DEFAULT_ARENA_CAPACITY, Vm, VmState};
use keleusma::{Arena, Value};
fn text_of(v: &Value, arena: &Arena) -> alloc::string::String {
match v {
Value::KStr(ks) => ks.get(arena).expect("kstr not stale").into(),
Value::StaticStr(s) => s.clone(),
other => panic!("expected text, got {:?}", other),
}
}
#[test]
fn static_text_field_in_flat_struct_round_trips() {
let src = "struct W { s: Text, n: Word }\n\
fn main() -> Text { let w = W { s: \"hello\", n: 7 }; w.s }";
let tokens = tokenize(src).expect("lex error");
let program = parse(&tokens).expect("parse error");
let module = compile(&program).expect("compile error");
let arena = Arena::with_capacity(DEFAULT_ARENA_CAPACITY);
let mut vm = Vm::new(module, &arena).expect("verify");
let val = match vm.call(&[]).expect("vm call") {
VmState::Finished(v) => v,
other => panic!("expected finished, got {:?}", other),
};
assert_eq!(text_of(&val, vm.arena()), "hello");
}
#[test]
fn flat_struct_with_text_and_scalar_reads_following_field() {
let src = "struct W { s: Text, n: Word }\n\
fn main() -> Word { let w = W { s: \"abcd\", n: 42 }; w.n }";
let tokens = tokenize(src).expect("lex error");
let program = parse(&tokens).expect("parse error");
let module = compile(&program).expect("compile error");
let arena = Arena::with_capacity(DEFAULT_ARENA_CAPACITY);
let mut vm = Vm::new(module, &arena).expect("verify");
let val = match vm.call(&[]).expect("vm call") {
VmState::Finished(v) => v,
other => panic!("expected finished, got {:?}", other),
};
assert_eq!(val, Value::Int(42));
}