--- a/progs/compile.scala Tue Nov 10 22:27:46 2015 +0000
+++ b/progs/compile.scala Sun Nov 15 21:31:31 2015 +0000
@@ -111,9 +111,13 @@
def compile_aexp(a: AExp, env : Env) : Instrs = a match {
case Num(i) => List("ldc " + i.toString + "\n")
case Var(s) => List("iload " + env(s) + "\n")
- 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("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")
}
// boolean expression compilation
@@ -121,18 +125,22 @@
case True => Nil
case False => List("goto " + jmp + "\n")
case Bop("=", a1, a2) =>
- compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("if_icmpne " + jmp + "\n")
+ 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")
+ compile_aexp(a1, env) ++ compile_aexp(a2, env) ++
+ List("if_icmpeq " + jmp + "\n")
case Bop("<", a1, a2) =>
- compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("if_icmpge " + jmp + "\n")
+ compile_aexp(a1, env) ++ compile_aexp(a2, env) ++
+ List("if_icmpge " + jmp + "\n")
}
// statement compilation
def compile_stmt(s: Stmt, env: Env) : (Instrs, Env) = s match {
case Skip => (Nil, env)
case Assign(x, a) => {
- val index = if (env.isDefinedAt(x)) env(x) else env.keys.size.toString
+ val index = if (env.isDefinedAt(x)) env(x) else
+ env.keys.size.toString
(compile_aexp(a, env) ++
List("istore " + index + "\n"), env + (x -> index))
}
@@ -159,9 +167,11 @@
List("\n" + loop_end + ":\n\n"), env1)
}
case Write(x) =>
- (List("iload " + env(x) + "\n" + "invokestatic XXX/XXX/write(I)V\n"), env)
+ (List("iload " + env(x) + "\n" +
+ "invokestatic XXX/XXX/write(I)V\n"), env)
case Read(x) => {
- val index = if (env.isDefinedAt(x)) env(x) else env.keys.size.toString
+ val index = if (env.isDefinedAt(x)) env(x) else
+ env.keys.size.toString
(List("invokestatic XXX/XXX/read()I\n" +
"istore " + index + "\n"), env + (x -> index))
}
@@ -191,10 +201,11 @@
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 };
+ 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