2021-04-03 05:49:16 +02:00
|
|
|
use rhai::{Dynamic, Engine, EvalAltResult, Module, Scope, AST};
|
2020-03-15 15:39:58 +01:00
|
|
|
|
2020-12-29 15:04:31 +01:00
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
fs::File,
|
|
|
|
io::{stdin, stdout, Read, Write},
|
2021-04-02 06:34:39 +02:00
|
|
|
path::Path,
|
2020-12-29 15:04:31 +01:00
|
|
|
process::exit,
|
|
|
|
};
|
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) {
|
2020-03-24 10:30:04 +01:00
|
|
|
let lines: Vec<_> = input.trim().split('\n').collect();
|
2021-02-28 07:38:34 +01:00
|
|
|
let pos = err.take_position();
|
2020-03-21 10:14:31 +01:00
|
|
|
|
|
|
|
let line_no = if lines.len() > 1 {
|
2020-06-12 12:04:16 +02:00
|
|
|
if pos.is_none() {
|
|
|
|
"".to_string()
|
|
|
|
} else {
|
|
|
|
format!("{}: ", pos.line().unwrap())
|
2020-03-21 10:14:31 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
};
|
2020-03-10 04:25:34 +01:00
|
|
|
|
2020-12-29 15:04:31 +01:00
|
|
|
// Print error position
|
2020-06-12 12:04:16 +02:00
|
|
|
if pos.is_none() {
|
|
|
|
// No position
|
|
|
|
println!("{}", err);
|
|
|
|
} else {
|
2020-12-29 15:04:31 +01:00
|
|
|
// Specific position - print line text
|
2020-06-12 12:04:16 +02:00
|
|
|
println!("{}{}", line_no, lines[pos.line().unwrap() - 1]);
|
|
|
|
|
2020-12-29 15:04:31 +01:00
|
|
|
// Display position marker
|
2020-06-12 12:04:16 +02:00
|
|
|
println!(
|
|
|
|
"{0:>1$} {2}",
|
|
|
|
"^",
|
|
|
|
line_no.len() + pos.position().unwrap(),
|
2021-01-08 07:29:57 +01:00
|
|
|
err
|
2020-06-12 12:04:16 +02:00
|
|
|
);
|
2020-03-10 04:25:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 15:04:31 +01:00
|
|
|
/// Print help text.
|
2020-03-21 10:14:31 +01:00
|
|
|
fn print_help() {
|
|
|
|
println!("help => print this help");
|
|
|
|
println!("quit, exit => quit");
|
2020-06-24 16:45:34 +02:00
|
|
|
println!("scope => print all variables in the scope");
|
2021-03-24 12:27:38 +01:00
|
|
|
#[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")]
|
|
|
|
println!("json => output all functions in JSON format");
|
2020-12-26 16:21:16 +01:00
|
|
|
println!("ast => print the last AST (optimized)");
|
2020-03-22 02:25:41 +01:00
|
|
|
println!("astu => print the last raw, un-optimized AST");
|
2020-03-21 10:14:31 +01:00
|
|
|
println!(r"end a line with '\' to continue to the next line.");
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
|
2020-03-10 04:25:34 +01:00
|
|
|
fn main() {
|
2021-04-21 11:39:45 +02:00
|
|
|
let title = format!("Rhai REPL tool (version {})", env!("CARGO_PKG_VERSION"));
|
|
|
|
println!("{}", title);
|
|
|
|
println!("{0:=<1$}", "", title.len());
|
2020-12-29 15:04:31 +01:00
|
|
|
print_help();
|
|
|
|
|
2021-03-10 16:01:04 +01:00
|
|
|
// Initialize scripting engine
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2020-12-30 08:37:39 +01:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2021-03-23 05:13:53 +01:00
|
|
|
#[cfg(not(feature = "no_std"))]
|
2020-12-30 08:37:39 +01:00
|
|
|
{
|
2021-03-10 16:37:04 +01:00
|
|
|
// Load init scripts
|
2020-12-30 08:37:39 +01:00
|
|
|
let mut contents = String::new();
|
|
|
|
let mut has_init_scripts = false;
|
|
|
|
|
|
|
|
for filename in env::args().skip(1) {
|
2021-04-02 06:34:39 +02:00
|
|
|
let filename = match Path::new(&filename).canonicalize() {
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("Error script file path: {}\n{}", filename, err);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-07-14 12:32:22 +02:00
|
|
|
Ok(f) => {
|
|
|
|
match f.strip_prefix(std::env::current_dir().unwrap().canonicalize().unwrap()) {
|
|
|
|
Ok(f) => f.into(),
|
|
|
|
_ => f,
|
|
|
|
}
|
|
|
|
}
|
2021-04-02 06:34:39 +02:00
|
|
|
};
|
|
|
|
|
2021-03-10 16:37:04 +01:00
|
|
|
contents.clear();
|
|
|
|
|
|
|
|
let mut f = match File::open(&filename) {
|
|
|
|
Err(err) => {
|
2021-04-02 06:34:39 +02:00
|
|
|
eprintln!(
|
|
|
|
"Error reading script file: {}\n{}",
|
|
|
|
filename.to_string_lossy(),
|
|
|
|
err
|
|
|
|
);
|
2020-12-30 08:37:39 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2021-03-10 16:37:04 +01:00
|
|
|
Ok(f) => f,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = f.read_to_string(&mut contents) {
|
2021-04-02 06:34:39 +02:00
|
|
|
println!(
|
|
|
|
"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
|
|
|
}
|
2020-12-29 15:04:31 +01:00
|
|
|
|
2021-01-03 06:30:01 +01:00
|
|
|
let module = match engine
|
2020-12-30 08:37:39 +01:00
|
|
|
.compile(&contents)
|
|
|
|
.map_err(|err| err.into())
|
2021-01-03 06:30:01 +01:00
|
|
|
.and_then(|mut ast| {
|
2021-05-08 16:59:33 +02:00
|
|
|
ast.set_source(filename.to_string_lossy().to_string());
|
2021-01-03 06:30:01 +01:00
|
|
|
Module::eval_ast_as_new(Default::default(), &ast, &engine)
|
|
|
|
}) {
|
2020-12-29 15:04:31 +01:00
|
|
|
Err(err) => {
|
2021-04-02 06:34:39 +02:00
|
|
|
let filename = filename.to_string_lossy();
|
|
|
|
|
2020-12-30 08:37:39 +01:00
|
|
|
eprintln!("{:=<1$}", "", filename.len());
|
|
|
|
eprintln!("{}", filename);
|
|
|
|
eprintln!("{:=<1$}", "", filename.len());
|
|
|
|
eprintln!("");
|
|
|
|
|
|
|
|
print_error(&contents, *err);
|
2020-12-29 15:04:31 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2020-12-30 08:37:39 +01:00
|
|
|
Ok(m) => m,
|
2020-12-29 15:04:31 +01:00
|
|
|
};
|
|
|
|
|
2020-12-30 08:37:39 +01:00
|
|
|
engine.register_global_module(module.into());
|
2020-12-29 15:04:31 +01:00
|
|
|
|
2020-12-30 08:37:39 +01:00
|
|
|
has_init_scripts = true;
|
2020-12-29 15:04:31 +01:00
|
|
|
|
2021-04-02 06:34:39 +02:00
|
|
|
println!("Script '{}' loaded.", filename.to_string_lossy());
|
2020-12-30 08:37:39 +01:00
|
|
|
}
|
2020-12-29 15:04:31 +01:00
|
|
|
|
2020-12-30 08:37:39 +01:00
|
|
|
if has_init_scripts {
|
|
|
|
println!();
|
|
|
|
}
|
2020-12-29 15:04:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup Engine
|
2020-03-15 15:39:58 +01:00
|
|
|
#[cfg(not(feature = "no_optimize"))]
|
2021-03-10 16:37:04 +01:00
|
|
|
engine.set_optimization_level(rhai::OptimizationLevel::None);
|
2020-03-15 15:39:58 +01:00
|
|
|
|
2021-04-02 06:34:39 +02:00
|
|
|
// Set a file module resolver without caching
|
2021-04-02 13:26:55 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2021-04-04 09:06:13 +02:00
|
|
|
#[cfg(not(feature = "no_std"))]
|
2021-04-02 13:26:55 +02:00
|
|
|
{
|
2021-04-03 05:49:16 +02:00
|
|
|
let mut resolver = rhai::module_resolvers::FileModuleResolver::new();
|
2021-04-02 13:26:55 +02:00
|
|
|
resolver.enable_cache(false);
|
|
|
|
engine.set_module_resolver(resolver);
|
|
|
|
}
|
2021-04-02 06:34:39 +02:00
|
|
|
|
|
|
|
// Make Engine immutable
|
|
|
|
let engine = engine;
|
|
|
|
|
|
|
|
// Create scope
|
2017-12-20 12:16:14 +01:00
|
|
|
let mut scope = Scope::new();
|
2017-10-15 17:50:39 +02:00
|
|
|
|
2021-04-02 06:34:39 +02:00
|
|
|
// REPL loop
|
2020-03-10 04:25:34 +01:00
|
|
|
let mut input = String::new();
|
2021-09-12 07:21:53 +02:00
|
|
|
let mut main_ast: AST = Default::default();
|
|
|
|
let mut ast_u: AST = Default::default();
|
|
|
|
let mut ast: AST = Default::default();
|
2017-10-15 17:50:39 +02:00
|
|
|
|
2020-07-27 06:52:32 +02:00
|
|
|
'main_loop: loop {
|
2020-12-26 08:41:41 +01:00
|
|
|
print!("rhai-repl> ");
|
2017-12-20 12:16:14 +01:00
|
|
|
stdout().flush().expect("couldn't flush stdout");
|
2020-03-02 08:19:59 +01:00
|
|
|
|
2020-03-10 04:25:34 +01:00
|
|
|
input.clear();
|
|
|
|
|
2020-03-21 10:14:31 +01:00
|
|
|
loop {
|
2020-07-27 06:52:32 +02:00
|
|
|
match stdin().read_line(&mut input) {
|
|
|
|
Ok(0) => break 'main_loop,
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => panic!("input error: {}", err),
|
2020-03-21 10:14:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let line = input.as_str().trim_end();
|
|
|
|
|
|
|
|
// Allow line continuation
|
|
|
|
if line.ends_with('\\') {
|
|
|
|
let len = line.len();
|
|
|
|
input.truncate(len - 1);
|
|
|
|
input.push('\n');
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
print!("> ");
|
|
|
|
stdout().flush().expect("couldn't flush stdout");
|
2017-12-20 12:16:14 +01:00
|
|
|
}
|
2017-10-15 17:50:39 +02:00
|
|
|
|
2020-03-21 10:14:31 +01:00
|
|
|
let script = input.trim();
|
|
|
|
|
2020-04-04 16:00:44 +02:00
|
|
|
if script.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-03-12 08:31:01 +01:00
|
|
|
// Implement standard commands
|
2020-03-21 10:14:31 +01:00
|
|
|
match script {
|
|
|
|
"help" => {
|
|
|
|
print_help();
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-12 08:31:01 +01:00
|
|
|
"exit" | "quit" => break, // quit
|
2020-06-24 16:45:34 +02:00
|
|
|
"scope" => {
|
2020-10-09 07:22:40 +02:00
|
|
|
scope
|
|
|
|
.iter_raw()
|
|
|
|
.enumerate()
|
|
|
|
.for_each(|(i, (name, constant, value))| {
|
2021-04-17 06:03:29 +02:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2021-07-10 04:11:14 +02:00
|
|
|
let value_is_shared = if value.is_shared() { " (shared)" } else { "" };
|
2021-04-17 06:03:29 +02:00
|
|
|
#[cfg(feature = "no_closure")]
|
|
|
|
let value_is_shared = "";
|
|
|
|
|
2020-10-09 07:22:40 +02:00
|
|
|
println!(
|
|
|
|
"[{}] {}{}{} = {:?}",
|
|
|
|
i + 1,
|
|
|
|
if constant { "const " } else { "" },
|
|
|
|
name,
|
2021-04-17 06:03:29 +02:00
|
|
|
value_is_shared,
|
2020-10-09 07:22:40 +02:00
|
|
|
*value.read_lock::<Dynamic>().unwrap(),
|
|
|
|
)
|
|
|
|
});
|
2020-10-27 16:36:05 +01:00
|
|
|
println!();
|
2020-06-24 16:45:34 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-03-22 02:25:41 +01:00
|
|
|
"astu" => {
|
2020-04-04 16:00:44 +02:00
|
|
|
// print the last un-optimized AST
|
2020-12-26 08:41:41 +01:00
|
|
|
println!("{:#?}\n", ast_u);
|
2020-03-22 02:25:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-03-12 08:31:01 +01:00
|
|
|
"ast" => {
|
2020-04-04 16:00:44 +02:00
|
|
|
// print the last AST
|
2020-12-26 08:41:41 +01:00
|
|
|
println!("{:#?}\n", ast);
|
2020-03-12 08:31:01 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-24 12:27:38 +01:00
|
|
|
#[cfg(feature = "metadata")]
|
2020-11-22 10:21:34 +01:00
|
|
|
"functions" => {
|
|
|
|
// print a list of all registered functions
|
|
|
|
engine
|
|
|
|
.gen_fn_signatures(false)
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|f| println!("{}", f));
|
2020-11-22 10:32:10 +01:00
|
|
|
|
2020-11-22 10:35:33 +01:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-12-12 11:44:28 +01:00
|
|
|
main_ast.iter_functions().for_each(|f| println!("{}", f));
|
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" => {
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
engine
|
|
|
|
.gen_fn_metadata_with_ast_to_json(&main_ast, true)
|
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-12 08:31:01 +01:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2020-04-13 04:27:08 +02:00
|
|
|
match engine
|
2020-03-21 10:14:31 +01:00
|
|
|
.compile_with_scope(&scope, &script)
|
2020-06-16 03:34:30 +02:00
|
|
|
.map_err(Into::into)
|
2020-03-12 08:31:01 +01:00
|
|
|
.and_then(|r| {
|
2020-04-05 11:44:48 +02:00
|
|
|
ast_u = r.clone();
|
2020-03-22 02:25:41 +01:00
|
|
|
|
2020-03-22 14:03:58 +01:00
|
|
|
#[cfg(not(feature = "no_optimize"))]
|
|
|
|
{
|
2021-03-10 16:37:04 +01:00
|
|
|
ast = engine.optimize_ast(&scope, r, rhai::OptimizationLevel::Simple);
|
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
|
|
|
}
|
2020-03-22 02:25:41 +01:00
|
|
|
|
2020-04-04 16:00:44 +02:00
|
|
|
// Merge the AST into the main
|
2020-10-07 06:11:25 +02:00
|
|
|
main_ast += ast.clone();
|
2020-04-04 16:00:44 +02:00
|
|
|
|
|
|
|
// Evaluate
|
2020-04-13 04:27:08 +02:00
|
|
|
engine.eval_ast_with_scope::<Dynamic>(&mut scope, &main_ast)
|
|
|
|
}) {
|
2020-04-16 05:57:08 +02:00
|
|
|
Ok(result) if !result.is::<()>() => {
|
2020-04-13 04:27:08 +02:00
|
|
|
println!("=> {:?}", result);
|
|
|
|
println!();
|
|
|
|
}
|
2020-04-16 05:57:08 +02:00
|
|
|
Ok(_) => (),
|
2020-04-13 04:27:08 +02:00
|
|
|
Err(err) => {
|
|
|
|
println!();
|
2020-04-21 17:25:12 +02:00
|
|
|
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
|
2020-06-30 12:34:58 +02:00
|
|
|
main_ast.clear_statements();
|
2017-12-20 12:16:14 +01:00
|
|
|
}
|
2017-10-15 17:50:39 +02:00
|
|
|
}
|