templates5/bfc.scala
author Christian Urban <urbanc@in.tum.de>
Sat, 22 Jun 2019 08:39:52 +0100
changeset 265 59779ce322a6
parent 233 38ea26f227af
child 285 bd9d142d2cd8
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 2 about a "Compiler" for the Brainf*** language
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//======================================================
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// !!! Copy any function you need from file bf.scala !!!
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
// If you need any auxiliary function, feel free to 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
// implement it, but do not make any changes to the
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
// templates below.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
def time_needed[T](n: Int, code: => T) = {
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  val start = System.nanoTime()
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  for (i <- 0 until n) code
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  val end = System.nanoTime()
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  (end - start)/(n * 1.0e9)
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
}
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
type Mem = Map[Int, Int]
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
import io.Source
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
import scala.util._
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
// !! COPY from your bf.scala !!
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// def load_bff(name: String) : String = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
// def sread(mem: Mem, mp: Int) : Int = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// def write(mem: Mem, mp: Int, v: Int) : Mem = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
// def jumpRight(prog: String, pc: Int, level: Int) : Int = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
// def jumpLeft(prog: String, pc: Int, level: Int) : Int = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
// def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
// def run(prog: String, m: Mem = Map()) = 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
// The baseline to what we can compare our "compiler"
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
// implemented below. It should require something like 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
// 60 seconds for the calculation on my laptop
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
//time_needed(1, run(load_bff("benchmark.bf")))
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// DEBUGGING INFORMATION!!!
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
// Compiler, even real ones, are fiendishly difficult to get
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
// to produce correct code. The point is that for example for
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
// the Sierpinski program, they need to still generate code
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
// that displays such a triangle. If yes, then one usually
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
// can take comfort that all is well. If not, then something
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
// went wrong during the optimisations.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
// ADVANCED TASKS
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
//================
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
// (5) Write a function jtable that precomputes the "jump
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
//     table" for a bf-program. This function takes a bf-program 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
//     as an argument and Returns a Map[Int, Int]. The 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
//     purpose of this map is to record the information
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
//     that given on the position pc is a '[' or a ']',
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
//     then to which pc-position do we need to jump next?
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
// 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
//     For example for the program
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
//    
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
//       "+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]"
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
//     we obtain the map
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
//       Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6)
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
//  
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
//     This states that for the '[' on position 5, we need to
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
//     jump to position 20, which is just after the corresponding ']'.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
//     Similarly, for the ']' on position 19, we need to jump to
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
//     position 6, which is just after the '[' on position 5, and so
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
//     on. The idea is to not calculate this information each time
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
//     we hit a bracket, but just look up this information in the 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
//     jtable. You can use the jumpLeft and jumpRight functions
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
//     from Part 1 for calculating the jtable.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
//     Then adapt the compute and run functions from Part 1 in order 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
//     to take advantage of the information stored in the jtable. 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
//     This means whenever jumpLeft and jumpRight was called previously,
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
//     you should look up the jump address in the jtable.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
//def jtable(pg: String) : Map[Int, Int] = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
// testcase
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
// jtable("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
// =>  Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6)
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
//def compute2(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
//def run2(pg: String, m: Mem = Map()) = ... 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
//testcase
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
//time_needed(1, run2(load_bff("benchmark.bf")))
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
// (6) Write a function optimise which deletes "dead code" (everything
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
// that is not a bf-command) and also replaces substrings of the form
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
// [-] by a new command 0. The idea is that the loop [-] just resets the
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
// memory at the current location to 0. In the compute3 and run3 functions
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
// below you implement this command by writing the number 0 to mem(mp), 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
// that is write(mem, mp, 0). 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
// The easiest way to modify a string in this way is to use the regular
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
// expression """[^<>+-.,\[\]]""", which recognises everything that is 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
// not a bf-command and replace it by the empty string. Similarly the
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
// regular expression """\[-\]""" finds all occurrences of [-] and 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
// by using the Scala method .replaceAll you can replace it with the 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
// string "0" standing for the new bf-command.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
//def optimise(s: String) : String = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
//def compute3(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
//def run3(pg: String, m: Mem = Map()) = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
// testcases
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
//optimise(load_bff("benchmark.bf"))          // should have inserted 0's
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
//optimise(load_bff("mandelbrot.bf")).length  // => 11203
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
//time_needed(1, run3(load_bff("benchmark.bf")))
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
// (7)  Write a function combine which replaces sequences
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
// of repeated increment and decrement commands by appropriate
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
// two-character commands. For example for sequences of +
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
//              orig bf-cmds  | replacement
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
//            ------------------------------
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
//              +             | +A 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
//              ++            | +B
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
//              +++           | +C
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
//                            |
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
//              ...           |
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
//                            | 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
//              +++....+++    | +Z
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
//                (where length = 26)
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
//  Similar for the bf-command -, > and <. All other commands should
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
//  be unaffected by this change.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
//  Adapt the compute4 and run4 functions such that they can deal
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
//  appropriately with such two-character commands.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
//def combine(s: String) : String = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
// testcase
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
//combine(load_bff("benchmark.bf"))
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
//def compute4(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
// should call first optimise and then combine on the input string
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
//def run4(pg: String, m: Mem = Map()) = ...
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
// testcases
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
//combine(optimise(load_bff("benchmark.bf"))) // => """>A+B[<A+M>A-A]<A[[....."""
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
//time_needed(1, run4(load_bff("benchmark.bf")))
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
//time_needed(1, run(load_bff("sierpinski.bf"))) 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
//time_needed(1, run4(load_bff("sierpinski.bf"))) 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
//time_needed(1, run4(load_bff("mandelbrot.bf")))
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179