Add --debug option for printing AST

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

View file

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