-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinterpreter.rs
256 lines (218 loc) · 8 KB
/
interpreter.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use brainfuck_compilers::{parse, ARRAY_LEN, EOF, Error, Inst, InstKind};
use std::env;
use std::io;
use std::io::{Read, Write, Bytes};
fn run<R: Read, W: Write>(insts: &[Inst], mut input: Bytes<R>, output: &mut W) -> Result<(), Error> {
let mut memory = [0_u8; ARRAY_LEN];
let mut ptr = 0;
let mut inst_ptr = 0;
while inst_ptr < insts.len() {
let Inst {kind, times, ..} = &insts[inst_ptr];
match kind {
InstKind::IncPtr => {
ptr += times;
if ptr >= ARRAY_LEN {
return Err(Error::PtrAboveLimit);
}
inst_ptr += 1;
},
InstKind::DecPtr => {
if *times > ptr {
return Err(Error::PtrBelowZero);
}
ptr -= times;
inst_ptr += 1;
},
InstKind::IncByte => {
memory[ptr] = memory[ptr].wrapping_add(*times as u8);
inst_ptr += 1;
},
InstKind::DecByte => {
memory[ptr] = memory[ptr].wrapping_sub(*times as u8);
inst_ptr += 1;
},
InstKind::WriteByte => {
for _ in 0..*times {
let result = output.write(&[memory[ptr]]);
match result {
Ok(bytes_written) if bytes_written < 1 => {
panic!("failed to write byte {} to output", memory[ptr]);
},
Err(error) => {
panic!("failed to write byte to put with error {}", error);
},
Ok(_) => (), // everything went fine
}
}
inst_ptr += 1;
},
InstKind::ReadByte => {
// before asking user for some input we have to make
// sure they've seen our prompt / output
if let Err(error) = output.flush() {
panic!("error while flushing to output: {}", error);
}
for _ in 0..*times {
let maybe_byte = input.next();
match maybe_byte {
Some(Ok(byte)) => {
memory[ptr] = byte;
},
None => { // received EOF
memory[ptr] = EOF;
},
Some(Err(error)) => {
panic!("error while trying to read byte from input {}", error);
},
}
}
inst_ptr += 1;
},
InstKind::LoopStart { end_idx } => {
if memory[ptr] == 0 {
inst_ptr = *end_idx;
} else {
inst_ptr += 1;
}
},
InstKind::LoopEnd { start_idx } => {
if memory[ptr] != 0 {
inst_ptr = *start_idx;
} else {
inst_ptr += 1;
}
}
}
}
Ok(())
}
fn parse_and_run<R: Read, W: Write>(src: &str, input: Bytes<R>, output: &mut W) -> Result<(), Error> {
let insts = parse(src)?;
run(&insts, input, output)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
println!("USEAGE: program path/to/brainfuck/src.b");
std::process::exit(1);
}
let src = std::fs::read_to_string(&args[1])?;
let stdin = io::stdin();
let input = stdin.lock().bytes();
let stdout = io::stdout();
let mut output = stdout.lock();
parse_and_run(&src, input, &mut output)?;
output.flush()?;
Ok(())
}
mod tests {
#[allow(unused_imports)]
use super::*;
#[test]
fn run_hello_world() {
let src = include_str!("../input/hello_world.b");
let input = [].bytes();
let mut output = Vec::new();
let result = parse_and_run(src, input, &mut output);
assert!(result.is_ok());
let output_result = std::str::from_utf8(&output);
assert!(output_result.is_ok());
let output_str = output_result.unwrap();
assert_eq!(
"Hello World!\n",
output_str,
);
}
#[test]
fn run_mini_hello_world() {
let src = include_str!("../input/mini_hello_world.b");
let input = [].bytes();
let mut output = Vec::new();
let result = parse_and_run(src, input, &mut output);
assert!(result.is_ok());
let output_result = std::str::from_utf8(&output);
assert!(output_result.is_ok());
let output_str = output_result.unwrap();
assert_eq!(
"hello world",
output_str,
);
}
#[test]
fn run_fibonacci() {
let src = include_str!("../input/fibonacci.b");
let input = [].bytes();
let mut output = Vec::new();
let result = parse_and_run(src, input, &mut output);
assert!(result.is_ok());
let output_result = std::str::from_utf8(&output);
assert!(output_result.is_ok());
let output_str = output_result.unwrap();
assert_eq!(
"1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89",
output_str,
);
}
#[test]
fn run_quine() {
let src = include_str!("../input/quine.b");
let input = [].bytes();
let mut output = Vec::new();
let result = parse_and_run(src, input, &mut output);
assert!(result.is_ok());
let output_result = std::str::from_utf8(&output);
assert!(output_result.is_ok());
let output_str = output_result.unwrap();
assert_eq!(
src,
output_str,
);
}
#[test]
fn run_head() {
let src = include_str!("../input/head.b");
let input_str = "1a\n2b\n3c\n4d\n5e\n6f\n7g\n8h\n9i\n10j\n";
let input = input_str.as_bytes().bytes();
let mut output = Vec::new();
let result = parse_and_run(src, input, &mut output);
assert!(result.is_ok());
let output_result = std::str::from_utf8(&output);
assert!(output_result.is_ok());
let output_str = output_result.unwrap();
assert_eq!(
input_str,
output_str,
);
}
#[test]
fn run_squares() {
let src = include_str!("../input/squares.b");
let input = [].bytes();
let mut output = Vec::new();
let result = parse_and_run(src, input, &mut output);
assert!(result.is_ok());
let output_result = std::str::from_utf8(&output);
assert!(output_result.is_ok());
let output_str = output_result.unwrap();
assert_eq!(
"0\n1\n4\n9\n16\n25\n36\n49\n64\n81\n100\n121\n144\n169\n196\n225\n256\n289\n324\n361\n400\n441\n484\n529\n576\n625\n676\n729\n784\n841\n900\n961\n1024\n1089\n1156\n1225\n1296\n1369\n1444\n1521\n1600\n1681\n1764\n1849\n1936\n2025\n2116\n2209\n2304\n2401\n2500\n2601\n2704\n2809\n2916\n3025\n3136\n3249\n3364\n3481\n3600\n3721\n3844\n3969\n4096\n4225\n4356\n4489\n4624\n4761\n4900\n5041\n5184\n5329\n5476\n5625\n5776\n5929\n6084\n6241\n6400\n6561\n6724\n6889\n7056\n7225\n7396\n7569\n7744\n7921\n8100\n8281\n8464\n8649\n8836\n9025\n9216\n9409\n9604\n9801\n10000\n",
output_str,
);
}
#[test]
fn run_rot13() {
let src = include_str!("../input/rot13.b");
let input_str = "unencrypted string";
let input = input_str.as_bytes().bytes();
let mut output = Vec::new();
let result = parse_and_run(src, input, &mut output);
assert!(result.is_ok());
let output_result = std::str::from_utf8(&output);
assert!(output_result.is_ok());
let output_str = output_result.unwrap();
assert_eq!(
"harapelcgrq fgevat",
output_str,
);
}
}