From 7e940200c6542b784d830ad804c5ebb4192c8989 Mon Sep 17 00:00:00 2001 From: GhostedGaming Date: Mon, 23 Dec 2024 23:14:11 -0500 Subject: [PATCH] Added a file for zlua and made a new function --- engine/src/core/repl/commands.rs | 91 ----------------------------- engine/src/core/repl/mod.rs | 6 +- engine/src/core/repl/zlua.rs | 98 ++++++++++++++++++++++++++++++++ engine/src/core/splash.rs | 4 ++ 4 files changed, 106 insertions(+), 93 deletions(-) create mode 100644 engine/src/core/repl/zlua.rs diff --git a/engine/src/core/repl/commands.rs b/engine/src/core/repl/commands.rs index f21e58f..55b6447 100644 --- a/engine/src/core/repl/commands.rs +++ b/engine/src/core/repl/commands.rs @@ -293,94 +293,3 @@ fn eval(input: String) -> Result>)>, anyhow::Err } Ok(evaluted) } -#[derive(Default)] -pub struct ZLua; - -impl Command for ZLua { - fn execute(&self, args: Option>) -> Result<(), anyhow::Error> { - let time = chrono::Local::now().format("%H:%M:%S.%3f").to_string(); - let prompt = format!("[{}/{}] {}", time, "ZLUA", ">>\t"); - let lua = Lua::new(); - let globals = lua.globals(); - let sum = 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. - Ok(list1 == list2) - })?; - globals.set("sum", sum)?; - let log = lua.create_function(|_, (msg,): (String,)| { - println!("{}", msg); - Ok(()) - })?; - globals.set("log", log)?; - let mut editor = DefaultEditor::new().expect("Failed to create editor"); - - loop { - let mut prompt = &prompt; - let mut line = String::new(); - - loop { - match editor.readline(prompt) { - Ok(input) => line.push_str(&input), - Err(ReadlineError::Interrupted) => { - println!("Exiting ZLUA shell..."); - return Ok(()); - } - Err(_) => {} - } - - match lua.load(&line).eval::() { - Ok(values) => { - editor.add_history_entry(line).unwrap(); - println!( - "{}", - values - .iter() - .map(|value| format!("{:#?}", value)) - .collect::>() - .join("\t") - ); - break; - } - Err(mlua::Error::SyntaxError { - incomplete_input: true, - .. - }) => { - // continue reading input and append it to `line` - line.push_str("\n"); // separate input lines - prompt = prompt; - } - - Err(e) => { - eprintln!("error: {}", e); - break; - } - } - } - } - } - - fn undo(&self) {} - - fn redo(&self) {} - - fn get_description(&self) -> String { - String::from("Runs the ZLua interpreter") - } - - fn get_name(&self) -> String { - String::from("zlua") - } - - fn get_help(&self) -> String { - String::from("zlua") - } - - fn get_params(&self) -> String { - String::from("No parameters required.") - } -} - - - diff --git a/engine/src/core/repl/mod.rs b/engine/src/core/repl/mod.rs index a1d85e0..d41fe90 100644 --- a/engine/src/core/repl/mod.rs +++ b/engine/src/core/repl/mod.rs @@ -1,13 +1,15 @@ use commands::{ - ClearCommand, CounterCommand, ExecFile, ExitCommand, HelpCommand, PanicCommmand, ZLua, + ClearCommand, CounterCommand, ExecFile, ExitCommand, HelpCommand, PanicCommmand }; use handler::{COMMAND_MANAGER, Category}; +use zlua::ZLua; use crate::commands; pub mod commands; pub mod handler; pub mod input; +pub mod zlua; pub fn setup() { commands!( @@ -16,7 +18,7 @@ pub fn setup() { ExitCommand, CounterCommand, PanicCommmand, - ZLua + zlua::ZLua ); let cat = Category::new("cr", "Core", "Core commands"); COMMAND_MANAGER.write().add_category(cat.clone()); diff --git a/engine/src/core/repl/zlua.rs b/engine/src/core/repl/zlua.rs new file mode 100644 index 0000000..ff911fa --- /dev/null +++ b/engine/src/core/repl/zlua.rs @@ -0,0 +1,98 @@ +use mlua::{Lua, MultiValue, Number}; +use rustyline::{error::ReadlineError, DefaultEditor}; +use crate::core::repl::handler::Command; + +#[derive(Default)] +pub struct ZLua; + +impl Command for ZLua { + fn execute(&self, _args: Option>) -> Result<(), anyhow::Error> { + let time = chrono::Local::now().format("%H:%M:%S.%3f").to_string(); + let prompt = format!("[{}/{}] {}", time, "ZLUA", ">>\t"); + let lua = Lua::new(); + let globals = lua.globals(); + let add = lua.create_function(|_, (number1,number2):(i32,i32)|{ + let result = number1 + number2; + println!("{result}"); + Ok(()) + })?; + 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. + Ok(list1 == list2) + })?; + globals.set("isequal", is_equal)?; + let log = lua.create_function(|_, (msg,): (String,)| { + println!("{}", msg); + Ok(()) + })?; + globals.set("log", log)?; + let mut editor = DefaultEditor::new().expect("Failed to create editor"); + globals.set("add", add)?; + + loop { + let mut prompt = &prompt; + let mut line = String::new(); + + loop { + match editor.readline(prompt) { + Ok(input) => line.push_str(&input), + Err(ReadlineError::Interrupted) => { + println!("Exiting ZLUA shell..."); + return Ok(()); + } + Err(_) => {} + } + + match lua.load(&line).eval::() { + Ok(values) => { + editor.add_history_entry(line).unwrap(); + println!( + "{}", + values + .iter() + .map(|value| format!("{:#?}", value)) + .collect::>() + .join("\t") + ); + break; + } + Err(mlua::Error::SyntaxError { + incomplete_input: true, + .. + }) => { + // continue reading input and append it to `line` + line.push_str("\n"); // separate input lines + prompt = prompt; + } + + Err(e) => { + eprintln!("error: {}", e); + break; + } + } + } + } + } + + fn undo(&self) {} + + fn redo(&self) {} + + fn get_description(&self) -> String { + String::from("Runs the ZLua interpreter") + } + + fn get_name(&self) -> String { + String::from("zlua") + } + + fn get_help(&self) -> String { + String::from("zlua") + } + + fn get_params(&self) -> String { + String::from("No parameters required.") + } +} diff --git a/engine/src/core/splash.rs b/engine/src/core/splash.rs index 94f4b5b..d882c87 100644 --- a/engine/src/core/splash.rs +++ b/engine/src/core/splash.rs @@ -1,6 +1,10 @@ use colored::Colorize; pub fn print_splash() { + println! +("# # +# Welcome to the Zenyx terminal # +# #"); println!( "{}", format!(