| 229 |      1 | // Part 1 about an Interpreter for the Brainf*** language
 | 
|  |      2 | //========================================================
 | 
|  |      3 | 
 | 
| 290 |      4 | object CW10a {  
 | 
| 229 |      5 | 
 | 
|  |      6 | type Mem = Map[Int, Int]
 | 
|  |      7 | 
 | 
|  |      8 | 
 | 
|  |      9 | import io.Source
 | 
|  |     10 | import scala.util._
 | 
|  |     11 | 
 | 
|  |     12 | // (1) Write a function that takes a file name as argument and
 | 
|  |     13 | // and requests the corresponding file from disk. It returns the
 | 
|  |     14 | // content of the file as a String. If the file does not exists,
 | 
|  |     15 | // the function should return the empty string.
 | 
|  |     16 | 
 | 
|  |     17 | def load_bff(name: String) : String = 
 | 
|  |     18 |   Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("")
 | 
|  |     19 | 
 | 
|  |     20 | 
 | 
|  |     21 | // (2) Complete the functions for safely reading  
 | 
|  |     22 | // and writing brainf*** memory. Safely read should
 | 
|  |     23 | // Return the value stored in the Map for a given memory
 | 
|  |     24 | // pointer, provided it exists; otherwise it Returns 0. The
 | 
|  |     25 | // writing function generates a new Map with the
 | 
|  |     26 | // same data, except at the given memory pointer the
 | 
|  |     27 | // value v is stored.
 | 
|  |     28 | 
 | 
|  |     29 | 
 | 
|  |     30 | def sread(mem: Mem, mp: Int) : Int = 
 | 
|  |     31 |   mem.getOrElse(mp, 0)
 | 
|  |     32 | 
 | 
|  |     33 | def write(mem: Mem, mp: Int, v: Int) : Mem =
 | 
|  |     34 |   mem.updated(mp, v)
 | 
|  |     35 | 
 | 
|  |     36 | 
 | 
|  |     37 | // (3) Implement the two jumping instructions in the 
 | 
|  |     38 | // brainf*** language. In jumpRight, given a program and 
 | 
|  |     39 | // a program counter move the program counter to the right 
 | 
|  |     40 | // until after the *matching* ]-command. Similarly, 
 | 
|  |     41 | // jumpLeft implements the move to the left to just after
 | 
|  |     42 | // the *matching* [-command.
 | 
|  |     43 | 
 | 
|  |     44 | def jumpRight(prog: String, pc: Int, level: Int) : Int = {
 | 
|  |     45 |   if (prog.length <= pc) pc 
 | 
|  |     46 |   else (prog(pc), level) match {
 | 
|  |     47 |     case (']', 0) => pc + 1
 | 
|  |     48 |     case (']', l) => jumpRight(prog, pc + 1, l - 1)
 | 
|  |     49 |     case ('[', l) => jumpRight(prog, pc + 1, l + 1)
 | 
|  |     50 |     case (_, l) => jumpRight(prog, pc + 1, l)
 | 
|  |     51 |   }
 | 
|  |     52 | }
 | 
|  |     53 | 
 | 
|  |     54 | def jumpLeft(prog: String, pc: Int, level: Int) : Int = {
 | 
|  |     55 |   if (pc < 0) pc 
 | 
|  |     56 |   else (prog(pc), level) match {
 | 
|  |     57 |     case ('[', 0) => pc + 1
 | 
|  |     58 |     case ('[', l) => jumpLeft(prog, pc - 1, l - 1)
 | 
|  |     59 |     case (']', l) => jumpLeft(prog, pc - 1, l + 1)
 | 
|  |     60 |     case (_, l) => jumpLeft(prog, pc - 1, l)
 | 
|  |     61 |   }
 | 
|  |     62 | }
 | 
|  |     63 | 
 | 
|  |     64 | // test cases
 | 
|  |     65 | //jumpRight("""--[..+>--],>,++""", 3, 0)         // => 10
 | 
|  |     66 | //jumpLeft("""--[..+>--],>,++""", 8, 0)          // => 3
 | 
|  |     67 | //jumpRight("""--[..[+>]--],>,++""", 3, 0)       // => 12
 | 
|  |     68 | //jumpRight("""--[..[[-]+>[.]]--],>,++""", 3, 0) // => 18
 | 
|  |     69 | //jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0)  // => 22 (outside)
 | 
|  |     70 | //jumpLeft("""[******]***""", 7, 0)              // => -1 (outside)
 | 
|  |     71 | 
 | 
| 230 |     72 | // (4) Complete the compute function that interprets (runs) a brainf***
 | 
|  |     73 | // program: the arguments are a program (represented as a string), a program 
 | 
|  |     74 | // counter, a memory counter and a brainf*** memory. It Returns the
 | 
|  |     75 | // memory at the stage when the execution of the brainf*** program
 | 
| 229 |     76 | // finishes. The interpretation finishes once the program counter
 | 
|  |     77 | // pc is pointing to something outside the program string.
 | 
|  |     78 | // If the pc points to a character inside the program, the pc, 
 | 
|  |     79 | // memory pointer and memory need to be updated according to 
 | 
| 230 |     80 | // rules of the brainf*** language. Then, recursively, the compute 
 | 
| 229 |     81 | // function continues with the command at the new program
 | 
|  |     82 | // counter. 
 | 
|  |     83 | // Implement the run function that calls compute with the program
 | 
|  |     84 | // counter and memory counter set to 0.
 | 
|  |     85 | 
 | 
|  |     86 | def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = {
 | 
|  |     87 |   if (0 <= pc && pc < prog.length) { 
 | 
|  |     88 |     val (new_pc, new_mp, new_mem) = prog(pc) match {
 | 
|  |     89 |       case '>' => (pc + 1, mp + 1, mem)
 | 
|  |     90 |       case '<' => (pc + 1, mp - 1, mem)
 | 
|  |     91 |       case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1))
 | 
|  |     92 |       case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1))
 | 
