diff -r 7c2c0f10c424 -r 96a91e4a8ac8 progs/bfi.scala --- a/progs/bfi.scala Thu Sep 26 12:59:33 2019 +0100 +++ b/progs/bfi.scala Thu Sep 26 14:12:11 2019 +0100 @@ -1,16 +1,14 @@ // An Interpreter for BF*** Programs //=================================== - import io.Source import scala.util._ - // loding a bf-file def load_bff(name: String) : String = Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("") - +// BF memory as a map type Mem = Map[Int, Int] // reading and writing BF memory @@ -42,7 +40,7 @@ } } - +// main interpreter loop def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = { if (0 <= pc && pc < prog.length) { val (new_pc, new_mp, new_mem) = prog(pc) match { @@ -67,7 +65,7 @@ def run(prog: String, m: Mem = Map()) = compute(prog, 0, 0, m) - +// helper code for timing information def time_needed[T](n: Int, code: => T) = { val start = System.nanoTime() for (i <- 0 until n) code @@ -75,12 +73,14 @@ (end - start)/(n * 1.0e9) } - +// Testcases +//=========== // a Mandelbrot set generator in brainf*** written by Erik Bosman // (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/) +val b0 = load_bff("mandelbrot.bf") -println(s"${time_needed(1, run(load_bff("mandelbrot.bf")))} secs") +println(s"${time_needed(1, run(b0))} secs") // a benchmark program (counts down from 'Z' to 'A')