progs/bf/bfi.sc
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 24 Jul 2020 12:58:19 +0100
changeset 738 084e2843f478
parent 737 14a348d050b3
child 742 b5b5583a3a08
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
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
import scala.util._
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 632
diff changeset
    12
// BF memory as a map
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
type Mem = Map[Int, Int]
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
// reading and writing BF memory
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
def sread(mem: Mem, mp: Int) : Int = 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  mem.getOrElse(mp, 0)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def write(mem: Mem, mp: Int, v: Int) : Mem =
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  mem.updated(mp, v)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
// Right and Left Jumps in BF loops
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
def jumpRight(prog: String, pc: Int, level: Int) : Int = {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  if (prog.length <= pc) pc 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  else (prog(pc), level) match {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
    case (']', 0) => pc + 1
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
    case (']', l) => jumpRight(prog, pc + 1, l - 1)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
    case ('[', l) => jumpRight(prog, pc + 1, l + 1)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
    case (_, l) => jumpRight(prog, pc + 1, l)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  }
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
}
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
def jumpLeft(prog: String, pc: Int, level: Int) : Int = {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  if (pc < 0) pc 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  else (prog(pc), level) match {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
    case ('[', 0) => pc + 1
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
    case ('[', l) => jumpLeft(prog, pc - 1, l - 1)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
    case (']', l) => jumpLeft(prog, pc - 1, l + 1)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
    case (_, l) => jumpLeft(prog, pc - 1, l)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  }
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
}
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 632
diff changeset
    43
// main interpreter loop
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  if (0 <= pc && pc < prog.length) { 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
    val (new_pc, new_mp, new_mem) = prog(pc) match {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
      case '>' => (pc + 1, mp + 1, mem)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
      case '<' => (pc + 1, mp - 1, mem)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
      case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1))
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
      case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1))
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
      case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
      case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte))
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
      case '['  => if (sread(mem, mp) == 0) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
                      (jumpRight(prog, pc + 1, 0), mp, mem) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
                   else (pc + 1, mp, mem) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
      case ']'  => if (sread(mem, mp) != 0) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
                      (jumpLeft(prog, pc - 1, 0), mp, mem) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
                   else (pc + 1, mp, mem) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
      case _ => (pc + 1, mp, 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
    compute(prog, new_pc, new_mp, new_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
  else mem
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
}
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
def run(prog: String, m: Mem = Map()) = compute(prog, 0, 0, m)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 632
diff changeset
    68
// helper code for timing information
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
def time_needed[T](n: Int, code: => T) = {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  val start = System.nanoTime()
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  for (i <- 0 until n) code
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  val end = System.nanoTime()
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  (end - start)/(n * 1.0e9)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
}
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
738
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 737
diff changeset
    76
// Running Testcases
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 737
diff changeset
    77
//===================
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
737
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    79
@doc(" the argument should be a BF program ")
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    80
@main
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    81
def main(fname: String) = {
14a348d050b3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 736
diff changeset
    82
  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
    83
  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
    84
}  
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85