| 399 |      1 | // Main Part 5 about an Interpreter for 
 | 
|  |      2 | // the Brainf*** language
 | 
| 285 |      3 | //==============================================
 | 
|  |      4 | 
 | 
|  |      5 | 
 | 
| 399 |      6 | object M5a {
 | 
| 230 |      7 | 
 | 
|  |      8 | 
 | 
| 399 |      9 | // representation of BF memory 
 | 
| 230 |     10 | 
 | 
|  |     11 | type Mem = Map[Int, Int]
 | 
|  |     12 | 
 | 
|  |     13 | 
 | 
|  |     14 | // (1) Write a function that takes a file name as argument and
 | 
| 400 |     15 | // and requests the corresponding file from disk. It returns the
 | 
| 230 |     16 | // content of the file as a String. If the file does not exists,
 | 
| 400 |     17 | // the function should return the empty string.
 | 
| 230 |     18 | 
 | 
|  |     19 | import io.Source
 | 
|  |     20 | import scala.util._
 | 
|  |     21 | 
 | 
| 347 |     22 | def load_bff(name: String) : String = ???
 | 
| 230 |     23 | 
 | 
|  |     24 | 
 | 
|  |     25 | 
 | 
|  |     26 | // (2) Complete the functions for safely reading  
 | 
| 399 |     27 | // and writing brainf*** memory. Safely read should
 | 
| 230 |     28 | // Return the value stored in the Map for a given memory
 | 
|  |     29 | // pointer, provided it exists; otherwise it Returns 0. The
 | 
|  |     30 | // writing function generates a new Map with the
 | 
|  |     31 | // same data, except at the given memory pointer the
 | 
|  |     32 | // value v is stored.
 | 
|  |     33 | 
 | 
|  |     34 | 
 | 
| 347 |     35 | def sread(mem: Mem, mp: Int) : Int = ???
 | 
| 230 |     36 | 
 | 
| 347 |     37 | def write(mem: Mem, mp: Int, v: Int) : Mem = ???
 | 
| 230 |     38 | 
 | 
|  |     39 | 
 | 
|  |     40 | 
 | 
|  |     41 | // (3) Implement the two jumping instructions in the 
 | 
| 399 |     42 | // brainf*** language. In jumpRight, given a program and 
 | 
| 230 |     43 | // a program counter move the program counter to the right 
 | 
|  |     44 | // until after the *matching* ]-command. Similarly, 
 | 
|  |     45 | // jumpLeft implements the move to the left to just after
 | 
|  |     46 | // the *matching* [-command.
 | 
|  |     47 | 
 | 
| 347 |     48 | def jumpRight(prog: String, pc: Int, level: Int) : Int = ???
 | 
| 230 |     49 | 
 | 
| 347 |     50 | def jumpLeft(prog: String, pc: Int, level: Int) : Int = ???
 | 
| 230 |     51 | 
 | 
|  |     52 | 
 | 
|  |     53 | // testcases
 | 
|  |     54 | //jumpRight("""--[..+>--],>,++""", 3, 0)         // => 10
 | 
|  |     55 | //jumpLeft("""--[..+>--],>,++""", 8, 0)          // => 3
 | 
|  |     56 | //jumpRight("""--[..[+>]--],>,++""", 3, 0)       // => 12
 | 
|  |     57 | //jumpRight("""--[..[[-]+>[.]]--],>,++""", 3, 0) // => 18
 | 
|  |     58 | //jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0)  // => 22 (outside)
 | 
|  |     59 | //jumpLeft("""[******]***""", 7, 0)              // => -1 (outside)
 | 
|  |     60 | 
 | 
|  |     61 | 
 | 
|  |     62 | 
 | 
| 399 |     63 | // (4) Complete the compute function that interprets (runs) a brainf***
 | 
| 230 |     64 | // program: the arguments are a program (represented as a string), a program 
 | 
| 399 |     65 | // counter, a memory counter and a brainf*** memory. It Returns the
 | 
|  |     66 | // memory at the stage when the execution of the brainf*** program
 | 
| 230 |     67 | // finishes. The interpretation finishes once the program counter
 | 
|  |     68 | // pc is pointing to something outside the program string.
 | 
|  |     69 | // If the pc points to a character inside the program, the pc, 
 | 
|  |     70 | // memory pointer and memory need to be updated according to 
 | 
| 399 |     71 | // rules of the brainf*** language. Then, recursively, the compute 
 | 
| 230 |     72 | // function continues with the command at the new program
 | 
|  |     73 | // counter. 
 | 
|  |     74 | //
 | 
|  |     75 | // Implement the run function that calls compute with the program
 | 
|  |     76 | // counter and memory counter set to 0.
 | 
|  |     77 | 
 | 
|  |     78 | 
 | 
| 347 |     79 | def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = ???
 | 
| 230 |     80 | 
 | 
| 347 |     81 | def run(prog: String, m: Mem = Map()) = ???
 | 
| 230 |     82 | 
 | 
|  |     83 | 
 | 
|  |     84 | 
 | 
| 399 |     85 | // some sample bf-programs collected from the Internet
 | 
|  |     86 | //=====================================================
 | 
| 230 |     87 | 
 | 
|  |     88 | 
 | 
| 338 |     89 | // some contrived (small) programs
 | 
|  |     90 | //---------------------------------
 | 
| 230 |     91 | 
 | 
|  |     92 | // clears the 0-cell
 | 
| 338 |     93 | //run("[-]", Map(0 -> 100))    // Map will be 0 -> 0
 | 
| 230 |     94 | 
 | 
| 338 |     95 | // moves content of the 0-cell to 1-cell
 | 
|  |     96 | //run("[->+<]", Map(0 -> 10))  // Map will be 0 -> 0, 1 -> 10
 | 
| 230 |     97 | 
 | 
|  |     98 | 
 | 
|  |     99 | // copies content of the 0-cell to 2-cell and 4-cell
 | 
| 338 |    100 | //run("[>>+>>+<<<<-]", Map(0 -> 42))    // Map(0 -> 0, 2 -> 42, 4 -> 42)
 | 
| 230 |    101 | 
 | 
|  |    102 | 
 | 
|  |    103 | // prints out numbers 0 to 9
 | 
| 338 |    104 | //run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
 | 
|  |    105 | 
 | 
| 230 |    106 | 
 | 
|  |    107 | // some more "useful" programs
 | 
| 338 |    108 | //-----------------------------
 | 
| 230 |    109 | 
 | 
|  |    110 | // hello world program 1
 | 
| 338 |    111 | //run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
 | 
|  |    112 | //       ..+++.>>.<-.<.+++.------.--------.>>+.>++.""")
 | 
| 230 |    113 | 
 | 
|  |    114 | // hello world program 2
 | 
| 338 |    115 | //run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+
 | 
|  |    116 | //       +.<<+++++++++++++++.>.+++.------.--------.>+.>.""")
 | 
| 230 |    117 | 
 | 
| 338 |    118 | // hello world program 3
 | 
|  |    119 | //run("""+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..
 | 
|  |    120 | //       +++.>-.------------.<++++++++.--------.+++.------.--------.>+.""")
 | 
| 230 |    121 | 
 | 
| 338 |    122 |  
 | 
| 230 |    123 | // draws the Sierpinski triangle
 | 
| 338 |    124 | //run(load_bff("sierpinski.bf"))
 | 
| 230 |    125 | 
 | 
|  |    126 | 
 | 
|  |    127 | //fibonacci numbers below 100
 | 
| 338 |    128 | //run("""+++++++++++
 | 
|  |    129 | //      >+>>>>++++++++++++++++++++++++++++++++++++++++++++
 | 
|  |    130 | //      >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
 | 
|  |    131 | //      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
 | 
|  |    132 | //      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
 | 
|  |    133 | //      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
 | 
|  |    134 | //      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
 | 
|  |    135 | //      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
 | 
|  |    136 | //      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
 | 
|  |    137 | //      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
 | 
|  |    138 | //      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""")
 | 
| 230 |    139 | 
 | 
|  |    140 | //outputs the square numbers up to 10000
 | 
| 338 |    141 | //run("""++++[>+++++<-]>[<+++++>-]+<+[>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
 | 
|  |    142 | //       >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
 | 
|  |    143 | //       <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""")
 | 
| 230 |    144 | 
 | 
|  |    145 | 
 | 
| 338 |    146 | // calculates 2 to the power of 6 
 | 
|  |    147 | //(example from a C-to-BF compiler at https://github.com/elikaski/BF-it)
 | 
|  |    148 | //run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-]
 | 
|  |    149 | //       <><[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]
 | 
|  |    150 | //       <[>+>+<<-]>[<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<[[-]>[-]<<[>+>
 | 
|  |    151 | //       +<<-]>>[<<+>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-]
 | 
|  |    152 | //       <<>>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<>[-]<<[-]
 | 
|  |    153 | //       >[>+<<+>-]>[<+>-]<><[-]>[-]<<<[>>+>+<<<-]>>>-[<<<+>>>-]<[-]>[-]
 | 
|  |    154 | //       <<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]<[>+>+<<-]>
 | 
|  |    155 | //       [<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<][-]>[-]<<[>+>+<<-]>>[<<+>
 | 
|  |    156 | //       >-]<<<<<[-]>>>>[<<<<+>>>>-]<<<<><>[-]<<[-]>[>+<<+>-]>[<+>-]<>
 | 
|  |    157 | //       <[-]>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<<>>[-]>[-]>[-]>[-]>[-]>
 | 
|  |    158 | //       [-]>[-]>[-]>[-]>[-]<<<<<<<<<<>>++++++++++<<[->+>-[>+>>]>[+[-<+
 | 
|  |    159 | //       >]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<
 | 
|  |    160 | //       ]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++
 | 
|  |    161 | //       ++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<<><<<""")
 | 
| 230 |    162 | 
 | 
|  |    163 | 
 | 
|  |    164 | 
 | 
|  |    165 | // a Mandelbrot set generator in brainf*** written by Erik Bosman
 | 
|  |    166 | // (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
 | 
| 338 |    167 | //
 | 
|  |    168 | //run(load_bff("mandelbrot.bf"))
 | 
| 230 |    169 | 
 | 
|  |    170 | 
 | 
|  |    171 | // a benchmark program (counts down from 'Z' to 'A')
 | 
| 338 |    172 | //
 | 
|  |    173 | //run(load_bff("benchmark.bf"))
 | 
| 230 |    174 | 
 | 
| 338 |    175 | // calculates the Collatz series for numbers from 1 to 30
 | 
|  |    176 | //
 | 
|  |    177 | //run(load_bff("collatz.bf"))
 | 
| 230 |    178 | 
 | 
| 285 |    179 | }
 |