Add --debug option for printing AST

This commit is contained in:
Alex Bethel 2021-06-16 10:29:27 -05:00
parent 098137082e
commit cdb4a22443
2 changed files with 18 additions and 7 deletions

View file

@ -31,8 +31,15 @@ fn main() {
.help("Set the path to interpret from")
.takes_value(true),
)
.arg(
Arg::with_name("debug")
.long("debug")
.help("Enable debug AST printing"),
)
.get_matches();
let ast_print = matches.is_present("debug");
match matches.value_of("file") {
Some(file_path) => {
// Read file
@ -46,10 +53,12 @@ fn main() {
// Parse & evaluate
let mut parser = Parser::new(&source);
if let Err(e) = parser
.init()
.and_then(|ast| ExecEnv::new().eval_stmts(&ast))
{
if let Err(e) = parser.init().and_then(|ast| {
if ast_print {
println!("{:#?}", ast);
}
ExecEnv::new().eval_stmts(&ast)
}) {
println!(
"Error `{:?}` occurred at span: {:?} = `{:?}`",
e.kind,
@ -60,7 +69,7 @@ fn main() {
}
None => {
println!("Hi [AbleScript {}]", env!("CARGO_PKG_VERSION"));
repl::repl();
repl::repl(ast_print);
}
}
}

View file

@ -3,7 +3,7 @@ use rustyline::Editor;
use crate::{interpret::ExecEnv, parser::Parser};
pub fn repl() {
pub fn repl(ast_print: bool) {
let mut rl = Editor::<()>::new();
let mut env = ExecEnv::new();
loop {
@ -16,7 +16,9 @@ pub fn repl() {
}
let mut parser = Parser::new(&line);
let value = parser.init().and_then(|ast| {
// println!("{:?}", &ast);
if ast_print {
println!("{:#?}", &ast);
}
env.eval_stmts(&ast)
});