progs/bfi.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 26 Sep 2019 14:12:11 +0100
changeset 636 96a91e4a8ac8
parent 632 b7b91158eded
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// An Interpreter for BF*** Programs
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//===================================
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
import io.Source
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import scala.util._
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
// loding a bf-file 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
def load_bff(name: String) : String = 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("")
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 632
diff changeset
    11
// BF memory as a map
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
type Mem = Map[Int, Int]
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
// reading and writing BF memory
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
def sread(mem: Mem, mp: Int) : Int = 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  mem.getOrElse(mp, 0)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
def write(mem: Mem, mp: Int, v: Int) : Mem =
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  mem.updated(mp, v)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
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
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 632
diff changeset
    76
// Testcases
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 632
diff changeset
    77
//===========
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
// a Mandelbrot set generator in brainf*** written by Erik Bosman
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
// (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 632
diff changeset
    81
val b0 = load_bff("mandelbrot.bf")
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
636
96a91e4a8ac8 updated
Christian Urban <urbanc@in.tum.de>
parents: 632
diff changeset
    83
println(s"${time_needed(1, run(b0))} secs")
632
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
// a benchmark program (counts down from 'Z' to 'A')
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
val b1 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
            [>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
            ++++++++[>++++++++++[>++++++++++[>++++++++++[>+
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
            +++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++."""
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
println(s"${time_needed(1, run(b1))} secs")
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93