remove naive interpreter

This commit is contained in:
nora 2022-04-12 20:21:54 +02:00
parent 2d854539aa
commit 66bd69e674
5 changed files with 73 additions and 28 deletions

33
rust2/src/opts.rs Normal file
View 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
}