main_testing5/bfc.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 02 Nov 2023 23:34:53 +0000
changeset 475 59e005dcf163
parent 463 0315d9983cd0
child 483 1a51207780e6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
     1
// Part 2 about a "Compiler" for the Brainf*** language
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
     2
//======================================================
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
     4
object M5b {
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 348
diff changeset
     5
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
// !!! Copy any function you need from file bf.scala !!!
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
//
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
// If you need any auxiliary function, feel free to 
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
// implement it, but do not make any changes to the
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
// templates below.
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
def time_needed[T](n: Int, code: => T) = {
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  val start = System.nanoTime()
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  for (i <- 0 until n) code
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  val end = System.nanoTime()
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  (end - start)/(n * 1.0e9)
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
}
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    20
type Mem = Map[Int, Int]
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    21
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    23
import io.Source
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    24
import scala.util._
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    25
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    26
def load_bff(name: String) : String = 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    27
  Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("")
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    28
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    29
def sread(mem: Mem, mp: Int) : Int = 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    30
  mem.getOrElse(mp, 0)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    31
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    32
def write(mem: Mem, mp: Int, v: Int) : Mem =
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    33
  mem.updated(mp, v)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    34
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    35
def jumpRight(prog: String, pc: Int, level: Int) : Int = {
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    36
  if (prog.length <= pc) pc 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    37
  else (prog(pc), level) match {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    38
    case (']', 0) => pc + 1
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    39
    case (']', l) => jumpRight(prog, pc + 1, l - 1)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    40
    case ('[', l) => jumpRight(prog, pc + 1, l + 1)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    41
    case (_, l) => jumpRight(prog, pc + 1, l)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    42
  }
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    43
}
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    44
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    45
def jumpLeft(prog: String, pc: Int, level: Int) : Int = {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    46
  if (pc < 0) pc 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    47
  else (prog(pc), level) match {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    48
    case ('[', 0) => pc + 1
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    49
    case ('[', l) => jumpLeft(prog, pc - 1, l - 1)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    50
    case (']', l) => jumpLeft(prog, pc - 1, l + 1)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    51
    case (_, l) => jumpLeft(prog, pc - 1, l)
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    52
  }
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    53
}
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    54
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    55
def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    56
  if (0 <= pc && pc < prog.length) { 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    57
    val (new_pc, new_mp, new_mem) = prog(pc) match {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    58
      case '>' => (pc + 1, mp + 1, mem)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    59
      case '<' => (pc + 1, mp - 1, mem)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    60
      case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    61
      case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    62
      case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    63
      case '['  => if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    64
      case ']'  => if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    65
      case _ => (pc + 1, mp, mem)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    66
    }		     
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    67
    compute(prog, new_pc, new_mp, new_mem)	
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    68
  }
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    69
  else mem
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    70
}
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    71
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    72
def run(prog: String, m: Mem = Map()) = compute(prog, 0, 0, m)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    73
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    74
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    75
// The baseline to what we can compare our "compiler"
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    76
// implemented below. It should require something like 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    77
// 60 seconds for the calculation on my laptop
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    78
//
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    79
//time_needed(1, run(load_bff("benchmark.bf")))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    80
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    81
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    82
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    83
// DEBUGGING INFORMATION!!!
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    84
//
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    85
// Compiler, even real ones, are fiedishly difficult to get
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    86
// to prduce correct code. The point is that for example for
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    87
// the sierpinski program, they need to still generate code
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    88
// that displays such a triangle. If yes, then one usually
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    89
// can take comfort that all is well. If not, then something
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    90
// went wrong during the optimisations.
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    91
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    92
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    94
// (5) Write a function jtable that precomputes the "jump
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    95
//     table" for a bf-program. This function takes a bf-program 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    96
//     as an argument and Returns a Map[Int, Int]. The 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    97
//     purpose of this map is to record the information
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    98
//     that given on the position pc is a '[' or a ']',
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    99
//     then to which pc-position do we need to jump next?
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   100
// 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   101
//     For example for the program
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   102
//    
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   103
//       "+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]"
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   104
//
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   105
//     we obtain the map
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   106
//
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   107
//       Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   108
//  
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   109
//     This states that for the '[' on position 5, we need to
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   110
//     jump to position 20, which is just after the corresponding ']'.
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   111
//     Similarly, for the ']' on position 19, we need to jump to
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   112
//     position 6, which is just after the '[' on position 5, and so
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   113
//     on. The idea is to not calculate this information each time
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   114
//     we hit a bracket, but just look up this information in the 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   115
//     jtable. You can use the jumpLeft and jumpRight functions
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   116
//     from Part 1 for calculating the jtable.
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   117
//
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   118
//     Then adapt the compute and run functions from Part 1 in order 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   119
//     to take advantage of the information stored in the jtable. 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   120
//     This means whenever jumpLeft and jumpRight was called previously,
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   121
//     you should look up the jump address in the jtable.
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   122
 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   123
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   124
def jtable(pg: String) : Map[Int, Int] = 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   125
    (0 until pg.length).collect { pc => pg(pc) match {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   126
      case '[' => (pc -> jumpRight(pg, pc + 1, 0))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   127
      case ']' => (pc -> jumpLeft(pg, pc - 1, 0))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   128
    }}.toMap
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   129
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   130
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   131
// testcase
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   132
// jtable("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   133
// =>  Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   134
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   135
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   136
def compute2(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   137
  if (0 <= pc && pc < pg.length) { 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   138
    val (new_pc, new_mp, new_mem) = pg(pc) match {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   139
      case '>' => (pc + 1, mp + 1, mem)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   140
      case '<' => (pc + 1, mp - 1, mem)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   141
      case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   142
      case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   143
      case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   144
      case '['  => if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   145
      case ']'  => if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   146
      case _ => (pc + 1, mp, mem)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   147
    }		     
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   148
    compute2(pg, tb, new_pc, new_mp, new_mem)	
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   149
  }
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   150
  else mem
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   151
}
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   152
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   153
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   154
def run2(pg: String, m: Mem = Map()) = 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   155
  compute2(pg, jtable(pg), 0, 0, m)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   156
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   157
//time_needed(1, run2(load_bff("benchmark.bf")))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   158
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   159
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   160
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   161
// (6) Write a function optimise which deletes "dead code" (everything
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   162
// that is not a bf-command) and also replaces substrings of the form
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   163
// [-] by a new command 0. The idea is that the loop [-] just resets the
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   164
// memory at the current location to 0. In the compute3 and run3 functions
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   165
// below you implement this command by writing the number 0 to mem(mp), 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   166
// that is write(mem, mp, 0). 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   167
//
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   168
// The easiest way to modify a string in this way is to use the regular
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   169
// expression """[^<>+-.\[\]""", which recognises everything that is 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   170
// not a bf-command and replace it by the empty string. Similarly the
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   171
// regular expression """\[-\]""" finds all occurences of [-] and 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   172
// by using the Scala method .replaceAll you can repplace it with the 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   173
// string "0" standing for the new bf-command.
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   174
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   175
def optimise(s: String) : String = {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   176
  s.replaceAll("""[^<>+-.\[\]]""","")
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   177
   .replaceAll("""\[-\]""", "0")
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   178
}
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   179
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   181
def compute3(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = {
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   182
  if (0 <= pc && pc < pg.length) { 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   183
    val (new_pc, new_mp, new_mem) = pg(pc) match {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   184
      case '0' => (pc + 1, mp, write(mem, mp, 0))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   185
      case '>' => (pc + 1, mp + 1, mem)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   186
      case '<' => (pc + 1, mp - 1, mem)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   187
      case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   188
      case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   189
      case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   190
      case '['  => if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   191
      case ']'  => if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   192
      case _ => (pc + 1, mp, mem)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   193
    }		     
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   194
    compute3(pg, tb, new_pc, new_mp, new_mem)	
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   195
  }
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   196
  else mem
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
}
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   199
def run3(pg: String, m: Mem = Map()) = { 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   200
  val pg_opt = optimise(pg)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   201
  compute3(pg_opt, jtable(pg_opt), 0, 0, m)
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
}
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
// testcases
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   206
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   207
//println(optimise(load_bff("collatz.bf")))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   208
//optimise(load_bff("benchmark.bf"))          // should have inserted 0's
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   209
//optimise(load_bff("mandelbrot.bf")).length  // => 11203
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   210
 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   211
//time_needed(1, run3(load_bff("benchmark.bf")))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   212
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   215
// (7)  Write a function combine which replaces sequences
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   216
// of repated increment and decrement commands by appropriate
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   217
// two-character commands. For example for sequences of +
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   218
//
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   219
//              orig bf-cmds  | replacement
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   220
//            ------------------------------
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   221
//              +             | +A 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   222
//              ++            | +B
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   223
//              +++           | +C
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   224
//                            |
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   225
//              ...           |
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   226
//                            | 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   227
//              +++....+++    | +Z
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   228
//                (where length = 26)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   229
//
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   230
//  Similar for the bf-command -, > and <. All other commands should
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   231
//  be unaffected by this change.
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   232
//
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   233
//  Adapt the compute4 and run4 functions such that they can deal
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   234
//  appropriately with such two-character commands.
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   235
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   236
def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   237
  case (Nil, acc) => acc  
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   238
  case ('[' :: cs, acc) => splice(cs, ('[', 1) :: acc)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   239
  case (']' :: cs, acc) => splice(cs, (']', 1) :: acc)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   240
  case ('.' :: cs, acc) => splice(cs, ('.', 1) :: acc)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   241
  case ('0' :: cs, acc) => splice(cs, ('0', 1) :: acc)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   242
  case (c :: cs, Nil) => splice(cs, List((c, 1)))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   243
  case (c :: cs, (d, n) :: acc) => 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   244
    if (c == d && n < 26) splice(cs, (c, n + 1) :: acc)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   245
    else splice(cs, (c, 1) :: (d, n) :: acc)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   246
}
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   247
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   248
def spl(s: String) = splice(s.toList, Nil).reverse
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   249
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   250
//spl(load_bff("benchmark.bf"))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   251
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   252
def combine(s: String) : String = {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   253
  (for ((c, n) <- spl(s)) yield c match {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   254
    case '>' => List('>', (n + '@').toChar)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   255
    case '<' => List('<', (n + '@').toChar)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   256
    case '+' => List('+', (n + '@').toChar)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   257
    case '-' => List('-', (n + '@').toChar)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   258
    case _ => List(c)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   259
  }).flatten.mkString
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   260
}
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   261
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   262
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   263
//combine(load_bff("benchmark.bf"))
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
   265
