mirror of
https://github.com/Noratrieb/brainfuck.git
synced 2026-01-14 21:35:02 +01:00
remove naive interpreter
This commit is contained in:
parent
2d854539aa
commit
66bd69e674
5 changed files with 73 additions and 28 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use crate::parse::Instr;
|
||||
use crate::opts::IrInstr;
|
||||
use std::io::{Read, Write};
|
||||
use std::num::Wrapping;
|
||||
|
||||
|
|
@ -6,50 +6,54 @@ const MEM_SIZE: usize = 32_000;
|
|||
|
||||
type Memory = [Wrapping<u8>; MEM_SIZE];
|
||||
|
||||
pub fn run(instrs: &[Instr<'_>]) {
|
||||
pub fn run(instrs: &[IrInstr<'_>]) {
|
||||
let mut mem = [Wrapping(0u8); MEM_SIZE];
|
||||
let mut ptr = 0;
|
||||
|
||||
execute(&mut mem, &mut ptr, instrs);
|
||||
}
|
||||
|
||||
fn execute(mem: &mut Memory, ptr: &mut usize, instrs: &[Instr<'_>]) {
|
||||
fn execute(mem: &mut Memory, ptr: &mut usize, instrs: &[IrInstr<'_>]) {
|
||||
for instr in instrs {
|
||||
match instr {
|
||||
Instr::Add => {
|
||||
mem[*ptr] += 1;
|
||||
IrInstr::Add(n) => {
|
||||
mem[*ptr] += n;
|
||||
}
|
||||
Instr::Sub => {
|
||||
mem[*ptr] -= 1;
|
||||
IrInstr::Sub(n) => {
|
||||
mem[*ptr] -= n;
|
||||
}
|
||||
Instr::Right => {
|
||||
*ptr += 1;
|
||||
IrInstr::Right(n) => {
|
||||
*ptr += n;
|
||||
if *ptr >= MEM_SIZE {
|
||||
*ptr = 0;
|
||||
}
|
||||
}
|
||||
Instr::Left => {
|
||||
if *ptr == 0 {
|
||||
*ptr = MEM_SIZE - 1;
|
||||
IrInstr::Left(n) => {
|
||||
if *ptr < *n {
|
||||
let diff = *n - *ptr;
|
||||
*ptr = MEM_SIZE - 1 - diff;
|
||||
} else {
|
||||
*ptr -= 1;
|
||||
*ptr -= n;
|
||||
}
|
||||
}
|
||||
Instr::Out => {
|
||||
IrInstr::Out => {
|
||||
let char = mem[*ptr].0 as char;
|
||||
print!("{char}",);
|
||||
print!("{char}");
|
||||
std::io::stdout().flush().unwrap();
|
||||
}
|
||||
Instr::In => {
|
||||
IrInstr::In => {
|
||||
let mut buf = [0; 1];
|
||||
std::io::stdin().read_exact(&mut buf).unwrap();
|
||||
mem[*ptr] = Wrapping(buf[0]);
|
||||
}
|
||||
Instr::Loop(body) => {
|
||||
IrInstr::Loop(body) => {
|
||||
while mem[*ptr] != Wrapping(0) {
|
||||
execute(mem, ptr, body);
|
||||
}
|
||||
}
|
||||
IrInstr::SetNull => {
|
||||
mem[*ptr] = Wrapping(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,8 @@
|
|||
use bumpalo::Bump;
|
||||
use std::{env, fs, process};
|
||||
|
||||
mod naive_interpreter;
|
||||
mod ir_interpreter;
|
||||
mod opts;
|
||||
mod parse;
|
||||
|
||||
fn main() {
|
||||
|
|
@ -25,5 +26,12 @@ fn main() {
|
|||
process::exit(1);
|
||||
});
|
||||
|
||||
naive_interpreter::run(&parsed);
|
||||
let ir_alloc = Bump::new();
|
||||
|
||||
let optimized_ir = opts::optimize(&ir_alloc, &parsed);
|
||||
|
||||
drop(parsed);
|
||||
drop(ast_alloc);
|
||||
|
||||
ir_interpreter::run(&optimized_ir);
|
||||
}
|
||||
|
|
|
|||
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