added examples for the rec to abacus compilation
authorChristian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 01 Mar 2013 23:46:02 +0000
changeset 208 3267acc1f97f
parent 207 b93ec66cf4bb
child 209 b16dfc467b67
added examples for the rec to abacus compilation
scala/abacus.scala
scala/ex.scala
--- a/scala/abacus.scala	Fri Mar 01 11:17:50 2013 +0000
+++ b/scala/abacus.scala	Fri Mar 01 23:46:02 2013 +0000
@@ -1,5 +1,6 @@
 package object abacus {
 
+import scala.annotation.tailrec
 import lib._
 
 // Abacus instructions
@@ -51,7 +52,8 @@
 
   def steps(regs: Regs, n: Int) : AConfig = steps(AConfig(0, regs), n)
 
-  def run(cf: AConfig) : AConfig = {
+  @tailrec
+  final def run(cf: AConfig) : AConfig = {
     if (cf.s >= p.length || cf.s < 0) cf else run(step(cf))
   }
  
--- a/scala/ex.scala	Fri Mar 01 11:17:50 2013 +0000
+++ b/scala/ex.scala	Fri Mar 01 23:46:02 2013 +0000
@@ -81,17 +81,33 @@
 println("NextPrime 3: " + NextPrime.eval(3))
 println("NthPrime 1:  " + NthPrime.eval(1))
 println("Listsum [2, 3, 4 , 5, 6]: " + Listsum(5, 4).eval(2, 3, 4, 5, 6))
-println("Strt:  " + Strt(2).eval(2, 3))
+println("Strt:  " + Strt(2).eval(2,3))
+
+
+// compilation of rec to abacus tests
+def test_comp2(f: Rec, ns: Int*) = {
+  val (abc_f, arity, _) = compile_rec(f)
+  val abc_map = (0 until ns.length).zip(ns).toMap[Int, Int]
+  val res = (abc_f.run(abc_map))(arity)
+  ("Result: " + res + "  length: " + abc_f.p.length)
+}
+
+println("S(3)          " + test_comp2(S, 3))
+println("Const(1)      " + test_comp2(Const(1), 0))
+println("Const(10)     " + test_comp2(Const(10), 0))
+println("Add(69, 30)   " + test_comp2(Add, 69, 30))
+println("Mult(13, 9)   " + test_comp2(recs.Mult, 13, 9))
+println("Power(3, 4)   " + test_comp2(Power, 3, 4))
+println("Minus(30, 4)  " + test_comp2(Minus, 30, 4))
+println("Fact(5)       " + test_comp2(Fact, 5))
+println("Prime(5)      " + test_comp2(Prime, 5))
+println("Prime(6)      " + test_comp2(Prime, 6))
+println("Strt(1)(2)    " + test_comp2(Strt(1), 2))
 
 
 
 
-println(Const(1))
-println(compile_rec(Const(1)))
-println(compile_rec(Const(1))._1.run(Map(0 -> 1, 1 -> 1, 2 -> 0)))
-println(toTM(compile_rec(Const(1))._1.p).run(Tape(1, 1, 0)))
-println(Add)
-println(compile_rec(Add)._1)
-println(toTM(compile_rec(Add)._1.p).run(Tape(3, 4, 0)))
-println(compile_rec(Add)._1.run(Map(0 -> 3, 1 -> 8, 2 -> 0)))
+
+//println(toTM(compile_rec(Add)._1.p).run(Tape(3, 4, 0)))
+//println(compile_rec(Add)._1.run(Map(0 -> 3, 1 -> 8, 2 -> 0)))
 //compile_rec(Add)._1.run(Map(0 -> 3, 1 -> 4, 2 -> 0))