ZLUA REPL!!!!! (#18)

This commit is contained in:
Chance 2024-12-21 16:28:32 -05:00 committed by BitSyndicate
parent f286a2b624
commit 3766da6bcf
Signed by: bitsyndicate
GPG key ID: 443E4198D6BBA6DE
6 changed files with 106 additions and 13 deletions

View file

@ -13,6 +13,7 @@ dirs-next = "2.0.0"
lazy_static.workspace = true
log = "0.4.22"
mlua = { version = "0.10.2", features = ["anyhow", "lua54", "vendored"] }
once_cell = "1.20.2"
parking_lot.workspace = true
regex = "1.11.1"

View file

@ -81,4 +81,4 @@ impl Log for DynamicLogger {
let mut writer = self.writer.lock();
writer.flush().unwrap();
}
}
}

View file

@ -1,8 +1,11 @@
use std::{fs, path::PathBuf, str::FromStr};
use colored::Colorize;
use mlua::prelude::*;
use anyhow::anyhow;
use mlua::{Lua, MultiValue};
use parking_lot::RwLock;
use regex::Regex;
use rustyline::{error::ReadlineError, DefaultEditor};
use super::{handler::Command, input::tokenize};
use crate::core::repl::handler::COMMAND_MANAGER;
@ -187,7 +190,6 @@ impl Command for ExecFile {
}
}
#[derive(Default)]
pub struct CounterCommand {
counter: RwLock<u32>,
@ -260,10 +262,7 @@ impl Command for PanicCommmand {
}
}
fn eval(input: String) -> Result<Vec<(String,Option<Vec<String>>)>, anyhow::Error> {
fn eval(input: String) -> Result<Vec<(String, Option<Vec<String>>)>, anyhow::Error> {
if input.trim().is_empty() {
return Err(anyhow!("Input was empty"));
}
@ -290,8 +289,98 @@ fn eval(input: String) -> Result<Vec<(String,Option<Vec<String>>)>, anyhow::Erro
} else {
None
};
evaluted.push((cmd_name.to_owned(),args));
evaluted.push((cmd_name.to_owned(), args));
}
Ok(evaluted)
}
#[derive(Default)]
pub struct ZLua;
impl Command for ZLua {
fn execute(&self, args: Option<Vec<String>>) -> 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::<MultiValue>() {
Ok(values) => {
editor.add_history_entry(line).unwrap();
println!(
"{}",
values
.iter()
.map(|value| format!("{:#?}", value))
.collect::<Vec<_>>()
.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.")
}
}

View file

@ -1,4 +1,5 @@
use std::collections::HashMap;
use colored::Colorize;
use lazy_static::lazy_static;
use parking_lot::RwLock;

View file

@ -1,4 +1,6 @@
use commands::{ClearCommand, CounterCommand, ExecFile, ExitCommand, HelpCommand, PanicCommmand};
use commands::{
ClearCommand, CounterCommand, ExecFile, ExitCommand, HelpCommand, PanicCommmand, ZLua,
};
use handler::{COMMAND_MANAGER, Category};
use crate::commands;
@ -13,7 +15,8 @@ pub fn setup() {
ClearCommand,
ExitCommand,
CounterCommand,
PanicCommmand
PanicCommmand,
ZLua
);
let cat = Category::new("cr", "Core", "Core commands");
COMMAND_MANAGER.write().add_category(cat.clone());

View file

@ -5,8 +5,8 @@ use core::{
splash, workspace,
};
use anyhow::Ok;
use colored::Colorize;
use mlua::Lua;
use tokio::runtime;
pub mod core;
@ -26,9 +26,8 @@ fn main() -> anyhow::Result<()> {
COMMAND_MANAGER.read().execute("help", None)?;
let t = tokio::spawn(core::repl::input::handle_repl());
t.await??;
Ok(())
Ok::<(), anyhow::Error>(())
})?;
Ok(())
}