compiler.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 16 May 2023 21:42:41 +0100
changeset 629 1926586b1baf
parent 506 8e633f1d0d38
permissions -rw-r--r--
nomination1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
506
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
type Num = Int
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
type Idn = String
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
var counter = -1
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
// for generating new variables
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
def Fresh(x: Idn) = {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  counter += 1
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  x ++ "_" ++ counter.toString()
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
def commas(s: List[String]) : String = s match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  case Nil => ""
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  case s::Nil => s
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  case s::ss => s + "," + commas(ss)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
def cutlines(s: List[String]) : String = s match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  case Nil => ""
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  case s::ss => s + "\n" + cutlines(ss)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
abstract class Prim
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case class PLUS() extends Prim { override def toString = " + " }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
case class MINUS() extends Prim { override def toString = " - " }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
case class MULT() extends Prim { override def toString = " * " }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
abstract class FTerm
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
case class FVar(x : Idn) extends FTerm { override def toString = x }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
case class FNum(i : Num) extends FTerm { override def toString = i.toString }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
case class FFix(x : Idn, x1: Idn, e : FTerm) extends FTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  override def toString = "fix " + x + "(" + x1 + ")" + "." + e 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
case class FApp(e1 : FTerm, e2 : FTerm) extends FTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  override def toString = "(" + e1 + ") (" + e2 + ")"
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
case class FTuple(es : List[FTerm]) extends FTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  override def toString = "<" + commas(es.map(_.toString)) + ">"
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
case class FProj(i : Num, e : FTerm) extends FTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  override def toString = "proj " + i.toString + " " + e
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
case class FPrimOp(e1 : FTerm, p : Prim, e2 : FTerm) extends FTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  override def toString = e1 + p.toString + e2
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
case class FIf0(e1 : FTerm, e2 : FTerm, e3 : FTerm) extends FTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  override def toString = "if0(" + e1 + "," + e2 + "," + e3 + ")"
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
abstract class KTerm
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
abstract class KVal
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
case class KApp(v : KVal, vs : List[KVal]) extends KTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  override def toString = v + "(" + commas(vs.map(_.toString)) + ")" 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
case class KIf0(v : KVal, e1 : KTerm, e2 : KTerm) extends KTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  override def toString = "if0(" + v + "," + e1 + "," + e2 + ")"
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
case class KHalt(v : KVal) extends KTerm { override def toString = "halt " + v }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
case class KLet(x : Idn, v : KVal, e : KTerm) extends KTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  override def toString = "let " + x + " = " + v + " in " + e
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
case class KLetProj(x : Idn, i : Num, v : KVal, e : KTerm) extends KTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  override def toString = "let " + x + " =" + i + " " + v + " in " + e
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
case class KLetPrimOp(x : Idn, v1 : KVal, p : Prim, v2 : KVal, e : KTerm) extends KTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  override def toString = "let " + x + " = " + v1 + p + v2 + " in " + e
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
case class KVVar(x : Idn) extends KVal { override def toString = x }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
case class KVNum(i : Num) extends KVal { override def toString = i.toString }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
case class KVTuple(vs : List[KVal]) extends KVal {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  override def toString = "<" + commas(vs.map(_.toString)) + ">"
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
case class KVFix(x : Idn, args : List[Idn], e : KTerm) extends KVal {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
  override def toString = "fix " + x + "(" + commas(args) + ")." + e
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
// CPS tail translation
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
def CPST(e: FTerm) : (KVal => KTerm) = e match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
  case FVar(x) => (c: KVal) => KApp(c, List(KVVar(x))) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  case FNum(i) => (c: KVal) => KApp(c, List(KVNum(i))) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
  case FIf0(e1, e2, e3) => (c: KVal) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
    val e1_prime = CPS(e1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
    val e2_prime = CPST(e2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
    val e3_prime = CPST(e3)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
    e1_prime((y: KVal) => KIf0(y, e2_prime(c), e3_prime(c)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  case FPrimOp(e1, op, e2) => (c: KVal) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
    val z = Fresh("z")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
    val e1_prime = CPS(e1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
    val e2_prime = CPS(e2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
    e1_prime((y1: KVal) => 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
      e2_prime((y2: KVal) => 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
        KLetPrimOp(z, y1, op, y2, KApp(c, List(KVVar(z))))))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  case FFix(x, x1, e) => (c: KVal) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
    val c_prime = Fresh("c'")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
    val e_prime = CPST(e)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
    KApp(c, List(KVFix(x, List(x1, c_prime), e_prime(KVVar(c_prime)))))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
  case FApp(e1, e2) => (c: KVal) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
    val e1_prime = CPS(e1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
    val e2_prime = CPS(e2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
    e1_prime((y1: KVal) => 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
      e2_prime((y2: KVal) => 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
        KApp(y1, List(y2, c))))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  case FTuple(es) => (c: KVal) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
    def aux(es: List[FTerm], vs: List[KVal]) : KTerm = es match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
      case Nil => KApp(c, vs.reverse)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
      case (e::es) => CPS(e)((y: KVal) => aux(es, y::vs)) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
    }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
    aux(es, Nil) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
  case FProj(i, e) => (c: KVal) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
    val z = Fresh("z")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
    CPS(e)((y: KVal) => KLetProj(z, i, y, KApp(c, List(KVVar(z)))))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
// CPS translation
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
def CPS(e: FTerm) : (KVal => KTerm) => KTerm = e match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
  case FVar(i) => (k: KVal => KTerm) => k(KVVar(i)) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
  case FNum(i) => (k: KVal => KTerm) => k(KVNum(i)) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
  case FFix(x, x1, e) => (k: KVal => KTerm) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
    val c = Fresh("c")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
    val e_prime = CPST(e)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
    k(KVFix(x, List(x1, c), e_prime(KVVar(c))))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
  case FApp(e1, e2) => (k: KVal => KTerm) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
    val fr = Fresh("")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
    val z = Fresh("z")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
    val e1_prime = CPS(e1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
    val e2_prime = CPS(e2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
    e1_prime((y1: KVal) => 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
      e2_prime((y2: KVal) =>
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
        KApp(y1, List(y2, KVFix(fr, List(z), k(KVVar(z)))))))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  case FIf0(e1, e2, e3) => (k: KVal => KTerm) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
    val fr = Fresh("")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
    val c = Fresh("c")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
    val z = Fresh("z")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
    val e1_prime = CPS(e1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
    val e2_prime = CPST(e2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
    val e3_prime = CPST(e3)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
    e1_prime((y: KVal) =>
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
      KLet(c, KVFix(fr, List(z), k(KVVar(z))),  
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
                KIf0(y, e2_prime(KVVar(c)), e3_prime(KVVar(c)))))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
  case FPrimOp(e1, op, e2) => (k: KVal => KTerm) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
    val z = Fresh("z")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
    val e1_prime = CPS(e1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
    val e2_prime = CPS(e2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
    e1_prime((y1: KVal) => e2_prime((y2: KVal) => KLetPrimOp(z, y1, op, y2, k(KVVar(z)))))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
  case FTuple(es) => (k: KVal => KTerm) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
    def aux(es: List[FTerm], vs: List[KVal]) : KTerm = es match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
      case Nil => k(KVTuple(vs.reverse))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
      case (e::es) => CPS(e)((y: KVal) => aux(es, y::vs)) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
    }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
    aux(es, Nil) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
  case FProj(i, e) => (k: KVal => KTerm) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
    val z = Fresh("z")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
    CPS(e)((y: KVal) => KLetProj(z, i, y, k(KVVar(z))))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
//free variable function
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
def FVKval(v: KVal) : Set[Idn] = v match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
  case KVVar(x) => Set(x)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
  case KVNum(i) => Set()
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
  case KVTuple(vs) => vs.flatMap{FVKval}.toSet
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
  case KVFix(x, args, e) => FVKexp(e) -- args - x 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
def FVKexp(e: KTerm) : Set[Idn] = e match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
  case KApp(v, vs) => FVKval(v) ++ vs.flatMap{FVKval}.toSet
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
  case KIf0(v, e1, e2) => FVKval(v) ++ FVKexp(e1) ++ FVKexp(e2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
  case KHalt(v) => FVKval(v)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
  case KLet(x, v, e) => FVKval(v) ++ (FVKexp(e) - x) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
  case KLetProj(x, i, v, e) => FVKval(v) ++ (FVKexp(e) - x)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
  case KLetPrimOp(x, v1, p, v2, e) => (FVKexp(e) - x) ++ FVKval(v1) ++ FVKval(v2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
abstract class CTerm 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
abstract class CVal 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
case class CApp(v : CVal, vs : List[CVal]) extends CTerm  {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
  override def toString = v + "(" + commas(vs.map(_.toString)) + ")"
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
case class CIf0(v : CVal, e1 : CTerm, e2 : CTerm) extends CTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
  override def toString = "if0(" + v + "," + e1 + "," + e2 + ")"
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
case class CHalt(v : CVal) extends CTerm { override def toString = "halt " + v }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
case class CLet(x : Idn, v : CVal, e : CTerm) extends CTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
  override def toString = "let " + x + " = " + v + " in\n" + e
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
case class CLetProj(x : Idn, i : Num, v : CVal, e : CTerm) extends CTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
  override def toString = "let " + x + " =" + i + " " + v + " in\n" + e
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
case class CLetPrimOp(x : Idn, v1 : CVal, p : Prim, v2 : CVal, e : CTerm) extends CTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
  override def toString = "let " + x + " =" + v1 + p + v2 + " in\n" + e
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
case class CVVar(x : Idn) extends CVal { override def toString = x }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
case class CVNum(i : Num) extends CVal { override def toString = i.toString }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
case class CVTuple(vs : List[CVal]) extends CVal {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
  override def toString = "<" + commas(vs.map(_.toString)) + ">"
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
case class CVFixCode(x : Idn, args : List[Idn], e : CTerm) extends CVal {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
  override def toString = "fixcode " + x + "(" + commas(args) + ")." + e 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
// closure conversion
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
def CExp(e: KTerm) : CTerm = e match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
  case KApp(v, vs) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
    val z = Fresh("z")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
    val z_code = Fresh("zcode")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
    val z_env = Fresh("zenv")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
    CLet(z, CVal(v), 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
      CLetProj(z_code, 0, CVVar(z),
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
        CLetProj(z_env, 1, CVVar(z),
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
          CApp(CVVar(z_code), CVVar(z_env) :: vs.map{CVal}))))       
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
  case KIf0(v, e1, e2) => CIf0(CVal(v), CExp(e1), CExp(e2))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
  case KHalt(v) => CHalt(CVal(v))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
  case KLet(x, v, e) => CLet(x, CVal(v), CExp(e))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
  case KLetProj(x, i, v, e) => CLetProj(x, i, CVal(v), CExp(e))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
  case KLetPrimOp(x, v1, p, v2, e) => CLetPrimOp(x, CVal(v1), p, CVal(v2), CExp(e))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
def CVal(v: KVal) : CVal = v match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
  case KVVar(x) => CVVar(x)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
  case KVNum(i) => CVNum(i)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
  case KVTuple(vs) => CVTuple(vs.map{CVal})
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
  case KVFix(x, args, e) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
    val x_env = Fresh(x + ".env")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
    val ys = FVKval(KVFix(x, args, e)).toList
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
    val ys_index = 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
      ys.zipWithIndex.foldRight(CExp(e)) {case ((x, n), e) => CLetProj(x, n, CVVar(x_env), e) } 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
    val v_code = CVFixCode(x, x_env :: args, ys_index)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
    val v_env = CVTuple(ys.map{CVVar(_)})
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
    CVTuple(List(v_code, v_env))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
abstract class HTerm
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
abstract class HVal { 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
  def eval(env: Map[Idn, HVal]) : HVal
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
case class HApp(v : HVal, vs : List[HVal]) extends HTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
  override def toString =  v + "(" + commas (vs.map(_.toString)) + ")" 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
case class HIf0(v : HVal, e1 : HTerm, e2 : HTerm) extends HTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
  override def toString = "if0(" + v + "," + e1 + "," + e2 + ")"
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
case class HHalt(v : HVal) extends HTerm { 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
  override def toString = "halt " + v 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
case class HLet(x : Idn, v : HVal, e : HTerm) extends HTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
  override def toString = "let " + x + " = " + v + " in\n" + e
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
case class HLetProj(x : Idn, i : Num, v : HVal, e : HTerm) extends HTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   275
  override def toString = "let " + x + " =" + i + " " + v + " in\n" + e
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   276
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
case class HLetPrimOp(x : Idn, v1 : HVal, p : Prim, v2 : HVal, e : HTerm) extends HTerm {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
  override def toString = "let " + x + " = " + v1 + p + v2 + " in\n" + e
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
case class HVVar(x : Idn) extends HVal { 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
  override def toString = x 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
  def eval(env: Map[Idn, HVal]) : HVal = env(x)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
case class HVNum(i : Num) extends HVal { 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   286
  override def toString = i.toString 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   287
  def eval(env: Map[Idn, HVal]) : HVal = HVNum(i)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   288
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
case class HVTuple(vs : List[HVal]) extends HVal {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
  override def toString = "<" + commas (vs.map(_.toString)) + ">"
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
  def eval(env: Map[Idn, HVal]) : HVal = HVTuple(vs.map(_.eval(env)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
case class HVLabel(l: Idn) extends HVal { 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   294
  override def toString = l 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
  def eval(env: Map[Idn, HVal]) : HVal = HVLabel(l)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
case class HBlock(args: List[Idn], term: HTerm) {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
  override def toString = "code(" + commas(args) + ").\n" + term + "\n"
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
case class HProg(blocks: Map[Idn, HBlock], term: HTerm) {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
  override def toString = "\nheap:\n" +
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
    cutlines(blocks.toList.map(_ match { case (x, y) => x + " -> " + y.toString })) +
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
      "in start:\n" + term.toString 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
  def run_block(pretty: Boolean, l: Idn, as: List[HVal], env: Map[Idn, HVal]) : Unit = {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
    val blk = blocks(l)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
    val env_prime = (blk.args zip as).toMap  
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   311
    if (pretty) println("Referenced: " + l + " -> " + blk)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   312
    run(pretty, blk.term, env ++ env_prime)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   313
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   314
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   315
  def run(pretty: Boolean, e: HTerm, env: Map[Idn, HVal]) : Unit = {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
    if (pretty) println("Env:" + env.toList.length  + " stored values)\n" + 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
                        cutlines(env.toList.sortBy {_._1}.map(_.toString)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
    if (pretty) println("Term:\n" + e.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   319
    if (pretty) Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
    e match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
      case HHalt(v) => println ("Finished with result " + v.eval(env))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
      case HApp(v, vs) => (v.eval(env), vs.map(_.eval(env))) match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
        case (HVLabel(l), vs) => run_block(pretty, l, vs, env)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
        case _ => throw new IllegalArgumentException("not a label")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
      }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   326
      case HLet(x, v, e) => run(pretty, e, env + (x -> v.eval(env)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
      case HLetProj(x, i, v, e) => v.eval(env) match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
        case HVTuple(vs) => run(pretty, e, env + (x -> vs(i)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
        case _ => throw new IllegalArgumentException("not a tuple")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
      }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
      case HLetPrimOp(x, v1, p, v2, e) => (v1.eval(env), p, v2.eval(env)) match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
        case (HVNum(m), PLUS(), HVNum(n)) =>  run(pretty, e, env + (x -> HVNum(m + n)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
        case (HVNum(m), MINUS(), HVNum(n)) => run(pretty, e, env + (x -> HVNum(m - n)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
        case (HVNum(m), MULT(), HVNum(n)) =>  run(pretty, e, env + (x -> HVNum(m * n)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
        case _ => throw new IllegalArgumentException("not a number")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   336
      }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   337
      case HIf0(v, e1, e2) => v.eval(env) match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
        case HVNum(0) => run(pretty, e1, env)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   339
        case _ => run(pretty, e2, env) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
      }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   341
    }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   343
  
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   344
  def run_prog(pretty: Boolean) = run(pretty, term, Map())
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   345
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   347
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   348
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   349
// hoisting
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   350
def H(e: CTerm, ls: Map[Idn, HVal]) : (HTerm, Map[Idn, HBlock]) = e match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
  case CApp(v, vs) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
    val (v_prime, hs) = HV(v, ls)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   353
    val (vs_prime, hss) = vs.map{HV(_, ls)}.unzip
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
    (HApp(v_prime, vs_prime), hs ++ hss.flatten)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   355
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
  case CIf0(v, e1, e2) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
    val (v_prime, hs1) = HV(v, ls)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
    val (e1_prime, hs2) = H(e1, ls)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
    val (e2_prime, hs3) = H(e2, ls)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
    (HIf0(v_prime, e1_prime, e2_prime), hs1 ++ hs2 ++ hs3)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   362
  case CHalt(v) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
    val (v_prime, hs) = HV(v, ls) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
    (HHalt(v_prime), hs)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
  case CLet(x, v, e) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
    val (v_prime, hs1) = HV(v, ls)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   368
    val (e_prime, hs2) = H(e, ls)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
    (HLet(x, v_prime, e_prime), hs1 ++ hs2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
  case CLetProj(x, i, v, e) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
    val (v_prime, hs1) = HV(v, ls)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
    val (e_prime, hs2) = H(e, ls)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
    (HLetProj(x, i, v_prime, e_prime), hs1 ++ hs2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   376
  case CLetPrimOp(x, v1, p, v2, e) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   377
    val (v1_prime, hs1) = HV(v1, ls)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   378
    val (v2_prime, hs2) = HV(v2, ls)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   379
    val (e_prime, hs3) = H(e, ls)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   380
    (HLetPrimOp(x, v1_prime, p, v2_prime, e_prime), hs1 ++ hs2 ++ hs3)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   381
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   382
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   383
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   384
def HV(v: CVal, ls: Map[Idn, HVal]) : (HVal, Map[Idn, HBlock]) = v match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   385
  case CVVar(x) => ls.get(x) match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   386
    case Some(v) => (v, Map())
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   387
    case None => (HVVar(x), Map())
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   388
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
  case CVNum(i) => (HVNum(i), Map())
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   390
  case CVTuple(vs) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   391
    val (vs_prime, hss) = vs.map{HV(_, ls)}.unzip
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   392
    (HVTuple(vs_prime), hss.flatten.toMap)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   393
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   394
  case CVFixCode(x, args, e) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   395
    val l = Fresh(x + ".block")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   396
    val (e_prime, hs) = H(e, ls + (x -> HVTuple(List(HVLabel(l), HVVar(args.head)))))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   397
    (HVLabel(l), hs + (l -> HBlock(args, e_prime)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   398
  } 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   399
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   400
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   401
def HP(e: CTerm) = {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   402
  val (e_prime, hs) = H(e, Map())
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   403
  HProg(hs, e_prime)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   404
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   405
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   406
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   407
abstract class TALVal {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   408
  def eval(regs: Map[Idn, TALVal]) : TALVal
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   409
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   410
abstract class TALInstr {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   411
  def update_regs(regs: Map[Idn, TALVal]) :  Map[Idn, TALVal] = regs
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   412
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   413
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   414
case class TALReg(r: Idn) extends TALVal { 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   415
  override def toString = r 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   416
  def eval(regs: Map[Idn, TALVal]) = regs(r).eval(regs)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   417
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   418
case class TALLabel(l: Idn) extends TALVal { 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   419
  override def toString = l 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   420
  def eval(regs: Map[Idn, TALVal]) = TALLabel(l)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   421
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   422
case class TALNum(i: Int) extends TALVal { 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   423
  override def toString = i.toString 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   424
  def eval(regs: Map[Idn, TALVal]) = TALNum(i)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   425
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   426
case class TALTuple(vs: List[TALVal]) extends TALVal {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   427
  override def toString = "<" + commas (vs.map(_.toString)) + ">"
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   428
  def eval(regs: Map[Idn, TALVal]) = TALTuple(vs.map(_.eval(regs)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   429
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   430
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   431
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   432
case class TALAdd(r1: Idn, r2: Idn, v: TALVal) extends TALInstr {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   433
  override def toString = "Add " + r1 + " " + r2 + " " + v
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   434
  override def update_regs (regs: Map[Idn, TALVal]) : Map[Idn, TALVal] = (regs(r2), v) match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   435
    case (TALNum(n), TALNum(m)) => regs + (r1 -> TALNum(n + m))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   436
    case (TALNum(n), TALReg(r3)) => regs(r3) match { 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   437
      case TALNum(m) => regs + (r1 -> TALNum (n + m))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   438
      case _ => throw new IllegalArgumentException("not a number")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   439
    }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   440
    case _ => throw new IllegalArgumentException("not a number")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   441
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   442
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   443
case class TALSub(r1: Idn, r2: Idn, v: TALVal) extends TALInstr {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   444
  override def toString = "Sub " + r1 + " " + r2 + " " + v
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   445
  override def update_regs (regs: Map[Idn, TALVal]) : Map[Idn, TALVal] = (regs(r2), v) match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   446
    case (TALNum(n), TALNum(m)) => regs + (r1 -> TALNum(n - m))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   447
    case (TALNum(n), TALReg(r3)) => regs(r3) match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   448
      case TALNum(m) => regs + (r1 -> TALNum (n - m))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   449
      case _ => throw new IllegalArgumentException("not a number")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   450
    }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   451
    case _ => throw new IllegalArgumentException("not a number")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   452
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   453
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   454
case class TALMul(r1: Idn, r2: Idn, v: TALVal) extends TALInstr {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   455
  override def toString = "Mul " + r1 + " " + r2 + " " + v
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   456
  override def update_regs (regs: Map[Idn, TALVal]) : Map[Idn, TALVal] = (regs(r2), v) match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   457
    case (TALNum(n), TALNum(m)) => regs + (r1 -> TALNum (n * m))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   458
    case (TALNum(n), TALReg(r3)) => regs(r3) match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   459
      case TALNum(m) => regs + (r1 -> TALNum (n * m))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   460
      case _ => throw new IllegalArgumentException("not a number")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   461
    }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   462
    case _ => throw new IllegalArgumentException("not a number")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   463
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   464
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   465
case class TALBnz(r: Idn, v: TALVal) extends TALInstr {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   466
  override def toString = "Bnz " + r + " " + v
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   467
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   468
case class TALMov(r: Idn, v: TALVal) extends TALInstr {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   469
  override def toString = "Mov " + r + " " + v
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   470
  override def update_regs (regs: Map[Idn, TALVal]) : Map[Idn, TALVal] = v match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   471
    case TALReg(r1) => regs + (r -> regs(r1)) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
    case _ => regs + (r -> v)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   473
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   474
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   475
case class TALProj(r1: Idn, r2: Idn, i: Num) extends TALInstr {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   476
  override def toString = "Ldi " + r1 + " <- " + r2 + "[" + i + "]" 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   477
  override def update_regs (regs: Map[Idn, TALVal]) : Map[Idn, TALVal] = { 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   478
    regs(r2) match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   479
      case TALTuple(vs) => regs + (r1 -> vs(i)) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   480
      case _ => throw new IllegalArgumentException("not a tuple: ")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   481
    }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   482
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   483
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   484
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   485
case class TALJmp(v: TALVal) extends TALInstr {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   486
  override def toString = "Jmp " + v
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   487
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   488
case class TALHalt() extends TALInstr { 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   489
  override def toString = "halt" 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   490
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   491
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   492
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   493
case class TALBlock(regs: List[Idn], cs: List[TALInstr]) {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   494
  override def toString = "code(" + commas(regs) + ").\n" + cutlines(cs.map{_.toString}) + "\n"
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   495
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   496
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   497
case class TALProg(blocks: Map[Idn, TALBlock], start: List[TALInstr]) {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
  override def toString =  "heap:\n" +
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   499
    cutlines(blocks.toList.map(_ match { case (x, y) => x + " -> " + y.toString })) +
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   500
      "start block:\n" + cutlines(start.map(_.toString)) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   501
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   502
  def run_block(pretty: Boolean, l: Idn, regs: Map[Idn, TALVal]) : Unit = {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   503
    val blk = blocks(l)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   504
    if (pretty) println("Referenced: " + l + " -> " + blk)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   505
    val regs_prime = for ((r, n) <- blk.regs.zipWithIndex) yield (r, TALReg("RArg_" + n.toString).eval(regs))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
    run(pretty, blk.cs, regs ++ regs_prime.toMap)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   507
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   508
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   509
  def run(pretty: Boolean, cs: List[TALInstr], regs: Map[Idn, TALVal]) : Unit = {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   510
    if (pretty) println("Regs:(" + regs.toList.length  + " stored values)\n" + 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   511
                        cutlines(regs.toList.sortBy {_._1}.map(_.toString)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   512
    if (pretty) println("Instrs:\n" + cutlines(cs.map(_.toString)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   513
    if (pretty) Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   514
    cs match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   515
      case (TALHalt() :: _) => println ("Finished with result " + regs("RArg_0"))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   516
      case (TALJmp(TALLabel(l)) :: _) => run_block(pretty, l, regs)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   517
      case (TALJmp(TALReg(r)) :: _) => regs(r) match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   518
        case TALLabel(l) => run_block(pretty, l, regs)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   519
        case _ => throw new IllegalArgumentException("jump to non-label")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   520
      }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   521
      case (TALBnz(r, TALLabel(l)) :: cs) => regs(r) match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   522
        case TALNum(0) => run(pretty, cs, regs)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   523
        case _ => run_block(pretty, l, regs) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   524
      }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   525
      case (c :: cs) => run(pretty, cs, c.update_regs(regs))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   526
      case Nil => throw new IllegalArgumentException("no instruction left")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   527
    }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   528
  } 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   529
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   530
  def run_prog(pretty: Boolean) : Unit = run(pretty, start, Map())
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   531
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   533
def mk_vreg(s: String) = "Reg_" + s
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
def mk_areg(i: Int) = "RArg_" + i.toString
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
  
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   537
def Tval(v: HVal) : TALVal = v match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   538
  case HVVar(x) => TALReg("Reg_" + x)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   539
  case HVNum(i) => TALNum(i)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   540
  case HVTuple(vs) => TALTuple(vs.map(Tval)) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   541
  case HVLabel(l) => TALLabel(l)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   542
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   543
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   544
def Texp(e: HTerm) : (Map[Idn, TALBlock], List[TALInstr]) = e match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   545
  case HApp(v, vs) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
    val fr_reg0 = Fresh("r0") 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   547
    val fr_regs = vs.map((v) => Fresh("r"))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   548
    val movs0 = List(TALMov(fr_reg0, Tval(v)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   549
    val movs1 = for ((r, v) <- (fr_regs zip vs)) yield TALMov(r, Tval(v))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
    val movs2 = for ((r, i) <- fr_regs.zipWithIndex) yield TALMov(mk_areg(i), TALReg(r))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
    val movs3 = List(TALJmp(TALReg(fr_reg0))) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
    (Map(), movs0 ::: movs1 ::: movs2 ::: movs3)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
  case HIf0(v, e1, e2) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   555
    val l = Fresh("else_branch")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   556
    val r = Fresh("r")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   557
    val (h1, comp1) = Texp(e1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   558
    val (h2, comp2) = Texp(e2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
    val code = List(TALMov(r, Tval(v)), TALBnz(r, TALLabel(l)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   560
    val h3 = TALBlock(Nil, comp2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   561
    (h1 ++ h2 + (l -> h3), code ::: comp1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   563
  case HHalt(v) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
    (Map(), List(TALMov(mk_areg(0), Tval(v)), TALHalt()))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   565
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
  case HLet(x, v, e) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
    val (h, comp) = Texp(e)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   568
    val code = TALMov(mk_vreg(x), Tval(v))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   569
    (h, code :: comp)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
  case HLetPrimOp(x, v1, PLUS(), v2, e) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
    val r = mk_vreg(x)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
    val (h, comp) = Texp(e)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   574
    val code = List(TALMov(r, Tval(v1)), TALAdd(r, r, Tval(v2)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
    (h, code ::: comp)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
  case HLetPrimOp(x, v1, MINUS(), v2, e) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
    val r = mk_vreg(x)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   579
    val (h, comp) = Texp(e)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   580
    val code = List(TALMov(r, Tval(v1)), TALSub(r, r, Tval(v2)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   581
    (h, code ::: comp)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   582
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   583
  case HLetPrimOp(x, v1, MULT(), v2, e) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   584
    val r = mk_vreg(x)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
    val (h, comp) = Texp(e)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   586
    val code = List(TALMov(r, Tval(v1)), TALMul(r, r, Tval(v2)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   587
    (h, code ::: comp)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   588
  }  
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   589
  case HLetProj(x, i, v, e) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   590
    val r = mk_vreg(x)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
    val (h, comp) = Texp(e)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   592
    val code = List(TALMov(r, Tval(v)), TALProj(r, r, i))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   593
    (h, code ::: comp)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   594
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   595
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   596
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   597
def Tblock(l: Idn, hb: HBlock) : List[(Idn, TALBlock)] = hb match {  
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   598
  case HBlock(args, e) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   599
    val (h, comp) = Texp(e)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   600
    (l, TALBlock(args.map("Reg_" + _), comp)) :: h.toList
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   601
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   602
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   603
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   604
def Tprog(prog: HProg) = prog match {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   605
  case HProg(heaps, e) => {
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   606
    val (hs, comp) = Texp(e)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   607
    val heap_prime = for ((l, hb) <- heaps) yield Tblock(l, hb)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   608
    TALProg(hs ++ heap_prime.flatten.toMap, comp)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   609
  }
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   610
}
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   611
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   612
/* Simple examples
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   613
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   614
// tuple <1,2,3>
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   615
val prog0 = FTuple(List(FNum(1), FNum(2), FNum(3)))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   616
val Kresult0 = CPS(prog0)((y:KVal) => KHalt(y))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   617
val Cresult0 = CExp(Kresult0)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   618
val Hresult0 = HP(Cresult0)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   619
val Tresult0 = Tprog(Hresult0)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   620
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   621
println("tuple example: \n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   622
println("F: " + prog0.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   623
println("K: " + Kresult0.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   624
println("C: " + Cresult0.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   625
println("H: " + Hresult0.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   626
println("T: " + Tresult0.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   627
Tresult0.run_prog(true)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   628
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   629
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   630
// tuple Proj 1 <1,2,3>
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   631
val prog1 = FProj(1, FTuple(List(FNum(1), FNum(2), FNum(3))))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   632
val Kresult1 = CPS(prog1)((y:KVal) => KHalt(y))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   633
val Cresult1 = CExp(Kresult1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   634
val Hresult1 = HP(Cresult1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   635
val Tresult1 = Tprog(Hresult1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   636
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   637
println("tuple - proj: \n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   638
println("F: " + prog1.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   639
println("K: " + Kresult1.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   640
println("C: " + Cresult1.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   641
println("H: " + Hresult1.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   642
println("T: " + Tresult1.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   643
Tresult1.run_prog(true)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   644
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   645
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   646
// 3 + 4
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   647
val prog2 = FPrimOp(FNum(3),PLUS(),FNum(4))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   648
val Kresult2 = CPS(prog2)((y:KVal) => KHalt(y))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   649
val Cresult2 = CExp(Kresult2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   650
val Hresult2 = HP(Cresult2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   651
val Tresult2 = Tprog(Hresult2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   652
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   653
println("3 + 4: \n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   654
println("F: " + prog2.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   655
println("K: " + Kresult2.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   656
println("C: " + Cresult2.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   657
println("H: " + Hresult2.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   658
println("T: " + Tresult2.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   659
Tresult2.run_prog(true)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   660
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   661
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   662
// (fix f(x). 18 * x + 32) 24
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   663
val d1 = FPrimOp(FPrimOp(FNum(18), MULT(), FVar("x")), PLUS(), FNum(32))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   664
val prog3 = FApp(FFix("f", "x", d1), FNum(24))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   665
val Kresult3 = CPS(prog3)((y:KVal) => KHalt(y))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   666
val Cresult3 = CExp(Kresult3)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   667
val Hresult3 = HP(Cresult3)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   668
val Tresult3 = Tprog(Hresult3)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   669
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   670
println("(fix f(x). 18 * x + 32) 24 \n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   671
println("F: " + prog3.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   672
println("K: " + Kresult3.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   673
println("C: " + Cresult3.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   674
println("H: " + Hresult3.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   675
Hresult3.run_prog(true)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   676
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   677
println("T: " + Tresult3.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   678
Tresult3.run_prog(true)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   679
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   680
*/
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   681
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   682
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   683
/*
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   684
// twice-apply example
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   685
// fix twice(f)._(x). f (f x)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   686
val fun = FFix("id", "x", FPrimOp(FVar("x"), MULT(), FVar("x")))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   687
val ffx = FApp(FVar("f"), FApp(FVar("f"), FVar("x")))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   688
val f1 = FFix("twice", "f", FFix("twicef", "x", ffx))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   689
val prog4 = FApp(FApp(f1, fun), FNum(2))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   690
val Kresult4 = CPS(prog4)((y:KVal) => KHalt(y))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   691
val Cresult4 = CExp(Kresult4)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   692
val Hresult4 = HP(Cresult4)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   693
val Tresult4 = Tprog(Hresult4)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   694
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   695
println("twice fun 2\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   696
println("F: " + prog4.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   697
println("K: " + Kresult4.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   698
println("C: " + Cresult4.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   699
println("H: " + Hresult4.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   700
//Hresult4.run_prog(false)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   701
//Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   702
println("T: " + Tresult4.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   703
Tresult4.run_prog(false)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   704
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   705
*/
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   706
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   707
/*
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   708
//identity function
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   709
//fix id(x). x
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   710
val id = FFix("id", "x", FVar("x"))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   711
val id_three = FApp(id, FNum(3))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   712
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   713
val Kresult5 = CPS(id_three)((y:KVal) => KHalt(y))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   714
val Cresult5 = CExp(Kresult5)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   715
val Hresult5 = HP(Cresult5)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   716
val Tresult5 = Tprog(Hresult5)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   717
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   718
println("id 3:")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   719
println("F: " + id_three.toString)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   720
println("K: " + Kresult5.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   721
println("C: " + Cresult5.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   722
println("H: " + Hresult5.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   723
println("T: " + Tresult5.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   724
Tresult5.run_prog(false)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   725
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   726
*/
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   727
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   728
/*
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   729
//example: factorial
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   730
val f = FVar("f")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   731
val n = FVar("n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   732
val one = FNum(1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   733
val six = FNum(15)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   734
val e0 = FPrimOp(n, MINUS(), one)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   735
val e1 = FApp(f, e0)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   736
val e2 = FPrimOp(n, MULT(), e1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   737
val fact: FTerm = FFix("f", "n", FIf0(n, one, e2))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   738
val fact_six = FApp(fact, six)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   739
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   740
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   741
val Kresult6 = CPS(fact_six)((y:KVal) => KHalt(y))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   742
val Cresult6 = CExp(Kresult6)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   743
val Hresult6 = HP(Cresult6)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   744
val Tresult6 = Tprog(Hresult6)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   745
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   746
println("fact 6: \n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   747
println("F: " + fact_six)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   748
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   749
println("K: " + Kresult6.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   750
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   751
println("C: " + Cresult6.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   752
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   753
println("H: " + Hresult6.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   754
println("-----------------------------")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   755
println("Execution")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   756
Hresult6.run_prog(false)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   757
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   758
println("T: " + Tresult6.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   759
println("-----------------------------")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   760
println("Execution")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   761
Tresult6.run_prog(false)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   762
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   763
*/
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   764
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   765
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   766
//example: fibonacci
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   767
// fib(n).if0(n, 1,if0(n - 1, 1, fib(n - 1) + fib(n - 2))) 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   768
val fib = FVar("fib")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   769
val n = FVar("n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   770
val one = FNum(1)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   771
val two = FNum(2)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   772
val minus_one = FPrimOp(n, MINUS(), one)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   773
val minus_two = FPrimOp(n, MINUS(), two)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   774
val fibonacci = 
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   775
          FFix("fib", "n",
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   776
            FIf0(n, one,
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   777
              FIf0(minus_one, one, FPrimOp(FApp(fib, minus_one), PLUS(), FApp(fib, minus_two)))))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   778
val fib_apply = FApp(fibonacci, FNum(4))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   779
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   780
val Kresult7 = CPS(fib_apply)((y:KVal) => KHalt(y))
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   781
val Cresult7 = CExp(Kresult7)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   782
val Hresult7 = HP(Cresult7)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   783
val Tresult7 = Tprog(Hresult7)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   784
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   785
println("fib: \n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   786
println("F: " + fib_apply)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   787
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   788
println("K: " + Kresult7.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   789
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   790
println("C: " + Cresult7.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   791
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   792
println("H: " + Hresult7.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   793
println("-----------------------------")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   794
println("H Prog")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   795
Hresult7.run_prog(false)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   796
Console.readLine
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   797
println("T: " + Tresult7.toString + "\n")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   798
println("-----------------------------")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   799
println("TAL")
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   800
Tresult7.run_prog(false)
8e633f1d0d38 added file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   801
Console.readLine