progs/compile.scala
changeset 609 e33545bb2eba
parent 600 d488a3e7b0ec
child 624 8d0af38389bc
--- a/progs/compile.scala	Tue Nov 20 13:37:40 2018 +0000
+++ b/progs/compile.scala	Tue Nov 27 00:45:26 2018 +0000
@@ -196,25 +196,9 @@
 }
 
 
-// Fibonacci numbers as a test-case
-val fib_test = 
-  List(Read("n"),                       //  read n;                     
-       Assign("minus1",Num(0)),         //  minus1 := 0;
-       Assign("minus2",Num(1)),         //  minus2 := 1;
-       Assign("temp",Num(0)),           //  temp := 0;
-       While(Bop("<",Num(0),Var("n")),  //  while n > 0 do  {
-          List(Assign("temp",Var("minus2")),    //  temp := minus2;
-               Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), 
-                                        //  minus2 := minus1 + minus2;
-               Assign("minus1",Var("temp")), //  minus1 := temp;
-               Assign("n",Aop("-",Var("n"),Num(1))))), //  n := n - 1 };
-       Write("minus1"))                 //  write minus1
-
-// prints out the JVM-assembly program
-
-println(compile(fib_test, "fib"))
-
-// can be assembled with 
+// compiling and running files
+//
+// JVM files can be assembled with 
 //
 //    java -jar jvm/jasmin-2.4/jasmin.jar fib.j
 //
@@ -222,6 +206,8 @@
 //
 //    java fib/fib
 
+
+
 import scala.util._
 import scala.sys.process._
 import scala.io
@@ -240,6 +226,36 @@
   println("assembled ")
 }
 
+def time_needed[T](i: Int, code: => T) = {
+  val start = System.nanoTime()
+  for (j <- 1 to i) code
+  val end = System.nanoTime()
+  (end - start)/(i * 1.0e9)
+}
 
-compile_all(fib_test, "fib")
+
+def compile_run(bl: Block, class_name: String) : Unit = {
+  println("Start compilation")
+  compile_all(bl, class_name)
+  println("Time: " + time_needed(1, ("java " + class_name + "/" + class_name).!))
+}
+
 
+// Fibonacci numbers as a test-case
+val fib_test = 
+  List(Assign("n", Num(10)),            //  n := 10;                     
+       Assign("minus1",Num(0)),         //  minus1 := 0;
+       Assign("minus2",Num(1)),         //  minus2 := 1;
+       Assign("temp",Num(0)),           //  temp := 0;
+       While(Bop("<",Num(0),Var("n")),  //  while n > 0 do  {
+          List(Assign("temp",Var("minus2")),    //  temp := minus2;
+               Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), 
+                                        //  minus2 := minus1 + minus2;
+               Assign("minus1",Var("temp")), //  minus1 := temp;
+               Assign("n",Aop("-",Var("n"),Num(1))))), //  n := n - 1 };
+       Write("minus1"))                 //  write minus1
+
+
+compile_run(fib_test, "fib")
+
+