main_testing5/bfc.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 02 Nov 2023 11:32:10 +0000
changeset 472 6a77c260c8a5
parent 463 0315d9983cd0
child 475 59e005dcf163
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
     1
// Main Part 5 about a "Compiler" for the Brainf*** language
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
     2
//============================================================
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
     3
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
     5
object M5b {
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 348
diff changeset
     6
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
// !!! Copy any function you need from file bf.scala !!!
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
//
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
// If you need any auxiliary function, feel free to 
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
// implement it, but do not make any changes to the
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
// templates below.
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    14
// DEBUGGING INFORMATION FOR COMPILERS!!!
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    15
//
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    16
// Compiler, even real ones, are fiendishly difficult to get
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    17
// to produce correct code. One way to debug them is to run
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    18
// example programs ``unoptimised''; and then optimised. Does
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    19
// the optimised version still produce the same result?
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    20
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    21
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    22
// for timing purposes
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
def time_needed[T](n: Int, code: => T) = {
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  val start = System.nanoTime()
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  for (i <- 0 until n) code
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  val end = System.nanoTime()
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  (end - start)/(n * 1.0e9)
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
}
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    30
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    31
type Mem = Map[Int, Int]
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    33
import io.Source
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    34
import scala.util._
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    35
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    36
// ADD YOUR CODE BELOW
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    37
//======================
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    38
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    39
// (6)
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    40
def jumpRight(prog: String, pc: Int, level: Int) : Int = {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    41
    if (pc >= prog.length) prog.length
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    42
    else if (prog(pc) == '[') jumpRight(prog, pc + 1, level + 1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    43
    else if (prog(pc) == ']' && level == 0) pc + 1
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    44
    else if (prog(pc) == ']') jumpRight(prog, pc + 1, level - 1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    45
    else jumpRight(prog, pc + 1, level)
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    46
}
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    47
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    48
def jtable(pg: String) : Map[Int, Int] = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    49
    val pairs = for {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    50
        i <- 0 until pg.length
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    51
        if pg.charAt(i) == '['
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    52
        j = jumpRight(pg, i+1, 0)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    53
    } yield (i, j)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    54
    pairs.flatMap { case (i, j) => 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    55
        List((i, j), (j-1, i+1))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    56
    }.toMap
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    57
}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    58
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    59
def write(mem: Mem, mp: Int, v: Int) : Mem = mem + (mp -> v)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    60
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    61
// testcase
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    62
//
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    63
// jtable("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""") 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    64
// =>  Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    65
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    66
def compute2(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    67
  if (pc >= pg.length) mem
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    68
  else {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    69
      val (npc, nmp, nmem) = pg(pc) match {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    70
          case '>' => (pc + 1, mp + 1, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    71
          case '<' => (pc + 1, mp - 1, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    72
          case '+' => (pc + 1, mp, mem + (mp -> (mem.getOrElse(mp, 0) + 1)))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    73
          case '-' => (pc + 1, mp, mem + (mp -> (mem.getOrElse(mp, 0) - 1)))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    74
          case '.' => {print(mem.getOrElse(mp,0).toChar);(pc + 1, mp, mem)}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    75
          case '[' => if (mem.getOrElse(mp, 0) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    76
          case ']' => if (mem.getOrElse(mp, 0) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    77
          case _ => (pc + 1, mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    78
      }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    79
      compute2(pg, tb, npc, nmp, nmem)
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    80
  }
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    81
}
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    82
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    83
def run2(pg: String, m: Mem = Map()) = 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    84
  compute2(pg, jtable(pg), 0, 0, m)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    85
  
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    86
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    87
// testcases
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    88
// time_needed(1, run2(load_bff("benchmark.bf")))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    89
// time_needed(1, run2(load_bff("sierpinski.bf")))
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    90
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    91
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    93
// (7) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    94
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    95
def optimise(s: String) : String =
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    96
  s.replaceAll("""[^<>+-.,\[\]]""","").replaceAll("""\[-\]""","0")
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    98
def compute3(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    99
    if (pc >= pg.length) mem
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   100
    else {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   101
      val (npc, nmp, nmem) = pg(pc) match {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   102
          case '>' => (pc + 1, mp + 1, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   103
          case '<' => (pc + 1, mp - 1, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   104
          case '+' => (pc + 1, mp, mem + (mp -> (mem.getOrElse(mp, 0) + 1)))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   105
          case '-' => (pc + 1, mp, mem + (mp -> (mem.getOrElse(mp, 0) - 1)))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   106
          case '.' => {print(mem.getOrElse(mp,0).toChar);(pc + 1, mp, mem)}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   107
          case '[' => if (mem.getOrElse(mp, 0) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   108
          case ']' => if (mem.getOrElse(mp, 0) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   109
          case _ => (pc + 1, mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   110
      }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   111
      compute3(pg, tb, npc, nmp, nmem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   112
    }
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
}
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   115
def run3(pg: String, m: Mem = Map()) = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   116
  val opt_pg = optimise(pg)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   117
  val jt = jtable(opt_pg)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   118
  compute3(opt_pg, jt, 0, 0, m)
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
}
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
// testcases
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   123
//
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   124
// optimise(load_bff("benchmark.bf"))          // should have inserted 0's
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   125
// optimise(load_bff("mandelbrot.bf")).length  // => 11203
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   126
// optimise(load_bff("benchmark.bf")).length
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   127
// time_needed(1, run3(load_bff("benchmark.bf")))
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   130
// (8)  
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   131
def combine(s: String): String = ???
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   132
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   133
// testcase
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   134
// combine(load_bff("benchmark.bf"))
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   136
def compute4(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   137
    if (pc >= pg.length) mem
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   138
    else {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   139
      val (npc, nmp, nmem) = pg(pc) match {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   140
          case '>' => (pc + 1, mp + 1, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   141
          case '<' => (pc + 1, mp - 1, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   142
          case '+' => (pc + 1, mp, mem + (mp -> (mem.getOrElse(mp, 0) + 1)))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   143
          case '-' => (pc + 1, mp, mem + (mp -> (mem.getOrElse(mp, 0) - 1)))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   144
          case '.' => {print(mem.getOrElse(mp,0).toChar);(pc + 1, mp, mem)}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   145
          case '[' => if (mem.getOrElse(mp, 0) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   146
          case ']' => if (mem.getOrElse(mp, 0) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   147
          case _ => (pc + 1, mp, mem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   148
      }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   149
      compute3(pg, tb, npc, nmp, nmem)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   150
    }
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 348
diff changeset
   151
}
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 348
diff changeset
   152
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   153
// should call first optimise and then combine on the input string
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   154
//
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   155
def run4(pg: String, m: Mem = Map()) = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   156
  val co_opt_pg = combine(optimise(pg))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   157
  val jt = jtable(co_opt_pg)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   158
  compute3(co_opt_pg, jt, 0, 0, m)
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   159
}
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   160
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   161
// testcases
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   162
// combine(optimise(load_bff("benchmark.bf"))) // => """>A+B[<A+M>A-A]<A[[....."""
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   163
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   164
// testcases (they should now run much faster)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   165
// time_needed(1, run4(load_bff("benchmark.bf")))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   166
// time_needed(1, run4(load_bff("sierpinski.bf"))) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   167
// time_needed(1, run4(load_bff("mandelbrot.bf")))
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   168
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   169
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   170
}
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   171
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   176
// This template code is subject to copyright 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   177
// by King's College London, 2022. Do not 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   178
// make the template code public in any shape 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   179
// or form, and do not exchange it with other 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   180
// students under any circumstance.