| author | Christian Urban <urbanc@in.tum.de> | 
| Sun, 26 Jan 2020 14:16:30 +0000 | |
| changeset 707 | 566f4b2750e8 | 
| parent 696 | 2726f2afad67 | 
| child 708 | 1b5dc2468ce3 | 
| permissions | -rw-r--r-- | 
| 609 | 1  | 
// A Small Compiler for the WHILE Language  | 
| 707 | 2  | 
//  | 
| 694 | 3  | 
// - includes arrays and a small parser for  | 
| 707 | 4  | 
// WHILE programs  | 
5  | 
//  | 
|
6  | 
// - transpiles BF programs into WHILE programs  | 
|
7  | 
// and compiles and runs them  | 
|
8  | 
//  | 
|
9  | 
// Call with  | 
|
10  | 
//  | 
|
11  | 
// scala compiler_arr.scala  | 
|
12  | 
||
13  | 
||
| 609 | 14  | 
|
15  | 
// the abstract syntax trees  | 
|
16  | 
abstract class Stmt  | 
|
17  | 
abstract class AExp  | 
|
18  | 
abstract class BExp  | 
|
19  | 
type Block = List[Stmt]  | 
|
20  | 
||
21  | 
// statements  | 
|
22  | 
case object Skip extends Stmt  | 
|
| 707 | 23  | 
case class ArrayDef(s: String, n: Int) extends Stmt  | 
| 609 | 24  | 
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt  | 
25  | 
case class While(b: BExp, bl: Block) extends Stmt  | 
|
| 707 | 26  | 
case class Assign(s: String, a: AExp) extends Stmt // var := exp  | 
27  | 
case class AssignA(s: String, a1: AExp, a2: AExp) extends Stmt // arr[exp1] := exp2  | 
|
| 694 | 28  | 
case class Write(s: String) extends Stmt  | 
29  | 
case class Read(s: String) extends Stmt  | 
|
| 609 | 30  | 
|
31  | 
// arithmetic expressions  | 
|
32  | 
case class Var(s: String) extends AExp  | 
|
33  | 
case class Num(i: Int) extends AExp  | 
|
34  | 
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp  | 
|
| 694 | 35  | 
case class Ref(s: String, a: AExp) extends AExp  | 
| 609 | 36  | 
|
37  | 
// boolean expressions  | 
|
38  | 
case object True extends BExp  | 
|
39  | 
case object False extends BExp  | 
|
40  | 
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp  | 
|
41  | 
||
42  | 
||
43  | 
// compiler headers needed for the JVM  | 
|
44  | 
// (contains an init method, as well as methods for read and write)  | 
|
45  | 
val beginning = """  | 
|
46  | 
.class public XXX.XXX  | 
|
47  | 
.super java/lang/Object  | 
|
48  | 
||
49  | 
.method public static write(I)V  | 
|
50  | 
.limit locals 1  | 
|
51  | 
.limit stack 2  | 
|
52  | 
getstatic java/lang/System/out Ljava/io/PrintStream;  | 
|
53  | 
iload 0  | 
|
| 694 | 54  | 
i2c ; Int => Char  | 
55  | 
invokevirtual java/io/PrintStream/print(C)V ; println(I)V => print(C)V  | 
|
| 609 | 56  | 
return  | 
57  | 
.end method  | 
|
58  | 
||
59  | 
.method public static main([Ljava/lang/String;)V  | 
|
60  | 
.limit locals 200  | 
|
61  | 
.limit stack 200  | 
|
62  | 
||
63  | 
"""  | 
|
64  | 
||
65  | 
val ending = """  | 
|
66  | 
||
67  | 
return  | 
|
68  | 
||
69  | 
.end method  | 
|
70  | 
"""  | 
|
71  | 
||
| 694 | 72  | 
|
| 609 | 73  | 
|
74  | 
||
75  | 
// for generating new labels  | 
|
76  | 
var counter = -1  | 
|
77  | 
||
78  | 
def Fresh(x: String) = {
 | 
|
79  | 
counter += 1  | 
|
80  | 
x ++ "_" ++ counter.toString()  | 
|
81  | 
}  | 
|
82  | 
||
83  | 
// environments and instructions  | 
|
| 694 | 84  | 
type Env = Map[String, Int]  | 
85  | 
||
86  | 
// convenient string interpolations  | 
|
87  | 
// for instructions and labels  | 
|
88  | 
import scala.language.implicitConversions  | 
|
89  | 
import scala.language.reflectiveCalls  | 
|
90  | 
||
91  | 
implicit def sring_inters(sc: StringContext) = new {
 | 
|
92  | 
def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n"  | 
|
93  | 
def l(args: Any*): String = sc.s(args:_*) ++ ":\n"  | 
|
94  | 
}  | 
|
95  | 
||
96  | 
def compile_op(op: String) = op match {
 | 
|
97  | 
case "+" => i"iadd"  | 
|
98  | 
case "-" => i"isub"  | 
|
99  | 
case "*" => i"imul"  | 
|
100  | 
}  | 
|
| 609 | 101  | 
|
102  | 
// arithmetic expression compilation  | 
|
| 694 | 103  | 
def compile_aexp(a: AExp, env : Env) : String = a match {
 | 
104  | 
case Num(i) => i"ldc $i"  | 
|
| 707 | 105  | 
  case Var(s) => i"iload ${env(s)}"
 | 
| 694 | 106  | 
case Aop(op, a1, a2) =>  | 
107  | 
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op)  | 
|
108  | 
case Ref(s, a) =>  | 
|
109  | 
    i"aload ${env(s)}" ++ compile_aexp(a, env) ++  i"iaload"
 | 
