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