From c11ae3b50dabc86b81e2041082c9f102403823b4 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 27 Mar 2018 00:50:31 -0700 Subject: [PATCH] Debug stages from command line --- schala-lang/src/lib.rs | 4 +++- schala-repl/src/language.rs | 13 ++++++++++++- schala-repl/src/lib.rs | 13 ++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/schala-lang/src/lib.rs b/schala-lang/src/lib.rs index ca20e71..64cb44b 100644 --- a/schala-lang/src/lib.rs +++ b/schala-lang/src/lib.rs @@ -86,7 +86,9 @@ impl ProgrammingLanguageInterface for Schala { match self.type_context.add_top_level_types(&ast) { Ok(()) => (), Err(msg) => { - evaluation.add_artifact(TraceArtifact::new("type_check", msg)); + if options.debug.type_checking { + evaluation.add_artifact(TraceArtifact::new("type_check", msg)); + } } }; diff --git a/schala-repl/src/language.rs b/schala-repl/src/language.rs index c21b1b0..24cd104 100644 --- a/schala-repl/src/language.rs +++ b/schala-repl/src/language.rs @@ -111,7 +111,18 @@ impl FinishedComputation { } pub fn to_noninteractive(&self) -> Option { match self.text_output { - Ok(_) => None, + Ok(_) => { + let mut buf = String::new(); + for stage in ["tokens", "parse_trace", "ast", "symbol_table", "type_check"].iter() { + if let Some(artifact) = self.artifacts.get(&stage.to_string()) { + let color = artifact.text_color; + let stage = stage.color(color).bold(); + let output = artifact.debug_output.color(color); + write!(&mut buf, "{}: {}\n", stage, output).unwrap(); + } + } + if buf == "" { None } else { Some(buf) } + }, Err(ref s) => Some(format!("{} {}", "Error: ".red().bold(), s)) } } diff --git a/schala-repl/src/lib.rs b/schala-repl/src/lib.rs index 7342eb9..774cf40 100644 --- a/schala-repl/src/lib.rs +++ b/schala-repl/src/lib.rs @@ -62,13 +62,20 @@ pub fn repl_main(generators: Vec) { exit(0); } + let mut options = EvalOptions::default(); + if let Some(ref ltrs) = option_matches.opt_str("debug") { + options.debug.tokens = ltrs.contains("l"); + options.debug.ast = ltrs.contains("a"); + options.debug.parse_tree = ltrs.contains("r"); + options.debug.symbol_table = ltrs.contains("s"); + } + let language_names: Vec = languages.iter().map(|lang| {lang.get_language_name()}).collect(); let initial_index: usize = option_matches.opt_str("lang") .and_then(|lang| { language_names.iter().position(|x| { x.to_lowercase() == lang.to_lowercase() }) }) .unwrap_or(0); - let mut options = EvalOptions::default(); options.execution_method = match option_matches.opt_str("eval-style") { Some(ref s) if s == "compile" => ExecutionMethod::Compile, _ => ExecutionMethod::Interpret, @@ -368,5 +375,9 @@ fn program_options() -> getopts::Options { options.optflag("w", "webapp", "Start up web interpreter"); + options.optopt("d", + "debug", + "Debug a stage (l = tokenizer, a = AST, r = parse trace, s = symbol table)", + "[l|a|r|s]"); options }