progs/while/compile.sc
changeset 961 c0600f8b6427
parent 943 5365ef60707e
child 974 0cb4bf2469d1
--- a/progs/while/compile.sc	Wed May 29 13:25:30 2024 +0100
+++ b/progs/while/compile.sc	Thu Sep 19 15:47:33 2024 +0100
@@ -81,7 +81,7 @@
 
 extension (sc: StringContext) {
     def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
-    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
+    def l(args: Any*): String = sc.s(args:_*) ++ ":"
 }
 
 // this allows us to write things like
@@ -131,22 +131,22 @@
     val if_end = Fresh("If_end")
     val (instrs1, env1) = compile_block(bl1, env)
     val (instrs2, env2) = compile_block(bl2, env1)
-    (compile_bexp(b, env, if_else) ++
-     instrs1 ++
-     i"goto $if_end" ++
-     l"$if_else" ++
-     instrs2 ++
-     l"$if_end", env2)
+    (s"""|${compile_bexp(b, env, if_else)}
+         |${instrs1}
+         |${i"goto $if_end"}
+         |${l"$if_else"}
+         |${instrs2}
+         |${l"$if_end"}""".stripMargin, env2)
   }
   case While(b, bl) => {
     val loop_begin = Fresh("Loop_begin")
     val loop_end = Fresh("Loop_end")
     val (instrs1, env1) = compile_block(bl, env)
-    (l"$loop_begin" ++
-     compile_bexp(b, env, loop_end) ++
-     instrs1 ++
-     i"goto $loop_begin" ++
-     l"$loop_end", env1)
+    (s"""|${l"$loop_begin"}
+         |${compile_bexp(b, env, loop_end)}
+         |$instrs1
+         |${i"goto $loop_begin"}
+         |${l"$loop_end"}""".stripMargin, env1)
   }
   case Write(x) => 
     (i"iload ${env(x)} \t\t; $x" ++ 
@@ -166,7 +166,7 @@
 // main compilation function for blocks
 def compile(bl: Block, class_name: String) : String = {
   val instructions = compile_block(bl, Map.empty)._1
-  (beginning ++ instructions ++ ending).replaceAllLiterally("XXX", class_name)
+  (beginning ++ instructions ++ ending).replace("XXX", class_name)
 }