2020-03-16 05:41:19 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, Scope, AST};
|
|
|
|
|
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) {
|
|
|
|
fn padding(pad: &str, len: usize) -> String {
|
|
|
|
iter::repeat(pad).take(len).collect::<String>()
|
|
|
|
}
|
|
|
|
|
|
|
|
let lines: Vec<_> = input.split("\n").collect();
|
|
|
|
|
|
|
|
// Print error
|
|
|
|
match err.position() {
|
|
|
|
p if p.is_eof() => {
|
|
|
|
// EOF
|
|
|
|
let last = lines[lines.len() - 2];
|
|
|
|
println!("{}", last);
|
|
|
|
println!("{}^ {}", padding(" ", last.len() - 1), err);
|
|
|
|
}
|
|
|
|
p if p.is_none() => {
|
|
|
|
// No position
|
|
|
|
println!("{}", err);
|
|
|
|
}
|
|
|
|
p => {
|
|
|
|
// Specific position
|
|
|
|
let pos_text = format!(
|
|
|
|
" (line {}, position {})",
|
|
|
|
p.line().unwrap(),
|
|
|
|
p.position().unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
println!("{}", 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)
|
|
|
|
}
|
|
|
|
_ => err.to_string(),
|
|
|
|
};
|
|
|
|
|
2020-03-10 04:25:34 +01:00
|
|
|
println!(
|
|
|
|
"{}^ {}",
|
|
|
|
padding(" ", p.position().unwrap() - 1),
|
2020-03-17 10:33:37 +01:00
|
|
|
err_text.replace(&pos_text, "")
|
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"))]
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Full);
|
|
|
|
|
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-03-12 08:31:01 +01:00
|
|
|
let mut ast: Option<AST> = None;
|
2017-10-15 17:50:39 +02:00
|
|
|
|
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();
|
|
|
|
|
|
|
|
if let Err(err) = stdin().read_line(&mut input) {
|
|
|
|
println!("input error: {}", err);
|
2017-12-20 12:16:14 +01:00
|
|
|
}
|
2017-10-15 17:50:39 +02:00
|
|
|
|
2020-03-12 08:31:01 +01:00
|
|
|
// Implement standard commands
|
|
|
|
match input.as_str().trim() {
|
|
|
|
"exit" | "quit" => break, // quit
|
|
|
|
"ast" => {
|
2020-03-14 16:40:30 +01:00
|
|
|
if matches!(&ast, Some(_)) {
|
|
|
|
// print the last AST
|
|
|
|
println!("{:#?}", ast.as_ref().unwrap());
|
|
|
|
} else {
|
|
|
|
println!("()");
|
2020-03-12 08:31:01 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(err) = engine
|
2020-03-14 07:30:44 +01:00
|
|
|
.compile_with_scope(&scope, &input)
|
2020-03-12 08:31:01 +01:00
|
|
|
.map_err(EvalAltResult::ErrorParsing)
|
|
|
|
.and_then(|r| {
|
|
|
|
ast = Some(r);
|
2020-03-17 10:33:37 +01:00
|
|
|
engine
|
|
|
|
.consume_ast_with_scope(&mut scope, true, ast.as_ref().unwrap())
|
|
|
|
.or_else(|err| match err {
|
|
|
|
EvalAltResult::Return(_, _) => Ok(()),
|
|
|
|
err => Err(err),
|
|
|
|
})
|
2020-03-12 08:31:01 +01:00
|
|
|
})
|
|
|
|
{
|
2020-03-10 04:25:34 +01:00
|
|
|
println!("");
|
|
|
|
print_error(&input, err);
|
|
|
|
println!("");
|
2017-12-20 12:16:14 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-15 17:50:39 +02:00
|
|
|
}
|