Skip to content

Commit

Permalink
Start testing decl info stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Feb 7, 2024
1 parent 18cd224 commit 46bce2b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
34 changes: 29 additions & 5 deletions crates/h10/src/decl_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Implements geneartion of defined and used values and types of top-level bindings.
#[cfg(test)]
mod tests;

use crate::ast;
use crate::collections::Set;
use crate::id::Id;
Expand Down Expand Up @@ -56,6 +59,7 @@ impl DeclInfo {
&mut info,
&mut Default::default(),
&mut Default::default(),
true,
),
ast::TopDeclKind::Type(ty) => analyze_ty_syn(ty, &mut info),
ast::TopDeclKind::KindSig(kind_sig) => analyze_kind_sig(kind_sig, &mut info),
Expand Down Expand Up @@ -93,6 +97,7 @@ fn analyze_value_decl<'a, 'b, 'c, 'd>(
info: &'b mut DeclInfo,
bound_ty_vars: &'c mut ScopeSet<&'a Id>,
local_bound_vars: &'d mut ScopeSet<&'a Id>,
top_level: bool,
) {
match &value.node {
ast::ValueDecl_::TypeSig {
Expand Down Expand Up @@ -138,7 +143,7 @@ fn analyze_value_decl<'a, 'b, 'c, 'd>(
}

ast::ValueDecl_::Value { lhs, rhs } => {
analyze_lhs(lhs, info, local_bound_vars);
analyze_lhs(lhs, info, local_bound_vars, top_level);
analyze_rhs(rhs, info, local_bound_vars);
}
}
Expand Down Expand Up @@ -189,6 +194,7 @@ fn analyze_lhs<'a>(
lhs: &'a ast::Lhs,
info: &mut DeclInfo,
local_bound_vars: &mut ScopeSet<&'a Id>,
top_level: bool,
) {
match &lhs.node {
ast::Lhs_::Pat(pat) => analyze_pat(pat, info, local_bound_vars),
Expand All @@ -197,6 +203,10 @@ fn analyze_lhs<'a>(
local_bound_vars.bind(var);
pats.iter()
.for_each(|pat| analyze_pat(pat, info, local_bound_vars));

if top_level {
info.defines.values.insert(var.clone());
}
}
}
}
Expand Down Expand Up @@ -276,7 +286,9 @@ fn analyze_exp<'a>(
) {
match &exp.node {
ast::Exp_::Var(var) | ast::Exp_::Con(var) => {
info.uses.values.insert(var.clone());
if !local_bound_vars.is_bound(var) {
info.uses.values.insert(var.clone());
}
}

ast::Exp_::Lit(_) => {}
Expand Down Expand Up @@ -389,7 +401,7 @@ fn analyze_stmt<'a>(
ast::Stmt_::Let(decls) => {
collect_local_binders(decls, info, local_bound_vars);
for decl in decls {
analyze_value_decl(decl, info, &mut Default::default(), local_bound_vars);
analyze_value_decl(decl, info, &mut Default::default(), local_bound_vars, false);
}
}
}
Expand Down Expand Up @@ -548,7 +560,13 @@ fn analyze_class<'a, 'b>(class: &'a ast::ClassDecl, info: &'b mut DeclInfo) {
}

for decl in decls {
analyze_value_decl(decl, info, &mut bound_ty_vars, &mut Default::default());
analyze_value_decl(
decl,
info,
&mut bound_ty_vars,
&mut Default::default(),
true,
);
}
}

Expand Down Expand Up @@ -579,7 +597,13 @@ fn analyze_instance<'a, 'b>(instance: &'a ast::InstanceDecl, info: &'b mut DeclI
analyze_ty(ty, info, &bound_ty_vars);

for decl in decls {
analyze_value_decl(decl, info, &mut bound_ty_vars, &mut Default::default());
analyze_value_decl(
decl,
info,
&mut bound_ty_vars,
&mut Default::default(),
true,
);
}
}

Expand Down
47 changes: 47 additions & 0 deletions crates/h10/src/decl_info/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use super::*;
use crate::ast::TopDecl;
use crate::parser::parse_module;

#[test]
fn id() {
let pgm = "id x = x";
let mut decl_infos = program_decl_infos(pgm);
assert_eq!(decl_infos.len(), 1);

let DeclInfo {
defines: Defs {
tys: def_tys,
values: def_values,
},
uses: Uses {
tys: used_tys,
values: used_values,
},
} = decl_infos.remove(0);

assert_eq!(to_sorted_vec(&def_tys), empty_id_vec());
assert_eq!(to_sorted_vec(&def_values), vec!["id"]);
assert_eq!(to_sorted_vec(&used_tys), empty_id_vec());
assert_eq!(to_sorted_vec(&used_values), empty_id_vec());
}

fn to_sorted_vec(set: &Set<String>) -> Vec<&str> {
let mut values: Vec<&str> = set.iter().map(|s| s.as_str()).collect();
values.sort();
values
}

fn empty_id_vec<'a>() -> Vec<&'a str> {
Vec::<&str>::new()
}

fn program_decl_infos(pgm: &str) -> Vec<DeclInfo> {
let decls: Vec<TopDecl> = parse_module(pgm).unwrap();
let mut decl_infos: Vec<DeclInfo> = Vec::with_capacity(decls.len());

for decl in decls {
decl_infos.push(DeclInfo::new(&decl.kind));
}

decl_infos
}

0 comments on commit 46bce2b

Please sign in to comment.