scala/ex.scala
changeset 210 5e2e576fac7c
parent 209 b16dfc467b67
child 221 18905d086cbb
equal deleted inserted replaced
209:b16dfc467b67 210:5e2e576fac7c
    15 
    15 
    16 println("TMCopy:    " + (TMCopy.run(Tape(3))))
    16 println("TMCopy:    " + (TMCopy.run(Tape(3))))
    17 println("TMfindnth: " + (TMFindnth(3).run(Tape(1,2,3,4,5))))
    17 println("TMfindnth: " + (TMFindnth(3).run(Tape(1,2,3,4,5))))
    18 println("TMMopup:   " + (TMMopup(3).run(Tape(1,2,3,4,5))))
    18 println("TMMopup:   " + (TMMopup(3).run(Tape(1,2,3,4,5))))
    19 
    19 
       
    20 println("TMCopyMop: " + ((TMCopy :+ TMMopup(0))))
       
    21 println("TMCopyMop: " + ((TMCopy :+ TMMopup(1)).run(Tape(3))))
    20 
    22 
    21 // Abacus machine examples
    23 // Abacus machine examples
    22 def Copy(in: Int, out: Int, jump: Int) = 
    24 def Copy(in: Int, out: Int, jump: Int) = 
    23   Abacus(Dec(in, jump), Inc(out), Goto(0)) 
    25   Abacus(Dec(in, jump), Inc(out), Goto(0)) 
    24 
    26 
   102 println("Fact(5)       " + test_comp2(Fact, 5))
   104 println("Fact(5)       " + test_comp2(Fact, 5))
   103 //println("Prime(5)      " + test_comp2(Prime, 5))
   105 //println("Prime(5)      " + test_comp2(Prime, 5))
   104 //println("Prime(4)      " + test_comp2(Prime, 4))
   106 //println("Prime(4)      " + test_comp2(Prime, 4))
   105 println("Strt(1)(2)    " + test_comp2(Strt(1), 2))
   107 println("Strt(1)(2)    " + test_comp2(Strt(1), 2))
   106 
   108 
   107 def test_comp1a(f: Rec, ns: Int*) = {
       
   108   val (abc_f, arity, _) = compile_rec(f)
       
   109   val tm1 = toTM(abc_f.p) 
       
   110   val res = tm1.run(Tape(ns.toList))
       
   111   ("Result: " + res + "  length: " + tm1.p.length + " tape: " + arity)
       
   112 }
       
   113 
   109 
   114 println("")
       
   115 println("S(3)          " + test_comp1a(S, 3))
       
   116 println("Const(10)     " + test_comp1a(Const(10), 0))
       
   117 println("Add(6, 3)     " + test_comp1a(Add, 6, 3))
       
   118 
       
   119 /*
       
   120 def test_comp1(f: Rec, ns: Int*) = {
   110 def test_comp1(f: Rec, ns: Int*) = {
   121   val (abc_f, arity, _) = compile_rec(f)
   111   val (abc_f, arity, _) = compile_rec(f)
   122   val tm = toTM(abc_f.p) :+ TMMopup(arity)
   112   val tm = toTM(abc_f.p) :+ TMMopup(arity)
   123   val res = tm.run(Tape(ns.toList))
   113   val res = tm.run(Tape(ns.toList))
   124   ("Result: " + res + "  length: " + tm.p.length + " tape: " + arity)
   114   ("length: " + tm.p.length + " tape: " + arity + "\nResult: " + res)
   125 }
   115 }
   126 
   116 
   127 println("")
   117 println("")
   128 println("S(3)          " + test_comp1(S, 3))
   118 println("S(3)          " + test_comp1(S, 3))
   129 println("Const(10)     " + test_comp1(Const(10), 0))
   119 println("Const(10)     " + test_comp1(Const(10), 0))
   130 println("Add(6, 3)     " + test_comp1(Add, 6, 3))
   120 println("Add(6, 3)     " + test_comp1(Add, 6, 3))
       
   121 println("Mult(4, 5)    " + test_comp1(recs.Mult, 4, 5))
       
   122 println("Fact(4)       " + test_comp1(Fact, 4))
   131 
   123 
   132 */ 
       
   133 //println(toTM(compile_rec(Add)._1.p).run(Tape(3, 4, 0)))
       
   134 //println(compile_rec(Add)._1.run(Map(0 -> 3, 1 -> 8, 2 -> 0)))
       
   135 //compile_rec(Add)._1.run(Map(0 -> 3, 1 -> 4, 2 -> 0))