Skip to content

Commit

Permalink
Refactor lowering (#184)
Browse files Browse the repository at this point in the history
* Split lowering/imp into downsweep and lower modules

Lowering conceptually happens in two consecutive phases, but these were
defined in the same module. This commit splits the logic into
the two files lower.rs and downsweep.rs.

* Split lowering/lower.rs into lower/exp.rs and lower/decls.rs
  • Loading branch information
BinderDavid authored Apr 21, 2024
1 parent f3d9b77 commit 562bb0e
Show file tree
Hide file tree
Showing 6 changed files with 814 additions and 784 deletions.
98 changes: 98 additions & 0 deletions lang/lowering/src/downsweep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use codespan::Span;
use miette_util::ToMiette;

use parser::cst;
use parser::cst::exp::Ident;
use syntax::common::*;
use syntax::generic::lookup_table;
use syntax::generic::lookup_table::DeclMeta;

use super::result::*;

/// Build the structure tracking the declaration order in the source code
pub fn build_lookup_table(
items: &[cst::decls::Decl],
) -> Result<(HashMap<Ident, DeclMeta>, lookup_table::LookupTable), LoweringError> {
let mut lookup_table = lookup_table::LookupTable::default();
let mut top_level_map = HashMap::default();

let mut add_top_level_decl = |name: &Ident, span: &Span, decl_kind: DeclMeta| {
if top_level_map.contains_key(name) {
return Err(LoweringError::AlreadyDefined {
name: name.to_owned(),
span: Some(span.to_miette()),
});
}
top_level_map.insert(name.clone(), decl_kind);
Ok(())
};

for item in items {
match item {
cst::decls::Decl::Data(data) => {
// top_level_map
add_top_level_decl(
&data.name,
&data.span,
DeclMeta::Data { arity: data.params.len() },
)?;
for ctor in &data.ctors {
add_top_level_decl(
&ctor.name,
&ctor.span,
DeclMeta::Ctor { ret_typ: data.name.clone() },
)?;
}

// lookup_table
let mut typ_decl = lookup_table.add_type_decl(data.name.clone());
let xtors = data.ctors.iter().map(|ctor| ctor.name.clone());
typ_decl.set_xtors(xtors);
}
cst::decls::Decl::Codata(codata) => {
// top_level_map
add_top_level_decl(
&codata.name,
&codata.span,
DeclMeta::Codata { arity: codata.params.len() },
)?;
for dtor in &codata.dtors {
add_top_level_decl(
&dtor.name,
&dtor.span,
DeclMeta::Dtor { self_typ: codata.name.clone() },
)?;
}

// lookup_table
let mut typ_decl = lookup_table.add_type_decl(codata.name.clone());
let xtors = codata.dtors.iter().map(|ctor| ctor.name.clone());
typ_decl.set_xtors(xtors);
}
cst::decls::Decl::Def(def) => {
// top_level_map
add_top_level_decl(&def.name, &def.span, DeclMeta::Def)?;

// lookup_table
let type_name = def.scrutinee.typ.name.clone();
lookup_table.add_def(type_name, def.name.to_owned());
}
cst::decls::Decl::Codef(codef) => {
// top_level_map
add_top_level_decl(&codef.name, &codef.span, DeclMeta::Codef)?;

// lookup_table
let type_name = codef.typ.name.clone();
lookup_table.add_def(type_name, codef.name.to_owned())
}
cst::decls::Decl::Let(tl_let) => {
// top_level_map
add_top_level_decl(&tl_let.name, &tl_let.span, DeclMeta::Let)?;

lookup_table.add_let(tl_let.name.clone());
}
}
}

Ok((top_level_map, lookup_table))
}
Loading

0 comments on commit 562bb0e

Please sign in to comment.