diff --git a/src/ast/compiler/mod.rs b/src/ast/compiler/mod.rs index 0856f88..9e06f13 100644 --- a/src/ast/compiler/mod.rs +++ b/src/ast/compiler/mod.rs @@ -19,3 +19,39 @@ pub use optimizer::*; pub use specializer::*; pub use lowering::*; pub use type_checker::*; + +use crate::ast::diagnostics::Diagnostics; + +/// The result of a compilation pass: either a typed AST or a set of diagnostics. +pub struct CompilationResult { + pub ast: Option, + pub diagnostics: Diagnostics, +} + +impl CompilationResult { + pub fn success(ast: TypedNode) -> Self { + Self { + ast: Some(ast), + diagnostics: Diagnostics::new(), + } + } + + pub fn error(msg: impl Into) -> Self { + let mut diag = Diagnostics::new(); + diag.push_error(msg, None); + Self { + ast: None, + diagnostics: diag, + } + } + + pub fn into_result(self) -> Result { + if self.diagnostics.has_errors() { + Err(self.diagnostics.format_errors()) + } else if let Some(ast) = self.ast { + Ok(ast) + } else { + Err("Compilation failed without diagnostics".to_string()) + } + } +} diff --git a/src/ast/environment.rs b/src/ast/environment.rs index 0569c7d..0ee3e89 100644 --- a/src/ast/environment.rs +++ b/src/ast/environment.rs @@ -26,6 +26,7 @@ use crate::ast::types::{ }; use crate::ast::diagnostics::Diagnostics; +pub use crate::ast::compiler::CompilationResult; pub type PipelineGenerator = Box bool>; const SYSTEM_LIB_SOURCE: &str = include_str!("rtl/prelude.myc"); @@ -35,39 +36,6 @@ fn make_rtl_lookup() -> RtlLookupFunc { Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args)) } -pub struct CompilationResult { - pub ast: Option, - pub diagnostics: Diagnostics, -} - -impl CompilationResult { - pub fn success(ast: TypedNode) -> Self { - Self { - ast: Some(ast), - diagnostics: Diagnostics::new(), - } - } - - pub fn error(msg: impl Into) -> Self { - let mut diag = Diagnostics::new(); - diag.push_error(msg, None); - Self { - ast: None, - diagnostics: diag, - } - } - - pub fn into_result(self) -> Result { - if self.diagnostics.has_errors() { - Err(self.diagnostics.format_errors()) - } else if let Some(ast) = self.ast { - Ok(ast) - } else { - Err("Compilation failed without diagnostics".to_string()) - } - } -} - /// Documentation entry for a single RTL symbol (function or constant). /// Populated via the builder returned by `register_native_fn` / `register_constant`. pub struct RtlDocEntry {