mirror of
https://github.com/Noratrieb/brainfuck.git
synced 2026-01-16 14:25:03 +01:00
remove naive interpreter
This commit is contained in:
parent
2d854539aa
commit
66bd69e674
5 changed files with 73 additions and 28 deletions
33
rust2/src/opts.rs
Normal file
33
rust2/src/opts.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
use crate::parse::Instr;
|
||||
use bumpalo::Bump;
|
||||
|
||||
pub type Ir<'ir> = Vec<IrInstr<'ir>, &'ir Bump>;
|
||||
|
||||
pub enum IrInstr<'ir> {
|
||||
Add(u8),
|
||||
Sub(u8),
|
||||
Right(usize),
|
||||
Left(usize),
|
||||
Loop(Ir<'ir>),
|
||||
Out,
|
||||
In,
|
||||
SetNull,
|
||||
}
|
||||
|
||||
pub fn optimize<'ir>(alloc: &'ir Bump, instrs: &[Instr<'_>]) -> Ir<'ir> {
|
||||
ast_to_ir(alloc, instrs)
|
||||
}
|
||||
|
||||
fn ast_to_ir<'ir>(alloc: &'ir Bump, ast: &[Instr<'_>]) -> Ir<'ir> {
|
||||
let mut vec = Vec::new_in(alloc);
|
||||
vec.extend(ast.iter().map(|instr| match instr {
|
||||
Instr::Add => IrInstr::Add(1),
|
||||
Instr::Sub => IrInstr::Sub(1),
|
||||
Instr::Right => IrInstr::Right(1),
|
||||
Instr::Left => IrInstr::Left(1),
|
||||
Instr::Out => IrInstr::Out,
|
||||
Instr::In => IrInstr::In,
|
||||
Instr::Loop(body) => IrInstr::Loop(ast_to_ir(alloc, body)),
|
||||
}));
|
||||
vec
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue