mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 18:58:30 +00:00
chore: let HIRBuilder/ASTLowerer
be generic
This commit is contained in:
parent
f4fcf3f2b2
commit
51c7b0778d
10 changed files with 126 additions and 75 deletions
|
@ -16,7 +16,7 @@ use erg_common::spawn::spawn_new_thread;
|
|||
use erg_common::traits::Locational;
|
||||
|
||||
use erg_compiler::artifact::{BuildRunnable, Buildable};
|
||||
use erg_compiler::build_package::PlainPackageBuilder;
|
||||
use erg_compiler::build_package::PackageBuilder;
|
||||
use erg_compiler::context::Context;
|
||||
use erg_compiler::erg_parser::parse::Parsable;
|
||||
use erg_compiler::erg_parser::token::TokenKind;
|
||||
|
@ -326,7 +326,7 @@ fn load_modules<'a>(
|
|||
..cfg
|
||||
};
|
||||
let shared = SharedCompilerResource::new(cfg.clone());
|
||||
let mut checker = PlainPackageBuilder::inherit(cfg, shared.clone());
|
||||
let mut checker = PackageBuilder::inherit(cfg, shared.clone());
|
||||
let _res = checker.build(src, "exec");
|
||||
let mut cache = cache.borrow_mut();
|
||||
if cache.get("<module>").is_none() {
|
||||
|
|
|
@ -5,14 +5,14 @@ use erg_common::traits::{ExitStatus, Runnable, Stream};
|
|||
use erg_common::Str;
|
||||
|
||||
use erg_parser::ast::{VarName, AST};
|
||||
use erg_parser::build_ast::ASTBuilder;
|
||||
use erg_parser::build_ast::{ASTBuildable, ASTBuilder as DefaultASTBuilder};
|
||||
|
||||
use crate::artifact::{BuildRunnable, Buildable, CompleteArtifact, IncompleteArtifact};
|
||||
use crate::context::{Context, ContextKind, ContextProvider, ModuleContext};
|
||||
use crate::effectcheck::SideEffectChecker;
|
||||
use crate::error::{CompileError, CompileErrors, LowerWarnings};
|
||||
use crate::link_hir::HIRLinker;
|
||||
use crate::lower::ASTLowerer;
|
||||
use crate::lower::GenericASTLowerer;
|
||||
use crate::module::SharedCompilerResource;
|
||||
use crate::ownercheck::OwnershipChecker;
|
||||
use crate::ty::VisibilityModifier;
|
||||
|
@ -22,24 +22,26 @@ use crate::varinfo::VarInfo;
|
|||
///
|
||||
/// NOTE: This does not perform dependency resolution, use `PackageBuilder` to build a package
|
||||
#[derive(Debug)]
|
||||
pub struct HIRBuilder {
|
||||
pub(crate) lowerer: ASTLowerer,
|
||||
pub struct GenericHIRBuilder<ASTBuilder: ASTBuildable = DefaultASTBuilder> {
|
||||
pub(crate) lowerer: GenericASTLowerer<ASTBuilder>,
|
||||
ownership_checker: OwnershipChecker,
|
||||
}
|
||||
|
||||
impl Default for HIRBuilder {
|
||||
pub type HIRBuilder = GenericHIRBuilder<DefaultASTBuilder>;
|
||||
|
||||
impl<ASTBuilder: ASTBuildable> Default for GenericHIRBuilder<ASTBuilder> {
|
||||
fn default() -> Self {
|
||||
HIRBuilder::new(ErgConfig::default())
|
||||
GenericHIRBuilder::new(ErgConfig::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl Runnable for HIRBuilder {
|
||||
impl<ASTBuilder: ASTBuildable> Runnable for GenericHIRBuilder<ASTBuilder> {
|
||||
type Err = CompileError;
|
||||
type Errs = CompileErrors;
|
||||
const NAME: &'static str = "Erg HIR builder";
|
||||
|
||||
fn new(cfg: ErgConfig) -> Self {
|
||||
HIRBuilder::new_with_cache(
|
||||
GenericHIRBuilder::new_with_cache(
|
||||
cfg.copy(),
|
||||
Str::ever("<module>"),
|
||||
SharedCompilerResource::new(cfg),
|
||||
|
@ -71,7 +73,7 @@ impl Runnable for HIRBuilder {
|
|||
fn exec(&mut self) -> Result<ExitStatus, Self::Errs> {
|
||||
let mut builder = ASTBuilder::new(self.cfg().copy());
|
||||
let artifact = builder
|
||||
.build(self.cfg_mut().input.read())
|
||||
.build_ast(self.cfg_mut().input.read())
|
||||
.map_err(|arti| arti.errors)?;
|
||||
artifact.warns.write_all_stderr();
|
||||
let artifact = self
|
||||
|
@ -84,7 +86,7 @@ impl Runnable for HIRBuilder {
|
|||
|
||||
fn eval(&mut self, src: String) -> Result<String, Self::Errs> {
|
||||
let mut builder = ASTBuilder::new(self.cfg().copy());
|
||||
let artifact = builder.build(src).map_err(|arti| arti.errors)?;
|
||||
let artifact = builder.build_ast(src).map_err(|arti| arti.errors)?;
|
||||
artifact.warns.write_all_stderr();
|
||||
let artifact = self
|
||||
.check(artifact.ast, "eval")
|
||||
|
@ -94,7 +96,7 @@ impl Runnable for HIRBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
impl Buildable for HIRBuilder {
|
||||
impl<ASTBuilder: ASTBuildable> Buildable for GenericHIRBuilder<ASTBuilder> {
|
||||
fn inherit(cfg: ErgConfig, shared: SharedCompilerResource) -> Self {
|
||||
let mod_name = Str::from(cfg.input.file_stem());
|
||||
Self::new_with_cache(cfg, mod_name, shared)
|
||||
|
@ -120,9 +122,9 @@ impl Buildable for HIRBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
impl BuildRunnable for HIRBuilder {}
|
||||
impl<ASTBuilder: ASTBuildable + 'static> BuildRunnable for GenericHIRBuilder<ASTBuilder> {}
|
||||
|
||||
impl ContextProvider for HIRBuilder {
|
||||
impl<ASTBuilder: ASTBuildable> ContextProvider for GenericHIRBuilder<ASTBuilder> {
|
||||
fn dir(&self) -> Dict<&VarName, &VarInfo> {
|
||||
self.lowerer.dir()
|
||||
}
|
||||
|
@ -136,14 +138,14 @@ impl ContextProvider for HIRBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
impl HIRBuilder {
|
||||
impl<ASTBuilder: ASTBuildable> GenericHIRBuilder<ASTBuilder> {
|
||||
pub fn new_with_cache<S: Into<Str>>(
|
||||
cfg: ErgConfig,
|
||||
mod_name: S,
|
||||
shared: SharedCompilerResource,
|
||||
) -> Self {
|
||||
Self {
|
||||
lowerer: ASTLowerer::new_with_cache(cfg.copy(), mod_name, shared),
|
||||
lowerer: GenericASTLowerer::new_with_cache(cfg.copy(), mod_name, shared),
|
||||
ownership_checker: OwnershipChecker::new(cfg),
|
||||
}
|
||||
}
|
||||
|
@ -157,7 +159,7 @@ impl HIRBuilder {
|
|||
);
|
||||
Self {
|
||||
ownership_checker: OwnershipChecker::new(mod_ctx.get_top_cfg()),
|
||||
lowerer: ASTLowerer::new_with_ctx(mod_ctx),
|
||||
lowerer: GenericASTLowerer::new_with_ctx(mod_ctx),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -184,7 +186,7 @@ impl HIRBuilder {
|
|||
) -> Result<CompleteArtifact, IncompleteArtifact> {
|
||||
let mut ast_builder = ASTBuilder::new(self.cfg().copy());
|
||||
let artifact = ast_builder
|
||||
.build(src)
|
||||
.build_ast(src)
|
||||
.map_err(|iart| IncompleteArtifact::new(None, iart.errors.into(), iart.warns.into()))?;
|
||||
self.lowerer
|
||||
.warns
|
||||
|
|
|
@ -15,7 +15,7 @@ use erg_common::str::Str;
|
|||
use erg_common::traits::{ExitStatus, Runnable, Stream};
|
||||
|
||||
use erg_parser::ast::{Expr, InlineModule, VarName, AST};
|
||||
use erg_parser::build_ast::{ASTBuildable, ASTBuilder};
|
||||
use erg_parser::build_ast::{ASTBuildable, ASTBuilder as DefaultASTBuilder};
|
||||
use erg_parser::parse::SimpleParser;
|
||||
|
||||
use crate::artifact::{
|
||||
|
@ -23,11 +23,11 @@ use crate::artifact::{
|
|||
};
|
||||
use crate::context::{Context, ContextProvider, ModuleContext};
|
||||
use crate::error::{CompileError, CompileErrors};
|
||||
use crate::lower::ASTLowerer;
|
||||
use crate::lower::GenericASTLowerer;
|
||||
use crate::module::SharedCompilerResource;
|
||||
use crate::ty::ValueObj;
|
||||
use crate::varinfo::VarInfo;
|
||||
use crate::HIRBuilder;
|
||||
use crate::GenericHIRBuilder;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ResolveError {
|
||||
|
@ -45,35 +45,42 @@ pub type ResolveResult<T> = Result<T, ResolveError>;
|
|||
/// Invariant condition: `build_module` must be idempotent.
|
||||
/// That is, the only thing that may differ as a result of analyzing the same package is the elapsed time.
|
||||
#[derive(Debug)]
|
||||
pub struct PackageBuilder<Parser: ASTBuildable = ASTBuilder, Builder: Buildable = HIRBuilder> {
|
||||
pub struct GenericPackageBuilder<
|
||||
ASTBuilder: ASTBuildable = DefaultASTBuilder,
|
||||
HIRBuilder: Buildable = GenericHIRBuilder,
|
||||
> {
|
||||
cfg: ErgConfig,
|
||||
shared: SharedCompilerResource,
|
||||
pub(crate) main_builder: Builder,
|
||||
pub(crate) main_builder: HIRBuilder,
|
||||
cyclic: Vec<NormalizedPathBuf>,
|
||||
submodules: Vec<NormalizedPathBuf>,
|
||||
asts: Dict<NormalizedPathBuf, (Str, AST)>,
|
||||
parse_errors: ErrorArtifact,
|
||||
_parser: PhantomData<fn() -> Parser>,
|
||||
_parser: PhantomData<fn() -> ASTBuilder>,
|
||||
}
|
||||
|
||||
pub type PlainPackageBuilder = PackageBuilder<ASTBuilder, HIRBuilder>;
|
||||
pub type PackageTypeChecker = PackageBuilder<ASTBuilder, ASTLowerer>;
|
||||
pub type FullPackageBuilder = PackageBuilder<ASTBuilder, HIRBuilder>;
|
||||
pub type PackageBuilder = GenericPackageBuilder<DefaultASTBuilder, GenericHIRBuilder>;
|
||||
pub type PackageTypeChecker =
|
||||
GenericPackageBuilder<DefaultASTBuilder, GenericASTLowerer<DefaultASTBuilder>>;
|
||||
|
||||
impl<Parser: ASTBuildable, Builder: Buildable> Default for PackageBuilder<Parser, Builder> {
|
||||
impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable> Default
|
||||
for GenericPackageBuilder<ASTBuilder, HIRBuilder>
|
||||
{
|
||||
fn default() -> Self {
|
||||
let cfg = ErgConfig::default();
|
||||
PackageBuilder::new(cfg.copy(), SharedCompilerResource::new(cfg))
|
||||
GenericPackageBuilder::new(cfg.copy(), SharedCompilerResource::new(cfg))
|
||||
}
|
||||
}
|
||||
|
||||
impl<Parser: ASTBuildable, Builder: BuildRunnable> Runnable for PackageBuilder<Parser, Builder> {
|
||||
impl<ASTBuilder: ASTBuildable, HIRBuilder: BuildRunnable> Runnable
|
||||
for GenericPackageBuilder<ASTBuilder, HIRBuilder>
|
||||
{
|
||||
type Err = CompileError;
|
||||
type Errs = CompileErrors;
|
||||
const NAME: &'static str = "Erg package builder";
|
||||
|
||||
fn new(cfg: ErgConfig) -> Self {
|
||||
PackageBuilder::new(cfg.copy(), SharedCompilerResource::new(cfg))
|
||||
GenericPackageBuilder::new(cfg.copy(), SharedCompilerResource::new(cfg))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -114,7 +121,9 @@ impl<Parser: ASTBuildable, Builder: BuildRunnable> Runnable for PackageBuilder<P
|
|||
}
|
||||
}
|
||||
|
||||
impl<Parser: ASTBuildable, Builder: Buildable> Buildable for PackageBuilder<Parser, Builder> {
|
||||
impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable> Buildable
|
||||
for GenericPackageBuilder<ASTBuilder, HIRBuilder>
|
||||
{
|
||||
fn inherit(cfg: ErgConfig, shared: SharedCompilerResource) -> Self {
|
||||
let mod_name = Str::from(cfg.input.file_stem());
|
||||
Self::new_with_cache(cfg, mod_name, shared)
|
||||
|
@ -140,13 +149,13 @@ impl<Parser: ASTBuildable, Builder: Buildable> Buildable for PackageBuilder<Pars
|
|||
}
|
||||
}
|
||||
|
||||
impl<Parser: ASTBuildable + 'static, Builder: BuildRunnable + 'static> BuildRunnable
|
||||
for PackageBuilder<Parser, Builder>
|
||||
impl<ASTBuilder: ASTBuildable + 'static, HIRBuilder: BuildRunnable + 'static> BuildRunnable
|
||||
for GenericPackageBuilder<ASTBuilder, HIRBuilder>
|
||||
{
|
||||
}
|
||||
|
||||
impl<Parser: ASTBuildable, Builder: Buildable + ContextProvider> ContextProvider
|
||||
for PackageBuilder<Parser, Builder>
|
||||
impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable + ContextProvider> ContextProvider
|
||||
for GenericPackageBuilder<ASTBuilder, HIRBuilder>
|
||||
{
|
||||
fn dir(&self) -> Dict<&VarName, &VarInfo> {
|
||||
self.main_builder.dir()
|
||||
|
@ -161,7 +170,9 @@ impl<Parser: ASTBuildable, Builder: Buildable + ContextProvider> ContextProvider
|
|||
}
|
||||
}
|
||||
|
||||
impl<Parser: ASTBuildable, Builder: Buildable> PackageBuilder<Parser, Builder> {
|
||||
impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable>
|
||||
GenericPackageBuilder<ASTBuilder, HIRBuilder>
|
||||
{
|
||||
pub fn new(cfg: ErgConfig, shared: SharedCompilerResource) -> Self {
|
||||
Self::new_with_cache(cfg, "<module>".into(), shared)
|
||||
}
|
||||
|
@ -173,7 +184,7 @@ impl<Parser: ASTBuildable, Builder: Buildable> PackageBuilder<Parser, Builder> {
|
|||
Self {
|
||||
cfg: cfg.copy(),
|
||||
shared: shared.clone(),
|
||||
main_builder: Builder::inherit_with_name(cfg, mod_name, shared),
|
||||
main_builder: HIRBuilder::inherit_with_name(cfg, mod_name, shared),
|
||||
cyclic: vec![],
|
||||
submodules: vec![],
|
||||
asts: Dict::new(),
|
||||
|
@ -187,7 +198,7 @@ impl<Parser: ASTBuildable, Builder: Buildable> PackageBuilder<Parser, Builder> {
|
|||
src: String,
|
||||
mode: &str,
|
||||
) -> Result<CompleteArtifact, IncompleteArtifact> {
|
||||
let mut ast_builder = Parser::new(self.cfg.copy());
|
||||
let mut ast_builder = ASTBuilder::new(self.cfg.copy());
|
||||
let artifact = ast_builder
|
||||
.build_ast(src)
|
||||
.map_err(|err| IncompleteArtifact::new(None, err.errors.into(), err.warns.into()))?;
|
||||
|
@ -242,6 +253,20 @@ impl<Parser: ASTBuildable, Builder: Buildable> PackageBuilder<Parser, Builder> {
|
|||
}
|
||||
}
|
||||
}
|
||||
Expr::Dummy(chunks) => {
|
||||
for chunk in chunks.iter_mut() {
|
||||
if let Err(err) = self.check_import(chunk, cfg) {
|
||||
result = Err(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Expr::Compound(chunks) => {
|
||||
for chunk in chunks.iter_mut() {
|
||||
if let Err(err) = self.check_import(chunk, cfg) {
|
||||
result = Err(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
result
|
||||
|
@ -284,7 +309,7 @@ impl<Parser: ASTBuildable, Builder: Buildable> PackageBuilder<Parser, Builder> {
|
|||
};
|
||||
let import_path = NormalizedPathBuf::from(import_path.clone());
|
||||
self.shared.graph.add_node_if_none(&import_path);
|
||||
let mut ast_builder = Parser::new(cfg.copy());
|
||||
let mut ast_builder = ASTBuilder::new(cfg.copy());
|
||||
let mut ast = match ast_builder.build_ast(src) {
|
||||
Ok(art) => {
|
||||
self.parse_errors
|
||||
|
@ -397,19 +422,19 @@ impl<Parser: ASTBuildable, Builder: Buildable> PackageBuilder<Parser, Builder> {
|
|||
return;
|
||||
}
|
||||
let run = move || {
|
||||
let mut builder = HIRBuilder::new_with_cache(cfg, name, shared.clone());
|
||||
let mut builder = HIRBuilder::inherit_with_name(cfg, name, shared.clone());
|
||||
let cache = if mode == "exec" {
|
||||
&shared.mod_cache
|
||||
} else {
|
||||
&shared.py_mod_cache
|
||||
};
|
||||
match builder.check(ast, mode) {
|
||||
match builder.build_from_ast(ast, mode) {
|
||||
Ok(artifact) => {
|
||||
cache.register(
|
||||
_path.clone(),
|
||||
raw_ast,
|
||||
Some(artifact.object),
|
||||
builder.pop_mod_ctx().unwrap(),
|
||||
builder.pop_context().unwrap(),
|
||||
);
|
||||
shared.warns.extend(artifact.warns);
|
||||
}
|
||||
|
@ -418,7 +443,7 @@ impl<Parser: ASTBuildable, Builder: Buildable> PackageBuilder<Parser, Builder> {
|
|||
_path.clone(),
|
||||
raw_ast,
|
||||
artifact.object,
|
||||
builder.pop_mod_ctx().unwrap(),
|
||||
builder.pop_context().unwrap(),
|
||||
);
|
||||
shared.warns.extend(artifact.warns);
|
||||
shared.errors.extend(artifact.errors);
|
||||
|
@ -463,15 +488,15 @@ impl<Parser: ASTBuildable, Builder: Buildable> PackageBuilder<Parser, Builder> {
|
|||
None
|
||||
};
|
||||
let mut builder =
|
||||
HIRBuilder::new_with_cache(cfg, self.mod_name(&path), self.shared.clone());
|
||||
match builder.check(ast, "declare") {
|
||||
HIRBuilder::inherit_with_name(cfg, self.mod_name(&path), self.shared.clone());
|
||||
match builder.build_from_ast(ast, "declare") {
|
||||
Ok(artifact) => {
|
||||
let ctx = builder.pop_mod_ctx().unwrap();
|
||||
let ctx = builder.pop_context().unwrap();
|
||||
py_mod_cache.register(path.clone(), raw_ast, Some(artifact.object), ctx);
|
||||
self.shared.warns.extend(artifact.warns);
|
||||
}
|
||||
Err(artifact) => {
|
||||
let ctx = builder.pop_mod_ctx().unwrap();
|
||||
let ctx = builder.pop_context().unwrap();
|
||||
py_mod_cache.register(path, raw_ast, artifact.object, ctx);
|
||||
self.shared.warns.extend(artifact.warns);
|
||||
self.shared.errors.extend(artifact.errors);
|
||||
|
|
|
@ -5,11 +5,12 @@ use erg_common::traits::{Locational, Runnable, Stream};
|
|||
use erg_common::{enum_unwrap, fn_name, log, set, Str, Triple};
|
||||
|
||||
use erg_parser::ast::{self, AscriptionKind, DefId, Identifier, TypeAppArgsKind, VarName, AST};
|
||||
use erg_parser::build_ast::ASTBuildable;
|
||||
use erg_parser::desugar::Desugarer;
|
||||
|
||||
use crate::context::instantiate::TyVarCache;
|
||||
use crate::context::{ClassDefType, Context, MethodContext, MethodPair, TraitImpl};
|
||||
use crate::lower::ASTLowerer;
|
||||
use crate::lower::GenericASTLowerer;
|
||||
use crate::ty::constructors::{array_t, mono, mono_q_tp, poly, v_enum};
|
||||
use crate::ty::free::{Constraint, HasLevel};
|
||||
use crate::ty::value::{GenTypeObj, TypeObj, ValueObj};
|
||||
|
@ -21,7 +22,7 @@ use crate::hir::HIR;
|
|||
use crate::varinfo::{Mutability, VarInfo, VarKind};
|
||||
use crate::{feature_error, hir};
|
||||
|
||||
impl ASTLowerer {
|
||||
impl<A: ASTBuildable> GenericASTLowerer<A> {
|
||||
fn declare_var(
|
||||
&mut self,
|
||||
sig: ast::VarSignature,
|
||||
|
|
|
@ -186,6 +186,15 @@ impl From<ParserRunnerError> for CompileError {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<CompileError> for ParserRunnerError {
|
||||
fn from(err: CompileError) -> Self {
|
||||
Self {
|
||||
core: *err.core,
|
||||
input: err.input,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ErrorDisplay for CompileError {
|
||||
fn core(&self) -> &ErrorCore {
|
||||
&self.core
|
||||
|
@ -512,6 +521,12 @@ impl From<ParserRunnerErrors> for CompileErrors {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<CompileErrors> for ParserRunnerErrors {
|
||||
fn from(err: CompileErrors) -> Self {
|
||||
Self::new(err.into_iter().map(|e| e.into()).collect())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<CompileError>> for CompileErrors {
|
||||
fn from(errs: Vec<CompileError>) -> Self {
|
||||
Self(errs)
|
||||
|
|
|
@ -27,6 +27,6 @@ pub mod transpile;
|
|||
pub mod ty;
|
||||
pub mod varinfo;
|
||||
|
||||
pub use build_hir::HIRBuilder;
|
||||
pub use build_hir::{GenericHIRBuilder, HIRBuilder};
|
||||
pub use erg_parser::build_ast::ASTBuilder;
|
||||
pub use transpile::Transpiler;
|
||||
|
|
|
@ -8,7 +8,7 @@ use erg_common::pathutil::NormalizedPathBuf;
|
|||
use erg_common::traits::{Locational, Runnable, Stream};
|
||||
use erg_common::Str;
|
||||
use erg_parser::ast::AST;
|
||||
use erg_parser::build_ast::ASTBuilder;
|
||||
use erg_parser::build_ast::ASTBuildable;
|
||||
use erg_parser::lex::Lexer;
|
||||
|
||||
use crate::context::ContextKind;
|
||||
|
@ -19,10 +19,10 @@ use crate::error::{
|
|||
CompileErrors, LowerError, LowerResult, LowerWarning, LowerWarnings, SingleLowerResult,
|
||||
};
|
||||
use crate::hir::{self, Expr, Signature, HIR};
|
||||
use crate::lower::ASTLowerer;
|
||||
use crate::lower::GenericASTLowerer;
|
||||
use crate::varinfo::VarInfo;
|
||||
|
||||
impl ASTLowerer {
|
||||
impl<ASTBuilder: ASTBuildable> GenericASTLowerer<ASTBuilder> {
|
||||
pub(crate) fn var_result_t_check(
|
||||
&self,
|
||||
loc: &impl Locational,
|
||||
|
@ -221,7 +221,7 @@ impl ASTLowerer {
|
|||
} else {
|
||||
format!("{}{code}", "\n".repeat(first_line as usize))
|
||||
};
|
||||
match ASTBuilder::new(self.cfg().clone()).build(code) {
|
||||
match ASTBuilder::new(self.cfg().clone()).build_ast(code) {
|
||||
Ok(artifact) => {
|
||||
self.check_doc_ast(artifact.ast);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
//! implements `ASTLowerer`.
|
||||
//!
|
||||
//! ASTLowerer(ASTからHIRへの変換器)を実装
|
||||
use std::marker::PhantomData;
|
||||
use std::mem;
|
||||
use std::path::Path;
|
||||
|
||||
|
@ -19,7 +20,7 @@ use erg_common::{fmt_option, fn_name, log, switch_lang, Str};
|
|||
|
||||
use erg_parser::ast::{self, AscriptionKind, DefId, InlineModule, VisModifierSpec};
|
||||
use erg_parser::ast::{OperationKind, TypeSpecWithOp, VarName, AST};
|
||||
use erg_parser::build_ast::ASTBuilder;
|
||||
use erg_parser::build_ast::{ASTBuildable, ASTBuilder as DefaultASTBuilder};
|
||||
use erg_parser::desugar::Desugarer;
|
||||
use erg_parser::token::{Token, TokenKind};
|
||||
use erg_parser::Parser;
|
||||
|
@ -45,12 +46,12 @@ use crate::error::{
|
|||
CompileError, CompileErrors, CompileWarning, LowerError, LowerErrors, LowerResult,
|
||||
LowerWarning, LowerWarnings, SingleLowerResult,
|
||||
};
|
||||
use crate::hir;
|
||||
use crate::hir::HIR;
|
||||
use crate::link_ast::ASTLinker;
|
||||
use crate::varinfo::{VarInfo, VarKind};
|
||||
use crate::AccessKind;
|
||||
use crate::{feature_error, unreachable_error};
|
||||
use crate::{hir, HIRBuilder};
|
||||
use crate::{AccessKind, GenericHIRBuilder};
|
||||
|
||||
use VisibilityModifier::*;
|
||||
|
||||
|
@ -66,15 +67,18 @@ pub fn expr_to_cast_target(expr: &ast::Expr) -> CastTarget {
|
|||
|
||||
/// Checks & infers types of an AST, and convert (lower) it into a HIR
|
||||
#[derive(Debug)]
|
||||
pub struct ASTLowerer {
|
||||
pub struct GenericASTLowerer<ASTBuilder: ASTBuildable = DefaultASTBuilder> {
|
||||
pub(crate) cfg: ErgConfig,
|
||||
pub(crate) module: ModuleContext,
|
||||
pub(crate) errs: LowerErrors,
|
||||
pub(crate) warns: LowerWarnings,
|
||||
fresh_gen: FreshNameGenerator,
|
||||
_parser: PhantomData<fn() -> ASTBuilder>,
|
||||
}
|
||||
|
||||
impl Default for ASTLowerer {
|
||||
pub type ASTLowerer = GenericASTLowerer<DefaultASTBuilder>;
|
||||
|
||||
impl<A: ASTBuildable> Default for GenericASTLowerer<A> {
|
||||
fn default() -> Self {
|
||||
Self::new_with_cache(
|
||||
ErgConfig::default(),
|
||||
|
@ -84,7 +88,7 @@ impl Default for ASTLowerer {
|
|||
}
|
||||
}
|
||||
|
||||
impl Runnable for ASTLowerer {
|
||||
impl<ASTBuilder: ASTBuildable> Runnable for GenericASTLowerer<ASTBuilder> {
|
||||
type Err = CompileError;
|
||||
type Errs = CompileErrors;
|
||||
const NAME: &'static str = "Erg lowerer";
|
||||
|
@ -123,7 +127,7 @@ impl Runnable for ASTLowerer {
|
|||
fn exec(&mut self) -> Result<ExitStatus, Self::Errs> {
|
||||
let mut ast_builder = ASTBuilder::new(self.cfg.copy());
|
||||
let artifact = ast_builder
|
||||
.build(self.cfg.input.read())
|
||||
.build_ast(self.cfg.input.read())
|
||||
.map_err(|artifact| artifact.errors)?;
|
||||
artifact.warns.write_all_to(&mut self.cfg.output);
|
||||
let artifact = self
|
||||
|
@ -137,7 +141,9 @@ impl Runnable for ASTLowerer {
|
|||
|
||||
fn eval(&mut self, src: String) -> Result<String, Self::Errs> {
|
||||
let mut ast_builder = ASTBuilder::new(self.cfg.copy());
|
||||
let artifact = ast_builder.build(src).map_err(|artifact| artifact.errors)?;
|
||||
let artifact = ast_builder
|
||||
.build_ast(src)
|
||||
.map_err(|artifact| artifact.errors)?;
|
||||
artifact.warns.write_all_stderr();
|
||||
let artifact = self
|
||||
.lower(artifact.ast, "eval")
|
||||
|
@ -147,7 +153,7 @@ impl Runnable for ASTLowerer {
|
|||
}
|
||||
}
|
||||
|
||||
impl ContextProvider for ASTLowerer {
|
||||
impl<A: ASTBuildable> ContextProvider for GenericASTLowerer<A> {
|
||||
fn dir(&self) -> Dict<&VarName, &VarInfo> {
|
||||
self.module.context.dir()
|
||||
}
|
||||
|
@ -161,7 +167,7 @@ impl ContextProvider for ASTLowerer {
|
|||
}
|
||||
}
|
||||
|
||||
impl Buildable for ASTLowerer {
|
||||
impl<ASTBuilder: ASTBuildable> Buildable for GenericASTLowerer<ASTBuilder> {
|
||||
fn inherit(cfg: ErgConfig, shared: SharedCompilerResource) -> Self {
|
||||
let mod_name = Str::from(cfg.input.file_stem());
|
||||
Self::new_with_cache(cfg, mod_name, shared)
|
||||
|
@ -177,7 +183,7 @@ impl Buildable for ASTLowerer {
|
|||
mode: &str,
|
||||
) -> Result<CompleteArtifact<HIR>, IncompleteArtifact<HIR>> {
|
||||
let mut ast_builder = ASTBuilder::new(self.cfg.copy());
|
||||
let artifact = ast_builder.build(src).map_err(|artifact| {
|
||||
let artifact = ast_builder.build_ast(src).map_err(|artifact| {
|
||||
IncompleteArtifact::new(None, artifact.errors.into(), artifact.warns.into())
|
||||
})?;
|
||||
self.lower(artifact.ast, mode)
|
||||
|
@ -199,9 +205,9 @@ impl Buildable for ASTLowerer {
|
|||
}
|
||||
}
|
||||
|
||||
impl BuildRunnable for ASTLowerer {}
|
||||
impl<A: ASTBuildable + 'static> BuildRunnable for GenericASTLowerer<A> {}
|
||||
|
||||
impl ASTLowerer {
|
||||
impl<ASTBuilder: ASTBuildable> GenericASTLowerer<ASTBuilder> {
|
||||
pub fn new_with_cache<S: Into<Str>>(
|
||||
cfg: ErgConfig,
|
||||
mod_name: S,
|
||||
|
@ -215,6 +221,7 @@ impl ASTLowerer {
|
|||
errs: LowerErrors::empty(),
|
||||
warns: LowerWarnings::empty(),
|
||||
fresh_gen: FreshNameGenerator::new("lower"),
|
||||
_parser: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -225,6 +232,7 @@ impl ASTLowerer {
|
|||
errs: LowerErrors::empty(),
|
||||
warns: LowerWarnings::empty(),
|
||||
fresh_gen: FreshNameGenerator::new("lower"),
|
||||
_parser: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -266,7 +274,7 @@ impl ASTLowerer {
|
|||
}
|
||||
}
|
||||
|
||||
impl ASTLowerer {
|
||||
impl<A: ASTBuildable> GenericASTLowerer<A> {
|
||||
pub(crate) fn lower_literal(
|
||||
&mut self,
|
||||
lit: ast::Literal,
|
||||
|
@ -2893,7 +2901,7 @@ impl ASTLowerer {
|
|||
};
|
||||
let parent = self.get_mod_ctx().context.get_module().unwrap().clone();
|
||||
let mod_ctx = ModuleContext::new(parent, dict! {});
|
||||
let mut builder = HIRBuilder::new_with_ctx(mod_ctx);
|
||||
let mut builder = GenericHIRBuilder::<A>::new_with_ctx(mod_ctx);
|
||||
builder.lowerer.module.context.cfg.input = inline.input.clone();
|
||||
builder.cfg_mut().input = inline.input.clone();
|
||||
let mode = if path.to_string_lossy().ends_with("d.er") {
|
||||
|
|
|
@ -6,7 +6,7 @@ use erg_common::config::{ErgConfig, ErgMode::*};
|
|||
use erg_common::spawn::exec_new_thread;
|
||||
use erg_common::traits::{ExitStatus, Runnable};
|
||||
|
||||
use erg_compiler::build_package::{FullPackageBuilder, PackageTypeChecker};
|
||||
use erg_compiler::build_package::{PackageBuilder, PackageTypeChecker};
|
||||
use erg_compiler::transpile::Transpiler;
|
||||
use erg_compiler::ty::deserialize::Deserializer;
|
||||
use erg_compiler::Compiler;
|
||||
|
@ -20,7 +20,7 @@ fn run() {
|
|||
Lex => LexerRunner::run(cfg),
|
||||
Parse => ParserRunner::run(cfg),
|
||||
TypeCheck => PackageTypeChecker::run(cfg),
|
||||
FullCheck => FullPackageBuilder::run(cfg),
|
||||
FullCheck => PackageBuilder::run(cfg),
|
||||
Transpile => Transpiler::run(cfg),
|
||||
Compile | Execute => Compiler::run(cfg),
|
||||
Read => Deserializer::run(cfg),
|
||||
|
|
|
@ -6,7 +6,7 @@ use erg_common::config::{ErgConfig, ErgMode::*};
|
|||
use erg_common::spawn::exec_new_thread;
|
||||
use erg_common::traits::{ExitStatus, Runnable};
|
||||
|
||||
use erg_compiler::build_package::{FullPackageBuilder, PackageTypeChecker};
|
||||
use erg_compiler::build_package::{PackageBuilder, PackageTypeChecker};
|
||||
use erg_parser::lex::LexerRunner;
|
||||
use erg_parser::ParserRunner;
|
||||
|
||||
|
@ -23,7 +23,7 @@ fn run() {
|
|||
Parse => ParserRunner::run(cfg),
|
||||
Desugar => ASTBuilder::run(cfg),
|
||||
TypeCheck => PackageTypeChecker::run(cfg),
|
||||
FullCheck => FullPackageBuilder::run(cfg),
|
||||
FullCheck => PackageBuilder::run(cfg),
|
||||
Compile => Compiler::run(cfg),
|
||||
Transpile => Transpiler::run(cfg),
|
||||
Execute => DummyVM::run(cfg),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue