84 lines
2.2 KiB
Rust
84 lines
2.2 KiB
Rust
use ferrite_lua::{
|
|
compile,
|
|
vm::{
|
|
RuntimeError, VirtualMachine,
|
|
value::{self, AsValue},
|
|
},
|
|
};
|
|
|
|
static TEST: &str = include_str!("../examples/test.lua");
|
|
|
|
type UserData = ();
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
pub struct Max;
|
|
impl value::RustFunction<UserData> for Max {
|
|
fn execute(
|
|
&self,
|
|
parameters: Vec<value::Value<UserData>>,
|
|
) -> Result<Vec<value::Value<UserData>>, RuntimeError<UserData>> {
|
|
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<UserData> for Print {
|
|
fn execute(
|
|
&self,
|
|
parameters: Vec<value::Value<UserData>>,
|
|
) -> Result<Vec<value::Value<UserData>>, RuntimeError<UserData>> {
|
|
println!(
|
|
"{}",
|
|
parameters
|
|
.iter()
|
|
.map(|v| format!("{}", v))
|
|
.collect::<Vec<_>>()
|
|
.join("\t")
|
|
);
|
|
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.as_value()).unwrap();
|
|
vm.set_global("print".into(), Print.as_value()).unwrap();
|
|
|
|
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());
|
|
}
|