while1.scala
changeset 80 191daa3ee29e
parent 76 373cf55a3ca5
child 83 bd61b03b9eaa
--- a/while1.scala	Sat Nov 24 15:10:43 2012 +0000
+++ b/while1.scala	Mon Nov 26 01:11:55 2012 +0000
@@ -10,7 +10,7 @@
 val NUM = PLUS(DIGIT)
 val KEYWORD = ALTS("skip", "while", "do", "if", "then", "else", "true", "false", "write") 
 val SEMI: Rexp = ";"
-val OP: Rexp = ALTS(":=", "=", "-", "+", "*", "!", "<", ">")
+val OP: Rexp = ALTS(":=", "=", "-", "+", "*", "!=", "<", ">")
 val WHITESPACE = PLUS(RANGE(" \n"))
 val RPAREN: Rexp = ")"
 val LPAREN: Rexp = "("
@@ -137,8 +137,6 @@
   case Bop("=", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
   case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env))
   case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
-  case Bop("&&", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env)
-  case Bop("||", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
 }
 
 def eval_aexp(a: AExp, env : Env) : Int = a match {
@@ -173,14 +171,49 @@
 
 //examples
 
-eval_prog("loops.while")
+//eval_prog("loops.while")
 //eval_prog("fib.while")
 
 
+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)
+}
+
+
+val test_prog = """
+start := XXX;
+x := start;
+y := start;
+z := start;
+while 0 < x do {
+ while 0 < y do {
+  while 0 < z do {
+    z := z - 1
+  };
+  z := start;
+  y := y - 1
+ };     
+ y := start;
+ x := x - 1
+}
+"""
+
+
+
+def eval_test(n: Int) : Unit = {
+  val tks = Tok.fromString(test_prog.replaceAllLiterally("XXX", n.toString))
+  val ast = Stmts.parse_single(tks)
+  println(n + " " + time_needed(2, eval_bl(ast, Map.empty)))
+}
+
+List(1, 200, 400, 600, 800, 1000, 1200, 1400, 1600).map(eval_test(_))
+
+
 
 
 
 
 
-
-