-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadderall.rs
92 lines (85 loc) · 3.02 KB
/
adderall.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
use std::env;
use std::fs::File;
use std::io::{self, Read};
const MEM_SIZE: usize = 30000;
const SRC_SIZE: usize = 30000;
struct Interpreter {
memory: [u8; MEM_SIZE],
ptr: usize,
source: Vec<u8>,
ip: usize,
}
impl Interpreter {
fn new() -> Self {
Interpreter {
memory: [0; MEM_SIZE],
ptr: 0,
source: Vec::with_capacity(SRC_SIZE),
ip: 0,
}
}
fn execute(&mut self) {
while self.ip < self.source.len() {
match self.source[self.ip] as char {
'>' => self.ptr = (self.ptr + 1) % MEM_SIZE,
'<' => self.ptr = (self.ptr + MEM_SIZE - 1) % MEM_SIZE,
'+' => self.memory[self.ptr] = self.memory[self.ptr].wrapping_add(1),
'-' => self.memory[self.ptr] = self.memory[self.ptr].wrapping_sub(1),
'.' => print!("{}", self.memory[self.ptr] as char),
',' => {
let mut input = [0; 1];
if io::stdin().read_exact(&mut input).is_ok() {
self.memory[self.ptr] = input[0];
}
}
'[' => {
if self.memory[self.ptr] == 0 {
let mut loop_count = 1;
while loop_count > 0 {
self.ip += 1;
match self.source[self.ip] as char {
'[' => loop_count += 1,
']' => loop_count -= 1,
_ => {}
}
}
}
}
']' => {
if self.memory[self.ptr] != 0 {
let mut loop_count = 1;
while loop_count > 0 {
self.ip -= 1;
match self.source[self.ip] as char {
'[' => loop_count -= 1,
']' => loop_count += 1,
_ => {}
}
}
self.ip -= 1;
}
}
_ => {}
}
self.ip += 1;
}
}
}
fn main() -> io::Result<()> {
let args: Vec<String> = env::args().collect();
let mut interpreter = Interpreter::new();
if args.len() == 1 {
println!("\n **** Adderall: A Brainfuck Interpreter ****\n");
println!(" 1. Type or paste in brainfuck source code");
println!(" 2. Use Ctrl-D (Unix) or Ctrl-Z (Windows) to run the code");
println!(" 3. Use rustc adderall.rs && ./adderall [filename] to execute source file");
io::stdin().read_to_end(&mut interpreter.source)?;
interpreter.execute();
println!();
} else if args.len() == 2 {
let mut file = File::open(&args[1])?;
file.read_to_end(&mut interpreter.source)?;
interpreter.execute();
}
Ok(())
}