Created
November 14, 2009 18:37
-
-
Save pocmo/234690 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/ruby | |
# Brainfuck Interpreter | |
# (C) Sebastian Kaspari 2009 | |
# Usage: bf2 [FILE] | |
require 'rubygems' | |
require "highline/system_extensions" | |
include HighLine::SystemExtensions | |
code = ARGF.read # code | |
cp = -1 # code pointer | |
p = 0 # cell pointer | |
c = [0] # cells | |
s = [] # bracket stack | |
until (cp+=1) == code.length | |
case code[cp].chr | |
when ">": (p += 1) && c[p].nil? && c[p] = 0 | |
when "<": p <= 1 ? p = 0 : p -= 1 | |
when "+": c[p] <= 254 ? c[p] += 1 : c[p] = 0 | |
when "-": c[p] >= 1 ? c[p] -= 1 : c[p] = 255 | |
when "[": s.push(cp + 1) | |
when "]": c[p] == 0 ? s.pop : cp = (s.last - 1) | |
when ".": print c[p].chr | |
when ",": c[p] = get_character.to_i | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The ehanced version of this interpreter is now part of this "project": http://github.com/pocmo/Ruby-Brainfuck