solutions5/bf.scala
changeset 230 bebe34c975a8
parent 229 5549016ab10f
child 231 eecbc9ae73c2
equal deleted inserted replaced
229:5549016ab10f 230:bebe34c975a8
     1 // Part 1 about an Interpreter for the Brainf*** language
     1 // Part 1 about an Interpreter for the Brainf*** language
     2 //========================================================
     2 //========================================================
     3 
     3 
     4 object CW10a {
     4 object CW10a {  // only for generating the Jar file
     5 
     5 
     6 
     6 
     7 type Mem = Map[Int, Int]
     7 type Mem = Map[Int, Int]
     8 
     8 
     9 
     9 
    68 //jumpRight("""--[..[+>]--],>,++""", 3, 0)       // => 12
    68 //jumpRight("""--[..[+>]--],>,++""", 3, 0)       // => 12
    69 //jumpRight("""--[..[[-]+>[.]]--],>,++""", 3, 0) // => 18
    69 //jumpRight("""--[..[[-]+>[.]]--],>,++""", 3, 0) // => 18
    70 //jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0)  // => 22 (outside)
    70 //jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0)  // => 22 (outside)
    71 //jumpLeft("""[******]***""", 7, 0)              // => -1 (outside)
    71 //jumpLeft("""[******]***""", 7, 0)              // => -1 (outside)
    72 
    72 
    73 // (4) Complete the compute function that interpretes (runs) a brainf***
    73 // (4) Complete the compute function that interprets (runs) a brainf***
    74 // program: the arguments are a program (represented as a string), a program counter,
    74 // program: the arguments are a program (represented as a string), a program 
    75 // a memory counter and a brainf*** memory. It Returns the
    75 // counter, a memory counter and a brainf*** memory. It Returns the
    76 // memory at the stage when the excution of the brainf*** program
    76 // memory at the stage when the execution of the brainf*** program
    77 // finishes. The interpretation finishes once the program counter
    77 // finishes. The interpretation finishes once the program counter
    78 // pc is pointing to something outside the program string.
    78 // pc is pointing to something outside the program string.
    79 // If the pc points to a character inside the program, the pc, 
    79 // If the pc points to a character inside the program, the pc, 
    80 // memory pointer and memory need to be updated according to 
    80 // memory pointer and memory need to be updated according to 
    81 // rules of the brainf*** language. Then, recursively, run 
    81 // rules of the brainf*** language. Then, recursively, the compute 
    82 // function continues with the command at the new program
    82 // function continues with the command at the new program
    83 // counter. 
    83 // counter. 
    84 // Implement the run function that calls compute with the program
    84 // Implement the run function that calls compute with the program
    85 // counter and memory counter set to 0.
    85 // counter and memory counter set to 0.
    86 
    86 
   198       [<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-]]]]]]]]]]<[>+<-]+>>]<<[<<]>>]""")
   198       [<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-]]]]]]]]]]<[>+<-]+>>]<<[<<]>>]""")
   199 
   199 
   200 
   200 
   201 
   201 
   202 // a Mandelbrot set generator in brainf*** written by Erik Bosman
   202 // a Mandelbrot set generator in brainf*** written by Erik Bosman
       
   203 // (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
       
   204 
   203 run(load_bff("mandelbrot.bf"))
   205 run(load_bff("mandelbrot.bf"))
   204 
   206 
   205 
   207 
   206 // a benchmark program (counts down from 'Z' to 'A')
   208 // a benchmark program (counts down from 'Z' to 'A')
   207 val b1 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
   209 val b1 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
   218 }
   220 }
   219 
   221 
   220 time_needed(1, run(b1))
   222 time_needed(1, run(b1))
   221 */
   223 */
   222 
   224 
   223 
   225 }
   224 }