solutions/cw4/compiler.sc
changeset 910 b655ce68983f
parent 905 15973df32613
child 920 7af2eea19646
--- a/solutions/cw4/compiler.sc	Tue May 30 16:58:06 2023 +0100
+++ b/solutions/cw4/compiler.sc	Fri Jun 02 15:58:57 2023 +0100
@@ -1,4 +1,9 @@
 // Compiler for JVM
+// 
+// call with 
+//
+//     amm2 compiler.sc 
+//
 
 import $file.lexer
 import lexer._
@@ -109,6 +114,13 @@
     compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op)
 }
 
+def compile_bop(op: String) = op match {
+  case "==" => "if_icmpne"
+  case "!=" => "if_icmpeq"
+  case "<" => "if_icmpge"
+  case ">" => "if_icmple"
+}
+
 def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
   case True => ""
   case False => i"goto $jmp"
@@ -122,14 +134,8 @@
     compile_bexp(b2, env, jmp) ++
     l"$or_end"
   }
-  case Bop("==", a1, a2) => 
-    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpne $jmp"
-  case Bop("!=", a1, a2) => 
-    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpeq $jmp"
-  case Bop("<", a1, a2) => 
-    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpge $jmp"
-  case Bop(">", a1, a2) => 
-    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmple $jmp"
+  case Bop(op, a1, a2) => 
+    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"${compile_bop(op)} $jmp"
 }
 
 def compile_stmt(s: Stmt, env: Env) : (String, Env) = s match {