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