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