|  |     93 |       case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
 | 
|  |     94 |       case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte))
 | 
|  |     95 |       case '['  => 
 | 
|  |     96 | 	if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) 
 | 
|  |     97 |       case ']'  => 
 | 
|  |     98 | 	if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) 
 | 
|  |     99 |       case _ => (pc + 1, mp, mem)
 | 
|  |    100 |     }		     
 | 
|  |    101 |     compute(prog, new_pc, new_mp, new_mem)	
 | 
|  |    102 |   }
 | 
|  |    103 |   else mem
 | 
|  |    104 | }
 | 
|  |    105 | 
 | 
|  |    106 | def run(prog: String, m: Mem = Map()) = compute(prog, 0, 0, m)
 | 
|  |    107 | 
 | 
|  |    108 | 
 | 
| 244 |    109 | 
 | 
| 264 |    110 | 
 | 
| 229 |    111 | 
 | 
|  |    112 | // some sample bf-programs collected from the Internet
 | 
|  |    113 | //=====================================================
 | 
|  |    114 | 
 | 
|  |    115 | 
 | 
|  |    116 | // first some contrived (small) programs
 | 
|  |    117 | 
 | 
|  |    118 | // clears the 0-cell
 | 
| 292 |    119 | //run("[-]", Map(0 -> 100))    // Map will be 0 -> 0
 | 
| 229 |    120 | 
 | 
|  |    121 | // copies content of the 0-cell to 1-cell
 | 
| 292 |    122 | //run("[->+<]", Map(0 -> 10))  // Map will be 0 -> 0, 1 -> 10
 | 
| 229 |    123 | 
 | 
|  |    124 | 
 | 
|  |    125 | // copies content of the 0-cell to 2-cell and 4-cell
 | 
| 292 |    126 | //run("[>>+>>+<<<<-]", Map(0 -> 42))
 | 
| 229 |    127 | 
 | 
|  |    128 | 
 | 
|  |    129 | // prints out numbers 0 to 9
 | 
| 292 |    130 | //run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
 | 
| 229 |    131 | 
 | 
