mech_interpreter/
interpreter.rsuse crate::*;
pub struct Interpreter {
pub symbols: SymbolTableRef,
pub plan: Plan,
pub functions: FunctionsRef,
}
impl Interpreter {
pub fn new() -> Interpreter {
let mut fxns = Functions::new();
fxns.function_compilers.insert(hash_str("stats/sum/row"),Box::new(StatsSumRow{}));
fxns.function_compilers.insert(hash_str("stats/sum/column"),Box::new(StatsSumColumn{}));
fxns.function_compilers.insert(hash_str("math/sin"),Box::new(MathSin{}));
fxns.function_compilers.insert(hash_str("math/cos"),Box::new(MathCos{}));
fxns.function_compilers.insert(hash_str("math/atan2"),Box::new(MathAtan2{}));
fxns.kinds.insert(hash_str("u8"),ValueKind::U8);
fxns.kinds.insert(hash_str("u16"),ValueKind::U16);
fxns.kinds.insert(hash_str("u32"),ValueKind::U32);
fxns.kinds.insert(hash_str("u64"),ValueKind::U64);
fxns.kinds.insert(hash_str("u128"),ValueKind::U128);
fxns.kinds.insert(hash_str("i8"),ValueKind::I8);
fxns.kinds.insert(hash_str("i16"),ValueKind::I16);
fxns.kinds.insert(hash_str("i32"),ValueKind::I32);
fxns.kinds.insert(hash_str("i64"),ValueKind::I64);
fxns.kinds.insert(hash_str("i128"),ValueKind::I128);
fxns.kinds.insert(hash_str("f32"),ValueKind::F32);
fxns.kinds.insert(hash_str("f64"),ValueKind::F64);
fxns.kinds.insert(hash_str("string"),ValueKind::String);
fxns.kinds.insert(hash_str("bool"),ValueKind::Bool);
Interpreter {
symbols: new_ref(SymbolTable::new()),
plan: new_ref(Vec::new()),
functions: new_ref(fxns),
}
}
pub fn interpret(&mut self, tree: &Program) -> MResult<Value> {
program(tree, self.plan.clone(), self.symbols.clone(), self.functions.clone())
}
}
pub fn program(program: &Program, plan: Plan, symbols: SymbolTableRef, functions: FunctionsRef) -> MResult<Value> {
body(&program.body, plan.clone(), symbols.clone(), functions.clone())
}