|
| 609 | 110  | 
}  | 
111  | 
||
112  | 
// boolean expression compilation  | 
|
| 694 | 113  | 
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
 | 
| 707 | 114  | 
case True => ""  | 
| 694 | 115  | 
case False => i"goto $jmp"  | 
116  | 
  case Bop("==", a1, a2) => 
 | 
|
117  | 
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpne $jmp"  | 
|
| 609 | 118  | 
  case Bop("!=", a1, a2) => 
 | 
| 694 | 119  | 
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpeq $jmp"  | 
| 609 | 120  | 
  case Bop("<", a1, a2) => 
 | 
| 694 | 121  | 
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpge $jmp"  | 
| 609 | 122  | 
}  | 
123  | 
||
124  | 
// statement compilation  | 
|
| 694 | 125  | 
def compile_stmt(s: Stmt, env: Env) : (String, Env) = s match {
 | 
126  | 
  case Skip => ("", env)
 | 
|
| 609 | 127  | 
  case Assign(x, a) => {
 | 
| 694 | 128  | 
val index = env.getOrElse(x, env.keys.size)  | 
129  | 
(compile_aexp(a, env) ++ i"istore $index \t\t; $x", env + (x -> index))  | 
|
| 609 | 130  | 
}  | 
131  | 
  case If(b, bl1, bl2) => {
 | 
|
132  | 
    val if_else = Fresh("If_else")
 | 
|
133  | 
    val if_end = Fresh("If_end")
 | 
|
134  | 
val (instrs1, env1) = compile_block(bl1, env)  | 
|
135  | 
val (instrs2, env2) = compile_block(bl2, env1)  | 
|
136  | 
(compile_bexp(b, env, if_else) ++  | 
|
137  | 
instrs1 ++  | 
|
| 694 | 138  | 
i"goto $if_end" ++  | 
139  | 
l"$if_else" ++  | 
|
| 609 | 140  | 
instrs2 ++  | 
| 694 | 141  | 
l"$if_end", env2)  | 
| 609 | 142  | 
}  | 
143  | 
  case While(b, bl) => {
 | 
|
144  | 
    val loop_begin = Fresh("Loop_begin")
 | 
|
145  | 
    val loop_end = Fresh("Loop_end")
 | 
|
146  | 
val (instrs1, env1) = compile_block(bl, env)  | 
|
| 694 | 147  | 
(l"$loop_begin" ++  | 
| 609 | 148  | 
compile_bexp(b, env, loop_end) ++  | 
149  | 
instrs1 ++  | 
|
| 694 | 150  | 
i"goto $loop_begin" ++  | 
151  | 
l"$loop_end", env1)  | 
|
| 609 | 152  | 
}  | 
153  | 
case Write(x) =>  | 
|
| 694 | 154  | 
    (i"iload ${env(x)} \t\t; $x" ++ 
 | 
155  | 
i"invokestatic XXX/XXX/write(I)V", env)  | 
|
156  | 
  case Read(x) => {
 | 
|
157  | 
val index = env.getOrElse(x, env.keys.size)  | 
|
158  | 
(i"invokestatic XXX/XXX/read()I" ++  | 
|
159  | 
i"istore $index \t\t; $x", env + (x -> index))  | 
|
160  | 
}  | 
|
| 707 | 161  | 
  case ArrayDef(s: String, n: Int) => {
 | 
| 694 | 162  | 
    val index = if (env.isDefinedAt(s)) throw new Exception("array def error") else 
 | 
163  | 
env.keys.size  | 
|
164  | 
(i"ldc $n" ++  | 
|
165  | 
i"newarray int" ++  | 
|
166  | 
i"astore $index", env + (s -> index))  | 
|
167  | 
}  | 
|
| 612 | 168  | 
  case AssignA(s, a1, a2) => {
 | 
169  | 
val index = if (env.isDefinedAt(s)) env(s) else  | 
|
| 694 | 170  | 
                    throw new Exception("array not defined")
 | 
171  | 
    (i"aload ${env(s)}" ++
 | 
|
| 612 | 172  | 
compile_aexp(a1, env) ++  | 
173  | 
compile_aexp(a2, env) ++  | 
|
| 694 | 174  | 
i"iastore", env)  | 
| 612 | 175  | 
}  | 
| 609 | 176  | 
}  | 
177  | 
||
| 707 | 178  | 
// compilation of a block (i.e. list of statements)  | 
| 694 | 179  | 
def compile_block(bl: Block, env: Env) : (String, Env) = bl match {
 | 
180  | 
  case Nil => ("", env)
 | 
|
| 609 | 181  | 
  case s::bl => {
 | 
182  | 
val (instrs1, env1) = compile_stmt(s, env)  | 
|
183  | 
val (instrs2, env2) = compile_block(bl, env1)  | 
|
184  | 
(instrs1 ++ instrs2, env2)  | 
|
185  | 
}  | 
|
186  | 
}  | 
|
187  | 
||
| 694 | 188  | 
|
| 609 | 189  | 
// main compilation function for blocks  | 
190  | 
def compile(bl: Block, class_name: String) : String = {
 | 
|
| 707 | 191  | 
val instructions = compile_block(bl, Map())._1  | 
192  | 
  (beginning ++ instructions ++ ending).replaceAllLiterally("XXX", class_name)
 | 
|
| 609 | 193  | 
}  | 
194  | 
||
195  | 
||
| 694 | 196  | 
// Fibonacci numbers as a test-case  | 
197  | 
val fib_test =  | 
|
198  | 
  List(Read("n"),                       //  read n;                     
 | 
|
199  | 
       Assign("minus1",Num(0)),         //  minus1 := 0;
 | 
|
200  | 
       Assign("minus2",Num(1)),         //  minus2 := 1;
 | 
|
201  | 
       Assign("temp",Num(0)),           //  temp := 0;
 | 
|
202  | 
       While(Bop("<",Num(0),Var("n")),  //  while n > 0 do  {
 | 
|
203  | 
          List(Assign("temp",Var("minus2")),    //  temp := minus2;
 | 
|
204  | 
               Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), 
 | 
|
205  | 
// minus2 := minus1 + minus2;  | 
|
206  | 
               Assign("minus1",Var("temp")), //  minus1 := temp;
 | 
|
207  | 
               Assign("n",Aop("-",Var("n"),Num(1))))), //  n := n - 1 };
 | 
