Skip to content

Instantly share code, notes, and snippets.

@keiya
Created October 3, 2010 12:44
Show Gist options
  • Select an option

  • Save keiya/608554 to your computer and use it in GitHub Desktop.

Select an option

Save keiya/608554 to your computer and use it in GitHub Desktop.
brainfsck, the compatible of brainfuck (ruby)
#!/usr/bin/ruby
# 言語仕様: http://wiki.keiyac.org/index.pl?page=Brainfsck
$KCODE = "UTF8"
$ptr = 0
$tkn_ptr = 0
$token = []
$mem = Array.new(30000)
$mem.fill(0)
def token_fwd
$tkn_ptr += 1
if $token[$tkn_ptr] != "滅"
token_fwd if $token[$tkn_ptr] == "屍"
$tkn_ptr += 1
end
end
def token_bak
$tkn_ptr -= 1
if $token[$tkn_ptr] != "屍"
token_bak if $token[$tkn_ptr] == "滅"
$tkn_ptr -= 1
end
end
def token_eval(op)
case op
when "害" then
$ptr += 1
when "死" then
$ptr -= 1
when "殺" then
$mem[$ptr] += 1
when "戮" then
$mem[$ptr] -= 1
when "人" then
$mem[$ptr] = STDIN.getc
when "破" then
#print $mem[$ptr].chr
print "#{$mem[$ptr]} "
when "屍" then
token_fwd if $mem[$ptr] != 0
when "滅" then
token_bak if $mem[$ptr] == 0
end
end
j = 0
gets.split(//).each do |char|
$token[j]= char
j += 1
end
while $tkn_ptr < $token.size
token_eval($token[$tkn_ptr])
$tkn_ptr += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment