mirror of
https://github.com/Noratrieb/brainfuck.git
synced 2026-01-14 13:35:00 +01:00
fix pass_group to use recursively optimize loop if loop is the first statement
This commit is contained in:
parent
53ad68e3f2
commit
c130627b63
2 changed files with 13 additions and 2 deletions
|
|
@ -40,6 +40,9 @@ where
|
|||
|
||||
let code = codegen::generate(&cg_alloc, &optimized_ir);
|
||||
|
||||
println!("{:#?}", optimized_ir);
|
||||
return Ok(());
|
||||
|
||||
drop(optimized_ir);
|
||||
drop(ir_alloc);
|
||||
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ fn ast_to_ir<'ir>(alloc: &'ir Bump, ast: &[(Instr<'_>, Span)]) -> Ir<'ir> {
|
|||
Instr::Out => StmtKind::Out,
|
||||
Instr::In => StmtKind::In,
|
||||
Instr::Loop(body) => {
|
||||
let ir_body = ast_to_ir(alloc, &body);
|
||||
let ir_body = ast_to_ir(alloc, body);
|
||||
StmtKind::Loop(ir_body)
|
||||
}
|
||||
};
|
||||
|
|
@ -72,7 +72,15 @@ fn pass_group<'ir>(alloc: &'ir Bump, ir: Ir<'ir>) -> Ir<'ir> {
|
|||
.into_iter()
|
||||
.fold(new_stmts, |mut stmts: BumpVec<'ir, Stmt<'ir>>, next| {
|
||||
let Some(old) = stmts.last_mut() else {
|
||||
if let StmtKind::Loop(body) = next.kind {
|
||||
let new_body = pass_group(alloc, body);
|
||||
stmts.push(Stmt {
|
||||
span: next.span,
|
||||
kind: StmtKind::Loop(new_body),
|
||||
});
|
||||
} else {
|
||||
stmts.push(next);
|
||||
}
|
||||
return stmts;
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue