From 325e690cd91479447f6864d0776382b04c3ada18 Mon Sep 17 00:00:00 2001 From: Brummel Date: Fri, 27 Mar 2026 13:17:58 +0100 Subject: [PATCH] Add format_errors to Diagnostics Introduces a new method `format_errors` to the `Diagnostics` struct. This method iterates through all diagnostics, filters for errors, and formats them into a newline-joined string. This simplifies error reporting in compilation results. --- src/ast/diagnostics.rs | 10 ++++++++++ src/ast/environment.rs | 36 ++++++++---------------------------- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/ast/diagnostics.rs b/src/ast/diagnostics.rs index 5d53a9a..20eb49a 100644 --- a/src/ast/diagnostics.rs +++ b/src/ast/diagnostics.rs @@ -51,4 +51,14 @@ impl Diagnostics { identity: id, }); } + + /// Formats all error-level diagnostics as a newline-joined string. + pub fn format_errors(&self) -> String { + self.items + .iter() + .filter(|d| d.level == DiagnosticLevel::Error) + .map(|d| d.message.as_str()) + .collect::>() + .join("\n") + } } diff --git a/src/ast/environment.rs b/src/ast/environment.rs index 51406e6..6b01ec4 100644 --- a/src/ast/environment.rs +++ b/src/ast/environment.rs @@ -25,7 +25,7 @@ use crate::ast::types::{ CommentLine, NativeFunction, NodeIdentity, Purity, SourceLocation, StaticType, Value, }; -use crate::ast::diagnostics::{DiagnosticLevel, Diagnostics}; +use crate::ast::diagnostics::Diagnostics; pub type PipelineGenerator = Box bool>; const SYSTEM_LIB_SOURCE: &str = include_str!("rtl/prelude.myc"); @@ -59,14 +59,7 @@ impl CompilationResult { pub fn into_result(self) -> Result { if self.diagnostics.has_errors() { - let errors: Vec = self - .diagnostics - .items - .into_iter() - .filter(|d| d.level == DiagnosticLevel::Error) - .map(|d| d.message) - .collect(); - Err(errors.join("\n")) + Err(self.diagnostics.format_errors()) } else if let Some(ast) = self.ast { Ok(ast) } else { @@ -198,12 +191,7 @@ impl MacroEvaluator for RuntimeMacroEvaluator { let typed_ast = checker.check(&bound_ast, &[], &mut diag); if diag.has_errors() { - return Err(diag - .items - .into_iter() - .map(|d| d.message) - .collect::>() - .join("\n")); + return Err(diag.format_errors()); } let exec_ast = Lowering::lower(Analyzer::analyze(&typed_ast, &self.root_purity.borrow())); // Minimal analysis for macro eval @@ -596,12 +584,7 @@ impl Environment { let typed_ast_opt = self.compile_pipeline(syntax_ast, &mut diagnostics); if diagnostics.has_errors() || typed_ast_opt.is_none() { - return Err(diagnostics - .items - .into_iter() - .map(|d| d.message) - .collect::>() - .join("\n")); + return Err(diagnostics.format_errors()); } Ok(typed_ast_opt.unwrap()) @@ -839,7 +822,9 @@ impl Environment { pub fn dump_ast(&self, source: &str) -> Result { self.preload_dependencies(source, None)?; - let compiled = self.compile(source).into_result()?; + let mut parser = Parser::new(source); + let syntax_ast = parser.parse_expression(); + let compiled = self.compile_syntax(syntax_ast)?; let linked = self.link(compiled); Ok(Dumper::dump(&linked)) } @@ -983,12 +968,7 @@ impl Environment { let retyped_ast = checker.check_node_as_bound(func_template.ty.original.as_ref(), arg_types, &mut diag); if diag.has_errors() { - return Err(diag - .items - .into_iter() - .map(|d| d.message) - .collect::>() - .join("\n")); + return Err(diag.format_errors()); } let analyzed = Analyzer::analyze(&retyped_ast, &root_purity.borrow());