bfcore/lib.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
#![no_std]
use core::mem::size_of;
/// 16 bit instruction pointer
const TAPE_SIZE: usize = 65535;
/// How many nested loops allowed
const NESTED_LOOP_LIMIT: usize = 1024;
/// Change this to u16 for 16 bit cell support
type Cell = u8;
/// Calculate the MAX_SIZE for a Cell based on its number of bytes
const MAX_SIZE: Cell = ((1 << (size_of::<Cell>() * 8)) - 1) as Cell;
/// Trait for getting a character
pub trait Input {
fn input(&mut self) -> char {
'\0'
}
}
/// Trait for outputing a character
pub trait Output {
fn output(&mut self, _: char) {}
}
/// The struct containing the data for interpretting brainfuck code
pub struct Interpreter<'a, I, O>
where
I: Input,
O: Output,
{
// The object used to get input
input: &'a mut I,
// The object used to output
output: &'a mut O,
// The pointer to the current data cell
data_ptr: usize,
// The tape of data cells
data_tape: [Cell; TAPE_SIZE],
// The pointer to the current instruction in the instruction tape
instruction_ptr: usize,
// The instruction tape
instruction_tape: [Cell; TAPE_SIZE],
// The current number of nested loops
nested_loop_counter: usize,
// The array of nested loop addresses
nested_loop_stack: [usize; NESTED_LOOP_LIMIT],
}
/// Create an instruction tape from a &str
/// Basically, it has an array of 2^16 slots for characters,
/// and fills them in as needed with the chars from the str.
/// The interpreter stops when it hits a '\0'.
fn instruction_tape_from_str(s: &str) -> [Cell; TAPE_SIZE] {
let mut tape = [0; TAPE_SIZE];
for (i, ch) in s.chars().enumerate() {
tape[i] = ch as Cell;
}
tape
}
impl<'a, I, O> Interpreter<'a, I, O>
where
I: Input,
O: Output,
{
/// Create a new interpreter from a program, an input object, and an output object
pub fn new(program: &str, input: &'a mut I, output: &'a mut O) -> Self {
Self {
input,
output,
data_ptr: 0,
data_tape: [0; TAPE_SIZE],
instruction_ptr: 0,
// Create instruction tape from program
instruction_tape: instruction_tape_from_str(program),
nested_loop_counter: 0,
nested_loop_stack: [0; NESTED_LOOP_LIMIT],
}
}
/// Resets the current interpreter.
/// Resets the instruction and data
/// pointers, loop counter and stack,
/// and the data tape.
fn reset(&mut self) {
self.data_ptr = 0;
self.data_tape = [0; TAPE_SIZE];
self.instruction_ptr = 0;
self.nested_loop_counter = 0;
self.nested_loop_stack = [0; NESTED_LOOP_LIMIT];
}
/// Execute the program.
/// This can be done over and over again, the interpreter is reset each time.
/// HOWEVER: State kept within your input and output objects CANNOT be reset!!!
/// This only resets the interpreter's internal state before execution!!!!
pub fn run(&mut self) {
// Reset interpreter state
self.reset();
// While the current instruction is not zero
while self.instruction_tape[self.instruction_ptr] != 0 {
// convert the instruction to a character
let ins = self.instruction_tape[self.instruction_ptr] as u8 as char;
match ins {
'[' => self.enter_loop(),
']' => self.exit_loop(),
'+' => self.increment(),
'-' => self.decrement(),
'>' => self.right(),
'<' => self.left(),
'.' => self.output(),
',' => self.input(),
_ => {}
}
// Move to next instruction
self.instruction_ptr += 1;
}
}
/// Increments the value in the current data cell.
/// Rust does not support overflow, so this automatically wraps.
fn increment(&mut self) {
self.data_tape[self.data_ptr] = if self.data_tape[self.data_ptr] == MAX_SIZE {
0
}
// If the current value is MAX, wrap
else {
self.data_tape[self.data_ptr] + 1
} // otherwise increment normally
}
/// Decrements the value in the current data cell.
/// Rust does not support overflow, so this automatically wraps.
fn decrement(&mut self) {
self.data_tape[self.data_ptr] = if self.data_tape[self.data_ptr] == 0 {
MAX_SIZE
}
// If the current value is zero, wrap
else {
self.data_tape[self.data_ptr] - 1
} // otherwise decrement normally
}
/// Decrements the data cell pointer.
/// If the pointer is zero, wrap around.
fn left(&mut self) {
if self.data_ptr == 0 {
self.data_ptr = TAPE_SIZE - 1;
} else {
self.data_ptr -= 1;
}
}
/// Increments the data cell pointer.
/// If the pointer is MAX, wrap around.
fn right(&mut self) {
if self.data_ptr == TAPE_SIZE - 1 {
self.data_ptr = 0;
} else {
self.data_ptr += 1;
}
}
/// Call the Output object's output method using the value of the current data cell
fn output(&mut self) {
self.output
.output(self.data_tape[self.data_ptr] as u8 as char)
}
/// Store the result of the Input object's input method in the current data cell
fn input(&mut self) {
self.data_tape[self.data_ptr] = self.input.input() as Cell
}
/// Move the instruction pointer to the topmost loop on the stack
fn goto_topmost_loop(&mut self) {
self.instruction_ptr = self.nested_loop_stack[self.nested_loop_counter - 1];
}
/// If the current cell is zero, go to the end of this loop
/// Otherwise, enter the loop and push this instruction pointer value
/// onto the top of the nested loop stack!
fn enter_loop(&mut self) {
// If the current data cell is not zero
if self.data_tape[self.data_ptr] != 0 {
// Then push this instruction pointer onto the top of the nested loop stack
self.nested_loop_stack[self.nested_loop_counter] = self.instruction_ptr;
self.nested_loop_counter += 1;
} else {
// Else, jump to the matching closing loop
let mut loop_counter = 1;
for (i, ins) in self.instruction_tape[self.instruction_ptr + 1..]
.iter()
.enumerate()
{
match *ins as u8 as char {
'[' => loop_counter += 1,
']' => loop_counter -= 1,
_ => {}
}
if loop_counter == 0 {
// Jump to the instruction AFTER the closing loop
self.instruction_ptr = i + self.instruction_ptr + 1;
break;
}
}
}
}
/// If the value of the current cell is zero,
/// continue and pop off the last instruction
/// pointer value from the nested loop stack
/// Otherwise, jump to the topmost loop instruction
/// pointer value on the nested loop stack.
fn exit_loop(&mut self) {
// If the current cell is not zero, goto the most recent loop beginning
if self.data_tape[self.data_ptr] != 0 {
self.goto_topmost_loop()
} else {
// If the current cell is zero, pop off the last instruction
// pointer value from the nested loop stack.
self.nested_loop_counter -= 1;
self.nested_loop_stack[self.nested_loop_counter] = 0;
}
}
}