mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-30 12:51:10 +00:00
impl Buildable for HIRBuilder, Transpiler
This commit is contained in:
parent
87d13b8f09
commit
e07b0dfa67
3 changed files with 40 additions and 4 deletions
|
@ -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<CompleteArtifact, IncompleteArtifact>;
|
||||
pub trait Buildable<T = HIR> {
|
||||
fn build(
|
||||
&mut self,
|
||||
src: String,
|
||||
mode: &str,
|
||||
) -> Result<CompleteArtifact<T>, IncompleteArtifact<T>>;
|
||||
fn pop_context(&mut self) -> Option<Context>;
|
||||
fn get_context(&self) -> Option<&Context>;
|
||||
}
|
||||
|
|
|
@ -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<CompleteArtifact, IncompleteArtifact> {
|
||||
self.build(src, mode)
|
||||
}
|
||||
fn pop_context(&mut self) -> Option<Context> {
|
||||
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()
|
||||
|
|
|
@ -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<PyScript> for Transpiler {
|
||||
fn build(
|
||||
&mut self,
|
||||
src: String,
|
||||
mode: &str,
|
||||
) -> Result<CompleteArtifact<PyScript>, IncompleteArtifact<PyScript>> {
|
||||
self.transpile(src, mode)
|
||||
.map_err(|err| IncompleteArtifact::new(None, err.errors, err.warns))
|
||||
}
|
||||
fn pop_context(&mut self) -> Option<Context> {
|
||||
self.builder.pop_context()
|
||||
}
|
||||
fn get_context(&self) -> Option<&Context> {
|
||||
self.builder.get_context()
|
||||
}
|
||||
}
|
||||
|
||||
impl Transpiler {
|
||||
pub fn transpile(
|
||||
&mut self,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue