diff --git a/compiler/erg_compiler/artifact.rs b/compiler/erg_compiler/artifact.rs index 4bb2f9ac..f6b8d3ce 100644 --- a/compiler/erg_compiler/artifact.rs +++ b/compiler/erg_compiler/artifact.rs @@ -2,6 +2,7 @@ use std::fmt; use erg_common::traits::Stream; +use crate::context::Context; use crate::error::CompileErrors; use crate::hir::HIR; @@ -77,6 +78,12 @@ impl ErrorArtifact { } } -pub trait Buildable { - fn build(&mut self, src: String, mode: &str) -> Result; +pub trait Buildable { + fn build( + &mut self, + src: String, + mode: &str, + ) -> Result, IncompleteArtifact>; + fn pop_context(&mut self) -> Option; + fn get_context(&self) -> Option<&Context>; } diff --git a/compiler/erg_compiler/build_hir.rs b/compiler/erg_compiler/build_hir.rs index 6640e2f7..54369ff5 100644 --- a/compiler/erg_compiler/build_hir.rs +++ b/compiler/erg_compiler/build_hir.rs @@ -6,7 +6,7 @@ use erg_common::Str; use erg_parser::ast::{VarName, AST}; use erg_parser::build_ast::ASTBuilder; -use crate::artifact::{CompleteArtifact, IncompleteArtifact}; +use crate::artifact::{Buildable, CompleteArtifact, IncompleteArtifact}; use crate::context::{Context, ContextProvider}; use crate::effectcheck::SideEffectChecker; use crate::error::{CompileError, CompileErrors}; @@ -72,6 +72,18 @@ impl Runnable for HIRBuilder { } } +impl Buildable for HIRBuilder { + fn build(&mut self, src: String, mode: &str) -> Result { + self.build(src, mode) + } + fn pop_context(&mut self) -> Option { + Some(self.pop_mod_ctx()) + } + fn get_context(&self) -> Option<&Context> { + Some(&self.lowerer.ctx) + } +} + impl ContextProvider for HIRBuilder { fn dir(&self) -> Vec<(&VarName, &VarInfo)> { self.lowerer.dir() diff --git a/compiler/erg_compiler/transpile.rs b/compiler/erg_compiler/transpile.rs index 62692769..9ff61dbd 100644 --- a/compiler/erg_compiler/transpile.rs +++ b/compiler/erg_compiler/transpile.rs @@ -10,7 +10,7 @@ use erg_common::Str; use erg_parser::ast::{ParamPattern, TypeSpec, VarName}; use erg_parser::token::TokenKind; -use crate::artifact::{CompleteArtifact, ErrorArtifact}; +use crate::artifact::{Buildable, CompleteArtifact, ErrorArtifact, IncompleteArtifact}; use crate::build_hir::HIRBuilder; use crate::codegen::PyCodeGenerator; use crate::context::{Context, ContextProvider}; @@ -174,6 +174,23 @@ impl ContextProvider for Transpiler { } } +impl Buildable for Transpiler { + fn build( + &mut self, + src: String, + mode: &str, + ) -> Result, IncompleteArtifact> { + self.transpile(src, mode) + .map_err(|err| IncompleteArtifact::new(None, err.errors, err.warns)) + } + fn pop_context(&mut self) -> Option { + self.builder.pop_context() + } + fn get_context(&self) -> Option<&Context> { + self.builder.get_context() + } +} + impl Transpiler { pub fn transpile( &mut self,