# HG changeset patch # User Christian Urban # Date 1362181562 0 # Node ID 3267acc1f97fd1baaafec0ca2c667e95f128c445 # Parent b93ec66cf4bb24d6d921d590602953434c81e6d8 added examples for the rec to abacus compilation diff -r b93ec66cf4bb -r 3267acc1f97f scala/abacus.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)) } diff -r b93ec66cf4bb -r 3267acc1f97f scala/ex.scala --- 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))