Skip to content

Instantly share code, notes, and snippets.

@brendanzab
Last active August 29, 2015 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brendanzab/9970322 to your computer and use it in GitHub Desktop.
Save brendanzab/9970322 to your computer and use it in GitHub Desktop.
use std::io;
fn main() {
eval(bytes!("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.")); // I stole this off of wikipedia, sorry, wikipedia :(
}
fn eval(program: &[u8]) {
let mut pointer = 0u;
let mut memory = [0u8, ..1024];
let mut jmp_stack = vec!();
let mut instr = 0u;
while instr < program.len() {
let command = program[instr] as char;
match command {
'>' => pointer += 1,
'<' if pointer > 0 => pointer -= 1,
'+' => memory[pointer] += 1,
'-' if memory[pointer] > 0 => memory[pointer] -= 1,
'.' => print!("{}", memory[pointer] as char),
',' => memory[pointer] = io::stdin().read_byte().unwrap(),
'[' if memory[pointer] == 0 => {
while program[instr] as char != ']' {
instr += 1;
}
},
'[' => jmp_stack.push(instr),
']' if memory[pointer] != 0 => {
instr = jmp_stack.pop().unwrap();
continue;
}
']' => { jmp_stack.pop(); },
_ => { /* ignore everything else */ },
}
instr += 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment