Skip to content

Instantly share code, notes, and snippets.

@shadowmint
Created August 26, 2014 02:23
Show Gist options
  • Save shadowmint/53140e43ea8e45bf7d9a to your computer and use it in GitHub Desktop.
Save shadowmint/53140e43ea8e45bf7d9a to your computer and use it in GitHub Desktop.
/// Invoke this when the last 'length' nodes are all the same repeat type.
/// If the node before the first one is *also* that type, merge into that
/// node; otherwise perform the default repeat action.
fn reduce_repeat<'a>(&self, stack:&mut Vec<AstNode<'a, T>>, id:T, length:uint) {
let mut merged:Vec<AstNode<'a, T>> = Vec::new();
for i in range(0, length) {
merged.push(stack.pop().unwrap());
}
trace!("Trying to peform a repeat reducation...!")
// Look for a previous parent, if not create a new one
let parent:AstNode<'a, T>;
if (stack.len() > 0) && ((*stack)[stack.len() - 1].id.repr() == id.repr()) {
parent = stack.pop().unwrap();
}
else {
parent = AstNode {
id: id,
symbol: None,
children: Vec::new()
};
}
// Todo: Push children back on, haha... fuck.
// Push parent back onto the stack
stack.push(parent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment