Skip to content

Instantly share code, notes, and snippets.

@enukane
Created October 11, 2012 16:38
Show Gist options
  • Save enukane/3873712 to your computer and use it in GitHub Desktop.
Save enukane/3873712 to your computer and use it in GitHub Desktop.
merged from hiraganafuck
def dprint str
end
class BrainFuckProc
def initialize
@tape = Array.new
@current_position = 0
end
def inc_pointer
dprint "up #{@current_position}->#{@current_position + 1}"
@current_position += 1
end
def dec_pointer
dprint "down #{@current_position}->#{@current_position -1}"
if @current_position == 0 then
puts "ERROR - pointer lower limit"
exit
end
@current_position -= 1
end
def inc_current_value
if @tape[@current_position] == nil then
@tape[@current_position] = 0
end
@tape[@current_position] += 1
dprint @tape
end
def dec_current_value
if @tape[@current_position] == nil || @tape[@current_position] == 0 then
puts "ERROR - value at the pointer is 0"
exit
end
@tape[@current_position] -= 1
dprint @tape
end
def print_current_value_char
dprint "tape => #{@tape}, pos = #{@current_position}"
print @tape[@current_position].chr
end
def get_char_to_current_pos
data = gets
@tape[@current_position] = data[0]
end
def is_zero?
if @tape[@current_position] == 0 || @tape[@current_position] == nil then
return true
else
return false
end
end
def jump_forward?
jump?
end
def jump_backward?
jump?
end
def print_all_tape
data = ""
@tape.each do |val|
if val then
data.concat(val.chr)
end
end
p "ALLDATA:#{data}"
end
end
class BrainFuck
def initialize(src_name)
@src_name = src_name
@bfpProc = BrainFuckProc.new()
end
def run
File.open(@src_name) do |file|
run_bfp(file)
end
end
def run_bfp io
while ch = io.read(1) do
if ch == ">" then
@bfpProc.inc_pointer
end
if ch == "<" then
@bfpProc.dec_pointer
end
if ch == "+" then
@bfpProc.inc_current_value
end
if ch == "-" then
@bfpProc.dec_current_value
end
if ch == "." then
@bfpProc.print_current_value_char
end
if ch == "," then
@bfpProc.get_char_to_current_pos
end
if ch == "[" then
jump_forward(io)
end
if ch == "]" then
jump_backward(io)
end
if ch == "*" then
@bfpProc.print_all_tape
end
end
end
def jump_forward(io)
dprint "jump forward:"
if [email protected]_zero? then
dprint "no jump\n"
return
end
dprint "jump\n"
stack_count = 0;
while true do
inner_ch = io.read(1)
if inner_ch == "" then # no matched clauses
puts "ERROR - no matched clauses"
exit
end
if inner_ch == "[" then
stack_count += 1
end
if inner_ch == "]" then
if stack_count > 0 then
stack_count -= 1
next
else # stack_count == 0 -> matched found
io.pos += 1
break # ends here
end
end
end
end
def jump_backward(io)
dprint "jump backward:"
if @bfpProc.is_zero? then
dprint "no jump\n"
return
end
dprint "jump\n"
stack_count = 0;
if io.pos < 2 then
puts "ERROR - guess only ] appears first?"
exit
end
while io.pos > -1 do
io.pos -= 2
inner_ch = io.read(1)
if inner_ch == "" then # no more backwards
puts "ERROR - no match clauses"
exit
end
if inner_ch == "]" then
stack_count += 1
end
if inner_ch == "[" then
if stack_count > 0 then
stack_count -= 1
next
else##matching
#io.pos += 1
break
end
end
end
end
end
bfp = BrainFuck.new(ARGV.shift)
bfp.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment