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

From Rosetta Code
Content added Content deleted
(Brat BF interpreter)
 
(Update to use prototype)
Line 26: Line 26:


#Run instructions
#Run instructions
bf.run = {
bf.prototype.run = {
current_instruction = null
current_instruction = null


Line 76: Line 76:


#Precalcuate '[' and ']' jump locations
#Precalcuate '[' and ']' jump locations
bf.calculate_jumps = {
bf.prototype.calculate_jumps = {
forwards = []
forwards = []
jumps = [:]
jumps = [:]

Revision as of 22:29, 14 November 2011

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

An implementation of a Brainf*** interpreter in Brat.

Does not handle EOF when getting input. Tape is "infinite" in the positive direction. Each cell holds an integer.

<lang brat>#BF machine bf = object.new

  1. Initialize with array of instructions

bf.init = { instructions |

 my.instructions = instructions
 my.program_counter = 0
 my.pointer = 0
 my.tape = []
 calculate_jumps
 #Override my.tape[] to return 0 instead of null
 my.tape.original_get = my.tape->get
 my.tape.get = { index |
   val = my.original_get index
   null? val
     { 0 }
     { val }
 }

}

  1. Run instructions

bf.prototype.run = {

 current_instruction = null
 while { current_instruction = instructions[my.program_counter] }
   { 
     action = my.interpreter[current_instruction]
     action
     my.program_counter = my.program_counter + 1
   }

}

  1. Holds all operations

bf.interpreter = [:]

bf.interpreter[:>] = {

 my.pointer = my.pointer + 1

}

bf.interpreter[:<] = {

 my.pointer = my.pointer - 1

}

bf.interpreter[:+] = {

 my.tape[my.pointer] = my.tape[my.pointer] + 1

}

bf.interpreter[:-] = {

 my.tape[my.pointer] = my.tape[my.pointer] - 1

}

bf.interpreter["."] = {

 print my.tape[my.pointer].to_char

}

bf.interpreter[","] = {

 my.tape[my.pointer] = g.to_byte

}

bf.interpreter["["] = {

 true? my.tape[my.pointer] == 0
   { my.program_counter = my.jumps[my.program_counter] }

}

bf.interpreter["]"] = {

 false? my.tape[my.pointer] == 0
   { my.program_counter = my.jumps[my.program_counter] }

}

  1. Precalcuate '[' and ']' jump locations

bf.prototype.calculate_jumps = {

 forwards = []
 jumps = [:]
 
 my.instructions.each_with_index { ins, index |
   when { ins == "[" } { forwards << index }
     { ins == "]" } { 
       match = forwards.pop
       jumps[match] = index
       jumps[index] = match
     }
 }
 my.jumps = jumps

}

  1. Get file name from user and run it

include :file

bf.new(file.read(ask "BF file: ").dice).run</lang>