Improve repl with command to print AST.

This commit is contained in:
Stephen Chung 2020-03-12 15:31:01 +08:00
parent 55dcd2f0f4
commit 7e9a4fd965

View File

@ -1,4 +1,4 @@
use rhai::{Engine, EvalAltResult, Scope}; use rhai::{Engine, EvalAltResult, Scope, AST};
use std::{ use std::{
io::{stdin, stdout, Write}, io::{stdin, stdout, Write},
iter, iter,
@ -46,6 +46,7 @@ fn main() {
let mut scope = Scope::new(); let mut scope = Scope::new();
let mut input = String::new(); let mut input = String::new();
let mut ast: Option<AST> = None;
loop { loop {
print!("rhai> "); print!("rhai> ");
@ -57,7 +58,28 @@ fn main() {
println!("input error: {}", err); println!("input error: {}", err);
} }
if let Err(err) = engine.consume_with_scope(&mut scope, true, &input) { // Implement standard commands
match input.as_str().trim() {
"exit" | "quit" => break, // quit
"ast" => {
// print the last AST
match &ast {
Some(ast) => println!("{:#?}", ast),
None => println!("()"),
}
continue;
}
_ => (),
}
if let Err(err) = engine
.compile(&input)
.map_err(EvalAltResult::ErrorParsing)
.and_then(|r| {
ast = Some(r);
engine.consume_ast_with_scope(&mut scope, true, ast.as_ref().unwrap())
})
{
println!(""); println!("");
print_error(&input, err); print_error(&input, err);
println!(""); println!("");