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