From 525cdd32e6bbb7ac1786b282e976da112ff3ae6e Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Fri, 17 May 2019 19:42:50 -0500 Subject: [PATCH] FEATURE: Use the AST Pretty Printer for TRACE and assert output. --- examples/module_example/modules/host_module.ucg | 2 +- src/ast/printer/mod.rs | 6 +++--- src/build/mod.rs | 17 +++++++++++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/examples/module_example/modules/host_module.ucg b/examples/module_example/modules/host_module.ucg index 6fb52aa..7f2d2e2 100644 --- a/examples/module_example/modules/host_module.ucg +++ b/examples/module_example/modules/host_module.ucg @@ -1,4 +1,4 @@ -let host_mod = module{ +let host_mod = TRACE module{ hostname="", mem=2048, cpu=2, diff --git a/src/ast/printer/mod.rs b/src/ast/printer/mod.rs index 62c4f25..e0cbfec 100644 --- a/src/ast/printer/mod.rs +++ b/src/ast/printer/mod.rs @@ -184,7 +184,7 @@ where Ok(()) } - fn render_expr(&mut self, expr: &Expression) -> std::io::Result<()> { + pub fn render_expr(&mut self, expr: &Expression) -> std::io::Result<()> { match expr { Expression::Binary(_def) => { let op = match _def.kind { @@ -331,7 +331,7 @@ where self.render_expr(e)?; write!(self.w, ") ")?; } - write!(self.w, "{{")?; + write!(self.w, "{{\n")?; self.curr_indent += self.indent; let indent = self.make_indent(); for stmt in _def.statements.iter() { @@ -372,7 +372,7 @@ where Ok(()) } - fn render_stmt(&mut self, stmt: &Statement) -> std::io::Result<()> { + pub fn render_stmt(&mut self, stmt: &Statement) -> std::io::Result<()> { // All statements start at the beginning of a line. match stmt { Statement::Let(def) => { diff --git a/src/build/mod.rs b/src/build/mod.rs index f889769..4e23874 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -1654,11 +1654,17 @@ impl<'a> FileBuilder<'a> { // we are not in validate_mode so build_asserts are noops. return Ok(Rc::new(Val::Empty)); } + let mut buffer: Vec = Vec::new(); + { + let mut printer = crate::ast::printer::AstPrinter::new(2, &mut buffer); + let _ = printer.render_expr(expr); + } + let expr_pretty = String::from_utf8(buffer).unwrap(); let ok = match self.eval_expr(expr, scope) { Ok(v) => v, Err(e) => { // failure! - let msg = format!("CompileError: {}\n", e); + let msg = format!("CompileError: {}\nfor expression:\n{}\n", e, expr_pretty); self.record_assert_result(&msg, false); return Ok(Rc::new(Val::Empty)); } @@ -1965,9 +1971,16 @@ impl<'a> FileBuilder<'a> { }; } &Expression::Debug(ref def) => { + let mut buffer: Vec = Vec::new(); + { + let mut printer = crate::ast::printer::AstPrinter::new(2, &mut buffer); + let _ = printer.render_expr(&def.expr); + } + let expr_pretty = String::from_utf8(buffer).unwrap(); + let val = self.eval_expr(&def.expr, scope); if let Ok(ref val) = val { - eprintln!("TRACE: {} at {}", val, def.pos); + eprintln!("TRACE: {} = {} at {}", expr_pretty, val, def.pos); } val }