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