use std::collections::HashMap;
use std::sync::Arc;
use relon_analyzer::analyze;
use relon_codegen_cranelift::AotEvaluator;
use relon_codegen_llvm::LlvmAotEvaluator;
use relon_eval_api::{Evaluator, Value, ValueDict};
use relon_evaluator::{Context, Scope, TreeWalkEvaluator};
use relon_parser::parse_document;
const SCHEMA_FIELD_SRC: &str = "\
#schema Point {
Int x: *,
Int y: *
}
#main(Point p) -> Int
p.x + p.y
";
fn point_arg(x: i64, y: i64) -> HashMap<String, Value> {
let dict = ValueDict::with_brand(
[
("x".to_string(), Value::Int(x)),
("y".to_string(), Value::Int(y)),
],
Some("Point".to_string()),
);
let mut args = HashMap::new();
args.insert("p".to_string(), Value::Dict(Arc::new(dict)));
args
}
#[test]
fn reference_tree_walk_value() {
let node = parse_document(SCHEMA_FIELD_SRC).expect("parse");
let analyzed = Arc::new(analyze(&node));
let ctx = Context::new()
.with_root(node)
.with_analyzed(Arc::clone(&analyzed));
let ev = TreeWalkEvaluator::new(Arc::new({
let mut ctx = ctx;
TreeWalkEvaluator::prepare_in_place(&mut ctx);
ctx
}));
let out = ev
.run_main(&Arc::new(Scope::default()), point_arg(3, 4))
.expect("tree-walk run_main");
assert_eq!(out, Value::Int(7));
}
#[test]
fn cranelift_schema_field_runs_end_to_end() {
let ev = AotEvaluator::from_source(SCHEMA_FIELD_SRC)
.expect("cranelift LoadSchemaPtr + LoadFieldAtAbsolute both lower: must compile");
let out = ev
.run_main(point_arg(3, 4))
.expect("schema-typed #main arg marshals + runs end to end");
assert_eq!(
out,
Value::Int(7),
"p.x + p.y must read back the gold-standard tree-walk value"
);
}
#[test]
fn llvm_schema_field_runs_end_to_end() {
let ev = LlvmAotEvaluator::from_source(SCHEMA_FIELD_SRC)
.expect("LoadSchemaPtr + LoadFieldAtAbsolute both lower: must compile");
let out = ev
.run_main(point_arg(3, 4))
.expect("schema-typed #main arg marshals + runs end to end");
assert_eq!(
out,
Value::Int(7),
"p.x + p.y must read back the gold-standard tree-walk value"
);
}