Added better syntax

This commit is contained in:
GhostedGaming 2024-12-24 13:40:14 -05:00 committed by BitSyndicate
parent cc79b751a7
commit f18a77e1f7
Signed by: bitsyndicate
GPG key ID: 443E4198D6BBA6DE

View file

@ -23,12 +23,12 @@ impl Command for ZLua {
globals.set("add", add)?;
let is_equal = lua.create_function(|_, (list1, list2): (i32, i32)| {
// This function just checks whether two string lists are equal, and in an inefficient way.
// Lua callbacks return `mlua::Result`, an Ok value is a normal return, and an Err return
// turns into a Lua 'error'. Again, any type that is convertible to Lua may be returned.
if list1 == 0 || list2 == 0 {
return Err(mlua::Error::RuntimeError("Zero values not allowed".to_string()));
}
Ok(list1 == list2)
})?;
globals.set("is_equal", is_equal)?;
globals.set("isEqual", is_equal)?;
let log = lua.create_function(|_, (msg,): (String,)| {
println!("{}", msg);
@ -36,7 +36,11 @@ impl Command for ZLua {
})?;
globals.set("log", log)?;
let fail_safe = lua.create_function(|_,()|{
println!("Failed");
Ok(())
})?;
globals.set("failSafe",fail_safe)?;
let mut editor = DefaultEditor::new().expect("Failed to create editor");
loop {
@ -55,6 +59,15 @@ impl Command for ZLua {
match lua.load(&line).eval::<MultiValue>() {
Ok(values) => {
for value in &values {
match value {
mlua::Value::Nil => println!("Got nil value"),
mlua::Value::Number(n) => println!("Got number: {}", n),
mlua::Value::String(s) => println!("Got string: {}", s.to_str()?),
mlua::Value::Boolean(b) => println!("Got boolean: {}", b),
_ => eprintln!("Got unexpected type: {:#?}", value)
}
}
editor.add_history_entry(line).unwrap();
println!(
"{}",
@ -76,9 +89,11 @@ impl Command for ZLua {
}
Err(e) => {
eprintln!("error: {}", e);
eprintln!("Error: {} at line {}", e, line.lines().count());
eprintln!("Input that caused error: {}", line);
break;
}
}
}
}