Working if statement

partially
This commit is contained in:
GhostedGaming 2024-12-24 03:35:41 -05:00 committed by BitSyndicate
parent 7e940200c6
commit f39df7d29d
2 changed files with 21 additions and 3 deletions

View file

@ -20,6 +20,8 @@ regex = "1.11.1"
rustyline = { version = "15.0.0", features = ["derive", "rustyline-derive"] } rustyline = { version = "15.0.0", features = ["derive", "rustyline-derive"] }
tokio = { version = "1.42.0", features = ["macros", "parking_lot", "rt", "rt-multi-thread"] } tokio = { version = "1.42.0", features = ["macros", "parking_lot", "rt", "rt-multi-thread"] }
rand = "0.8.5"
[profile.dev] [profile.dev]
debug-assertions = true debug-assertions = true

View file

@ -1,4 +1,5 @@
use mlua::{Lua, MultiValue, Number}; use rand::Rng;
use mlua::{Function, Lua, MultiValue, Number, Value::Nil};
use rustyline::{error::ReadlineError, DefaultEditor}; use rustyline::{error::ReadlineError, DefaultEditor};
use crate::core::repl::handler::Command; use crate::core::repl::handler::Command;
@ -11,25 +12,40 @@ impl Command for ZLua {
let prompt = format!("[{}/{}] {}", time, "ZLUA", ">>\t"); let prompt = format!("[{}/{}] {}", time, "ZLUA", ">>\t");
let lua = Lua::new(); let lua = Lua::new();
let globals = lua.globals(); let globals = lua.globals();
//This just adds 2 numbers together
let add = lua.create_function(|_, (number1,number2):(i32,i32)|{ let add = lua.create_function(|_, (number1,number2):(i32,i32)|{
let result = number1 + number2; let result = number1 + number2;
println!("{result}"); println!("{result}");
Ok(()) Ok(())
})?; })?;
globals.set("add", add)?;
let is_equal = lua.create_function(|_, (list1, list2): (i32, i32)| { 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. // 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 // 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. // turns into a Lua 'error'. Again, any type that is convertible to Lua may be returned.
Ok(list1 == list2) Ok(list1 == list2)
})?; })?;
globals.set("isequal", is_equal)?; globals.set("is_equal", is_equal)?;
//This is just true or false
let log = lua.create_function(|_, (msg,): (String,)| { let log = lua.create_function(|_, (msg,): (String,)| {
println!("{}", msg); println!("{}", msg);
Ok(()) Ok(())
})?; })?;
globals.set("log", log)?; globals.set("log", log)?;
let if_statement = lua.create_function(|_, (condition, then_value, else_value): (bool, String, String)| {
if condition {
println!("{}", then_value);
} else {
println!("{}", else_value);
}
Ok(())
})?;
globals.set("if_then", if_statement)?;
let mut editor = DefaultEditor::new().expect("Failed to create editor"); let mut editor = DefaultEditor::new().expect("Failed to create editor");
globals.set("add", add)?;
loop { loop {
let mut prompt = &prompt; let mut prompt = &prompt;