From cdb4a22443e5a447b47d3b88abe2b3a52c41d8c0 Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Wed, 16 Jun 2021 10:29:27 -0500 Subject: [PATCH] Add `--debug` option for printing AST --- src/main.rs | 19 ++++++++++++++----- src/repl.rs | 6 ++++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index eb1e2c5..f63bdfb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); } } } diff --git a/src/repl.rs b/src/repl.rs index 55395c6..21fd50c 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -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) });