| 460 |      1 | // Main Part 5 about an Interpreter for 
 | 
|  |      2 | // the Brainf*** language
 | 
| 404 |      3 | //==============================================
 | 
|  |      4 | 
 | 
|  |      5 | 
 | 
| 460 |      6 | object M5a {
 | 
|  |      7 | 
 | 
|  |      8 | // representation of BF memory 
 | 
| 404 |      9 | 
 | 
|  |     10 | type Mem = Map[Int, Int]
 | 
|  |     11 | 
 | 
|  |     12 | import io.Source
 | 
|  |     13 | import scala.util._
 | 
|  |     14 | 
 | 
| 460 |     15 | // ADD YOUR CODE BELOW
 | 
|  |     16 | //======================
 | 
| 404 |     17 | 
 | 
| 460 |     18 | // (1)
 | 
|  |     19 | def load_bff(name: String) : String = {
 | 
|  |     20 |     Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("")
 | 
|  |     21 | }
 | 
| 404 |     22 | 
 | 
| 460 |     23 | // (2) 
 | 
| 404 |     24 | 
 | 
| 460 |     25 | def sread(mem: Mem, mp: Int) : Int = {
 | 
|  |     26 |     Try(mem.apply(mp)).getOrElse(0)
 | 
|  |     27 | }
 | 
| 404 |     28 | 
 | 
| 460 |     29 | def write(mem: Mem, mp: Int, v: Int) : Mem = {
 | 
|  |     30 |     mem + (mp -> v)
 | 
| 404 |     31 | }
 | 
|  |     32 | 
 | 
| 460 |     33 | // sread(Map(), 2) == 0
 | 
|  |     34 | // sread(Map(2 -> 1), 2) == 1
 | 
|  |     35 | // write(Map(), 1, 2) == Map(1 -> 2)
 | 
|  |     36 | // write(Map(1 -> 0), 1, 2) == Map(1 -> 2)
 | 
|  |     37 | 
 | 
|  |     38 | // val current = sread(Map(2 -> 1), 2)
 | 
|  |     39 | // write(Map(2 -> 1), 2, current+1)
 | 
|  |     40 | 
 | 
|  |     41 | // (3) 
 | 
|  |     42 | 
 | 
|  |     43 | def jumpRight(prog: String, pc: Int, level: Int) : Int = prog.toList.apply(pc) match{
 | 
|  |     44 |     case '[' => jumpRight(prog, pc+1, level+1)
 | 
|  |     45 |     case ']' => level match {
 | 
|  |     46 |         case 0 => pc+1
 | 
|  |     47 |         case _ => if (pc+1 >= prog.length) prog.length else jumpRight(prog, pc+1, level-1)
 | 
|  |     48 |     }
 | 
|  |     49 |     case _ => if (pc+1 >= prog.length) prog.length else jumpRight(prog, pc+1, level)
 | 
| 404 |     50 | }
 | 
|  |     51 | 
 | 
| 460 |     52 | def jumpLeft(prog: String, pc: Int, level: Int) : Int = prog.toList.apply(pc) match{
 | 
|  |     53 |     case ']' => jumpLeft(prog, pc-1, level+1)
 | 
|  |     54 |     case '[' => level match {
 | 
|  |     55 |         case 0 => pc+1
 | 
|  |     56 |         case _ => if (pc-1 < 0) -1 else jumpLeft(prog, pc-1, level-1)
 | 
|  |     57 |     }
 | 
|  |     58 |     case _ => if (pc-1 < 0) -1 else jumpLeft(prog, pc-1, level)
 | 
| 404 |     59 | }
 | 
|  |     60 | 
 | 
| 460 |     61 | 
 | 
|  |     62 | // testcases
 | 
|  |     63 | // jumpRight("""--[..+>--],>,++""", 3, 0)         // => 10
 | 
|  |     64 | // jumpLeft("""--[..+>--],>,++""", 8, 0)          // => 3
 | 
|  |     65 | // jumpRight("""--[..[+>]--],>,++""", 3, 0)       // => 12
 | 
|  |     66 | // jumpRight("""--[..[[-]+>[.]]--],>,++""", 3, 0) // => 18
 | 
|  |     67 | // jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0)  // => 22 (outside)
 | 
|  |     68 | // jumpLeft("""[******]***""", 7, 0)              // => -1 (outside)
 | 
| 404 |     69 | 
 | 
|  |     70 | 
 | 
|  |     71 | 
 | 
| 460 |     72 | // (4)
 | 
|  |     73 | def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = (pc >= 0 && pc < prog.length) match {
 | 
|  |     74 |     case false => mem
 | 
|  |     75 |     case true => prog.toList.apply(pc) match{
 | 
|  |     76 |         case '>' => compute(prog, pc+1, mp+1, mem)
 | 
|  |     77 |         case '<' => compute(prog, pc+1, mp-1, mem)
 | 
|  |     78 |         case '+' => {
 | 
|  |     79 |             val current = sread(mem, mp)
 | 
|  |     80 |             compute(prog, pc+1, mp, write(mem, mp, current+1))
 | 
|  |     81 |         }
 | 
|  |     82 |         case '-' => {
 | 
|  |     83 |             val current = sread(mem, mp)
 | 
|  |     84 |             compute(prog, pc+1, mp, write(mem, mp, current-1))
 | 
|  |     85 |         }
 | 
|  |     86 |         case '.' => {
 | 
|  |     87 |             print(mem.apply(mp).toChar)
 | 
|  |     88 |             compute(prog, pc+1, mp, mem)
 | 
|  |     89 |         }
 | 
|  |     90 |         case '[' => {
 | 
|  |     91 |             if (mem.apply(mp) == 0) compute(prog, jumpRight(prog, pc+1, 0), mp, mem)
 | 
|  |     92 |             else compute(prog, pc+1, mp, mem)
 | 
|  |     93 |         }
 | 
|  |     94 |         case ']' => {
 | 
|  |     95 |            if (mem.apply(mp) != 0) compute(prog, jumpLeft(prog, pc-1, 0), mp, mem)
 | 
|  |     96 |            else compute(prog, pc+1, mp, mem) 
 | 
|  |     97 |         }
 | 
|  |     98 |         case _ => compute(prog, pc, mp, mem)
 | 
|  |     99 |     }
 | 
|  |    100 | }
 | 
| 404 |    101 | 
 | 
| 460 |    102 | def run(prog: String, m: Mem = Map()) = {
 | 
|  |    103 |     compute(prog, 0, 0, m)
 | 
|  |    104 | }
 | 
|  |    105 | 
 | 
|  |    106 | // run("[-]", Map(0 -> 100)) == Map(0 -> 0)
 | 
|  |    107 | // run("[->+<]", Map(0 -> 10)) == Map(0 -> 0, 1 -> 10)
 | 
|  |    108 | // run("[>>+>>+<<<<-]", Map(0 -> 42)) == Map(0 -> 0, 2 -> 42, 4 -> 42)
 | 
|  |    109 | // run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""") == Map(0 -> 0, 1 -> 58, 2 -> 32)
 | 
|  |    110 | 
 | 
|  |    111 | // (5)
 | 
|  |    112 | def generate(msg: List[Char]) : String = msg match {
 | 
|  |    113 |     case Nil => ""
 | 
|  |    114 |     case c => "+"*c.head.toInt + ".[-]" + generate(msg.tail)
 | 
|  |    115 | }
 | 
|  |    116 | 
 | 
|  |    117 | // generate("ABC".toList)
 | 
|  |    118 | 
 | 
|  |    119 | // run("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]", Map())
 | 
|  |    120 | 
 | 
|  |    121 | // some sample bf-programs collected from the Internet
 | 
|  |    122 | //=====================================================
 | 
| 404 |    123 | 
 | 
|  |    124 | 
 | 
|  |    125 | // some contrived (small) programs
 | 
|  |    126 | //---------------------------------
 | 
|  |    127 | 
 | 
| 460 |    128 | // // clears the 0-cell
 | 
|  |    129 | // run("[-]", Map(0 -> 100))    // Map will be 0 -> 0
 | 
| 404 |    130 | 
 | 
| 460 |    131 | // // moves content of the 0-cell to 1-cell
 | 
|  |    132 | // run("[->+<]", Map(0 -> 10))  // Map will be 0 -> 0, 1 -> 10
 | 
| 404 |    133 | 
 | 
|  |    134 | 
 | 
| 460 |    135 | // // copies content of the 0-cell to 2-cell and 4-cell
 | 
|  |    136 | // run("[>>+>>+<<<<-]", Map(0 -> 42))    // Map(0 -> 0, 2 -> 42, 4 -> 42)
 | 
| 404 |    137 | 
 | 
|  |    138 | 
 | 
| 460 |    139 | // // prints out numbers 0 to 9
 | 
|  |    140 | // run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
 | 
| 404 |    141 | 
 | 
|  |    142 | 
 | 
| 460 |    143 | // // some more "useful" programs
 | 
|  |    144 | // //-----------------------------
 | 
| 404 |    145 | 
 | 
| 460 |    146 | // // hello world program 1
 | 
|  |    147 | // run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.""")
 | 
| 404 |    148 | 
 | 
| 460 |    149 | // // hello world program 2
 | 
|  |    150 | // run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.""")
 | 
| 404 |    151 | 
 | 
| 460 |    152 | // // hello world program 3
 | 
|  |    153 | // run("""+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..+++.>-.------------.<++++++++.--------.+++.------.--------.>+.""")
 | 
| 404 |    154 | 
 | 
|  |    155 |  
 | 
| 460 |    156 | // // draws the Sierpinski triangle
 | 
|  |    157 | // run(load_bff("sierpinski.bf"))
 | 
| 404 |    158 | 
 | 
|  |    159 | 
 | 
| 460 |    160 | // //fibonacci numbers below 100
 | 
|  |    161 | // run("""+++++++++++
 | 
| 404 |    162 | //      >+>>>>++++++++++++++++++++++++++++++++++++++++++++
 | 
|  |    163 | //      >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
 | 
|  |    164 | //      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
 | 
|  |    165 | //      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
 | 
|  |    166 | //      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
 | 
|  |    167 | //      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
 | 
|  |    168 | //      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
 | 
|  |    169 | //      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
 | 
|  |    170 | //      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
 | 
|  |    171 | //      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""")
 | 
|  |    172 | 
 | 
| 460 |    173 | // //outputs the square numbers up to 10000
 | 
|  |    174 | // run("""++++[>+++++<-]>[<+++++>-]+<+[>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
 | 
| 404 |    175 | //       >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
 | 
|  |    176 | //       <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""")
 | 
|  |    177 | 
 | 
|  |    178 | 
 | 
| 460 |    179 | // // calculates 2 to the power of 6 
 | 
|  |    180 | // // (example from a C-to-BF compiler at https://github.com/elikaski/BF-it)
 | 
|  |    181 | // run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-]
 | 
| 404 |    182 | //       <><[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]
 | 
|  |    183 | //       <[>+>+<<-]>[<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<[[-]>[-]<<[>+>
 | 
|  |    184 | //       +<<-]>>[<<+>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-]
 | 
|  |    185 | //       <<>>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<>[-]<<[-]
 | 
|  |    186 | //       >[>+<<+>-]>[<+>-]<><[-]>[-]<<<[>>+>+<<<-]>>>-[<<<+>>>-]<[-]>[-]
 | 
|  |    187 | //       <<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]<[>+>+<<-]>
 | 
|  |    188 | //       [<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<][-]>[-]<<[>+>+<<-]>>[<<+>
 | 
|  |    189 | //       >-]<<<<<[-]>>>>[<<<<+>>>>-]<<<<><>[-]<<[-]>[>+<<+>-]>[<+>-]<>
 | 
|  |    190 | //       <[-]>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<<>>[-]>[-]>[-]>[-]>[-]>
 | 
|  |    191 | //       [-]>[-]>[-]>[-]>[-]<<<<<<<<<<>>++++++++++<<[->+>-[>+>>]>[+[-<+
 | 
|  |    192 | //       >]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<
 | 
|  |    193 | //       ]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++
 | 
|  |    194 | //       ++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<<><<<""")
 | 
|  |    195 | 
 | 
|  |    196 | 
 | 
|  |    197 | 
 | 
| 460 |    198 | // // a Mandelbrot set generator in brainf*** written by Erik Bosman
 | 
| 404 |    199 | // (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
 | 
| 460 |    200 | 
 | 
|  |    201 | // run(load_bff("mandelbrot.bf"))
 | 
| 404 |    202 | 
 | 
|  |    203 | 
 | 
| 460 |    204 | // // a benchmark program (counts down from 'Z' to 'A')
 | 
|  |    205 | // //
 | 
|  |    206 | // run(load_bff("benchmark.bf"))
 | 
| 404 |    207 | 
 | 
| 460 |    208 | // // calculates the Collatz series for numbers from 1 to 30
 | 
|  |    209 | // //
 | 
|  |    210 | // run(load_bff("collatz.bf"))
 | 
| 404 |    211 | 
 | 
|  |    212 | }
 | 
| 460 |    213 | 
 | 
|  |    214 | 
 | 
|  |    215 | 
 | 
|  |    216 | 
 | 
|  |    217 | 
 | 
|  |    218 | // This template code is subject to copyright 
 | 
|  |    219 | // by King's College London, 2022. Do not 
 | 
|  |    220 | // make the template code public in any shape 
 | 
|  |    221 | // or form, and do not exchange it with other 
 | 
|  |    222 | // students under any circumstance.
 |