|
208  | 
       Write("minus1"))                 //  write minus1
 | 
|
209  | 
||
| 707 | 210  | 
// prints out the JVM-assembly instructions for fib  | 
211  | 
//  | 
|
| 694 | 212  | 
// println(compile(fib_test, "fib"))  | 
| 707 | 213  | 
//  | 
214  | 
// can be assembled by hand with  | 
|
| 609 | 215  | 
//  | 
216  | 
// java -jar jvm/jasmin-2.4/jasmin.jar fib.j  | 
|
217  | 
//  | 
|
218  | 
// and started with  | 
|
219  | 
//  | 
|
220  | 
// java fib/fib  | 
|
221  | 
||
222  | 
import scala.util._  | 
|
223  | 
import scala.sys.process._  | 
|
224  | 
||
225  | 
def time_needed[T](i: Int, code: => T) = {
 | 
|
226  | 
val start = System.nanoTime()  | 
|
| 707 | 227  | 
for (j <- 2 to i) code  | 
228  | 
val result = code  | 
|
| 609 | 229  | 
val end = System.nanoTime()  | 
| 707 | 230  | 
((end - start) / (i * 1.0e9), result)  | 
231  | 
}  | 
|
232  | 
||
233  | 
def compile_to_file(bl: Block, class_name: String) : Unit =  | 
|
234  | 
  Using(new java.io.FileWriter(class_name + ".j")) {
 | 
|
235  | 
_.write(compile(bl, class_name))  | 
|
236  | 
}  | 
|
237  | 
||
238  | 
def compile_and_run(bl: Block, class_name: String) : Unit = {
 | 
|
239  | 
println(s"Start compilation of $class_name")  | 
|
240  | 
compile_to_file(bl, class_name)  | 
|
241  | 
  println("generated .j file")
 | 
|
242  | 
  (s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!!
 | 
|
243  | 
  println("generated .class file ")
 | 
|
244  | 
  println("Time: " + time_needed(1, (s"java ${class_name}/${class_name}").!)._1)
 | 
|
| 609 | 245  | 
}  | 
246  | 
||
247  | 
||
| 694 | 248  | 
val arr_test =  | 
| 707 | 249  | 
  List(ArrayDef("a", 10),
 | 
250  | 
       ArrayDef("b", 2),
 | 
|
| 694 | 251  | 
       AssignA("a", Num(0), Num(10)),
 | 
252  | 
       Assign("x", Ref("a", Num(0))),
 | 
|
253  | 
       Write("x"),
 | 
|
254  | 
       AssignA("b", Num(1), Num(5)),
 | 
|
255  | 
       Assign("x", Ref("b", Num(1))),
 | 
|
256  | 
       Write("x"))       
 | 
|
| 609 | 257  | 
|
258  | 
||
| 707 | 259  | 
//compile_and_run(arr_test, "a")  | 
| 616 | 260  | 
|
261  | 
//====================  | 
|
262  | 
// Parser Combinators  | 
|
263  | 
//====================  | 
|
264  | 
||
265  | 
import scala.language.implicitConversions  | 
|
266  | 
import scala.language.reflectiveCalls  | 
|
267  | 
||
| 707 | 268  | 
// more convenience for the semantic actions later on  | 
269  | 
case class ~[+A, +B](_1: A, _2: B)  | 
|
270  | 
||
| 694 | 271  | 
type IsSeq[A] = A => Seq[_]  | 
| 616 | 272  | 
|
| 694 | 273  | 
abstract class Parser[I : IsSeq, T] {
 | 
| 616 | 274  | 
def parse(ts: I): Set[(T, I)]  | 
275  | 
||
276  | 
def parse_all(ts: I) : Set[T] =  | 
|
277  | 
for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head  | 
|
278  | 
}  | 
|
279  | 
||
| 707 | 280  | 
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
 | 
| 616 | 281  | 
def parse(sb: I) =  | 
282  | 
for ((head1, tail1) <- p.parse(sb);  | 
|
| 707 | 283  | 
(head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)  | 
| 616 | 284  | 
}  | 
285  | 
||
| 694 | 286  | 
class AltParser[I : IsSeq, T](p: => Parser[I, T], q: => Parser[I, T]) extends Parser[I, T] {
 | 
| 616 | 287  | 
def parse(sb: I) = p.parse(sb) ++ q.parse(sb)  | 
288  | 
}  | 
|
289  | 
||
| 694 | 290  | 
class FunParser[I : IsSeq, T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] {
 | 
| 616 | 291  | 
def parse(sb: I) =  | 
292  | 
for ((head, tail) <- p.parse(sb)) yield (f(head), tail)  | 
|
293  | 
}  | 
|
294  | 
||
295  | 
||
296  | 
import scala.util.matching.Regex  | 
|
297  | 
case class RegexParser(reg: Regex) extends Parser[String, String] {
 | 
|
298  | 
  def parse(sb: String) = reg.findPrefixMatchOf(sb) match {
 | 
|
299  | 
case None => Set()  | 
|
300  | 
case Some(m) => Set((m.matched, m.after.toString))  | 
|
301  | 
}  | 
|
302  | 
}  | 
|
303  | 
||
304  | 
def StringParser(s: String) = RegexParser(Regex.quote(s).r)  | 
|
305  | 
||
306  | 
||
307  | 
implicit def string2parser(s : String) = StringParser(s)  | 
|
308  | 
||
| 694 | 309  | 
implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
 | 
| 616 | 310  | 
def | (q : => Parser[I, T]) = new AltParser[I, T](p, q)  | 
311  | 
def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)  | 
|
312  | 
def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)  | 
|
313  | 
}  | 
|
314  | 
||
315  | 
implicit def StringOps(s: String) = new {
 | 
|
316  | 
def | (q : => Parser[String, String]) = new AltParser[String, String](s, q)  | 
|
317  | 
def | (r: String) = new AltParser[String, String](s, r)  | 
|
318  | 
def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)  | 
|
319  | 
def ~[S](q : => Parser[String, S]) =  | 
|
320  | 
new SeqParser[String, String, S](s, q)  | 
|
321  | 
def ~ (r: String) =  | 
|
322  | 
new SeqParser[String, String, String](s, r)  | 
|
323  | 
}  | 
|
324  | 
||
325  | 
||
326  | 
val NumParser = RegexParser("[0-9]+".r) ==> (s => s.toInt : Int)
 | 
