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