66 lines
1.9 KiB
Rust
66 lines
1.9 KiB
Rust
use ferrite_lua::{
|
|
compile,
|
|
vm::{RuntimeError, VirtualMachine, value},
|
|
};
|
|
|
|
static TEST: &str = include_str!("../examples/test.lua");
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
pub struct Max;
|
|
impl value::RustFunction for Max {
|
|
fn execute(&self, parameters: Vec<value::Value>) -> Result<Vec<value::Value>, RuntimeError> {
|
|
let lhs = parameters.get(0).cloned().unwrap_or(value::Value::Nil);
|
|
let rhs = parameters.get(1).cloned().unwrap_or(value::Value::Nil);
|
|
match lhs.lt(&rhs)? {
|
|
value::Value::Boolean(value) => Ok(vec![if value.0 { rhs } else { lhs }]),
|
|
_ => Ok(vec![value::Value::Nil]),
|
|
}
|
|
}
|
|
|
|
fn as_indexable(&self) -> String {
|
|
"std::max".to_owned()
|
|
}
|
|
}
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
pub struct Print;
|
|
impl value::RustFunction for Print {
|
|
fn execute(&self, parameters: Vec<value::Value>) -> Result<Vec<value::Value>, RuntimeError> {
|
|
println!("{:?}", parameters);
|
|
Ok(Vec::new())
|
|
}
|
|
|
|
fn as_indexable(&self) -> String {
|
|
"std::print".to_owned()
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let compilation_unit = compile(TEST, None).unwrap();
|
|
|
|
let mut vm = VirtualMachine::default();
|
|
|
|
vm.set_global("max".into(), Max.into());
|
|
vm.set_global("print".into(), Print.into());
|
|
|
|
dbg!(&compilation_unit);
|
|
|
|
let mut runner = compilation_unit.with_virtual_machine(&mut vm).execute();
|
|
|
|
while runner.next().unwrap().is_none() {
|
|
// let inner = compile("print(b)", Some(&compilation_unit)).unwrap();
|
|
// let mut inner_runner = runner.execute(&inner);
|
|
// while {
|
|
// match inner_runner.next() {
|
|
// Ok(Some(_)) => false,
|
|
// Ok(None) => true,
|
|
// Err(e) => {
|
|
// println!("Error: {}", e);
|
|
// false
|
|
// }
|
|
// }
|
|
// } {}
|
|
}
|
|
|
|
dbg!(&vm.get_globals());
|
|
}
|