|
327  | 
val IdParser = RegexParser("[a-z][a-z,0-9]*".r)
 | 
|
328  | 
||
329  | 
||
330  | 
||
| 707 | 331  | 
// Grammar Rules for WHILE with arrays  | 
| 616 | 332  | 
|
333  | 
lazy val AExp: Parser[String, AExp] =  | 
|
| 707 | 334  | 
  (Te ~ "+" ~ AExp) ==> { case x ~ _ ~ z => Aop("+", x, z):AExp } |
 | 
335  | 
  (Te ~ "-" ~ AExp) ==> { case x ~ _ ~ z => Aop("-", x, z):AExp } | Te
 | 
|
| 616 | 336  | 
lazy val Te: Parser[String, AExp] =  | 
| 707 | 337  | 
  (Fa ~ "*" ~ Te) ==> { case x ~ _ ~ z => Aop("*", x, z):AExp } | Fa
 | 
| 616 | 338  | 
lazy val Fa: Parser[String, AExp] =  | 
| 707 | 339  | 
   ("(" ~ AExp ~ ")") ==> { case _ ~ y ~ _ => y } | 
 | 
340  | 
   (IdParser ~ "[" ~ AExp ~ "]") ==> { case x ~ _ ~ y ~ _ => Ref(x, y) } |
 | 
