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