Execute Brain****/Ruby: Difference between revisions

m
simplify a bit
m (Categorization now in master page)
m (simplify a bit)
Line 12:
def read_program
jumpback_table = {}
nest_leveljump_to = 0[]
start_idx = []
@program.each_char.each_with_index do |char, idx|
case char
when "[" then jump_to.push(idx)
when "]" start_idxthen jumpback_table[nest_levelidx] = idxjump_to.pop
nest_level += 1
when "]"
nest_level -= 1
jumpback_table[idx] = start_idx[nest_level]
end
end
Line 49 ⟶ 44:
print "\t#{dc},#{@d[dc]}\t" if $DEBUG
print @d[dc].chr
when ?, then
@d[dc] = $stdin.getc
print "\t#{dc},#{@d[dc]}" if $DEBUG
when ?[ then
if @d[dc] == 0
pc = @jumpback_table.invert[pc]
p " #{[pc,@program[pc].chr].inspect}" if $DEBUG
end
when ?] then
if @d[dc] != 0
pc = @jumpback_table[pc]
Line 77 ⟶ 72:
bf.run
 
# use nested loop to increment count to 64 and print (should be '@')"
# followed by a newline
# 64 = 4*4*4
nestedloop = RCBF.new('>>++++[<++++[<++++>-]>-]<<.[-]++++++++++.').run</lang>
bf = RCBF.new(nestedloop)
bf.run</lang>
 
Output:
Anonymous user