Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor lowering #184

Merged
merged 2 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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