|
| 616 | 341  | 
IdParser ==> Var |  | 
342  | 
NumParser ==> Num  | 
|
343  | 
||
344  | 
// boolean expressions  | 
|
345  | 
lazy val BExp: Parser[String, BExp] =  | 
|
| 707 | 346  | 
   (AExp ~ "=" ~ AExp) ==> { case x ~ y ~ z => Bop("=", x, z):BExp } | 
 | 
347  | 
   (AExp ~ "!=" ~ AExp) ==> { case x ~ y ~ z => Bop("!=", x, z):BExp } | 
 | 
|
348  | 
   (AExp ~ "<" ~ AExp) ==> { case x ~ y ~ z => Bop("<", x, z):BExp } | 
 | 
|
349  | 
   (AExp ~ ">" ~ AExp) ==> { case x ~ y ~ z => Bop("<", z, x):BExp } | 
 | 
|
| 616 | 350  | 
   ("true" ==> ((_) => True:BExp )) | 
 | 
351  | 
   ("false" ==> ((_) => False:BExp )) |
 | 
|
| 707 | 352  | 
   ("(" ~ BExp ~ ")") ==> { case x ~ y ~ z => y}
 | 
| 616 | 353  | 
|
354  | 
lazy val Stmt: Parser[String, Stmt] =  | 
|
355  | 
   ("skip" ==> (_ => Skip: Stmt)) |
 | 
