progs/bf/bfi.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 06 Oct 2021 20:43:35 +0100
changeset 842 b53ac1bb5f43
parent 825 dca072e2bb7d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
737
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     1
// A simple Interpreter for BF*** Programs
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     2
//=========================================
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     3
//
738
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 737
diff changeset
     4
// Call with
737
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     5
//
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     6
//  amm bfi.sc <<bf_program.bf>>
738
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 737
diff changeset
     7
//
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
737
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
     9
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 738
diff changeset
    10
// BF memory is represented as a Map
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
type Mem = Map[Int, Int]
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 738
diff changeset
    13
// safe reading and writing of BF memory
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
def sread(mem: Mem, mp: Int) : Int = 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  mem.getOrElse(mp, 0)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
def write(mem: Mem, mp: Int, v: Int) : Mem =
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  mem.updated(mp, v)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
// Right and Left Jumps in BF loops
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
def jumpRight(prog: String, pc: Int, level: Int) : Int = {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  if (prog.length <= pc) pc 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  else (prog(pc), level) match {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
    case (']', 0) => pc + 1
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
    case (']', l) => jumpRight(prog, pc + 1, l - 1)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
    case ('[', l) => jumpRight(prog, pc + 1, l + 1)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
    case (_, l) => jumpRight(prog, pc + 1, l)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  }
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
}
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
def jumpLeft(prog: String, pc: Int, level: Int) : Int = {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  if (pc < 0) pc 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  else (prog(pc), level) match {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
    case ('[', 0) => pc + 1
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
    case ('[', l) => jumpLeft(prog, pc - 1, l - 1)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
    case (']', l) => jumpLeft(prog, pc - 1, l + 1)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
    case (_, l) => jumpLeft(prog, pc - 1, l)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  }
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
}
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 632
diff changeset
    41
// main interpreter loop
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  if (0 <= pc && pc < prog.length) { 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    val (new_pc, new_mp, new_mem) = prog(pc) match {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
      case '>' => (pc + 1, mp + 1, mem)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
      case '<' => (pc + 1, mp - 1, mem)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
      case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1))
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
      case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1))
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
      case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
743
6acabeecdf75 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    50
      case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte))   //scala.io.StdIn.readLine()
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
      case '['  => if (sread(mem, mp) == 0) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
                      (jumpRight(prog, pc + 1, 0), mp, mem) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
                   else (pc + 1, mp, mem) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
      case ']'  => if (sread(mem, mp) != 0) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
                      (jumpLeft(prog, pc - 1, 0), mp, mem) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
                   else (pc + 1, mp, mem) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
      case _ => (pc + 1, mp, mem)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
    }		     
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
    compute(prog, new_pc, new_mp, new_mem)	
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  }
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  else mem
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
}
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
def run(prog: String, m: Mem = Map()) = compute(prog, 0, 0, m)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 632
diff changeset
    66
// helper code for timing information
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
def time_needed[T](n: Int, code: => T) = {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  val start = System.nanoTime()
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  for (i <- 0 until n) code
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  val end = System.nanoTime()
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  (end - start)/(n * 1.0e9)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
}
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
738
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 737
diff changeset
    74
// Running Testcases
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 737
diff changeset
    75
//===================
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 743
diff changeset
    77
//@doc(" the argument should be a BF program ")
737
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    78
@main
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    79
def main(fname: String) = {
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    80
  val bf_str = os.read(os.pwd / fname)
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    81
  println(s"${time_needed(1, run(bf_str))} secs")
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    82
}  
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83