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