|  |    132 | 
 | 
|  |    133 | // some more "useful" programs
 | 
|  |    134 | 
 | 
|  |    135 | // hello world program 1
 | 
| 292 |    136 | //run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
 | 
|  |    137 | //       ..+++.>>.<-.<.+++.------.--------.>>+.>++.""")
 | 
| 229 |    138 | 
 | 
|  |    139 | // hello world program 2
 | 
| 292 |    140 | //run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+
 | 
|  |    141 | //       +.<<+++++++++++++++.>.+++.------.--------.>+.>.""")
 | 
| 229 |    142 | 
 | 
| 244 |    143 | // hello world program 3
 | 
| 292 |    144 | //run("""+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..
 | 
|  |    145 | //       +++.>-.------------.<++++++++.--------.+++.------.--------.>+.""")
 | 
| 229 |    146 | 
 | 
| 244 |    147 |  
 | 
| 229 |    148 | // draws the Sierpinski triangle
 | 
| 292 |    149 | //run("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[
 | 
|  |    150 | //      ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<
 | 
|  |    151 | //      ]>.>+[>>]>+]""")
 | 
| 229 |    152 | 
 | 
| 292 |    153 | //run(load_bff("sierpinski.bf"))
 | 
| 229 |    154 | 
 | 
|  |    155 | 
 | 
|  |    156 | //fibonacci numbers below 100
 | 
| 292 |    157 | //run("""+++++++++++
 | 
|  |    158 | //      >+>>>>++++++++++++++++++++++++++++++++++++++++++++
 | 
|  |    159 | //      >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
 | 
|  |    160 | //      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
 | 
|  |    161 | //      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
 | 
|  |    162 | //      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
 | 
|  |    163 | //      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
 | 
|  |    164 | //      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
 | 
|  |    165 | //      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
 | 
|  |    166 | //      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
 | 
|  |    167 | //      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""")
 | 
| 229 |    168 | 
 | 
