mirror of
https://github.com/Noratrieb/brainfuck.git
synced 2026-01-14 13:35:00 +01:00
remove SetNull from IR
This commit is contained in:
parent
d9f163ef3a
commit
4d9648bf97
3 changed files with 13 additions and 8 deletions
|
|
@ -91,7 +91,7 @@ fn ir_to_stmt<'c>(code: &mut Code<'c>, ir_stmt: &IrStmt<'_>) {
|
||||||
StmtKind::Left(n) => Stmt::Left(u32::try_from(*n).unwrap()),
|
StmtKind::Left(n) => Stmt::Left(u32::try_from(*n).unwrap()),
|
||||||
StmtKind::Out => Stmt::Out,
|
StmtKind::Out => Stmt::Out,
|
||||||
StmtKind::In => Stmt::In,
|
StmtKind::In => Stmt::In,
|
||||||
StmtKind::SetNull => Stmt::SetNull,
|
StmtKind::SetN(0) => Stmt::SetNull,
|
||||||
StmtKind::SetN(n) => Stmt::SetN(*n),
|
StmtKind::SetN(n) => Stmt::SetN(*n),
|
||||||
StmtKind::Loop(instr) => {
|
StmtKind::Loop(instr) => {
|
||||||
let skip_jmp_idx = code.stmts.len();
|
let skip_jmp_idx = code.stmts.len();
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,17 @@ use crate::BumpVec;
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Ir<'ir> {
|
pub struct Ir<'ir> {
|
||||||
pub stmts: BumpVec<'ir, Stmt<'ir>>,
|
pub stmts: BumpVec<'ir, Stmt<'ir>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Debug for Ir<'_> {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
self.stmts.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Stmt<'ir> {
|
pub struct Stmt<'ir> {
|
||||||
pub kind: StmtKind<'ir>,
|
pub kind: StmtKind<'ir>,
|
||||||
|
|
@ -26,7 +32,7 @@ impl<'ir> Stmt<'ir> {
|
||||||
|
|
||||||
impl Debug for Stmt<'_> {
|
impl Debug for Stmt<'_> {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
f.debug_tuple("Stmt").field(&self.kind).finish()
|
self.kind.fmt(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,7 +45,6 @@ pub enum StmtKind<'ir> {
|
||||||
Loop(Ir<'ir>),
|
Loop(Ir<'ir>),
|
||||||
Out,
|
Out,
|
||||||
In,
|
In,
|
||||||
SetNull,
|
|
||||||
SetN(u8),
|
SetN(u8),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -143,7 +148,7 @@ fn pass_find_set_null(ir: &mut Ir<'_>) {
|
||||||
span,
|
span,
|
||||||
}] = body.stmts.as_slice()
|
}] = body.stmts.as_slice()
|
||||||
{
|
{
|
||||||
*stmt = Stmt::new(StmtKind::SetNull, *span);
|
*stmt = Stmt::new(StmtKind::SetN(0), *span);
|
||||||
} else {
|
} else {
|
||||||
pass_find_set_null(body);
|
pass_find_set_null(body);
|
||||||
}
|
}
|
||||||
|
|
@ -151,7 +156,7 @@ fn pass_find_set_null(ir: &mut Ir<'_>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// pass that replaces `SetNull Add(5)` with `SetN(5)`
|
/// pass that replaces `SetN(0) Add(5)` with `SetN(5)`
|
||||||
fn pass_set_n(ir: &mut Ir<'_>) {
|
fn pass_set_n(ir: &mut Ir<'_>) {
|
||||||
let stmts = &mut ir.stmts;
|
let stmts = &mut ir.stmts;
|
||||||
for i in 0..stmts.len() {
|
for i in 0..stmts.len() {
|
||||||
|
|
@ -165,7 +170,7 @@ fn pass_set_n(ir: &mut Ir<'_>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
let a = &stmts[i];
|
let a = &stmts[i];
|
||||||
if let StmtKind::SetNull = a.kind() {
|
if let StmtKind::SetN(0) = a.kind() {
|
||||||
let b = &stmts[i + 1];
|
let b = &stmts[i + 1];
|
||||||
let new = match b.kind() {
|
let new = match b.kind() {
|
||||||
StmtKind::Add(n) => StmtKind::SetN(*n),
|
StmtKind::Add(n) => StmtKind::SetN(*n),
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
[>[-]+++++++++++++++++++++++++[>[-]++++++[-]<-]<-]
|
++++++++++[>[-]++++++++++++++++++[>[-]+++++++++[-]<-]<-]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue