908
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// Source language: arithmetic expressions with function calls
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
enum Expr {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
case Num(n: Int)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
case Aop(op: String, e1: Expr, e2: Expr)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
case Call(fname: String, args: List[Expr])
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
import Expr._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
// Target language
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
// "trivial" KValues
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
enum KVal {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
case KVar(s: String)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
case KNum(n: Int)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
case KAop(op: String, v1: KVal, v2: KVal)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
case KCall(fname: String, args: List[KVal])
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
import KVal._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
// KExpressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
enum KExp {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
case KReturn(v: KVal)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
case KLet(x: String, v: KVal, e: KExp)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
import KExp._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
var cnt = -1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
def Fresh(s: String) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
cnt = cnt + 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
s"${s}_${cnt}"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
def CPS(e: Expr)(k: KVal => KExp): KExp = e match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
case Num(i) => k(KNum(i))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
case Aop(op, l, r) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
val z = Fresh("z")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
CPS(l)(l =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
CPS(r)(r => KLet(z, KAop(op, l, r), k(KVar(z)))))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
case Call(fname, args) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
def aux(args: List[Expr], vs: List[KVal]) : KExp = args match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
case Nil => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
val z = Fresh("tmp")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
KLet(z, KCall(fname, vs), k(KVar(z)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
case a::as => CPS(a)(r => aux(as, vs ::: List(r)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
aux(args, Nil)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
def CPSi(e: Expr) : KExp = CPS(e)(KReturn(_))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
//1 + foo(bar(4 * -7), 3, id(12))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
val etest =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
Aop("+", Num(1),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
Call("foo",
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
List(Call("bar",
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
List(Aop("*", Num(4), Num(-7)))),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
Num(3),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
Call("id", List(Num(12))))))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
println(CPSi(etest))
|