main_testing5/bfc.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 26 Apr 2024 17:35:36 +0100
changeset 486 9c03b5e89a2a
parent 483 1a51207780e6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
     1
// Main Part 5 about a "Compiler" for the Brainf*** language
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
     2
//============================================================
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
     3
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
     5
object M5b {
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 348
diff changeset
     6
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
// !!! Copy any function you need from file bf.scala !!!
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
//
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
// If you need any auxiliary function, feel free to 
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
// implement it, but do not make any changes to the
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
// templates below.
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    14
// DEBUGGING INFORMATION FOR COMPILERS!!!
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    15
//
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    16
// Compiler, even real ones, are fiendishly difficult to get
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    17
// to produce correct code. One way to debug them is to run
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    18
// example programs ``unoptimised''; and then optimised. Does
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    19
// the optimised version still produce the same result?
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    20
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    21
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    22
// for timing purposes
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
def time_needed[T](n: Int, code: => T) = {
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  val start = System.nanoTime()
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  for (i <- 0 until n) code
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  val end = System.nanoTime()
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  (end - start)/(n * 1.0e9)
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
}
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    30
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    31
type Mem = Map[Int, Int]
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    32
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    33
import scala.io.Source
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    34
import scala.util._
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    35
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    36
// ADD YOUR CODE BELOW
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    37
//======================
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    38
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    39
def sread(mem: Mem, mp: Int) : Int = mem.getOrElse(mp, 0)
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    40
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    41
def write(mem: Mem, mp: Int, v: Int) : Mem = mem + (mp -> v)
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    42
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    43
// (6)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    44
def empty_stack(len : Int, st : List[Int]) : Map[Int, Int] = st match {
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    45
  case Nil => Map()
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    46
  case n :: tail => empty_stack(len, tail) + (n -> len)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    47
}
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    48
def jtable_helper(pg : List[Char], st : List[Int] = List(), index : Int = 0) : Map[Int, Int] = pg match {
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    49
  case Nil => empty_stack(pg.length, st)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    50
  case '[' :: tail => jtable_helper(tail, index :: st, index + 1)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    51
  case ']' :: tail => st match {
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    52
    case Nil => jtable_helper(tail, st, index + 1) + (index -> -1)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    53
    case n :: stail => jtable_helper(tail, stail, index + 1) + (n -> (index + 1)) + (index -> (n + 1))
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    54
  }
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    55
  case _ :: tail => jtable_helper(tail, st, index + 1)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    56
}
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    57
def jtable(pg: String) : Map[Int, Int] = jtable_helper(pg.toList)
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    58
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    59
// testcase
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    60
//
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    61
// jtable("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    62
// =>  Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    63
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    64
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    65
def compute2(prog: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = 
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    66
    if (pc < 0 || pc >= prog.length) mem else prog.charAt(pc) match {
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    67
        case '>' => compute2(prog, tb, pc+1, mp+1, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    68
        case '<' => compute2(prog, tb, pc+1, mp-1, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    69
        case '+' => compute2(prog, tb, pc+1, mp, mem + (mp -> (sread(mem, mp) + 1)))
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    70
        case '-' => compute2(prog, tb, pc+1, mp, mem + (mp -> (sread(mem, mp) - 1)))
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    71
        case '.' => print(sread(mem, mp).toChar); compute2(prog, tb, pc+1, mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    72
        case '[' => if (sread(mem, mp) == 0) compute2(prog, tb, tb.getOrElse(pc, -2), mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    73
                    else compute2(prog, tb, pc+1, mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    74
        case ']' => if (sread(mem, mp) == 0) compute2(prog, tb, pc+1, mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    75
                    else compute2(prog, tb, tb.getOrElse(pc, -2), mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    76
        case _ => compute2(prog, tb, pc+1, mp, mem)
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    77
}
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    78
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    79
def run2(prog: String, m: Mem = Map()) = compute2(prog, jtable(prog), 0, 0, m)
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
// testcases
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    82
// time_needed(1, run2(load_bff("benchmark.bf")))
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    83
// time_needed(1, run2(load_bff("sierpinski.bf")))
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    84
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    87
// (7) 
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    88
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    89
def optimise(s: String) : String =
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    90
  s.filter(List('>', '<', '+', '-', '[', ']', '.').contains(_)).replaceAllLiterally("[-]", "0")
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 404
diff changeset
    91
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    92
def compute3(prog: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = 
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    93
  if (pc < 0 || pc >= prog.length) mem else prog.charAt(pc) match {
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    94
        case '>' => compute3(prog, tb, pc+1, mp+1, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    95
        case '<' => compute3(prog, tb, pc+1, mp-1, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    96
        case '+' => compute3(prog, tb, pc+1, mp, mem + (mp -> (sread(mem, mp) + 1)))
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    97
        case '-' => compute3(prog, tb, pc+1, mp, mem + (mp -> (sread(mem, mp) - 1)))
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    98
        case '.' => print(sread(mem, mp).toChar); compute3(prog, tb, pc+1, mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
    99
        case '[' => if (sread(mem, mp) == 0) compute3(prog, tb, tb.getOrElse(pc, -2), mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   100
                    else compute3(prog, tb, pc+1, mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   101
        case ']' => if (sread(mem, mp) == 0) compute3(prog, tb, pc+1, mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   102
                    else compute3(prog, tb, tb.getOrElse(pc, -2), mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   103
        case '0' => compute3(prog, tb, pc+1, mp, mem + (mp -> 0))
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   104
        case _ => compute3(prog, tb, pc+1, mp, mem)
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   105
}
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   106
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   107
def run3(prog: String, m: Mem = Map()) = 
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   108
  val optimized = optimise(prog)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   109
  compute3(optimized, jtable(optimized), 0, 0, m)
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   110
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   111
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   112
// testcases
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   113
//
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   114
// optimise(load_bff("benchmark.bf"))          // should have inserted 0's
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   115
// optimise(load_bff("mandelbrot.bf")).length  // => 11205 // this is wrong, it's 11203!
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   116
// 
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   117
// time_needed(1, run3(load_bff("benchmark.bf")))
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   119
// (8)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   120
def combine_helper(c : String, s : String) : String =
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   121
  val temp1 = s.replaceAllLiterally(c*26, c ++ "Z").replaceAllLiterally(c*25, c ++ "Y").replaceAllLiterally(c*24, c ++ "X").replaceAllLiterally(c*23, c ++ "W")
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   122
  val temp2 = temp1.replaceAllLiterally(c*22, c ++ "V").replaceAllLiterally(c*21, c ++ "U").replaceAllLiterally(c*20, c ++ "T").replaceAllLiterally(c*19, c ++ "S")
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   123
  val temp3 = temp2.replaceAllLiterally(c*18, c ++ "R").replaceAllLiterally(c*17, c ++ "Q").replaceAllLiterally(c*16, c ++ "P").replaceAllLiterally(c*15, c ++ "O")
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   124
  val temp4 = temp3.replaceAllLiterally(c*14, c ++ "N").replaceAllLiterally(c*13, c ++ "M").replaceAllLiterally(c*12, c ++ "L").replaceAllLiterally(c*11, c ++ "K")
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   125
  val temp5 = temp4.replaceAllLiterally(c*10, c ++ "J").replaceAllLiterally(c*9, c ++ "I").replaceAllLiterally(c*8, c ++ "H").replaceAllLiterally(c*7, c ++ "G")
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   126
  val temp6 = temp5.replaceAllLiterally(c*6, c ++ "F").replaceAllLiterally(c*5, c ++ "E").replaceAllLiterally(c*4, c ++ "D").replaceAllLiterally(c*3, c ++ "C")
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   127
  val temp7 = temp6.replaceAllLiterally(c*2, c ++ "B").replaceAllLiterally(c ++ ">", c ++ "A>").replaceAllLiterally(c ++ "<", c ++ "A<")
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   128
  val temp8 = temp7.replaceAllLiterally(c ++ "+", c ++ "A+").replaceAllLiterally(c ++ "-", c ++ "A-").replaceAllLiterally(c ++ "[", c ++ "A[")
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   129
  temp8.replaceAllLiterally(c ++ "]", c ++ "A]").replaceAllLiterally(c ++ ".", c ++ "A.").replaceAllLiterally(c ++ "0", c ++ "A0")
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   130
def combine(s: String) : String = 
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   131
  val temp1 = combine_helper(">", s)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   132
  val temp2 = combine_helper("<", temp1)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   133
  val temp3 = combine_helper("+", temp2)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   134
  val temp4 = combine_helper("-", temp3)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   135
  temp4
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   136
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   137
// testcase
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   138
// combine(load_bff("benchmark.bf"))
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   139
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   140
def compute4(prog: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = if (pc < 0 || pc >= prog.length) mem else prog.charAt(pc) match {
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   141
  case '>' => val n = (prog.charAt(pc+1).toInt - 64)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   142
              compute4(prog, tb, pc+1, mp+n, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   143
  case '<' => val n = (prog.charAt(pc+1).toInt - 64)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   144
              compute4(prog, tb, pc+1, mp-n, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   145
  case '+' => val n = (prog.charAt(pc+1).toInt - 64)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   146
              compute4(prog, tb, pc+1, mp, mem + (mp -> (sread(mem, mp) + n)))
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   147
  case '-' => val n = (prog.charAt(pc+1).toInt - 64)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   148
              compute4(prog, tb, pc+1, mp, mem + (mp -> (sread(mem, mp) - n)))
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   149
  case '.' => print(sread(mem, mp).toChar); compute4(prog, tb, pc+1, mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   150
  case '[' => if (sread(mem, mp) == 0) compute4(prog, tb, tb.getOrElse(pc, -2), mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   151
              else compute4(prog, tb, pc+1, mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   152
  case ']' => if (sread(mem, mp) == 0) compute4(prog, tb, pc+1, mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   153
              else compute4(prog, tb, tb.getOrElse(pc, -2), mp, mem)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   154
  case '0' => compute4(prog, tb, pc+1, mp, mem + (mp -> 0))
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   155
  case _ => compute4(prog, tb, pc+1, mp, mem)
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 348
diff changeset
   156
}
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 348
diff changeset
   157
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   158
// should call first optimise and then combine on the input string
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   159
//
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   160
def run4(prog: String, m: Mem = Map()) = 
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   161
  val optimized = combine(optimise(prog))
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   162
  compute4(optimized, jtable(optimized), 0, 0, m)
404
bf20a9fa5c29 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   163
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   166
// testcases
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   167
// combine(optimise(load_bff("benchmark.bf"))) // => """>A+B[<A+M>A-A]<A[[....."""
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   168
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   169
// testcases (they should now run much faster)
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   170
// time_needed(1, run4(load_bff("benchmark.bf")))
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   171
// time_needed(1, run4(load_bff("sierpinski.bf"))) 
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 475
diff changeset
   172
// time_needed(1, run4(load_bff("mandelbrot.bf")))
235
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
c989f50da5de updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   175
}