main_templates5/bfc.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 15 Sep 2024 12:57:59 +0100
changeset 493 244df77507c2
parent 463 0315d9983cd0
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
399
b17a98b0c52f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
     1
// Main Part 5 about a "Compiler" for the Brainf*** language
b17a98b0c52f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
     2
//============================================================
233
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
399
b17a98b0c52f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
     5
object M5b {
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
     6
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
// !!! Copy any function you need from file bf.scala !!!
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
//
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
// If you need any auxiliary function, feel free to 
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
// implement it, but do not make any changes to the
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
// templates below.
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    13
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    14
// DEBUGGING INFORMATION FOR COMPILERS!!!
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    15
//
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    16
// Compiler, even real ones, are fiendishly difficult to get
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    17
// to produce correct code. One way to debug them is to run
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    18
// example programs ``unoptimised''; and then optimised. Does
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    19
// the optimised version still produce the same result?
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    20
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
// for timing purposes
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
def time_needed[T](n: Int, code: => T) = {
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  val start = System.nanoTime()
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  for (i <- 0 until n) code
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  val end = System.nanoTime()
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  (end - start)/(n * 1.0e9)
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
}
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    30
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
type Mem = Map[Int, Int]
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
import io.Source
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
import scala.util._
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
    36
// ADD YOUR CODE BELOW
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
    37
//======================
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
429
126d0e47ac85 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    39
// (6) 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    40
def jtable(pg: String) : Map[Int, Int] = ???
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
// testcase
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    43
//
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// jtable("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// =>  Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6)
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    48
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
    49
def run2(pg: String, m: Mem = Map()) = ???
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    51
// testcases
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    52
// time_needed(1, run2(load_bff("benchmark.bf")))
333
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 285
diff changeset
    53
// time_needed(1, run2(load_bff("sierpinski.bf")))
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
429
126d0e47ac85 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    57
// (7) 
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    59
def optimise(s: String) : String = ???
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    61
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
    62
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    63
def run3(pg: String, m: Mem = Map()) = ???
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
// testcases
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    67
//
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    68
// optimise(load_bff("benchmark.bf"))          // should have inserted 0's
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 432
diff changeset
    69
// optimise(load_bff("mandelbrot.bf")).length  // => 11205
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    70
// 
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    71
// time_needed(1, run3(load_bff("benchmark.bf")))
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
429
126d0e47ac85 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    75
// (8)  
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    76
def combine(s: String) : String = ???
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    78
// testcase
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    79
// combine(load_bff("benchmark.bf"))
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    81
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
    82
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
// should call first optimise and then combine on the input string
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    84
//
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    85
def run4(pg: String, m: Mem = Map()) = ???
233
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
// testcases
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    89
// 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
    90
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    91
// testcases (they should now run much faster)
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    92
// time_needed(1, run4(load_bff("benchmark.bf")))
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    93
// time_needed(1, run4(load_bff("sierpinski.bf"))) 
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    94
// time_needed(1, run4(load_bff("mandelbrot.bf")))
233
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
38ea26f227af updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
285
bd9d142d2cd8 updated
Christian Urban <urbanc@in.tum.de>
parents: 233
diff changeset
    97
}