2020-03-25 04:51:13 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, Position, Scope, AST};
|
2020-03-16 05:41:19 +01:00
|
|
|
|
2020-03-15 15:39:58 +01:00
|
|
|
#[cfg(not(feature = "no_optimize"))]
|
|
|
|
use rhai::OptimizationLevel;
|
|
|
|
|
2020-03-10 04:25:34 +01:00
|
|
|
use std::{
|
|
|
|
io::{stdin, stdout, Write},
|
|
|
|
iter,
|
|
|
|
};
|
2017-10-15 17:50:39 +02:00
|
|
|
|
2020-03-10 04:25:34 +01:00
|
|
|
fn print_error(input: &str, err: EvalAltResult) {
|
2020-03-24 10:30:04 +01:00
|
|
|
let lines: Vec<_> = input.trim().split('\n').collect();
|
2020-03-21 10:14:31 +01:00
|
|
|
|
|
|
|
let line_no = if lines.len() > 1 {
|
|
|
|
match err.position() {
|
|
|
|
p if p.is_none() => "".to_string(),
|
|
|
|
p if p.is_eof() => format!("{}: ", lines.len()),
|
|
|
|
p => format!("{}: ", p.line().unwrap()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
};
|
2020-03-10 04:25:34 +01:00
|
|
|
|
|
|
|
// Print error
|
2020-03-25 04:51:13 +01:00
|
|
|
let pos = err.position();
|
|
|
|
let pos_text = format!(" ({})", pos);
|
2020-03-22 14:03:58 +01:00
|
|
|
|
2020-03-25 04:51:13 +01:00
|
|
|
let pos = if pos.is_eof() {
|
|
|
|
let last = lines[lines.len() - 1];
|
|
|
|
Position::new(lines.len(), last.len() + 1)
|
|
|
|
} else {
|
|
|
|
pos
|
|
|
|
};
|
2020-03-22 14:03:58 +01:00
|
|
|
|
2020-03-25 04:51:13 +01:00
|
|
|
match pos {
|
|
|
|
p if p.is_eof() => panic!("should not be EOF"),
|
2020-03-10 04:25:34 +01:00
|
|
|
p if p.is_none() => {
|
|
|
|
// No position
|
|
|
|
println!("{}", err);
|
|
|
|
}
|
|
|
|
p => {
|
|
|
|
// Specific position
|
2020-03-21 10:14:31 +01:00
|
|
|
println!("{}{}", line_no, lines[p.line().unwrap() - 1]);
|
2020-03-17 10:33:37 +01:00
|
|
|
|
|
|
|
let err_text = match err {
|
|
|
|
EvalAltResult::ErrorRuntime(err, _) if !err.is_empty() => {
|
|
|
|
format!("Runtime error: {}", err)
|
|
|
|
}
|
2020-03-25 04:51:13 +01:00
|
|
|
err => err.to_string(),
|
2020-03-17 10:33:37 +01:00
|
|
|
};
|
|
|
|
|
2020-03-10 04:25:34 +01:00
|
|
|
println!(
|
2020-04-02 16:37:35 +02:00
|
|
|
"{0:>1$} {2}",
|
|
|
|
"^",
|
|
|
|
line_no.len() + p.position().unwrap(),
|
2020-03-17 10:33:37 +01:00
|
|
|
err_text.replace(&pos_text, "")
|
2020-03-10 04:25:34 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 10:14:31 +01:00
|
|
|
fn print_help() {
|
|
|
|
println!("help => print this help");
|
|
|
|
println!("quit, exit => quit");
|
|
|
|
println!("ast => print the last AST");
|
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() {
|
2017-12-20 12:16:14 +01:00
|
|
|
let mut engine = Engine::new();
|
2020-03-15 15:39:58 +01:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_optimize"))]
|
2020-03-22 02:25:41 +01:00
|
|
|
engine.set_optimization_level(OptimizationLevel::None);
|
2020-03-15 15:39:58 +01:00
|
|
|
|
2017-12-20 12:16:14 +01:00
|
|
|
let mut scope = Scope::new();
|
2017-10-15 17:50:39 +02:00
|
|
|
|
2020-03-10 04:25:34 +01:00
|
|
|
let mut input = String::new();
|
2020-04-04 16:00:44 +02:00
|
|
|
let mut main_ast = AST::new();
|
|
|
|
let mut ast_u = AST::new();
|
|
|
|
let mut ast = AST::new();
|
2017-10-15 17:50:39 +02:00
|
|
|
|
2020-03-21 10:14:31 +01:00
|
|
|
println!("Rhai REPL tool");
|
|
|
|
println!("==============");
|
|
|
|
print_help();
|
|
|
|
|
2017-12-20 12:16:14 +01:00
|
|
|
loop {
|
2020-03-10 04:25:34 +01:00
|
|
|
print!("rhai> ");
|
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 {
|
|
|
|
if let Err(err) = stdin().read_line(&mut input) {
|
|
|
|
panic!("input error: {}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-22 02:25:41 +01:00
|
|
|
"astu" => {
|
2020-04-04 16:00:44 +02:00
|
|
|
// print the last un-optimized AST
|
|
|
|
println!("{:#?}", &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
|
|
|
|
println!("{:#?}", &ast);
|
2020-03-12 08:31:01 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(err) = engine
|
2020-03-21 10:14:31 +01:00
|
|
|
.compile_with_scope(&scope, &script)
|
2020-03-12 08:31:01 +01:00
|
|
|
.map_err(EvalAltResult::ErrorParsing)
|
|
|
|
.and_then(|r| {
|
2020-04-04 16:00:44 +02:00
|
|
|
ast_u = r;
|
2020-03-22 02:25:41 +01:00
|
|
|
|
2020-03-22 14:03:58 +01:00
|
|
|
#[cfg(not(feature = "no_optimize"))]
|
|
|
|
{
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Full);
|
2020-04-04 16:00:44 +02:00
|
|
|
ast = engine.optimize_ast(&scope, &ast_u);
|
2020-03-22 14:03:58 +01:00
|
|
|
engine.set_optimization_level(OptimizationLevel::None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "no_optimize")]
|
|
|
|
{
|
|
|
|
ast = ast_u.clone();
|
|
|
|
}
|
2020-03-22 02:25:41 +01:00
|
|
|
|
2020-04-04 16:00:44 +02:00
|
|
|
// Merge the AST into the main
|
|
|
|
main_ast = main_ast.merge(&ast);
|
|
|
|
|
|
|
|
// Evaluate
|
|
|
|
let result = engine
|
|
|
|
.consume_ast_with_scope(&mut scope, &main_ast)
|
2020-03-17 10:33:37 +01:00
|
|
|
.or_else(|err| match err {
|
|
|
|
EvalAltResult::Return(_, _) => Ok(()),
|
|
|
|
err => Err(err),
|
2020-04-04 16:00:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Throw away all the statements, leaving only the functions
|
|
|
|
main_ast.retain_functions();
|
|
|
|
|
|
|
|
result
|
2020-03-12 08:31:01 +01:00
|
|
|
})
|
|
|
|
{
|
2020-03-24 10:30:04 +01:00
|
|
|
println!();
|
2020-03-10 04:25:34 +01:00
|
|
|
print_error(&input, err);
|
2020-03-24 10:30:04 +01:00
|
|
|
println!();
|
2017-12-20 12:16:14 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-15 17:50:39 +02:00
|
|
|
}
|