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