Skip to content

Commit

Permalink
[sc-489] Improve implementation of AST iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
developedby committed Mar 6, 2024
1 parent 323fd10 commit 5804450
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 519 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(box_patterns)]
#![feature(let_chains)]
#![feature(trusted_len)]

use diagnostics::{Info, Warning};
use hvmc::{
Expand Down
49 changes: 24 additions & 25 deletions src/term/check/unbound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,34 +82,33 @@ pub fn check_uses<'a>(
globals: &mut HashMap<&'a Name, (usize, usize)>,
errs: &mut Vec<UnboundVarErr>,
) {
Term::recursive_call(move || {
match term {
Term::Var { nam } => {
if !scope.contains_key(nam) {
errs.push(UnboundVarErr::Local(nam.clone()));
*term = Term::Err;
}
}
Term::Chn { nam, bod, .. } => {
if let Some(nam) = nam {
globals.entry(nam).or_default().0 += 1;
}
check_uses(bod, scope, globals, errs);
Term::recursive_call(move || match term {
Term::Var { nam } => {
if !scope.contains_key(nam) {
errs.push(UnboundVarErr::Local(nam.clone()));
*term = Term::Err;
}
Term::Lnk { nam } => {
globals.entry(nam).or_default().1 += 1;
}
Term::Chn { nam, bod, .. } => {
if let Some(nam) = nam {
globals.entry(nam).or_default().0 += 1;
}
check_uses(bod, scope, globals, errs);
}
Term::Lnk { nam } => {
globals.entry(nam).or_default().1 += 1;
}

_ => {
for (child, binds) in term.children_mut_with_binds() {
// TODO: how to avoid creating this vec for the binds?
for bind in binds.iter() {
push_scope(bind.as_ref(), scope);
}
check_uses(child, scope, globals, errs);
for bind in binds.iter().rev() {
pop_scope(bind.as_ref(), scope);
}
_ => {
for (child, binds) in term.children_mut_with_binds() {
let binds: Vec<_> = binds.collect();

for bind in binds.iter() {
push_scope(bind.as_ref(), scope);
}
check_uses(child, scope, globals, errs);
for bind in binds.iter().rev() {
pop_scope(bind.as_ref(), scope);
}
}
}
Expand Down
Loading

0 comments on commit 5804450

Please sign in to comment.