|
| 707 | 356  | 
   (IdParser ~ ":=" ~ AExp) ==> { case x ~ y ~z => Assign(x, z): Stmt } |
 | 
| 616 | 357  | 
   (IdParser ~ "[" ~ AExp ~ "]" ~ ":=" ~ AExp) ==> { 
 | 
| 707 | 358  | 
case x ~ _ ~ z ~ _ ~ _ ~ u => AssignA(x, z, u): Stmt } |  | 
| 616 | 359  | 
   ("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block) ==>
 | 
| 707 | 360  | 
    { case _ ~ y ~ _ ~ u ~ _ ~w => If(y, u, w): Stmt } |
 | 
361  | 
   ("while" ~ BExp ~ "do" ~ Block) ==> { case _ ~ y ~ _ ~ w => While(y, w) } |
 | 
|
362  | 
   ("new" ~ IdParser ~ "[" ~ NumParser ~ "]") ==> { 
 | 
|
363  | 
case _ ~ y ~ _ ~ u ~ _ => ArrayDef(y, u) } |  | 
|
364  | 
   ("write" ~ IdParser) ==> { case _ ~ y => Write(y) } 
 | 
|
| 616 | 365  | 
|
366  | 
lazy val Stmts: Parser[String, Block] =  | 
|
| 707 | 367  | 
  (Stmt ~ ";" ~ Stmts) ==> { case x ~ _ ~ z => x :: z : Block } |
 | 
| 616 | 368  | 
(Stmt ==> ((s) => List(s) : Block))  | 
| 611 | 369  | 
|
| 616 | 370  | 
|
371  | 
lazy val Block: Parser[String, Block] =  | 
|
| 707 | 372  | 
   ("{" ~ Stmts ~ "}") ==> { case _ ~ y ~ _ => y} | 
 | 
| 616 | 373  | 
(Stmt ==> (s => List(s)))  | 
374  | 
||
| 694 | 375  | 
//Stmts.parse_all("x2:=5+a")
 | 
376  | 
//Stmts.parse_all("x2:=5+a[3+a]")
 | 
|
377  | 
//Stmts.parse_all("a[x2+3]:=5+a[3+a]")
 | 
|
378  | 
//Block.parse_all("{x:=5;y:=8}")
 | 
|
379  | 
//Block.parse_all("if(false)then{x:=5}else{x:=10}")
 | 
|
| 616 | 380  | 
|
381  | 
||
382  | 
||
383  | 
val fib = """  | 
|
384  | 
n := 10;  | 
|
385  | 
minus1 := 0;  | 
|
386  | 
minus2 := 1;  | 
|
387  | 
temp:=0;  | 
|
388  | 
   while (n > 0) do {
 | 
|
389  | 
temp := minus2;  | 
|
390  | 
minus2 := minus1 + minus2;  | 
|
391  | 
minus1 := temp;  | 
|
392  | 
n := n - 1};  | 
|
393  | 
result := minus2;  | 
|
394  | 
write result  | 
|
395  | 
   """.replaceAll("\\s+", "")
 | 
|
396  | 
||
397  | 
val fib_prog = Stmts.parse_all(fib).toList  | 
|
| 707 | 398  | 
//compile_and_run(fib_prog.head, "fib")  | 
| 616 | 399  | 
|
| 707 | 400  | 
//======================================  | 
401  | 
// BF transpiler into WHILE with arrays  | 
|
402  | 
//======================================  | 
|
| 616 | 403  | 
|
| 707 | 404  | 
// simple BF instructions translation  | 
| 616 | 405  | 
def instr(c: Char) : String = c match {
 | 
406  | 
case '>' => "ptr := ptr + 1;"  | 
|
407  | 
case '<' => "ptr := ptr - 1;"  | 
|
408  | 
case '+' => "field[ptr] := field[ptr] + 1;"  | 
|
409  | 
case '-' => "field[ptr] := field[ptr] - 1;"  | 
|
410  | 
case '.' => "x := field[ptr]; write x;"  | 
|
411  | 
//case ',' => "XXX" // "ptr = getchar();\n"  | 
|
412  | 
  case '['  => "while (field[ptr] != 0) do {"
 | 
|
413  | 
case ']' => "skip};"  | 
|
414  | 
case _ => ""  | 
|
415  | 
}  | 
|
416  | 
||
417  | 
def instrs(prog: String) : String =  | 
|
418  | 
prog.toList.map(instr).mkString  | 
|
419  | 
||
420  | 
||
| 707 | 421  | 
// Note: the mandelbrot.bf program is so large that  | 
422  | 
// it does not fit inside the limitations of what the  | 
|
423  | 
// JVM imposes on methods (only 64K of instructions).  | 
|
424  | 
// Therefore some optimisations are first applied to  | 
|
425  | 
// BF programs before WHILE programs are created. The  | 
|
426  | 
// optimisations are  | 
|
427  | 
//  | 
|
428  | 
// - replacing BF-loops of the form [-]  | 
|
429  | 
// - combining single increment/decrement instructions  | 
|
| 
696
 
2726f2afad67
added krakatau version and made the mandel program work
 
Christian Urban <urbanc@in.tum.de> 
parents: 
695 
diff
changeset
 | 
430  | 
//  | 
| 707 | 431  | 
// The size of the resulting .j-file is 270K.  | 
| 
696
 
2726f2afad67
added krakatau version and made the mandel program work
 
Christian Urban <urbanc@in.tum.de> 
parents: 
695 
diff
changeset
 | 
432  | 
|
| 616 | 433  | 
def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match {
 | 
434  | 
case (Nil, acc) => acc  | 
|
435  | 
case (c :: cs, Nil) => splice(cs, List((c, 1)))  | 
|
436  | 
case (c :: cs, (d, n) :: acc) =>  | 
|
437  | 
if (c == d) splice(cs, (c, n + 1) :: acc)  | 
|
438  | 
else splice(cs, (c, 1) :: (d, n) :: acc)  | 
|
439  | 
}  | 
|
440  | 
||
441  | 
def spl(s: String) = splice(s.toList, Nil).reverse  | 
|
442  | 
||
443  | 
def instr2(c: Char, n: Int) : String = c match {
 | 
|
| 
696
 
2726f2afad67
added krakatau version and made the mandel program work
 
Christian Urban <urbanc@in.tum.de> 
parents: 
695 
diff
changeset
 | 
444  | 
case '>' => s"ptr := ptr + $n;"  | 
| 
 
2726f2afad67
added krakatau version and made the mandel program work
 
Christian Urban <urbanc@in.tum.de> 
parents: 
695 
diff
changeset
 | 
445  | 
case '<' => s"ptr := ptr - $n;"  | 
| 
 
2726f2afad67
added krakatau version and made the mandel program work
 
Christian Urban <urbanc@in.tum.de> 
parents: 
695 
diff
changeset
 | 
446  | 
case '0' => s"field[ptr] := 0;"  | 
| 
 
2726f2afad67
added krakatau version and made the mandel program work
 
Christian Urban <urbanc@in.tum.de> 
parents: 
695 
diff
changeset
 | 
447  | 
case '+' => s"field[ptr] := field[ptr] + $n;"  | 
| 
 
2726f2afad67
added krakatau version and made the mandel program work
 
Christian Urban <urbanc@in.tum.de> 
parents: 
695 
diff
changeset
 | 
448  | 
case '-' => s"field[ptr] := field[ptr] - $n;"  | 
| 
 
2726f2afad67
added krakatau version and made the mandel program work
 
Christian Urban <urbanc@in.tum.de> 
parents: 
695 
diff
changeset
 | 
449  | 
case '.' => s"x := field[ptr]; write x;"  | 
| 616 | 450  | 
  case '['  => "while (field[ptr] != 0) do {" * n 
 | 
451  | 
case ']' => "skip};" * n  | 
|
452  | 
case _ => ""  | 
|
453  | 
}  | 
|
454  | 
||
455  | 
def instrs2(prog: String) : String =  | 
|
| 
696
 
2726f2afad67
added krakatau version and made the mandel program work
 
Christian Urban <urbanc@in.tum.de> 
parents: 
695 
diff
changeset
 | 
456  | 
  spl(prog.replaceAll("""\[-\]""", "0")).map{ case (c, n) => instr2(c, n) }.mkString
 | 
| 616 | 457  | 
|
458  | 
def bf_str(prog: String) : String = {
 | 
|
459  | 
"\n" ++  | 
|
460  | 
"ptr := 15000;" ++  | 
|
| 
696
 
2726f2afad67
added krakatau version and made the mandel program work
 
Christian Urban <urbanc@in.tum.de> 
parents: 
695 
diff
changeset
 | 
461  | 
instrs2(prog) ++  | 
| 616 | 462  | 
"skip"  | 
463  | 
}  | 
|
464  | 
||
465  | 
def bf_run(prog: String, name: String) = {
 | 
|
| 707 | 466  | 
println(s"BF pre-processing of $name")  | 
| 616 | 467  | 
  val bf_string = bf_str(prog).replaceAll("\\s", "")
 | 
| 707 | 468  | 
  println(s"BF parsing (program length ${bf_string.length} characters)")
 | 
469  | 
val (time, bf_prog) = time_needed(1, Stmts.parse_all(bf_string).toList.head)  | 
|
470  | 
println(s"BF generated WHILE program (needed $time for parsing)")  | 
|
471  | 
  compile_and_run(ArrayDef("field", 30000) :: bf_prog, name)
 | 
|
| 616 | 472  | 
}  | 
473  | 
||
| 694 | 474  | 
// a benchmark program (counts down from 'Z' to 'A')  | 
475  | 
val bf0 = """>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++  | 
|
476  | 
[>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++  | 
|
477  | 
++++++++[>++++++++++[>++++++++++[>++++++++++[>+  | 
|
478  | 
+++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++."""  | 
|
479  | 
||
480  | 
bf_run(bf0, "bench")  | 
|
481  | 
||
| 707 | 482  | 
// Sierpinski triangle  | 
| 616 | 483  | 
val bf1 = """++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[  | 
484  | 
->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<  | 
|
485  | 
]>.>+[>>]>+]"""  | 
|
486  | 
||
487  | 
bf_run(bf1, "sier")  | 
|
488  | 
||
| 707 | 489  | 
// Helo World!  | 
| 616 | 490  | 
bf_run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
 | 
491  | 
..+++.>>.<-.<.+++.------.--------.>>+.>++.""", "hello")  | 
|
492  | 
||
| 707 | 493  | 
// Fibonacci  | 
| 694 | 494  | 
val bf2 = """+++++++++++  | 
| 616 | 495  | 
>+>>>>++++++++++++++++++++++++++++++++++++++++++++  | 
496  | 
>++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>  | 
|
497  | 
+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-  | 
|
498  | 
<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<  | 
|
499  | 
-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]  | 
|
500  | 
>[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++  | 
|
501  | 
+++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++  | 
|
502  | 
++++++++++++++++++++++++++++++++++++++++++++.[-]<<  | 
|
503  | 
<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<  | 
|
| 694 | 504  | 
[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]"""  | 
505  | 
||
506  | 
bf_run(bf2, "fibs")  | 
|
507  | 
||
| 707 | 508  | 
// Mandelbrot Set  | 
509  | 
//----------------  | 
|
510  | 
//  | 
|
511  | 
// Note: Parsing of the generated WHILE program (around 60K in size)  | 
|
512  | 
// takes approximately 10 minutes  | 
|
| 694 | 513  | 
|
| 707 | 514  | 
bf_run("""A mandelbrot set fractal viewer in brainf*** written by Erik Bosman
 | 
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]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>>>>>[-<<<<<<<<->>>>>>>>]+<<<<<<<<[->>>>>>>>  | 
|
655  | 
-<<[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>  | 
|
656  | 
>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<++++  | 
|
657  | 
+[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<  | 
|
658  | 
<<<<<]]>>>]""", "mand")  | 
|
659  | 
||
| 
696
 
2726f2afad67
added krakatau version and made the mandel program work
 
Christian Urban <urbanc@in.tum.de> 
parents: 
695 
diff
changeset
 | 
660  |