progs/fun.scala
changeset 223 e4b29b57f6a3
parent 221 824ffbf66ab4
child 225 ef573e8fc86e
--- a/progs/fun.scala	Mon Dec 02 23:21:22 2013 +0000
+++ b/progs/fun.scala	Wed Dec 04 01:51:22 2013 +0000
@@ -23,7 +23,7 @@
   def apply(s: String) : RANGE = RANGE(s.toList)
 }
 def NMTIMES(r: Rexp, n: Int, m: Int) = {
-  if(m < n) throw new IllegalArgumentException("the number m cannot be smaller than n.")
+  if (m < n) throw new IllegalArgumentException("the number m cannot be smaller than n.")
   else NUPTOM(r, n, m - n)
 }
 
@@ -341,14 +341,6 @@
   (Defn ~ T_SEMI ~ Prog) ==> { case ((x, y), z) => x :: z : List[Decl] } ||
   (Exp ==> ((s) => List(Main(s)) : List[Decl]))
 
-// parser examples
-
-val p12_toks = tokenizer(fromFile("defs.rec"))
-val p12_ast = Prog.parse_all(p12_toks)
-//println(p12_toks.mkString(","))
-//println(p12_ast)
-
-
 
 // compiler - built-in functions 
 // copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html
@@ -431,57 +423,6 @@
     compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("if_icmpgt " + jmp + "\n")
 }
 
-
-def compile_expT(a: Exp, env : Mem, name: String) : Instrs = a match {
-  case Num(i) => List("ldc " + i.toString + "\n")
-  case Var(s) => List("iload " + env(s).toString + "\n")
-  case Aop("+", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("iadd\n")
-  case Aop("-", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("isub\n")
-  case Aop("*", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("imul\n")
-  case Aop("/", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("idiv\n")
-  case Aop("%", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("irem\n")
-  case If(b, a1, a2) => {
-    val if_else = Fresh("If_else")
-    val if_end = Fresh("If_end")
-    compile_bexp(b, env, if_else) ++
-    compile_expT(a1, env, name) ++
-    List("goto " + if_end + "\n") ++
-    List("\n" + if_else + ":\n\n") ++
-    compile_expT(a2, env, name) ++
-    List("\n" + if_end + ":\n\n")
-  }
-  case Call(n, args) => if (name == n) { 
-    val stores = args.zipWithIndex.map { case (x, y) => "istore " + y.toString + "\n" } 
-    args.flatMap(a => compile_expT(a, env, "")) ++
-    stores.reverse ++ 
-    List ("goto " + n + "_Start\n") 
-  } else {
-    val is = "I" * args.length
-    args.flatMap(a => compile_expT(a, env, "")) ++
-    List ("invokestatic XXX/XXX/" + n + "(" + is + ")I\n")
-  }
-  case Sequ(a1, a2) => {
-    compile_expT(a1, env, "") ++ List("pop\n") ++ compile_expT(a2, env, name)
-  }
-  case Write(a1) => {
-    compile_expT(a1, env, "") ++
-    List("dup\n",
-         "invokestatic XXX/XXX/write(I)V\n")
-  }
-}
-
-def compile_bexpT(b: BExp, env : Mem, jmp: String) : Instrs = b match {
-  case Bop("==", a1, a2) => 
-    compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("if_icmpne " + jmp + "\n")
-  case Bop("!=", a1, a2) => 
-    compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("if_icmpeq " + jmp + "\n")
-  case Bop("<", a1, a2) => 
-    compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("if_icmpge " + jmp + "\n")
-  case Bop("<=", a1, a2) => 
-    compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ List("if_icmpgt " + jmp + "\n")
-}
-
-
 def compile_decl(d: Decl) : Instrs = d match {
   case Def(name, args, a) => { 
     val env = args.zipWithIndex.toMap
@@ -490,21 +431,18 @@
          ".limit locals " + args.length.toString + "\n",
          ".limit stack " + (1 + max_stack_exp(a)).toString + "\n",
          name + "_Start:\n") ++   
-    //compile_exp(a, env) ++
-    compile_expT(a, env, name) ++
+    compile_exp(a, env) ++
     List("ireturn\n",
          ".end method \n\n")
   }
   case Main(a) => {
-    val main_begin = 
-      List(".method public static main([Ljava/lang/String;)V\n",
-           ".limit locals 200\n",
-           ".limit stack 200\n")
-    val main_end = 
-      List("invokestatic XXX/XXX/write(I)V\n",
-           "return\n",
-           ".end method\n")
-    main_begin ++ compile_exp(a, Map()) ++ main_end
+    List(".method public static main([Ljava/lang/String;)V\n",
+         ".limit locals 200\n",
+         ".limit stack 200\n") ++
+    compile_exp(a, Map()) ++
+    List("invokestatic XXX/XXX/write(I)V\n",
+         "return\n",
+         ".end method\n")
   }
 }