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

From Rosetta Code
Content added Content deleted
m (Categorization now in master page)
m (simplify a bit)
Line 12: Line 12:
def read_program
def read_program
jumpback_table = {}
jumpback_table = {}
nest_level = 0
jump_to = []
start_idx = []
@program.each_char.each_with_index do |char, idx|
@program.each_char.each_with_index do |char, idx|
case char
case char
when "["
when "[" then jump_to.push(idx)
start_idx[nest_level] = idx
when "]" then jumpback_table[idx] = jump_to.pop
nest_level += 1
when "]"
nest_level -= 1
jumpback_table[idx] = start_idx[nest_level]
end
end
end
end
Line 49: Line 44:
print "\t#{dc},#{@d[dc]}\t" if $DEBUG
print "\t#{dc},#{@d[dc]}\t" if $DEBUG
print @d[dc].chr
print @d[dc].chr
when ?, then
when ?,
@d[dc] = $stdin.getc
@d[dc] = $stdin.getc
print "\t#{dc},#{@d[dc]}" if $DEBUG
print "\t#{dc},#{@d[dc]}" if $DEBUG
when ?[ then
when ?[
if @d[dc] == 0
if @d[dc] == 0
pc = @jumpback_table.invert[pc]
pc = @jumpback_table.invert[pc]
p " #{[pc,@program[pc].chr].inspect}" if $DEBUG
p " #{[pc,@program[pc].chr].inspect}" if $DEBUG
end
end
when ?] then
when ?]
if @d[dc] != 0
if @d[dc] != 0
pc = @jumpback_table[pc]
pc = @jumpback_table[pc]
Line 77: Line 72:
bf.run
bf.run


# use nested loop to increment count to 64 and print (should be '@')"
# 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:
Output:

Revision as of 20:35, 30 December 2009

Execute Brain****/Ruby is an implementation of Brainf***. Other implementations of Brainf***.
Execute Brain****/Ruby is part of RCBF. You may find other members of RCBF at Category:RCBF.

An implementation of a Brainf*** interpreter in Ruby. More effort could be made to read a program from a file or from stdin.

<lang ruby>class RCBF

 def initialize(program)
   @d = [0] * 30_000
   @program = program
   @jumpback_table = read_program
 end
 
 def read_program
   jumpback_table = {}
   jump_to = []
   @program.each_char.each_with_index do |char, idx|
     case char
     when "[" then jump_to.push(idx)
     when "]" then jumpback_table[idx] = jump_to.pop
     end
   end
   jumpback_table
 end
 def run
   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.invert[pc]
         p "  #{[pc,@program[pc].chr].inspect}" if $DEBUG
       end
     when ?] 
       if @d[dc] != 0
         pc = @jumpback_table[pc]
         p "  #{[pc,@program[pc].chr].inspect}" if $DEBUG
       end
     end
     puts if $DEBUG
     pc += 1
   end
 end

end

  1. output 'Hello World!\n'

helloworld = <<PROGRAM ++++++++++[>+++++++>++++++++++>+++>+<<<<-] >++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>. PROGRAM bf = RCBF.new(helloworld) bf.run

  1. use nested loop to increment count to 64 and print (should be '@')
  2. followed by a newline

RCBF.new('>>++++[<++++[<++++>-]>-]<<.[-]++++++++++.').run</lang>

Output:

Hello World!
@