progs/fun/fun_llvm.sc
changeset 819 fd88a0656164
parent 813 059f970287d1
child 869 81ee93b87258
equal deleted inserted replaced
818:6928a677d26f 819:fd88a0656164
    61 case class KNum(i: Int) extends KVal
    61 case class KNum(i: Int) extends KVal
    62 case class Kop(o: String, v1: KVal, v2: KVal) extends KVal
    62 case class Kop(o: String, v1: KVal, v2: KVal) extends KVal
    63 case class KCall(o: String, vrs: List[KVal]) extends KVal
    63 case class KCall(o: String, vrs: List[KVal]) extends KVal
    64 case class KWrite(v: KVal) extends KVal
    64 case class KWrite(v: KVal) extends KVal
    65 
    65 
       
    66 case class KLet(x: String, e1: KVal, e2: KExp) extends KExp {
       
    67   override def toString = s"LET $x = $e1 in \n$e2" 
       
    68 }
    66 case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp {
    69 case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp {
    67   override def toString = s"KIf $x1\nIF\n$e1\nELSE\n$e2"
    70   def pad(e: KExp) = e.toString.replaceAll("(?m)^", "  ")
    68 }
    71 
    69 case class KLet(x: String, e1: KVal, e2: KExp) extends KExp {
    72   override def toString = 
    70   override def toString = s"let $x = $e1 in \n$e2" 
    73      s"IF $x1\nTHEN\n${pad(e1)}\nELSE\n${pad(e2)}"
    71 }
    74 }
    72 case class KReturn(v: KVal) extends KExp
    75 case class KReturn(v: KVal) extends KExp
    73 
    76 
    74 
    77 
    75 // CPS translation from Exps to KExps using a
    78 // CPS translation from Exps to KExps using a
   107 }   
   110 }   
   108 
   111 
   109 //initial continuation
   112 //initial continuation
   110 def CPSi(e: Exp) = CPS(e)(KReturn)
   113 def CPSi(e: Exp) = CPS(e)(KReturn)
   111 
   114 
   112 // some testcases
   115 //some testcases:
       
   116 // numbers and vars   
       
   117 println(CPSi(Num(1)).toString)
       
   118 println(CPSi(Var("z")).toString)
       
   119 
       
   120 //  a * 3
   113 val e1 = Aop("*", Var("a"), Num(3))
   121 val e1 = Aop("*", Var("a"), Num(3))
   114 CPSi(e1)
   122 println(CPSi(e1).toString)
   115 
   123 
       
   124 // (a * 3) + 4
   116 val e2 = Aop("+", Aop("*", Var("a"), Num(3)), Num(4))
   125 val e2 = Aop("+", Aop("*", Var("a"), Num(3)), Num(4))
   117 CPSi(e2)
   126 println(CPSi(e2).toString)
   118 
   127 
       
   128 // 2 + (a * 3)
   119 val e3 = Aop("+", Num(2), Aop("*", Var("a"), Num(3)))
   129 val e3 = Aop("+", Num(2), Aop("*", Var("a"), Num(3)))
   120 CPSi(e3)
   130 println(CPSi(e3).toString)
   121 
   131 
       
   132 //(1 - 2) + (a * 3)
   122 val e4 = Aop("+", Aop("-", Num(1), Num(2)), Aop("*", Var("a"), Num(3)))
   133 val e4 = Aop("+", Aop("-", Num(1), Num(2)), Aop("*", Var("a"), Num(3)))
   123 CPSi(e4)
   134 println(CPSi(e4).toString)
   124 
   135 
       
   136 // 3 + 4 ; 1 * 7
       
   137 val es = Sequence(Aop("+", Num(3), Num(4)),
       
   138                   Aop("*", Num(1), Num(7)))
       
   139 println(CPSi(es).toString)
       
   140 
       
   141 // if (1 == 1) then 3 else 4
   125 val e5 = If(Bop("==", Num(1), Num(1)), Num(3), Num(4))
   142 val e5 = If(Bop("==", Num(1), Num(1)), Num(3), Num(4))
   126 CPSi(e5)
   143 println(CPSi(e5).toString)
   127 
   144 
       
   145 // if (1 == 1) then 3 + 7 else 4 * 2
       
   146 val ei = If(Bop("==", Num(1), Num(1)), 
       
   147                 Aop("+", Num(3), Num(7)),
       
   148                 Aop("*", Num(4), Num(2)))
       
   149 println(CPSi(ei).toString)
       
   150 
       
   151 
       
   152 // if (10 != 10) then e5 else 40
   128 val e6 = If(Bop("!=", Num(10), Num(10)), e5, Num(40))
   153 val e6 = If(Bop("!=", Num(10), Num(10)), e5, Num(40))
   129 CPSi(e6)
   154 println(CPSi(e6).toString)
   130 
   155 
       
   156 
       
   157 // foo(3)
   131 val e7 = Call("foo", List(Num(3)))
   158 val e7 = Call("foo", List(Num(3)))
   132 CPSi(e7)
   159 println(CPSi(e7).toString)
   133 
   160 
   134 val e8 = Call("foo", List(Aop("*", Num(3), Num(1)), Num(4), Aop("+", Num(5), Num(6))))
   161 // foo(3 * 1, 4, 5 + 6)
   135 CPSi(e8)
   162 val e8 = Call("foo", List(Aop("*", Num(3), Num(1)), 
   136 
   163                           Num(4), 
   137 val e9 = Sequence(Aop("*", Var("a"), Num(3)), Aop("+", Var("b"), Num(6)))
   164                           Aop("+", Num(5), Num(6))))
   138 CPSi(e9)
   165 println(CPSi(e8).toString)
   139 
   166 
   140 val e = Aop("*", Aop("+", Num(1), Call("foo", List(Var("a"), Num(3)))), Num(4))
   167 // a * 3 ; b + 6
   141 CPSi(e)
   168 val e9 = Sequence(Aop("*", Var("a"), Num(3)), 
       
   169                   Aop("+", Var("b"), Num(6)))
       
   170 println(CPSi(e9).toString)
       
   171 
       
   172 
       
   173 val e10 = Aop("*", Aop("+", Num(1), Call("foo", List(Var("a"), Num(3)))), Num(4))
       
   174 println(CPSi(e10).toString)
       
   175 
   142 
   176 
   143 
   177 
   144 
   178 
   145 
   179 
   146 // convenient string interpolations 
   180 // convenient string interpolations 
   264 }
   298 }
   265 
   299 
   266 
   300 
   267 
   301 
   268 
   302 
   269 
   303 // CPS functions
       
   304 /*
       
   305 
       
   306 def fact(n: Int) : Int = 
       
   307   if (n == 0) 1 else n * fact(n - 1)
       
   308 
       
   309 fact(6)
       
   310 
       
   311 def factT(n: Int, acc: Int) : Int =
       
   312   if (n == 0) acc else factT(n - 1, acc * n)
       
   313 
       
   314 factT(6, 1)
       
   315 
       
   316 def factC(n: Int, ret: Int => Int) : Int = {
       
   317   if (n == 0) ret(1) 
       
   318   else factC(n - 1, x => ret(x * n))
       
   319 }
       
   320 
       
   321 factC(6, x => x)
       
   322 factC(6, x => {println(s"The final Result is $x") ; 0})
       
   323 factC(6, _ + 1)
       
   324 
       
   325 def fibC(n: Int, ret: Int => Int) : Int = {
       
   326   if (n == 0 || n == 1) ret(1)
       
   327   else fibC(n - 1, x => fibC(n - 2, y => ret(x + y)))
       
   328 }
       
   329 
       
   330 fibC(10, x => {println(s"Result: $x") ; 1})
       
   331 
       
   332 
       
   333 */