rhai/src/bin/rhai-repl.rs

584 lines
19 KiB
Rust
Raw Normal View History

2022-03-26 11:19:42 +01:00
use rhai::plugin::*;
2022-01-19 07:02:55 +01:00
use rhai::{Dynamic, Engine, EvalAltResult, Module, Scope, AST, INT};
2022-01-30 02:42:04 +01:00
use rustyline::config::Builder;
use rustyline::error::ReadlineError;
use rustyline::{Cmd, Editor, Event, EventHandler, KeyCode, KeyEvent, Modifiers, Movement};
use std::{env, fs::File, io::Read, path::Path, process::exit};
2022-01-30 02:42:04 +01:00
const HISTORY_FILE: &str = ".rhai-repl-history";
2017-10-15 17:50:39 +02:00
2020-12-29 15:04:31 +01:00
/// Pretty-print error.
2021-01-08 07:29:57 +01:00
fn print_error(input: &str, mut err: EvalAltResult) {
2022-02-08 15:29:38 +01:00
let lines: Vec<_> = input.split('\n').collect();
2021-02-28 07:38:34 +01:00
let pos = err.take_position();
let line_no = if lines.len() > 1 {
if pos.is_none() {
2022-08-21 11:35:44 +02:00
String::new()
} else {
format!("{}: ", pos.line().unwrap())
}
} else {
2022-08-21 11:35:44 +02:00
String::new()
};
2020-12-29 15:04:31 +01:00
// Print error position
if pos.is_none() {
// No position
println!("{err}");
} else {
2020-12-29 15:04:31 +01:00
// Specific position - print line text
println!("{line_no}{}", lines[pos.line().unwrap() - 1]);
2022-11-27 11:00:02 +01:00
for (i, err_line) in err.to_string().split('\n').enumerate() {
// Display position marker
println!(
"{0:>1$}{err_line}",
2022-11-27 15:35:17 +01:00
if i > 0 { "| " } else { "^ " },
2022-11-27 11:00:02 +01:00
line_no.len() + pos.position().unwrap() + 1,
);
}
}
}
2020-12-29 15:04:31 +01:00
/// Print help text.
fn print_help() {
println!("help => print this help");
println!("quit, exit => quit");
2022-01-31 14:02:36 +01:00
println!("keys => print list of key bindings");
2022-01-30 02:42:04 +01:00
println!("history => print lines history");
2022-01-31 14:02:36 +01:00
println!("!! => repeat the last history line");
println!("!<#> => repeat a particular history line");
println!("!<text> => repeat the last history line starting with some text");
println!("!?<text> => repeat the last history line containing some text");
2020-06-24 16:45:34 +02:00
println!("scope => print all variables in the scope");
2022-01-19 07:02:55 +01:00
println!("strict => toggle on/off Strict Variables Mode");
2022-01-24 08:50:25 +01:00
#[cfg(not(feature = "no_optimize"))]
println!("optimize => toggle on/off script optimization");
#[cfg(feature = "metadata")]
2020-11-22 10:21:34 +01:00
println!("functions => print all functions defined");
2021-07-08 08:09:31 +02:00
#[cfg(feature = "metadata")]
2022-03-26 11:19:42 +01:00
println!("json => output all functions to `metadata.json`");
2020-12-26 16:21:16 +01:00
println!("ast => print the last AST (optimized)");
2022-02-08 15:30:48 +01:00
#[cfg(not(feature = "no_optimize"))]
println!("astu => print the last raw, un-optimized AST");
2022-01-30 02:42:04 +01:00
println!();
2022-02-08 15:16:12 +01:00
println!("press Ctrl-Enter or end a line with `\\`");
println!("to continue to the next line.");
2022-01-30 02:42:04 +01:00
println!();
}
/// Print key bindings.
fn print_keys() {
println!("Home => move to beginning of line");
println!("Ctrl-Home => move to beginning of input");
println!("End => move to end of line");
println!("Ctrl-End => move to end of input");
println!("Left => move left");
println!("Ctrl-Left => move left by one word");
println!("Right => move right by one word");
println!("Ctrl-Right => move right");
println!("Up => previous line or history");
println!("Ctrl-Up => previous history");
println!("Down => next line or history");
println!("Ctrl-Down => next history");
println!("Ctrl-R => reverse search history");
println!(" (Ctrl-S forward, Ctrl-G cancel)");
println!("Ctrl-L => clear screen");
2022-02-09 01:39:33 +01:00
#[cfg(target_family = "windows")]
2022-01-30 02:42:04 +01:00
println!("Escape => clear all input");
println!("Ctrl-C => exit");
println!("Ctrl-D => EOF (when line empty)");
println!("Ctrl-H, Backspace => backspace");
println!("Ctrl-D, Del => delete character");
println!("Ctrl-U => delete from start");
println!("Ctrl-W => delete previous word");
println!("Ctrl-T => transpose characters");
println!("Ctrl-V => insert special character");
println!("Ctrl-Y => paste yank");
2022-02-09 01:39:33 +01:00
#[cfg(target_family = "unix")]
println!("Ctrl-Z => suspend");
#[cfg(target_family = "windows")]
println!("Ctrl-Z => undo");
2022-01-30 02:42:04 +01:00
println!("Ctrl-_ => undo");
println!("Enter => run code");
println!("Shift-Ctrl-Enter => continue to next line");
println!();
println!("Plus all standard Emacs key bindings");
println!();
}
2022-01-30 02:42:04 +01:00
// Load script files specified in the command line.
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_std"))]
fn load_script_files(engine: &mut Engine) {
// Load init scripts
let mut contents = String::new();
let mut has_init_scripts = false;
2020-12-30 08:37:39 +01:00
2022-01-30 02:42:04 +01:00
for filename in env::args().skip(1) {
let filename = match Path::new(&filename).canonicalize() {
Err(err) => {
eprintln!("Error script file path: {filename}\n{err}");
2022-01-30 02:42:04 +01:00
exit(1);
}
Ok(f) => {
match f.strip_prefix(std::env::current_dir().unwrap().canonicalize().unwrap()) {
Ok(f) => f.into(),
_ => f,
}
2022-01-30 02:42:04 +01:00
}
};
2022-01-30 02:42:04 +01:00
contents.clear();
2021-03-10 16:37:04 +01:00
2022-01-30 02:42:04 +01:00
let mut f = match File::open(&filename) {
Err(err) => {
eprintln!(
"Error reading script file: {}\n{}",
filename.to_string_lossy(),
err
);
2021-03-10 16:37:04 +01:00
exit(1);
2020-12-30 08:37:39 +01:00
}
2022-01-30 02:42:04 +01:00
Ok(f) => f,
};
if let Err(err) = f.read_to_string(&mut contents) {
println!(
"Error reading script file: {}\n{}",
filename.to_string_lossy(),
err
);
exit(1);
}
2020-12-29 15:04:31 +01:00
2022-01-30 02:42:04 +01:00
let module = match engine
.compile(&contents)
.map_err(|err| err.into())
.and_then(|mut ast| {
ast.set_source(filename.to_string_lossy().to_string());
2022-07-27 10:04:24 +02:00
Module::eval_ast_as_new(Scope::new(), &ast, engine)
2022-01-30 02:42:04 +01:00
}) {
Err(err) => {
let filename = filename.to_string_lossy();
2022-01-30 02:42:04 +01:00
eprintln!("{:=<1$}", "", filename.len());
eprintln!("{filename}");
2022-01-30 02:42:04 +01:00
eprintln!("{:=<1$}", "", filename.len());
2022-07-27 10:04:24 +02:00
eprintln!();
2020-12-30 08:37:39 +01:00
2022-01-30 02:42:04 +01:00
print_error(&contents, *err);
exit(1);
}
Ok(m) => m,
};
2020-12-29 15:04:31 +01:00
2022-01-30 02:42:04 +01:00
engine.register_global_module(module.into());
2020-12-29 15:04:31 +01:00
2022-01-30 02:42:04 +01:00
has_init_scripts = true;
2020-12-29 15:04:31 +01:00
2022-01-30 02:42:04 +01:00
println!("Script '{}' loaded.", filename.to_string_lossy());
}
2020-12-29 15:04:31 +01:00
2022-01-30 02:42:04 +01:00
if has_init_scripts {
println!();
2020-12-29 15:04:31 +01:00
}
2022-01-30 02:42:04 +01:00
}
// Setup the Rustyline editor.
fn setup_editor() -> Editor<()> {
2022-02-08 15:16:12 +01:00
//env_logger::init();
2022-01-30 02:42:04 +01:00
let config = Builder::new()
.tab_stop(4)
.indent_size(4)
.bracketed_paste(true)
.build();
2022-07-17 12:49:12 +02:00
let mut rl = Editor::<()>::with_config(config).unwrap();
2022-01-30 02:42:04 +01:00
// Bind more keys
// On Windows, Esc clears the input buffer
#[cfg(target_family = "windows")]
rl.bind_sequence(
2022-04-11 10:32:23 +02:00
Event::KeySeq(vec![KeyEvent(KeyCode::Esc, Modifiers::empty())]),
2022-01-30 02:42:04 +01:00
EventHandler::Simple(Cmd::Kill(Movement::WholeBuffer)),
);
// On Windows, Ctrl-Z is undo
#[cfg(target_family = "windows")]
rl.bind_sequence(
2022-04-11 10:32:23 +02:00
Event::KeySeq(vec![KeyEvent::ctrl('z')]),
2022-01-30 02:42:04 +01:00
EventHandler::Simple(Cmd::Undo(1)),
);
2022-02-08 15:16:12 +01:00
// Map Ctrl-Enter to insert a new line - bypass need for `\` continuation
2022-01-30 02:42:04 +01:00
rl.bind_sequence(
2022-04-11 10:32:23 +02:00
Event::KeySeq(vec![KeyEvent(KeyCode::Char('J'), Modifiers::CTRL)]),
2022-01-30 02:42:04 +01:00
EventHandler::Simple(Cmd::Newline),
);
rl.bind_sequence(
2022-04-11 10:32:23 +02:00
Event::KeySeq(vec![KeyEvent(KeyCode::Enter, Modifiers::CTRL)]),
2022-01-30 02:42:04 +01:00
EventHandler::Simple(Cmd::Newline),
);
// Map Ctrl-Home and Ctrl-End for beginning/end of input
rl.bind_sequence(
2022-04-11 10:32:23 +02:00
Event::KeySeq(vec![KeyEvent(KeyCode::Home, Modifiers::CTRL)]),
2022-01-30 02:42:04 +01:00
EventHandler::Simple(Cmd::Move(Movement::BeginningOfBuffer)),
);
rl.bind_sequence(
2022-04-11 10:32:23 +02:00
Event::KeySeq(vec![KeyEvent(KeyCode::End, Modifiers::CTRL)]),
2022-01-30 02:42:04 +01:00
EventHandler::Simple(Cmd::Move(Movement::EndOfBuffer)),
);
// Map Ctrl-Up and Ctrl-Down to skip up/down the history, even through multi-line histories
rl.bind_sequence(
2022-04-11 10:32:23 +02:00
Event::KeySeq(vec![KeyEvent(KeyCode::Down, Modifiers::CTRL)]),
2022-01-30 02:42:04 +01:00
EventHandler::Simple(Cmd::NextHistory),
);
rl.bind_sequence(
2022-04-11 10:32:23 +02:00
Event::KeySeq(vec![KeyEvent(KeyCode::Up, Modifiers::CTRL)]),
2022-01-30 02:42:04 +01:00
EventHandler::Simple(Cmd::PreviousHistory),
);
// Load the history file
if rl.load_history(HISTORY_FILE).is_err() {
eprintln!("! No previous lines history!");
}
rl
}
2022-03-26 11:19:42 +01:00
#[export_module]
mod sample_functions {
/// This is a sample function.
///
/// It takes two numbers and prints them to a string.
///
/// # Example
///
/// ```rhai
/// let result = test(42, 123);
///
/// print(result); // prints "42 123"
/// ```
pub fn test(x: INT, y: INT) -> String {
2022-08-11 13:01:23 +02:00
format!("{x} {y}")
2022-03-26 11:19:42 +01:00
}
/// This is a sample method for integers.
///
/// # Example
///
/// ```rhai
/// let x = 42;
///
/// x.test(123, "hello");
///
/// print(x); // prints 170
/// ```
#[rhai_fn(name = "test")]
pub fn test2(x: &mut INT, y: INT, z: &str) {
*x += y + (z.len() as INT);
println!("{x} {y} {z}");
2022-03-26 11:19:42 +01:00
}
}
2022-01-30 02:42:04 +01:00
fn main() {
let title = format!("Rhai REPL tool (version {})", env!("CARGO_PKG_VERSION"));
println!("{title}");
2022-01-30 02:42:04 +01:00
println!("{0:=<1$}", "", title.len());
#[cfg(not(feature = "no_optimize"))]
let mut optimize_level = rhai::OptimizationLevel::Simple;
// Initialize scripting engine
let mut engine = Engine::new();
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_std"))]
load_script_files(&mut engine);
2020-12-29 15:04:31 +01:00
// Setup Engine
#[cfg(not(feature = "no_optimize"))]
2021-03-10 16:37:04 +01:00
engine.set_optimization_level(rhai::OptimizationLevel::None);
// Set a file module resolver without caching
#[cfg(not(feature = "no_module"))]
2021-04-04 09:06:13 +02:00
#[cfg(not(feature = "no_std"))]
{
2021-04-03 05:49:16 +02:00
let mut resolver = rhai::module_resolvers::FileModuleResolver::new();
resolver.enable_cache(false);
engine.set_module_resolver(resolver);
}
// Register sample functions
2022-10-29 04:40:07 +02:00
engine.register_global_module(exported_module!(sample_functions).into());
2022-01-17 14:50:36 +01:00
// Create scope
2017-12-20 12:16:14 +01:00
let mut scope = Scope::new();
2017-10-15 17:50:39 +02:00
2022-01-29 02:29:25 +01:00
// REPL line editor setup
2022-01-30 02:42:04 +01:00
let mut rl = setup_editor();
2022-01-29 02:29:25 +01:00
// REPL loop
let mut input = String::new();
2022-01-31 14:02:36 +01:00
let mut replacement = None;
let mut replacement_index = 0;
let mut history_offset = 1;
let mut main_ast = AST::empty();
2022-02-08 15:30:48 +01:00
#[cfg(not(feature = "no_optimize"))]
let mut ast_u = AST::empty();
let mut ast = AST::empty();
2017-10-15 17:50:39 +02:00
2022-01-24 08:50:25 +01:00
print_help();
'main_loop: loop {
2022-01-31 14:02:36 +01:00
if let Some(replace) = replacement.take() {
input = replace;
if rl.add_history_entry(input.clone()) {
history_offset += 1;
}
if input.contains('\n') {
println!("[{replacement_index}] ~~~~");
println!("{input}");
2022-01-31 14:02:36 +01:00
println!("~~~~");
2022-01-30 02:42:04 +01:00
} else {
println!("[{replacement_index}] {input}");
2022-01-31 14:02:36 +01:00
}
replacement_index = 0;
} else {
input.clear();
loop {
2022-04-11 10:32:23 +02:00
let prompt = if input.is_empty() { "repl> " } else { " > " };
2022-01-31 14:02:36 +01:00
match rl.readline(prompt) {
// Line continuation
2022-07-27 10:04:24 +02:00
Ok(mut line) if line.ends_with('\\') => {
2022-01-31 14:02:36 +01:00
line.pop();
2022-02-08 15:29:38 +01:00
input += &line;
2022-01-31 14:02:36 +01:00
input.push('\n');
}
Ok(line) => {
2022-02-08 15:29:38 +01:00
input += &line;
let cmd = input.trim();
2022-07-27 10:04:24 +02:00
if !cmd.is_empty()
&& !cmd.starts_with('!')
&& cmd.trim() != "history"
&& rl.add_history_entry(input.clone())
{
history_offset += 1;
2022-01-31 14:02:36 +01:00
}
break;
2022-01-30 02:42:04 +01:00
}
2022-01-31 14:02:36 +01:00
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break 'main_loop,
2022-01-31 14:02:36 +01:00
Err(err) => {
eprintln!("Error: {err:?}");
2022-01-31 14:02:36 +01:00
break 'main_loop;
}
2022-01-30 02:42:04 +01:00
}
2022-01-29 02:29:25 +01:00
}
2017-12-20 12:16:14 +01:00
}
2017-10-15 17:50:39 +02:00
2022-02-08 15:29:38 +01:00
let cmd = input.trim();
2022-02-08 15:29:38 +01:00
if cmd.is_empty() {
continue;
}
// Implement standard commands
2022-02-08 15:29:38 +01:00
match cmd {
"help" => {
print_help();
continue;
}
2022-01-30 02:42:04 +01:00
"keys" => {
print_keys();
continue;
}
"exit" | "quit" => break, // quit
2022-01-30 02:42:04 +01:00
"history" => {
for (i, h) in rl.history().iter().enumerate() {
match &h.split('\n').collect::<Vec<_>>()[..] {
[line] => println!("[{}] {line}", history_offset + i),
2022-01-30 02:42:04 +01:00
lines => {
for (x, line) in lines.iter().enumerate() {
2022-01-31 14:02:36 +01:00
let number = format!("[{}]", history_offset + i);
2022-01-30 02:42:04 +01:00
if x == 0 {
println!("{number} {}", line.trim_end());
2022-01-30 02:42:04 +01:00
} else {
println!("{0:>1$} {2}", "", number.len(), line.trim_end());
}
}
}
}
}
continue;
}
2022-01-19 07:02:55 +01:00
"strict" if engine.strict_variables() => {
engine.set_strict_variables(false);
println!("Strict Variables Mode turned OFF.");
continue;
}
"strict" => {
engine.set_strict_variables(true);
println!("Strict Variables Mode turned ON.");
continue;
}
2022-01-24 08:50:25 +01:00
#[cfg(not(feature = "no_optimize"))]
"optimize" if optimize_level == rhai::OptimizationLevel::Simple => {
optimize_level = rhai::OptimizationLevel::None;
println!("Script optimization turned OFF.");
continue;
}
#[cfg(not(feature = "no_optimize"))]
"optimize" => {
optimize_level = rhai::OptimizationLevel::Simple;
println!("Script optimization turned ON.");
continue;
}
2020-06-24 16:45:34 +02:00
"scope" => {
println!("{scope}");
2020-06-24 16:45:34 +02:00
continue;
}
2022-02-08 15:30:48 +01:00
#[cfg(not(feature = "no_optimize"))]
"astu" => {
// print the last un-optimized AST
println!("{ast_u:#?}\n");
continue;
}
"ast" => {
// print the last AST
println!("{ast:#?}\n");
continue;
}
#[cfg(feature = "metadata")]
2020-11-22 10:21:34 +01:00
"functions" => {
// print a list of all registered functions
2022-01-28 11:59:18 +01:00
for f in engine.gen_fn_signatures(false) {
println!("{f}")
2022-01-28 11:59:18 +01:00
}
2020-11-22 10:32:10 +01:00
2022-01-28 15:37:59 +01:00
#[cfg(not(feature = "no_function"))]
2022-01-28 11:59:18 +01:00
for f in main_ast.iter_functions() {
println!("{f}")
2022-01-28 11:59:18 +01:00
}
2020-11-22 10:32:10 +01:00
2020-11-22 10:21:34 +01:00
println!();
continue;
}
2021-07-08 08:09:31 +02:00
#[cfg(feature = "metadata")]
"json" => {
2022-03-26 11:19:42 +01:00
use std::io::Write;
let json = engine
.gen_fn_metadata_with_ast_to_json(&main_ast, false)
.unwrap();
let mut f = std::fs::File::create("metadata.json")
.expect("Unable to create `metadata.json`");
f.write_all(json.as_bytes()).expect("Unable to write data");
println!("Functions metadata written to `metadata.json`.");
2021-07-08 08:09:31 +02:00
continue;
}
2022-01-31 14:02:36 +01:00
"!!" => {
2022-10-10 10:46:35 +02:00
match rl.history().last() {
Some(line) => {
replacement = Some(line.clone());
replacement_index = history_offset + rl.history().len() - 1;
}
None => eprintln!("No lines history!"),
2022-01-31 14:02:36 +01:00
}
continue;
}
2022-02-08 15:29:38 +01:00
_ if cmd.starts_with("!?") => {
let text = cmd[2..].trim();
2022-10-10 10:46:35 +02:00
let history = rl
2022-01-31 14:02:36 +01:00
.history()
.iter()
.rev()
.enumerate()
2022-10-10 10:46:35 +02:00
.find(|&(.., h)| h.contains(text));
match history {
Some((n, line)) => {
replacement = Some(line.clone());
replacement_index = history_offset + (rl.history().len() - 1 - n);
}
None => eprintln!("History line not found: {text}"),
2022-01-31 14:02:36 +01:00
}
continue;
}
2022-02-08 15:29:38 +01:00
_ if cmd.starts_with('!') => {
if let Ok(num) = cmd[1..].parse::<usize>() {
2022-01-31 14:02:36 +01:00
if num >= history_offset {
if let Some(line) = rl.history().get(num - history_offset) {
replacement = Some(line.clone());
replacement_index = num;
continue;
}
}
} else {
2022-02-08 15:29:38 +01:00
let prefix = cmd[1..].trim();
2022-01-31 14:02:36 +01:00
if let Some((n, line)) = rl
.history()
.iter()
.rev()
.enumerate()
2022-02-08 02:02:15 +01:00
.find(|&(.., h)| h.trim_start().starts_with(prefix))
2022-01-31 14:02:36 +01:00
{
replacement = Some(line.clone());
replacement_index = history_offset + (rl.history().len() - 1 - n);
continue;
}
}
2022-02-08 15:29:38 +01:00
eprintln!("History line not found: {}", &cmd[1..]);
2022-01-31 14:02:36 +01:00
continue;
}
_ => (),
}
2020-04-13 04:27:08 +02:00
match engine
2022-02-08 15:29:38 +01:00
.compile_with_scope(&scope, &input)
2020-06-16 03:34:30 +02:00
.map_err(Into::into)
.and_then(|r| {
2020-03-22 14:03:58 +01:00
#[cfg(not(feature = "no_optimize"))]
{
2022-02-08 15:30:48 +01:00
ast_u = r.clone();
2022-01-24 08:50:25 +01:00
ast = engine.optimize_ast(&scope, r, optimize_level);
2020-03-22 14:03:58 +01:00
}
#[cfg(feature = "no_optimize")]
{
2020-04-05 11:44:48 +02:00
ast = r;
2020-03-22 14:03:58 +01:00
}
// Merge the AST into the main
main_ast += ast.clone();
// Evaluate
2020-04-13 04:27:08 +02:00
engine.eval_ast_with_scope::<Dynamic>(&mut scope, &main_ast)
}) {
2022-11-09 05:44:57 +01:00
Ok(result) if !result.is_unit() => {
println!("=> {result:?}");
2020-04-13 04:27:08 +02:00
println!();
}
2020-04-16 05:57:08 +02:00
Ok(_) => (),
2020-04-13 04:27:08 +02:00
Err(err) => {
println!();
print_error(&input, *err);
2020-04-13 04:27:08 +02:00
println!();
}
2017-12-20 12:16:14 +01:00
}
2020-04-13 04:27:08 +02:00
// Throw away all the statements, leaving only the functions
main_ast.clear_statements();
2017-12-20 12:16:14 +01:00
}
2022-01-29 02:29:25 +01:00
2022-01-30 02:42:04 +01:00
rl.save_history(HISTORY_FILE).unwrap();
println!("Bye!");
2017-10-15 17:50:39 +02:00
}