| 480 |      1 | // Main Part 5 about a "Compiler" for the Brainf*** language
 | 
|  |      2 | //============================================================
 | 
|  |      3 | 
 | 
| 235 |      4 | 
 | 
| 404 |      5 | object M5b {
 | 
| 384 |      6 | 
 | 
| 235 |      7 | // !!! Copy any function you need from file bf.scala !!!
 | 
|  |      8 | //
 | 
|  |      9 | // If you need any auxiliary function, feel free to 
 | 
|  |     10 | // implement it, but do not make any changes to the
 | 
|  |     11 | // templates below.
 | 
|  |     12 | 
 | 
|  |     13 | 
 | 
| 480 |     14 | // DEBUGGING INFORMATION FOR COMPILERS!!!
 | 
|  |     15 | //
 | 
|  |     16 | // Compiler, even real ones, are fiendishly difficult to get
 | 
|  |     17 | // to produce correct code. One way to debug them is to run
 | 
|  |     18 | // example programs ``unoptimised''; and then optimised. Does
 | 
|  |     19 | // the optimised version still produce the same result?
 | 
|  |     20 | 
 | 
|  |     21 | 
 | 
|  |     22 | // for timing purposes
 | 
| 235 |     23 | def time_needed[T](n: Int, code: => T) = {
 | 
|  |     24 |   val start = System.nanoTime()
 | 
|  |     25 |   for (i <- 0 until n) code
 | 
|  |     26 |   val end = System.nanoTime()
 | 
|  |     27 |   (end - start)/(n * 1.0e9)
 | 
|  |     28 | }
 | 
|  |     29 | 
 | 
| 480 |     30 | 
 | 
| 472 |     31 | type Mem = Map[Int, Int]
 | 
| 460 |     32 | 
 | 
| 480 |     33 | import scala.io.Source
 | 
| 404 |     34 | import scala.util._
 | 
|  |     35 | 
 | 
| 480 |     36 | // ADD YOUR CODE BELOW
 | 
|  |     37 | //======================
 | 
| 404 |     38 | 
 | 
| 480 |     39 | def sread(mem: Mem, mp: Int) : Int = mem.getOrElse(mp, 0)
 | 
| 404 |     40 | 
 | 
| 480 |     41 | def write(mem: Mem, mp: Int, v: Int) : Mem = mem + (mp -> v)
 | 
| 472 |     42 | 
 | 
| 480 |     43 | // (6)
 | 
|  |     44 | def empty_stack(len : Int, st : List[Int]) : Map[Int, Int] = st match {
 | 
|  |     45 |   case Nil => Map()
 | 
|  |     46 |   case n :: tail => empty_stack(len, tail) + (n -> len)
 | 
|  |     47 | }
 | 
|  |     48 | def jtable_helper(pg : List[Char], st : List[Int] = List(), index : Int = 0) : Map[Int, Int] = pg match {
 | 
|  |     49 |   case Nil => empty_stack(pg.length, st)
 | 
|  |     50 |   case '[' :: tail => jtable_helper(tail, index :: st, index + 1)
 | 
|  |     51 |   case ']' :: tail => st match {
 | 
|  |     52 |     case Nil => jtable_helper(tail, st, index + 1) + (index -> -1)
 | 
|  |     53 |     case n :: stail => jtable_helper(tail, stail, index + 1) + (n -> (index + 1)) + (index -> (n + 1))
 | 
|  |     54 |   }
 | 
|  |     55 |   case _ :: tail => jtable_helper(tail, st, index + 1)
 | 
|  |     56 | }
 | 
|  |     57 | def jtable(pg: String) : Map[Int, Int] = jtable_helper(pg.toList)
 | 
| 472 |     58 | 
 | 
|  |     59 | // testcase
 | 
| 480 |     60 | //
 | 
| 472 |     61 | // jtable("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
 | 
|  |     62 | // =>  Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6)
 | 
|  |     63 | 
 | 
| 460 |     64 | 
 | 
| 480 |     65 | def compute2(prog: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = 
 | 
|  |     66 |     if (pc < 0 || pc >= prog.length) mem else prog.charAt(pc) match {
 | 
|  |     67 |         case '>' => compute2(prog, tb, pc+1, mp+1, mem)
 | 
|  |     68 |         case '<' => compute2(prog, tb, pc+1, mp-1, mem)
 | 
|  |     69 |         case '+' => compute2(prog, tb, pc+1, mp, mem + (mp -> (sread(mem, mp) + 1)))
 | 
|  |     70 |         case '-' => compute2(prog, tb, pc+1, mp, mem + (mp -> (sread(mem, mp) - 1)))
 | 
|  |     71 |         case '.' => print(sread(mem, mp).toChar); compute2(prog, tb, pc+1, mp, mem)
 | 
|  |     72 |         case '[' => if (sread(mem, mp) == 0) compute2(prog, tb, tb.getOrElse(pc, -2), mp, mem)
 | 
|  |     73 |                     else compute2(prog, tb, pc+1, mp, mem)
 | 
|  |     74 |         case ']' => if (sread(mem, mp) == 0) compute2(prog, tb, pc+1, mp, mem)
 | 
|  |     75 |                     else compute2(prog, tb, tb.getOrElse(pc, -2), mp, mem)
 | 
|  |     76 |         case _ => compute2(prog, tb, pc+1, mp, mem)
 | 
| 472 |     77 | }
 | 
|  |     78 | 
 | 
| 480 |     79 | def run2(prog: String, m: Mem = Map()) = compute2(prog, jtable(prog), 0, 0, m)
 | 
| 235 |     80 | 
 | 
|  |     81 | // testcases
 | 
| 480 |     82 | // time_needed(1, run2(load_bff("benchmark.bf")))
 | 
|  |     83 | // time_needed(1, run2(load_bff("sierpinski.bf")))
 | 
| 472 |     84 | 
 | 
| 235 |     85 | 
 | 
|  |     86 | 
 | 
| 480 |     87 | // (7) 
 | 
|  |     88 | 
 | 
|  |     89 | def optimise(s: String) : String =
 | 
|  |     90 |   s.filter(List('>', '<', '+', '-', '[', ']', '.').contains(_)).replaceAllLiterally("[-]", "0")
 | 
| 460 |     91 | 
 | 
| 480 |     92 | def compute3(prog: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = 
 | 
|  |     93 |   if (pc < 0 || pc >= prog.length) mem else prog.charAt(pc) match {
 | 
|  |     94 |         case '>' => compute3(prog, tb, pc+1, mp+1, mem)
 | 
|  |     95 |         case '<' => compute3(prog, tb, pc+1, mp-1, mem)
 | 
|  |     96 |         case '+' => compute3(prog, tb, pc+1, mp, mem + (mp -> (sread(mem, mp) + 1)))
 | 
|  |     97 |         case '-' => compute3(prog, tb, pc+1, mp, mem + (mp -> (sread(mem, mp) - 1)))
 | 
|  |     98 |         case '.' => print(sread(mem, mp).toChar); compute3(prog, tb, pc+1, mp, mem)
 | 
|  |     99 |         case '[' => if (sread(mem, mp) == 0) compute3(prog, tb, tb.getOrElse(pc, -2), mp, mem)
 | 
|  |    100 |                     else compute3(prog, tb, pc+1, mp, mem)
 | 
|  |    101 |         case ']' => if (sread(mem, mp) == 0) compute3(prog, tb, pc+1, mp, mem)
 | 
|  |    102 |                     else compute3(prog, tb, tb.getOrElse(pc, -2), mp, mem)
 | 
|  |    103 |         case '0' => compute3(prog, tb, pc+1, mp, mem + (mp -> 0))
 | 
|  |    104 |         case _ => compute3(prog, tb, pc+1, mp, mem)
 | 
| 472 |    105 | }
 | 
|  |    106 | 
 | 
| 480 |    107 | def run3(prog: String, m: Mem = Map()) = 
 | 
|  |    108 |   val optimized = optimise(prog)
 | 
|  |    109 |   compute3(optimized, jtable(optimized), 0, 0, m)
 | 
| 472 |    110 | 
 | 
|  |    111 | 
 | 
| 480 |    112 | // testcases
 | 
|  |    113 | //
 | 
|  |    114 | // optimise(load_bff("benchmark.bf"))          // should have inserted 0's
 | 
|  |    115 | // optimise(load_bff("mandelbrot.bf")).length  // => 11205 // this is wrong, it's 11203!
 | 
|  |    116 | // 
 | 
|  |    117 | // time_needed(1, run3(load_bff("benchmark.bf")))
 | 
| 235 |    118 | 
 | 
| 480 |    119 | // (8)
 | 
|  |    120 | def combine_helper(c : String, s : String) : String =
 | 
|  |    121 |   val temp1 = s.replaceAllLiterally(c*26, c ++ "Z").replaceAllLiterally(c*25, c ++ "Y").replaceAllLiterally(c*24, c ++ "X").replaceAllLiterally(c*23, c ++ "W")
 | 
|  |    122 |   val temp2 = temp1.replaceAllLiterally(c*22, c ++ "V").replaceAllLiterally(c*21, c ++ "U").replaceAllLiterally(c*20, c ++ "T").replaceAllLiterally(c*19, c ++ "S")
 | 
|  |    123 |   val temp3 = temp2.replaceAllLiterally(c*18, c ++ "R").replaceAllLiterally(c*17, c ++ "Q").replaceAllLiterally(c*16, c ++ "P").replaceAllLiterally(c*15, c ++ "O")
 | 
|  |    124 |   val temp4 = temp3.replaceAllLiterally(c*14, c ++ "N").replaceAllLiterally(c*13, c ++ "M").replaceAllLiterally(c*12, c ++ "L").replaceAllLiterally(c*11, c ++ "K")
 | 
|  |    125 |   val temp5 = temp4.replaceAllLiterally(c*10, c ++ "J").replaceAllLiterally(c*9, c ++ "I").replaceAllLiterally(c*8, c ++ "H").replaceAllLiterally(c*7, c ++ "G")
 | 
|  |    126 |   val temp6 = temp5.replaceAllLiterally(c*6, c ++ "F").replaceAllLiterally(c*5, c ++ "E").replaceAllLiterally(c*4, c ++ "D").replaceAllLiterally(c*3, c ++ "C")
 | 
|  |    127 |   val temp7 = temp6.replaceAllLiterally(c*2, c ++ "B").replaceAllLiterally(c ++ ">", c ++ "A>").replaceAllLiterally(c ++ "<", c ++ "A<")
 | 
|  |    128 |   val temp8 = temp7.replaceAllLiterally(c ++ "+", c ++ "A+").replaceAllLiterally(c ++ "-", c ++ "A-").replaceAllLiterally(c ++ "[", c ++ "A[")
 | 
|  |    129 |   temp8.replaceAllLiterally(c ++ "]", c ++ "A]").replaceAllLiterally(c ++ ".", c ++ "A.").replaceAllLiterally(c ++ "0", c ++ "A0")
 | 
|  |    130 | def combine(s: String) : String = 
 | 
|  |    131 |   val temp1 = combine_helper(">", s)
 | 
|  |    132 |   val temp2 = combine_helper("<", temp1)
 | 
|  |    133 |   val temp3 = combine_helper("+", temp2)
 | 
|  |    134 |   val temp4 = combine_helper("-", temp3)
 | 
|  |    135 |   temp4
 | 
|  |    136 | 
 | 
|  |    137 | // testcase
 | 
|  |    138 | // combine(load_bff("benchmark.bf"))
 | 
|  |    139 | 
 | 
|  |    140 | def compute4(prog: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = if (pc < 0 || pc >= prog.length) mem else prog.charAt(pc) match {
 | 
|  |    141 |   case '>' => val n = (prog.charAt(pc+1).toInt - 64)
 | 
|  |    142 |               compute4(prog, tb, pc+1, mp+n, mem)
 | 
|  |    143 |   case '<' => val n = (prog.charAt(pc+1).toInt - 64)
 | 
|  |    144 |               compute4(prog, tb, pc+1, mp-n, mem)
 | 
|  |    145 |   case '+' => val n = (prog.charAt(pc+1).toInt - 64)
 | 
|  |    146 |               compute4(prog, tb, pc+1, mp, mem + (mp -> (sread(mem, mp) + n)))
 | 
|  |    147 |   case '-' => val n = (prog.charAt(pc+1).toInt - 64)
 | 
|  |    148 |               compute4(prog, tb, pc+1, mp, mem + (mp -> (sread(mem, mp) - n)))
 | 
|  |    149 |   case '.' => print(sread(mem, mp).toChar); compute4(prog, tb, pc+1, mp, mem)
 | 
|  |    150 |   case '[' => if (sread(mem, mp) == 0) compute4(prog, tb, tb.getOrElse(pc, -2), mp, mem)
 | 
|  |    151 |               else compute4(prog, tb, pc+1, mp, mem)
 | 
|  |    152 |   case ']' => if (sread(mem, mp) == 0) compute4(prog, tb, pc+1, mp, mem)
 | 
|  |    153 |               else compute4(prog, tb, tb.getOrElse(pc, -2), mp, mem)
 | 
|  |    154 |   case '0' => compute4(prog, tb, pc+1, mp, mem + (mp -> 0))
 | 
|  |    155 |   case _ => compute4(prog, tb, pc+1, mp, mem)
 | 
| 384 |    156 | }
 | 
|  |    157 | 
 | 
| 480 |    158 | // should call first optimise and then combine on the input string
 | 
|  |    159 | //
 | 
|  |    160 | def run4(prog: String, m: Mem = Map()) = 
 | 
|  |    161 |   val optimized = combine(optimise(prog))
 | 
|  |    162 |   compute4(optimized, jtable(optimized), 0, 0, m)
 | 
| 404 |    163 | 
 | 
| 235 |    164 | 
 | 
|  |    165 | 
 | 
| 480 |    166 | // testcases
 | 
|  |    167 | // combine(optimise(load_bff("benchmark.bf"))) // => """>A+B[<A+M>A-A]<A[[....."""
 | 
|  |    168 | 
 | 
|  |    169 | // testcases (they should now run much faster)
 | 
|  |    170 | // time_needed(1, run4(load_bff("benchmark.bf")))
 | 
|  |    171 | // time_needed(1, run4(load_bff("sierpinski.bf"))) 
 | 
|  |    172 | // time_needed(1, run4(load_bff("mandelbrot.bf")))
 | 
| 235 |    173 | 
 | 
|  |    174 | 
 | 
| 472 |    175 | }
 |