progs/compile-lexer.scala
changeset 227 93bd75031ced
parent 219 599e6a6ab6c6
child 323 4ce07c4abdb4
--- a/progs/compile-lexer.scala	Fri Apr 18 21:05:07 2014 +0100
+++ b/progs/compile-lexer.scala	Thu Aug 21 15:10:53 2014 +0100
@@ -124,7 +124,7 @@
 val LPAREN: Rexp = "("
 val BEGIN: Rexp = "{"
 val END: Rexp = "}"
-val ALL = SYM | DIGIT | " " | ":" | ";" | "\"" | "="
+val ALL = SYM | DIGIT | OP | " " | ":" | ";" | "\"" | "(" | ")" | "{" | "}" 
 val ALL2 = ALL | "\n"
 val COMMENT2 = ("/*" ~ NOT(ALL.% ~ "*/" ~ ALL.%) ~ "*/")
 val COMMENT = ("/*" ~ ALL2.% ~ "*/") | ("//" ~ ALL.% ~ "\n")
@@ -567,12 +567,14 @@
   case Aop("+", a1, a2) => compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("iadd\n")
   case Aop("-", a1, a2) => compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("isub\n")
   case Aop("*", a1, a2) => compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("imul\n")
+  case Aop("/", a1, a2) => compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("idiv\n")
+  case Aop("%", a1, a2) => compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("irem\n") 
 }
 
 def compile_bexp(b: BExp, env : Mem, jmp: String) : Instrs = b match {
   case True => Nil
   case False => List("goto " + jmp + "\n")
-  case Bop("=", a1, a2) => 
+  case Bop("==", a1, a2) => 
     compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("if_icmpne " + jmp + "\n")
   case Bop("!=", a1, a2) => 
     compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("if_icmpeq " + jmp + "\n")
@@ -635,6 +637,7 @@
 
 def compile(class_name: String, input: String) : String = {
   val tks = tokenizer(input)
+  println("Output\n" + Stmts.parse(tks))
   val ast = Stmts.parse_single(tks)
   val instructions = compile_bl(ast, Map.empty)._1
   (beginning ++ instructions.mkString ++ ending).replaceAllLiterally("XXX", class_name)
@@ -661,12 +664,12 @@
   val class_name = file_name.split('.')(0)
   compile_file(file_name)
   val test = ("java -jar jvm/jasmin-2.4/jasmin.jar " + class_name + ".j").!!
-  //("java " + class_name + "/" + class_name).!
+  ("java " + class_name + "/" + class_name).!
 }
 
 
 //examples
 //println(compile("test", p9))
 //compile_run("loops.while")
-compile_run("fib.while")
+compile_run("p.while")
 //compile_run("test.while")