scala/comp2.scala
changeset 271 4457185b22ef
parent 205 c7975ab7c52e
--- a/scala/comp2.scala	Wed Jun 26 14:42:42 2013 +0100
+++ b/scala/comp2.scala	Wed Jul 17 10:33:19 2013 +0100
@@ -23,42 +23,49 @@
   case n => ClearRegs(n - 1) :+ Abacus(Dec(n - 1, 2), Goto(0))
 }
 
-def cn_merge_gs(x: List[(Abacus, Int, Int)], p: Int) : Abacus = x match {
+def cn_merge_gs(x: List[(Abacus, Int)], n: Int, p: Int) : Abacus = x match {
   case Nil => Abacus(Nil)
-  case (gprog, gpara, gn)::gs => gprog :+ MVReg(gpara, p) :+ cn_merge_gs(gs, p + 1)
+  case (gprog, gn)::gs => gprog :+ MVReg(n, p) :+ cn_merge_gs(gs, n, p + 1)
 }
 
 // translation: 
 // second component is the arity of the recursive function, 
 // third componen is the maximum of registers used
-def compile_rec(f: Rec) : (Abacus, Int, Int) = f match {
-  case Z => (Abacus(Goto(1)), 1, 2)
-  case S => (AbacusAdd(0, 1, 2) :+ Abacus(Inc(1)), 1, 3)
-  case Id(i, j) => (AbacusAdd(j, i, i + 1), i, i + 2) 
+def compile_rec(f: Rec) : (Abacus, Int) = f match {
+  case Z => (Abacus(Goto(1)), 2)
+  case S => (AbacusAdd(0, 1, 2) :+ Abacus(Inc(1)), 3)
+  case Id(i, j) => (AbacusAdd(j, i, i + 1), i + 2) 
   case Cn(n, f, gs) => {
     val cied_gs = gs.map(compile_rec)
-    val (fprog, fpara, fn) = compile_rec(f) 
-    val pstr = (1 + n :: fn :: (cied_gs.map(_._3))).max
-    val qstr = pstr + gs.length + 1 
-    (cn_merge_gs(cied_gs, pstr) :+ MVRegs(0, qstr, n) :+
-     MVRegs(pstr, 0, gs.length) :+ fprog :+ 
-     MVReg(fpara, pstr) :+ ClearRegs(gs.length) :+
-     MVReg(pstr, n) :+ MVRegs(qstr, 0, n), n,  qstr + n)
+    val (fprog, fn) = compile_rec(f) 
+    val qstr = (fn :: (cied_gs.map(_._2))).max
+    (cn_merge_gs(cied_gs, n, qstr) :+ 
+     MVRegs(0, qstr + gs.length, n) :+
+     MVRegs(qstr, 0, gs.length) :+
+     fprog :+ 
+     MVReg(f.arity, qstr + gs.length + 1) :+ 
+     ClearRegs(gs.length) :+
+     MVRegs(qstr + gs.length , 0, n + 1), qstr + gs.length + n + 1)
   }
   case Pr(n, f, g) => { 
-    val (fprog, fpara, fn) = compile_rec(f) 
-    val (gprog, gpara, gn) = compile_rec(g) 
-    val p = List(n + 3, fn, gn).max 
+    val (fprog, fn) = compile_rec(f) 
+    val (gprog, gn) = compile_rec(g) 
+    val qstr = List(fn, gn).max 
     val e = gprog.p.length + 7 
-    (MVReg(n, p) :+ fprog :+ MVReg(n, n + 1) :+ 
-     ((Abacus(Dec(p, e)) :+ gprog :+ Abacus(Inc(n), Dec(n + 1, 3), Goto(1))) ++
-      Abacus(Dec(n + 2, 0), Inc (n + 1), Goto (gprog.p.length + 4))), n + 1, p + 1)
+    (MVReg(0, qstr) :+
+     MVRegs(1, 0, n) :+ 
+     fprog :+ 
+     MVReg(n, n + 1) :+ 
+     MVRegs(0, 2, n) :+ 
+     ((Abacus(Dec(qstr, e)) :+ gprog :+ Abacus(Inc(0), Dec(n + 1, 3), Goto(1))) ++
+       Abacus(Dec(n + 2, 0), Inc (1), Goto (gprog.p.length + 4))), qstr + 1)
   }
   case Mn(n, f) => {
-    val (fprog, fpara, fn) = compile_rec(f)
+    val (fprog, fn) = compile_rec(f)
     val len = fprog.p.length
-    (fprog ++ Abacus(Dec(n + 1, len + 5), Dec(n + 1, len + 3), Goto(len + 1), Inc(n), Goto(0)), 
-     n, List(n + 1, fn).max)
+    (MVRegs(0, 1, n) :+
+     (fprog ++ Abacus(Dec(n + 1, len + 5), Dec(n + 1, len + 3), Goto(len + 1), Inc(0), Goto(0))), 
+     fn)
   }
 }