def compute4(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = {
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   266
  if (0 <= pc && pc < pg.length) { 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   267
    val (new_pc, new_mp, new_mem) = pg(pc) match {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   268
      case '0' => (pc + 1, mp, write(mem, mp, 0))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   269
      case '>' => (pc + 2, mp + (pg(pc + 1) - '@'), mem)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   270
      case '<' => (pc + 2, mp - (pg(pc + 1) - '@'), mem)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   271
      case '+' => (pc + 2, mp, write(mem, mp, sread(mem, mp) + (pg(pc + 1) - '@')))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   272
      case '-' => (pc + 2, mp, write(mem, mp, sread(mem, mp) - (pg(pc + 1) - '@')))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   273
      case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   274
      case '['  => if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   275
      case ']'  => if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   276
      case _ => (pc + 1, mp, mem)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   277
    }		     
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   278
    compute4(pg, tb, new_pc, new_mp, new_mem)	
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   279
  }
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   280
  else mem
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 348
diff changeset
   281
}
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 348
diff changeset
   282
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   283
def run4(pg: String, m: Mem = Map()) = { 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   284
  val pg_opt = combine(optimise(pg))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   285
  compute4(pg_opt, jtable(pg_opt), 0, 0, m)
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   286
}
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   287
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   288
// testcases
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   289
//println(combine(optimise(load_bff("mandelbrot.bf").drop(123))))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   290
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   291
//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
   292
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   293
//time_needed(1, run4(load_bff("benchmark.bf")))
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   294
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   295
//time_needed(1, run(load_bff("sierpinski.bf"))) 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   296
//time_needed(1, run4(load_bff("sierpinski.bf"))) 
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   297
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   298
//println(time_needed(1, run4(load_bff("mandelbrot.bf"))))
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   299
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   304
}
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   305
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   306
/*
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   307
import CW10b._
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   308
println(time_needed(1, run(load_bff("collatz.bf"))))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   309
println(time_needed(1, run2(load_bff("collatz.bf"))))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   310
println(time_needed(1, run3(load_bff("collatz.bf"))))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   311
println(time_needed(1, run4(load_bff("collatz.bf"))))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   312
*/