| 292 |    169 | /*
 | 
| 229 |    170 | //outputs the square numbers up to 10000
 | 
|  |    171 | run("""++++[>+++++<-]>[<+++++>-]+<+[
 | 
|  |    172 |     >[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
 | 
|  |    173 |     >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
 | 
|  |    174 |     <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""")
 | 
|  |    175 | 
 | 
|  |    176 | //collatz numbers (needs a number to be typed in)
 | 
|  |    177 | run(""">,[[----------[
 | 
|  |    178 |       >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<]
 | 
|  |    179 |       ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],<]>]>>>++>+>>[
 | 
|  |    180 |       <<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>]
 | 
|  |    181 |       >[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]]
 | 
|  |    182 |       >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>>
 | 
|  |    183 |       ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<,]""")
 | 
|  |    184 | 
 | 
|  |    185 | 
 | 
|  |    186 | // infinite collatz (never stops)
 | 
|  |    187 | run(""">>+>+<[[->>[>>]>>>[>>]+[<<]<<<[<<]>[>[>>]>>+>[>>]<+<[<<]<<<[<
 | 
|  |    188 |       <]>-]>[>>]>>[<<<<[<<]>+>[>>]>>-]<<<<[<<]+>>]<<[+++++[>+++++++
 | 
|  |    189 |       +<-]>.<++++++[>--------<-]+<<]>>[>>]+[>>>>[<<+>+>-]<-[>+<-]+<
 | 
|  |    190 |       [<<->>-[<<+>>[-]]]>>>[<<<+<<+>>>>>-]<<<[>>>+<<<-]<<[[-]>+>>->
 | 
|  |    191 |       [<+<[<<+>>-]<[>+<-]<[>+<-]>>>>-]<[>+<-]+<[->[>>]<<[->[<+++>-[
 | 
|  |    192 |       <+++>-[<+++>-[<[-]++>>[-]+>+<<-[<+++>-[<+++>-[<[-]+>>>+<<-[<+
 | 
|  |    193 |       ++>-[<+++>-]]]]]]]]]<[>+<-]+<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<
 | 
|  |    194 |       +>-[<+>-[<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-[<+>-]]]]]]]]]]]<[>+<-
 | 
|  |    195 |       ]+>>]<<[<<]>]<[->>[->+>]<[-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<-
 | 
|  |    196 |       >>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>
 | 
|  |    197 |       -[<->>+<-[<+>-[<->>+<-[<+>-]]]]]]]]]]]]]]]]]]]>[<+>-]<+<[<+++
 | 
|  |    198 |       +++++++>-]<]>>[<+>->>]<<[>+>+<<-]>[<+>-]+>[<->[-]]<[-<<-]<<[<
 | 
|  |    199 |       <]]++++++[>+++++++<-]>++.------------.[-]>[>>]<<[+++++[>+++++
 | 
|  |    200 |       +++<-]>.<++++++[>--------<-]+<<]+<]>[<+>-]<]>>>[>>]<<[>[-]<-<
 | 
|  |    201 |       <]++++++++++.[-]<<<[<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<+>-[<+>-
 | 
|  |    202 |       [<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-]]]]]]]]]]<[>+<-]+>>]<<[<<]>>]""")
 | 
|  |    203 | 
 | 
| 264 |    204 | // 2 to the power of 6 
 | 
|  |    205 | //(example from a C-to-BF compiler at https://github.com/elikaski/BF-it)
 | 
|  |    206 | run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-]
 | 
|  |    207 |        <><[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]
 | 
|  |    208 |        <[>+>+<<-]>[<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<[[-]>[-]<<[>+>
 | 
|  |    209 |        +<<-]>>[<<+>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-]
 | 
|  |    210 |        <<>>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<>[-]<<[-]
 | 
|  |    211 |        >[>+<<+>-]>[<+>-]<><[-]>[-]<<<[>>+>+<<<-]>>>-[<<<+>>>-]<[-]>[-]
 | 
|  |    212 |        <<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]<[>+>+<<-]>
 | 
|  |    213 |        [<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<][-]>[-]<<[>+>+<<-]>>[<<+>
 | 
|  |    214 |        >-]<<<<<[-]>>>>[<<<<+>>>>-]<<<<><>[-]<<[-]>[>+<<+>-]>[<+>-]<>
 | 
|  |    215 |        <[-]>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<<>>[-]>[-]>[-]>[-]>[-]>
 | 
|  |    216 |        [-]>[-]>[-]>[-]>[-]<<<<<<<<<<>>++++++++++<<[->+>-[>+>>]>[+[-<+
 | 
|  |    217 |        >]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<
 | 
|  |    218 |        ]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++
 | 
|  |    219 |        ++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<<><<<""")
 | 
|  |    220 | 
 | 
| 229 |    221 | 
 | 
|  |    222 | 
 | 
|  |    223 | // a Mandelbrot set generator in brainf*** written by Erik Bosman
 | 
| 230 |    224 | // (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
 | 
|  |    225 | 
 | 
| 229 |    226 | run(load_bff("mandelbrot.bf"))
 | 
|  |    227 | 
 | 
|  |    228 | 
 | 
|  |    229 | // a benchmark program (counts down from 'Z' to 'A')
 | 
|  |    230 | val b1 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
 | 
|  |    231 |             [>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
 | 
|  |    232 |             ++++++++[>++++++++++[>++++++++++[>++++++++++[>+
 | 
|  |    233 |             +++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++."""
 | 
|  |    234 | 
 | 
|  |    235 | 
 | 
|  |    236 | def time_needed[T](n: Int, code: => T) = {
 | 
|  |    237 |   val start = System.nanoTime()
 | 
|  |    238 |   for (i <- 0 until n) code
 | 
|  |    239 |   val end = System.nanoTime()
 | 
| 231 |    240 |   (end - start)/(n * 1.0e9)
 | 
| 229 |    241 | }
 | 
|  |    242 | 
 | 
|  |    243 | time_needed(1, run(b1))
 | 
| 292 |    244 | */
 | 
| 229 |    245 | 
 | 
|  |    246 | }
 |