use leo_ast::{AstReconstructor, Block, Statement, *};
use crate::SymbolTable;
pub fn duplicate(block: Block, symbol_table: &mut SymbolTable, node_builder: &NodeBuilder) -> Block {
Duplicator { symbol_table, node_builder }.reconstruct_block(block).0
}
struct Duplicator<'a> {
symbol_table: &'a mut SymbolTable,
node_builder: &'a NodeBuilder,
}
impl Duplicator<'_> {
fn in_scope_duped<T>(&mut self, old_id: NodeID, func: impl FnOnce(&mut Self, NodeID) -> T) -> T {
let new_id = self.symbol_table.enter_scope_duped(old_id, self.node_builder);
let result = func(self, new_id);
self.symbol_table.enter_parent();
result
}
}
impl AstReconstructor for Duplicator<'_> {
type AdditionalInput = ();
type AdditionalOutput = ();
fn reconstruct_statement(&mut self, input: Statement) -> (Statement, Self::AdditionalOutput) {
match input {
Statement::Block(stmt) => {
let (stmt, output) = self.reconstruct_block(stmt);
(stmt.into(), output)
}
Statement::Conditional(stmt) => self.reconstruct_conditional(stmt),
Statement::Iteration(stmt) => self.reconstruct_iteration(*stmt),
stmt => (stmt, Default::default()),
}
}
fn reconstruct_block(&mut self, mut input: Block) -> (Block, Self::AdditionalOutput) {
self.in_scope_duped(input.id(), |slf, new_id| {
input.id = new_id;
input.statements = input.statements.into_iter().map(|stmt| slf.reconstruct_statement(stmt).0).collect();
(input, Default::default())
})
}
fn reconstruct_conditional(&mut self, mut input: ConditionalStatement) -> (Statement, Self::AdditionalOutput) {
input.then = self.reconstruct_block(input.then).0;
if let Some(mut otherwise) = input.otherwise {
*otherwise = self.reconstruct_statement(*otherwise).0;
input.otherwise = Some(otherwise);
}
(input.into(), Default::default())
}
fn reconstruct_iteration(&mut self, mut input: IterationStatement) -> (Statement, Self::AdditionalOutput) {
self.in_scope_duped(input.id(), |slf, new_id| {
input.id = new_id;
input.block = slf.reconstruct_block(input.block).0;
(input.into(), Default::default())
})
}
}