handle Suffixed is in the middle of our Defs

This commit is contained in:
Luke Boswell 2024-03-20 11:48:08 +11:00
parent 248c2a3f34
commit cc10df6db9
No known key found for this signature in database
GPG key ID: F6DB3C9DB47377B0
2 changed files with 84 additions and 30 deletions

View file

@ -503,38 +503,32 @@ impl<'a> Defs<'a> {
.split()
{
Ok(type_index) => {
let index = type_index.index();
let index = type_index.index();
// remove from vec
self.type_defs.remove(index);
// update all of the remaining indexes in type_defs
// update all of the remaining indexes in type_defs
for (tag_index, tag) in self.tags.iter_mut().enumerate() {
// only update later indexes into type_defs
if tag_index > index && tag.split().is_ok() {
tag.decrement_index();
}
}
}
Err(value_index) => {
let index: usize = value_index.index();
let index: usize = value_index.index();
// remove from vec
self.value_defs.remove(index);
// update all of the remaining indexes in value_defs
// update all of the remaining indexes in value_defs
for (tag_index, tag) in self.tags.iter_mut().enumerate() {
// only update later indexes into value_defs
if tag_index > index && tag.split().is_err() {
tag.decrement_index();
}
}
}
}
self.tags.remove(index);
@ -605,7 +599,7 @@ impl<'a> Defs<'a> {
pub fn search_suffixed_defs(&self) -> Option<(usize, usize)> {
for (tag_index, tag) in self.tags.iter().enumerate() {
let index = match tag.split() {
Ok(_) => break,
Ok(_) => continue,
Err(value_index) => value_index.index(),
};
@ -615,16 +609,33 @@ impl<'a> Defs<'a> {
Expr::Suffixed(_) => {
return Some((tag_index, index));
}
_ => break,
_ => continue,
},
_ => break,
_ => continue,
},
_ => break,
_ => continue,
}
}
None
}
// For desugaring Suffixed Defs we need to split the defs around the Suffixed value
pub fn split_values_either_side_of(&self, index: usize) -> (Self, Self) {
let mut before = self.clone();
let mut after = self.clone();
before.tags = self.tags[0..index].to_vec();
if index + 1 > self.tags.len() {
after.tags = self.tags.clone();
after.tags.clear();
} else {
after.tags = self.tags[(index + 1)..].to_vec();
}
(before, after)
}
}
/// Should always be a zero-argument `Apply`; we'll check this in canonicalization