use std::fmt; use erg_common::config::ErgConfig; use erg_common::traits::{Runnable, Stream}; use erg_common::Str; use erg_parser::ast::AST; use crate::context::ModuleContext; use crate::error::CompileErrors; use crate::hir::HIR; use crate::module::SharedCompilerResource; #[derive(Debug)] pub struct CompleteArtifact { pub object: Inner, pub warns: CompileErrors, } impl CompleteArtifact { pub const fn new(object: Inner, warns: CompileErrors) -> Self { Self { object, warns } } } #[derive(Debug)] pub struct IncompleteArtifact { pub object: Option, pub errors: CompileErrors, pub warns: CompileErrors, } impl fmt::Display for IncompleteArtifact { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if !self.warns.is_empty() { writeln!(f, "{}", self.warns)?; } write!(f, "{}", self.errors) } } impl std::error::Error for IncompleteArtifact {} impl From> for IncompleteArtifact { fn from(artifact: CompleteArtifact) -> Self { Self { object: Some(artifact.object), errors: CompileErrors::empty(), warns: artifact.warns, } } } impl IncompleteArtifact { pub const fn new(object: Option, errors: CompileErrors, warns: CompileErrors) -> Self { Self { object, errors, warns, } } } #[derive(Debug)] pub struct ErrorArtifact { pub errors: CompileErrors, pub warns: CompileErrors, } impl fmt::Display for ErrorArtifact { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if !self.warns.is_empty() { writeln!(f, "{}", self.warns)?; } write!(f, "{}", self.errors) } } impl std::error::Error for ErrorArtifact {} impl From> for ErrorArtifact { fn from(artifact: IncompleteArtifact) -> Self { Self { errors: artifact.errors, warns: artifact.warns, } } } impl From for ErrorArtifact { fn from(errors: CompileErrors) -> Self { Self { errors, warns: CompileErrors::empty(), } } } impl ErrorArtifact { pub const fn new(errors: CompileErrors, warns: CompileErrors) -> Self { Self { errors, warns } } pub fn clear(&mut self) { self.errors.clear(); self.warns.clear(); } } pub trait Buildable { fn inherit(cfg: ErgConfig, shared: SharedCompilerResource) -> Self where Self: Sized; fn inherit_with_name(cfg: ErgConfig, mod_name: Str, shared: SharedCompilerResource) -> Self where Self: Sized; fn build( &mut self, src: String, mode: &str, ) -> Result, IncompleteArtifact>; fn build_from_ast( &mut self, ast: AST, mode: &str, ) -> Result, IncompleteArtifact>; fn pop_context(&mut self) -> Option; fn get_context(&self) -> Option<&ModuleContext>; } pub trait BuildRunnable: Buildable + Runnable + 'static { fn build_module(&mut self) -> Result, IncompleteArtifact> { let src = self.cfg_mut().input.read(); self.build(src, "exec") } }