Last active
August 29, 2015 13:58
-
-
Save brendanzab/9970322 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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