-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
203 lines (188 loc) · 6.41 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! An implementation of [Brainfuck].
//!
//! This implementation uses a cyclic memory tape of fixed length (65536) with 8-bit wrapping cells.
//! On EOF, `,` command does not modify the current cell.
//!
//! Since optimizing Brainfuck is a well-studied area and there are various extremely
//! performant implementations out there, this one mostly serves as a practice implementation
//! of a naive bytecode interpreter.
//!
//! [Brainfuck]: https://esolangs.org/wiki/Brainfuck
use std::io::{self, BufRead, Write};
use thiserror::Error;
/// Error enum for Brainfuck.
#[derive(Error, Debug)]
pub enum Error {
/// Syntax error; the source code contains unmatched brackets.
#[error("unmatched bracket `{0}`")]
SyntaxError(char),
/// I/O error, which may occur during I/O operations.
#[error("unexpected I/O error")]
IoError(#[from] io::Error),
}
const MEMORY_SIZE: usize = 65536;
/// Brainfuck interpreter.
pub fn run<I: BufRead, O: Write>(source: &str, input: &mut I, output: &mut O) -> Result<(), Error> {
let basic_blocks = into_basic_blocks(source)?;
let mut bb_no = 0usize;
let mut memory = vec![0u8; MEMORY_SIZE];
let mut ptr = 0usize;
loop {
let BasicBlock { instrs, jz, jnz } = &basic_blocks[bb_no];
for &instr in instrs {
match instr {
Cmd::Inc => memory[ptr] = memory[ptr].wrapping_add(1),
Cmd::Dec => memory[ptr] = memory[ptr].wrapping_sub(1),
Cmd::Left => {
ptr = ptr.wrapping_sub(1) % MEMORY_SIZE;
}
Cmd::Right => {
ptr = (ptr + 1) % MEMORY_SIZE;
}
Cmd::Getc => {
if let Some(byte) = getc(input)? {
memory[ptr] = byte;
}
}
Cmd::Putc => putc(output, memory[ptr])?,
}
}
if let &Some(next_bb) = if memory[ptr] == 0 { jz } else { jnz } {
bb_no = next_bb;
} else {
break;
}
}
Ok(())
}
#[derive(Debug, Clone, Copy)]
enum Cmd {
Inc,
Dec,
Left,
Right,
Getc,
Putc,
}
#[derive(Debug)]
struct BasicBlock {
instrs: Vec<Cmd>,
jz: Option<usize>,
jnz: Option<usize>,
}
type ByteCodeProgram = Vec<BasicBlock>;
fn into_basic_blocks(source: &str) -> Result<ByteCodeProgram, Error> {
let mut bbno_stack = vec![]; // stores ids right before `[`
let mut basic_blocks = vec![];
let mut cur_basic_block = vec![];
let mut cur_bb_id = 0usize;
for c in source.chars() {
match c {
'+' => cur_basic_block.push(Cmd::Inc),
'-' => cur_basic_block.push(Cmd::Dec),
'<' => cur_basic_block.push(Cmd::Left),
'>' => cur_basic_block.push(Cmd::Right),
',' => cur_basic_block.push(Cmd::Getc),
'.' => cur_basic_block.push(Cmd::Putc),
'[' => {
// starts next basic block
// jnz target is always bb+1; handle jz target when `]` is found
let bb = BasicBlock {
instrs: cur_basic_block,
jz: None,
jnz: Some(cur_bb_id + 1),
};
basic_blocks.push(bb);
bbno_stack.push(cur_bb_id);
cur_bb_id += 1;
cur_basic_block = vec![];
}
']' => {
// starts next basic block
// jz target is bb+1; jnz target is popped+1; jz of popped is bb+1
let popped = bbno_stack.pop().ok_or(Error::SyntaxError(']'))?;
let bb = BasicBlock {
instrs: cur_basic_block,
jz: Some(cur_bb_id + 1),
jnz: Some(popped + 1),
};
basic_blocks.push(bb);
basic_blocks[popped].jz = Some(cur_bb_id + 1);
cur_bb_id += 1;
cur_basic_block = vec![];
}
_ => (),
}
}
if !bbno_stack.is_empty() {
return Err(Error::SyntaxError('['));
}
let bb = BasicBlock {
instrs: cur_basic_block,
jz: None,
jnz: None,
};
basic_blocks.push(bb);
Ok(basic_blocks)
}
fn getc<I: BufRead>(input: &mut I) -> Result<Option<u8>, Error> {
let buf = input.fill_buf()?;
let value = buf.get(0).copied();
input.consume(1);
Ok(value)
}
fn putc<O: Write>(output: &mut O, byte: u8) -> Result<(), Error> {
output.write_all(&[byte][..])?;
Ok(())
}
#[cfg(test)]
mod tests {
use std::io::BufReader;
use super::*;
#[test]
fn test_printer_1() {
// from https://codegolf.stackexchange.com/a/108062/78410
let code = "+[[-<]-[->]<-]<.<<<<.>>>>-.<<-.<.>>.<<<+++.>>>---.<++.";
let mut stdin = BufReader::new(&b""[..]);
let mut stdout: Vec<u8> = vec![];
let res = run(code, &mut stdin, &mut stdout);
assert!(res.is_ok());
assert_eq!(stdout, b"brainfuck");
}
#[test]
fn test_printer_2() {
// from https://codegolf.stackexchange.com/a/99016/78410
let code = "++[>+++++<-]++[>>>+++++<<<-]++++++[>>+++++++<<-]>[>..........>.<<-]";
let mut stdin = BufReader::new(&b""[..]);
let mut stdout: Vec<u8> = vec![];
let res = run(code, &mut stdin, &mut stdout);
assert!(res.is_ok());
assert_eq!(stdout, b"**********\n**********\n**********\n**********\n**********\n**********\n**********\n**********\n**********\n**********\n");
}
#[test]
fn test_io_1() {
// from https://codegolf.stackexchange.com/a/210478/78410
let code = "+[,>++++[<-------->-]<]>++++[<++++++++>-]<[>,]++++[<-------->-]<[[-]++++[<-------->-]<]<[<]>>[.>]";
let testcases = [
&b"Samantha Vee Hills"[..],
b"Bob Dillinger",
b"John Jacob Jingleheimer Schmidt",
b"Jose Mario Carasco-Williams",
b"James Alfred Van Allen",
];
let outputs = [
&b"Vee"[..],
b"",
b"Jacob Jingleheimer",
b"Mario",
b"Alfred Van",
];
for (testcase, expected) in testcases.into_iter().zip(outputs) {
let mut stdin = BufReader::new(testcase);
let mut stdout: Vec<u8> = vec![];
let res = run(code, &mut stdin, &mut stdout);
assert!(res.is_ok());
assert_eq!(stdout, expected);
}
}
}