progs/fun/simple-cps.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 17 Nov 2023 20:06:43 +0000
changeset 955 47acfd7f9096
parent 911 df8660143051
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Source language: arithmetic expressions with function calls 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
enum Expr {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
    case Num(n: Int)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
    case Aop(op: String, e1: Expr, e2: Expr)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
    case Call(fname: String, args: List[Expr])
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
import Expr._
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
// Target language 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
// "trivial" KValues
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
enum KVal {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
    case KVar(s: String)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
    case KNum(n: Int)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
    case KAop(op: String, v1: KVal, v2: KVal)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
    case KCall(fname: String, args: List[KVal])
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
import KVal._
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
// KExpressions 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
enum KExp {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
    case KReturn(v: KVal)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
    case KLet(x: String, v: KVal, e: KExp)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
import KExp._
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
911
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    26
def pexp(e: KExp): String = e match {
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    27
    case KReturn(v) => s"KReturn($v)"
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    28
    case KLet(x,e1,e2) => s"KLet($x = ${e1} \n in ${pexp(e2)})"
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    29
}
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    30
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
var cnt = -1
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
def Fresh(s: String) = {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
    cnt = cnt + 1
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
    s"${s}_${cnt}"
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
def CPS(e: Expr)(k: KVal => KExp): KExp = e match { 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
    case Num(i) => k(KNum(i))
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
    case Aop(op, l, r) => {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
        val z = Fresh("z")
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
        CPS(l)(l => 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
          CPS(r)(r => KLet(z, KAop(op, l, r), k(KVar(z)))))
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
    }
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
    case Call(fname, args) => {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
        def aux(args: List[Expr], vs: List[KVal]) : KExp = args match {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
            case Nil => {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
                val z = Fresh("tmp")
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
                KLet(z, KCall(fname, vs), k(KVar(z)))
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
            }
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
            case a::as => CPS(a)(r => aux(as, vs ::: List(r)))
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
        }
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
        aux(args, Nil)  
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
    }
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
def CPSi(e: Expr) : KExp = CPS(e)(KReturn(_))
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
//1 + foo(bar(4 * -7), 3, id(12))
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
val etest = 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
    Aop("+", Num(1),
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
             Call("foo", 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
                List(Call("bar", 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
                             List(Aop("*", Num(4), Num(-7)))), 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
                     Num(3), 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
                     Call("id", List(Num(12))))))
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
911
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    68
println(pexp(CPSi(etest)))
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    69
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    70
// Constant Folding
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    71
def opt(v: KVal, env: Map[String, Int]) : KVal = v match {
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    72
    case KVar(s) => if (env.isDefinedAt(s)) KNum(env(s)) else KVar(s)
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    73
    case KNum(n) => KNum(n)
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    74
    case KAop(op, v1, v2) => (op, opt(v1, env), opt(v2, env)) match {
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    75
        case ("+", KNum(n1), KNum(n2)) => KNum(n1 + n2)
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    76
        case ("*", KNum(n1), KNum(n2)) => KNum(n1 * n2)
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    77
        case (_, v1o, v2o) => KAop(op, v1o, v2o)
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    78
    }
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    79
    case KCall(fname, args) => KCall(fname, args.map(opt(_, env)))
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    80
}
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    81
    
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    82
def Koptimise(ke: KExp, env: Map[String, Int] = Map()) : KExp = ke match {
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    83
    case KReturn(v) => KReturn(opt(v, env)) 
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    84
    case KLet(x, v, e) => opt(v, env) match {
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    85
        case KNum(n) => Koptimise(e, env + (x -> n))
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    86
        case vo => KLet(x, vo, Koptimise(e, env))
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    87
    }
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    88
}    
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    89
df8660143051 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    90
println("\n" ++ pexp(Koptimise(CPSi(etest))))