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

improvement of the debugging output, each_with_index->with_index
(improvement of the debugging output, each_with_index->with_index)
Line 13:
jumpback_table = {}
jump_to = []
@program.each_char.each_with_indexwith_index do |char, idx|
case char
when "[" then jump_to.push(idx)
Line 21:
jumpback_table
end
 
def run
invert_table = @jumpback_table.invert
dc = 0
pc = 0
while pc < @program.length
print [pc, @program[pc].chr].inspect if $DEBUG
 
case @program[pc]
when ?>
dc += 1
print "\t#{dc}" if $DEBUG
when ?<
dc -= 1
print "\t#{dc}" if $DEBUG
when ?+
@d[dc] += 1
print "\t#{dc},#{@d[dc]}" if $DEBUG
when ?-
@d[dc] -= 1
print "\t#{dc},#{@d[dc]}" if $DEBUG
when ?.
print "\t#{dc},#{@d[dc]}\t" if $DEBUG
print @d[dc].chr
when ?,
@d[dc] = $stdin.getc
print "\t#{dc},#{@d[dc]}" if $DEBUG
when ?[
if @d[dc] == 0
pc = @jumpback_table.invertinvert_table[pc]
pprint " #{[pc,@program[pc].chr].inspect}" if $DEBUG
end
when ?]
if @d[dc] != 0
pc = @jumpback_table[pc]
pprint " #{[pc,@program[pc].chr].inspect}" if $DEBUG
end
end
Line 71 ⟶ 72:
bf = RCBF.new(helloworld)
bf.run
 
# use nested loop to increment count to 64 and print (should be '@')
# followed by a newline
RCBF.new('>>++++[<++++[<++++>-]>-]<<.[-]++++++++++.').run</lang>
 
{{out}}
Output:
<pre>Hello World!
@</pre>
Anonymous user