Skip to content

Instantly share code, notes, and snippets.

@pocmo
Created November 14, 2009 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pocmo/234690 to your computer and use it in GitHub Desktop.
Save pocmo/234690 to your computer and use it in GitHub Desktop.
#!/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
@pocmo
Copy link
Author

pocmo commented Mar 30, 2010

The ehanced version of this interpreter is now part of this "project": http://github.com/pocmo/Ruby-Brainfuck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment