main_templates5/bfc.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 28 Nov 2020 15:37:30 +0000
changeset 376 6cc36d0ef79e
parent 348 b5b6ed38c2f2
child 384 6e1237691307
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
     1
// Core Part about a "Compiler" for the Brainf*** language
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//======================================================
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
     4
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
     5
object CW10b {
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
     6
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
     7
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
// !!! Copy any function you need from file bf.scala !!!
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
// If you need any auxiliary function, feel free to 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
// implement it, but do not make any changes to the
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
// templates below.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    14
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    15
// DEBUGGING INFORMATION FOR COMPILERS!!!
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    16
//
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    17
// Compiler, even real ones, are fiendishly difficult to get
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    18
// to produce correct code. One way to debug them is to run
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    19
// example programs ``unoptimised''; and then optimised. Does
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    20
// the optimised version still produce the same result?
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    21
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    22
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    23
// for timing purposes
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
def time_needed[T](n: Int, code: => T) = {
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  val start = System.nanoTime()
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  for (i <- 0 until n) code
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  val end = System.nanoTime()
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  (end - start)/(n * 1.0e9)
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
}
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    31
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
type Mem = Map[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
import io.Source
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
import scala.util._
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    38
// TASKS
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    39
//=======
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
// (5) Write a function jtable that precomputes the "jump
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
//     table" for a bf-program. This function takes a bf-program 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
//     as an argument and Returns a Map[Int, Int]. The 
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    44
//     purpose of this map is to record the information about
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    45
//     pc positions where '[' or a ']' are stored. The information
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    46
//     is to which pc-position do we need to jump next?
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
// 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
//     For example for the program
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
//    
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
//       "+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]"
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
//     we obtain the map
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
//       Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6)
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
//  
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
//     This states that for the '[' on position 5, we need to
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
//     jump to position 20, which is just after the corresponding ']'.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
//     Similarly, for the ']' on position 19, we need to jump to
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
//     position 6, which is just after the '[' on position 5, and so
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
//     on. The idea is to not calculate this information each time
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
//     we hit a bracket, but just look up this information in the 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
//     jtable. You can use the jumpLeft and jumpRight functions
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
//     from Part 1 for calculating the jtable.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
//
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    65
//     Then adapt the compute and run functions from Part 1 
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    66
//     in order to take advantage of the information stored in the jtable. 
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
//     This means whenever jumpLeft and jumpRight was called previously,
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    68
//     you should immediately look up the jump address in the jtable.
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    71
def jtable(pg: String) : Map[Int, Int] = ???
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    73
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
// testcase
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    75
//
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
// jtable("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
// =>  Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6)
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    80
def compute2(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = ???
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    81
def run2(pg: String, m: Mem = Map()) = ???
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    84
// testcases
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    85
// time_needed(1, run2(load_bff("benchmark.bf")))
333
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 285
diff changeset
    86
// time_needed(1, run2(load_bff("sierpinski.bf")))
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
// (6) Write a function optimise which deletes "dead code" (everything
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
// that is not a bf-command) and also replaces substrings of the form
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
// [-] 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
    93
// memory at the current location to 0. In the compute3 and run3 functions
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
// below you implement this command by writing the number 0 to mem(mp), 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
// that is write(mem, mp, 0). 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
// 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
    98
// expression """[^<>+-.,\[\]]""", which recognises everything that is 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
// not a bf-command and replace it by the empty string. Similarly the
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
// regular expression """\[-\]""" finds all occurrences of [-] and 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
// by using the Scala method .replaceAll you can replace it with the 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
// string "0" standing for the new bf-command.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
   104
def optimise(s: String) : String = ???
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
   106
def compute3(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = ???
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
   108
def run3(pg: String, m: Mem = Map()) = ???
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
// testcases
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   112
//
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   113
// optimise(load_bff("benchmark.bf"))          // should have inserted 0's
348
b5b6ed38c2f2 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   114
// optimise(load_bff("mandelbrot.bf")).length  // => 11205
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   115
// 
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   116
// time_needed(1, run3(load_bff("benchmark.bf")))
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
// (7)  Write a function combine which replaces sequences
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
// of repeated increment and decrement commands by appropriate
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
// two-character commands. For example for sequences of +
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
//              orig bf-cmds  | replacement
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
//            ------------------------------
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
//              +             | +A 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
//              ++            | +B
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
//              +++           | +C
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
//                            |
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
//              ...           |
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
//                            | 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
//              +++....+++    | +Z
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
//                (where length = 26)
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
//  Similar for the bf-command -, > and <. All other commands should
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
//  be unaffected by this change.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
//  Adapt the compute4 and run4 functions such that they can deal
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
//  appropriately with such two-character commands.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
   142
def combine(s: String) : String = ???
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   144
// testcase
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   145
// combine(load_bff("benchmark.bf"))
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
   148
def compute4(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = ???
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   150
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
// should call first optimise and then combine on the input string
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   152
//
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
   153
def run4(pg: String, m: Mem = Map()) = ???
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
// testcases
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   157
// combine(optimise(load_bff("benchmark.bf"))) // => """>A+B[<A+M>A-A]<A[[....."""
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   159
// testcases (they should now run much faster)
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   160
// time_needed(1, run4(load_bff("benchmark.bf")))
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   161
// time_needed(1, run4(load_bff("sierpinski.bf"))) 
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   162
// time_needed(1, run4(load_bff("mandelbrot.bf")))
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
   165
}