use alduin::compiler::compiled_code::Symbol; use alduin::compiler::graph::{BaseGraphBuilder, Graph, Signature, Type}; use alduin::rt::vm::VirtualMachine; use alduin::rt::Value; #[test] fn simple_func_call() { let vm = VirtualMachine::new(); vm.add(Symbol::named("add_one"), { let graph = Graph::new(Signature(vec![Type::I32], Type::I32)); let mut builder = BaseGraphBuilder::new(graph); let start = builder.new_start(); builder.bind(start); let param = builder.new_param::(0); let one = builder.new_constant::(1); let ret = builder.new_add(param, one); builder.new_return(Some(ret)); builder.finalize() }); let code = vm.resolve(&Symbol::named("add_one")); assert_eq!(code.invoke(&[Value::I32(233)]), Some(Value::I32(234))); } #[test] fn chained_func_call_with_lazy_compilation() { let vm = VirtualMachine::new(); vm.add(Symbol::named("add_one"), { let graph = Graph::new(Signature(vec![Type::I32], Type::I32)); let mut builder = BaseGraphBuilder::new(graph); let start = builder.new_start(); builder.bind(start); let param = builder.new_param::(0); let one = builder.new_constant::(1); let ret = builder.new_add(param, one); builder.new_return(Some(ret)); builder.finalize() }); vm.add(Symbol::named("add_two"), { let graph = Graph::new(Signature(vec![Type::I32], Type::I32)); let mut builder = BaseGraphBuilder::new(graph); let start = builder.new_start(); builder.bind(start); let param = builder.new_param::(0); let x = builder.new_call::(Symbol::named("add_one"), &[param.cast()]); let one = builder.new_constant::(1); let ret = builder.new_add(one, x); builder.new_return(Some(ret)); builder.finalize() }); vm.add(Symbol::named("add_three"), { let graph = Graph::new(Signature(vec![Type::I32], Type::I32)); let mut builder = BaseGraphBuilder::new(graph); let start = builder.new_start(); builder.bind(start); let param = builder.new_param::(0); let x = builder.new_call::(Symbol::named("add_two"), &[param.cast()]); let one = builder.new_constant::(1); let ret = builder.new_add(one, x); builder.new_return(Some(ret)); builder.finalize() }); let code = vm.resolve(&Symbol::named("add_three")); assert_eq!(code.invoke(&[Value::I32(233)]), Some(Value::I32(236))); }