--- a/progs/compile_arr.scala Wed Nov 20 17:10:03 2019 +0000
+++ b/progs/compile_arr.scala Thu Nov 21 00:49:21 2019 +0000
@@ -445,6 +445,13 @@
prog.toList.map(instr).mkString
+// the mandelbrot.bf program is so large that
+// it does not fit into the limitations of what the
+// JVM imposes on methods.
+//
+// below it just about fits by optimising [-] loops
+// and combining instructions
+
def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match {
case (Nil, acc) => acc
case (c :: cs, Nil) => splice(cs, List((c, 1)))
@@ -456,26 +463,24 @@
def spl(s: String) = splice(s.toList, Nil).reverse
def instr2(c: Char, n: Int) : String = c match {
- case '>' => "ptr := ptr + " + n.toString + ";"
- case '<' => "ptr := ptr - " + n.toString + ";"
- case '+' => "field[ptr] := field[ptr] + " + n.toString + ";"
- case '-' => "field[ptr] := field[ptr] - " + n.toString + ";"
- case '.' => "x := field[ptr]; write x;" //* n
- //case ',' => "*ptr = getchar();\n" * n
+ case '>' => s"ptr := ptr + $n;"
+ case '<' => s"ptr := ptr - $n;"
+ case '0' => s"field[ptr] := 0;"
+ case '+' => s"field[ptr] := field[ptr] + $n;"
+ case '-' => s"field[ptr] := field[ptr] - $n;"
+ case '.' => s"x := field[ptr]; write x;"
case '[' => "while (field[ptr] != 0) do {" * n
case ']' => "skip};" * n
case _ => ""
}
def instrs2(prog: String) : String =
- spl(prog).map{ case (c, n) => instr2(c, n) }.mkString
-
+ spl(prog.replaceAll("""\[-\]""", "0")).map{ case (c, n) => instr2(c, n) }.mkString
def bf_str(prog: String) : String = {
"\n" ++
- //"new field[30000];\n" ++
"ptr := 15000;" ++
- instrs(prog) ++
+ instrs2(prog) ++
"skip"
}
@@ -523,7 +528,6 @@
bf_run(bf2, "fibs")
-/*
bf_run(""" A mandelbrot set fractal viewer in brainf*** written by Erik Bosman
@@ -672,4 +676,4 @@
+[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<
<<<<<]]>>>]""", "mand")
-*/
+