progs/bfi.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 26 Sep 2019 10:59:52 +0100
changeset 632 b7b91158eded
child 636 96a91e4a8ac8
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
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import io.Source
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
import scala.util._
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
// loding a bf-file 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
def load_bff(name: String) : String = 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("")
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
type Mem = Map[Int, Int]
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
// reading and writing BF memory
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
def sread(mem: Mem, mp: Int) : Int = 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  mem.getOrElse(mp, 0)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
def write(mem: Mem, mp: Int, v: Int) : Mem =
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  mem.updated(mp, v)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// Right and Left Jumps in BF loops
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
def jumpRight(prog: String, pc: Int, level: Int) : Int = {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  if (prog.length <= pc) pc 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  else (prog(pc), level) match {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
    case (']', 0) => pc + 1
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
    case (']', l) => jumpRight(prog, pc + 1, l - 1)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
    case ('[', l) => jumpRight(prog, pc + 1, l + 1)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
    case (_, l) => jumpRight(prog, pc + 1, l)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  }
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
}
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
def jumpLeft(prog: String, pc: Int, level: Int) : Int = {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  if (pc < 0) pc 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  else (prog(pc), level) match {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
    case ('[', 0) => pc + 1
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
    case ('[', l) => jumpLeft(prog, pc - 1, l - 1)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
    case (']', l) => jumpLeft(prog, pc - 1, l + 1)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
    case (_, l) => jumpLeft(prog, pc - 1, l)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  }
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
}
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  if (0 <= pc && pc < prog.length) { 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
    val (new_pc, new_mp, new_mem) = prog(pc) match {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
      case '>' => (pc + 1, mp + 1, mem)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
      case '<' => (pc + 1, mp - 1, mem)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
      case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1))
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
      case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1))
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
      case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
      case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte))
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
      case '['  => if (sread(mem, mp) == 0) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
                      (jumpRight(prog, pc + 1, 0), mp, mem) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
                   else (pc + 1, mp, mem) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
      case ']'  => if (sread(mem, mp) != 0) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
                      (jumpLeft(prog, pc - 1, 0), mp, mem) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
                   else (pc + 1, mp, mem) 
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
      case _ => (pc + 1, mp, 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
    compute(prog, new_pc, new_mp, new_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
  else mem
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
}
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
def run(prog: String, m: Mem = Map()) = compute(prog, 0, 0, m)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
def time_needed[T](n: Int, code: => T) = {
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  val start = System.nanoTime()
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  for (i <- 0 until n) code
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  val end = System.nanoTime()
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  (end - start)/(n * 1.0e9)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
}
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
// a Mandelbrot set generator in brainf*** written by Erik Bosman
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
// (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
b7b91158eded updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
println(s"${time_needed(1, run(load_bff("mandelbrot.bf")))} secs")
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