progs/compile.scala
changeset 674 e0a41a1f24cf
parent 668 9ce78065f68d
child 687 8865f4f2be59
equal deleted inserted replaced
673:715b46eee102 674:e0a41a1f24cf
     1 // A Small Compiler for the WHILE Language
     1 // A Small Compiler for the WHILE Language
     2 // (it does not use a parser and lexer)
     2 // (it does not use a parser nor lexer)
     3 
     3 
     4 
     4 
     5 // the abstract syntax trees
     5 // the abstract syntax trees
     6 abstract class Stmt
     6 abstract class Stmt
     7 abstract class AExp
     7 abstract class AExp
   120 
   120 
   121 
   121 
   122 // environments 
   122 // environments 
   123 type Env = Map[String, Int]
   123 type Env = Map[String, Int]
   124 
   124 
       
   125 
       
   126 def compile_op(op: String) = op match {
       
   127   case "+" => i"iadd"
       
   128   case "-" => i"isub"
       
   129   case "*" => i"imul"
       
   130 }
       
   131 
   125 // arithmetic expression compilation
   132 // arithmetic expression compilation
   126 def compile_aexp(a: AExp, env : Env) : String = a match {
   133 def compile_aexp(a: AExp, env : Env) : String = a match {
   127   case Num(i) => i"ldc $i"
   134   case Num(i) => i"ldc $i"
   128   case Var(s) => i"iload ${env(s)}"
   135   case Var(s) => i"iload ${env(s)}"
   129   case Aop("+", a1, a2) => 
   136   case Aop(op, a1, a2) => 
   130     compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"iadd"
   137     compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op)
   131   case Aop("-", a1, a2) => 
       
   132     compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"isub"
       
   133   case Aop("*", a1, a2) => 
       
   134     compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"imul"
       
   135 }
   138 }
   136 
   139 
   137 // boolean expression compilation
   140 // boolean expression compilation
   138 def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
   141 def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
   139   case True => ""
   142   case True => ""
   246   println("running")
   249   println("running")
   247   println("Time: " + time_needed(1, ("java " + class_name + "/" + class_name).!))
   250   println("Time: " + time_needed(1, ("java " + class_name + "/" + class_name).!))
   248 }
   251 }
   249 
   252 
   250 
   253 
   251 // Fibonacci numbers as a test-case
   254 // Fibonacci numbers as a bare-bone test-case
   252 val fib_test = 
   255 val fib_test = 
   253   List(Assign("n", Num(10)),            //  n := 10;                     
   256   List(Assign("n", Num(10)),            //  n := 10;                     
   254        Assign("minus1",Num(0)),         //  minus1 := 0;
   257        Assign("minus1",Num(0)),         //  minus1 := 0;
   255        Assign("minus2",Num(1)),         //  minus2 := 1;
   258        Assign("minus2",Num(1)),         //  minus2 := 1;
   256        Assign("temp",Num(0)),           //  temp := 0;
   259        Assign("temp",Num(0)),           //  temp := 0;