updated
authorChristian Urban <christian.urban@kcl.ac.uk>
Thu, 17 Dec 2020 17:43:40 +0000
changeset 819 fd88a0656164
parent 818 6928a677d26f
child 820 7fd1f611c21d
updated
langs.sty
progs/fun/fun_llvm.sc
slides/slides09.pdf
slides/slides09.tex
--- a/langs.sty	Mon Dec 14 19:22:12 2020 +0000
+++ b/langs.sty	Thu Dec 17 17:43:40 2020 +0000
@@ -70,7 +70,7 @@
 }[keywords]
 
 \lstdefinelanguage{LLVMIR}{
-  otherkeywords={let,in,add,mul},
+  otherkeywords={let,in,add,mul,KLet},
 }[strings]
 
 \newcommand{\code}[1]{{\lstinline{#1}}}
--- a/progs/fun/fun_llvm.sc	Mon Dec 14 19:22:12 2020 +0000
+++ b/progs/fun/fun_llvm.sc	Thu Dec 17 17:43:40 2020 +0000
@@ -63,11 +63,14 @@
 case class KCall(o: String, vrs: List[KVal]) extends KVal
 case class KWrite(v: KVal) extends KVal
 
-case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp {
-  override def toString = s"KIf $x1\nIF\n$e1\nELSE\n$e2"
+case class KLet(x: String, e1: KVal, e2: KExp) extends KExp {
+  override def toString = s"LET $x = $e1 in \n$e2" 
 }
-case class KLet(x: String, e1: KVal, e2: KExp) extends KExp {
-  override def toString = s"let $x = $e1 in \n$e2" 
+case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp {
+  def pad(e: KExp) = e.toString.replaceAll("(?m)^", "  ")
+
+  override def toString = 
+     s"IF $x1\nTHEN\n${pad(e1)}\nELSE\n${pad(e2)}"
 }
 case class KReturn(v: KVal) extends KExp
 
@@ -109,36 +112,67 @@
 //initial continuation
 def CPSi(e: Exp) = CPS(e)(KReturn)
 
-// some testcases
+//some testcases:
+// numbers and vars   
+println(CPSi(Num(1)).toString)
+println(CPSi(Var("z")).toString)
+
+//  a * 3
 val e1 = Aop("*", Var("a"), Num(3))
-CPSi(e1)
+println(CPSi(e1).toString)
 
+// (a * 3) + 4
 val e2 = Aop("+", Aop("*", Var("a"), Num(3)), Num(4))
-CPSi(e2)
+println(CPSi(e2).toString)
 
+// 2 + (a * 3)
 val e3 = Aop("+", Num(2), Aop("*", Var("a"), Num(3)))
-CPSi(e3)
+println(CPSi(e3).toString)
 
+//(1 - 2) + (a * 3)
 val e4 = Aop("+", Aop("-", Num(1), Num(2)), Aop("*", Var("a"), Num(3)))
-CPSi(e4)
+println(CPSi(e4).toString)
 
+// 3 + 4 ; 1 * 7
+val es = Sequence(Aop("+", Num(3), Num(4)),
+                  Aop("*", Num(1), Num(7)))
+println(CPSi(es).toString)
+
+// if (1 == 1) then 3 else 4
 val e5 = If(Bop("==", Num(1), Num(1)), Num(3), Num(4))
-CPSi(e5)
+println(CPSi(e5).toString)
 
+// if (1 == 1) then 3 + 7 else 4 * 2
+val ei = If(Bop("==", Num(1), Num(1)), 
+                Aop("+", Num(3), Num(7)),
+                Aop("*", Num(4), Num(2)))
+println(CPSi(ei).toString)
+
+
+// if (10 != 10) then e5 else 40
 val e6 = If(Bop("!=", Num(10), Num(10)), e5, Num(40))
-CPSi(e6)
+println(CPSi(e6).toString)
 
-val e7 = Call("foo", List(Num(3)))
-CPSi(e7)
 
-val e8 = Call("foo", List(Aop("*", Num(3), Num(1)), Num(4), Aop("+", Num(5), Num(6))))
-CPSi(e8)
+// foo(3)
+val e7 = Call("foo", List(Num(3)))
+println(CPSi(e7).toString)
+
+// foo(3 * 1, 4, 5 + 6)
+val e8 = Call("foo", List(Aop("*", Num(3), Num(1)), 
+                          Num(4), 
+                          Aop("+", Num(5), Num(6))))
+println(CPSi(e8).toString)
 
-val e9 = Sequence(Aop("*", Var("a"), Num(3)), Aop("+", Var("b"), Num(6)))
-CPSi(e9)
+// a * 3 ; b + 6
+val e9 = Sequence(Aop("*", Var("a"), Num(3)), 
+                  Aop("+", Var("b"), Num(6)))
+println(CPSi(e9).toString)
 
-val e = Aop("*", Aop("+", Num(1), Call("foo", List(Var("a"), Num(3)))), Num(4))
-CPSi(e)
+
+val e10 = Aop("*", Aop("+", Num(1), Call("foo", List(Var("a"), Num(3)))), Num(4))
+println(CPSi(e10).toString)
+
 
 
 
@@ -266,4 +300,34 @@
 
 
 
+// CPS functions
+/*
 
+def fact(n: Int) : Int = 
+  if (n == 0) 1 else n * fact(n - 1)
+
+fact(6)
+
+def factT(n: Int, acc: Int) : Int =
+  if (n == 0) acc else factT(n - 1, acc * n)
+
+factT(6, 1)
+
+def factC(n: Int, ret: Int => Int) : Int = {
+  if (n == 0) ret(1) 
+  else factC(n - 1, x => ret(x * n))
+}
+
+factC(6, x => x)
+factC(6, x => {println(s"The final Result is $x") ; 0})
+factC(6, _ + 1)
+
+def fibC(n: Int, ret: Int => Int) : Int = {
+  if (n == 0 || n == 1) ret(1)
+  else fibC(n - 1, x => fibC(n - 2, y => ret(x + y)))
+}
+
+fibC(10, x => {println(s"Result: $x") ; 1})
+
+
+*/
Binary file slides/slides09.pdf has changed
--- a/slides/slides09.tex	Mon Dec 14 19:22:12 2020 +0000
+++ b/slides/slides09.tex	Thu Dec 17 17:43:40 2020 +0000
@@ -327,6 +327,60 @@
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \begin{frame}[fragile,c]
+\frametitle{KLet}
+
+\begin{lstlisting}[language=LLVM]
+tmp0 = add 1 a   
+tmp1 = mul b 5 
+tmp2 = add 3 tmp1 
+tmp3 = add tmp0 tmp2  
+\end{lstlisting}
+
+\begin{lstlisting}[language=LLVMIR]
+  KLet tmp0 , add 1 a in  
+   KLet tmp1 , mul b 5 in
+    KLet tmp2 , add 3 tmp1 in 
+     KLet tmp3 , add tmp0 tmp2 in
+      ...
+\end{lstlisting}
+
+\begin{lstlisting}[language=Scala,numbers=none]
+case class KLet(x: String, e1: KVal, e2: KExp)
+\end{lstlisting}
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[fragile,c]
+\frametitle{KLet}
+
+\begin{lstlisting}[language=LLVM]
+tmp0 = add 1 a   
+tmp1 = mul b 5 
+tmp2 = add 3 tmp1 
+tmp3 = add tmp0 tmp2  
+\end{lstlisting}
+
+\begin{lstlisting}[language=LLVMIR]
+  let tmp0 = add 1 a in  
+   let tmp1 = mul b 5 in
+    let tmp2 = add 3 tmp1 in 
+     let tmp3 = add tmp0 tmp2 in
+      ...
+\end{lstlisting}
+
+\begin{lstlisting}[language=Scala,numbers=none]
+case class KLet(x: String, e1: KVal, e2: KExp)
+\end{lstlisting}
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[fragile,c]
 \frametitle{CPS-Translation}
 \small