progs/funt.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 28 Jul 2019 17:29:53 +0100
changeset 626 2d91b2107656
parent 609 e33545bb2eba
child 628 8067d0a8ba04
permissions -rw-r--r--
updated to 2.13
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
     1
// A Small Compiler for a Simple Functional Language
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
     2
// (includes a lexer and a parser)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
     3
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
import scala.language.implicitConversions    
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
import scala.language.reflectiveCalls 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
abstract class Rexp 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
     8
case object ZERO extends Rexp
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
     9
case object ONE extends Rexp
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
case class CHAR(c: Char) extends Rexp
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
case class STAR(r: Rexp) extends Rexp 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    14
case class RECD(x: String, r: Rexp) extends Rexp
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    15
  
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    16
abstract class Val
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    17
case object Empty extends Val
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    18
case class Chr(c: Char) extends Val
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    19
case class Sequ(v1: Val, v2: Val) extends Val
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    20
case class Left(v: Val) extends Val
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    21
case class Right(v: Val) extends Val
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    22
case class Stars(vs: List[Val]) extends Val
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    23
case class Rec(x: String, v: Val) extends Val
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    24
   
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
// some convenience for typing in regular expressions
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    26
def charlist2rexp(s : List[Char]): Rexp = s match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    27
  case Nil => ONE
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  case c::Nil => CHAR(c)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
}
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    31
implicit def string2rexp(s : String) : Rexp = 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    32
  charlist2rexp(s.toList)
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    34
implicit def RexpOps(r: Rexp) = new {
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  def | (s: Rexp) = ALT(r, s)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  def % = STAR(r)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  def ~ (s: Rexp) = SEQ(r, s)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    40
implicit def stringOps(s: String) = new {
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  def | (r: Rexp) = ALT(s, r)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  def | (r: String) = ALT(s, r)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
  def % = STAR(s)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  def ~ (r: Rexp) = SEQ(s, r)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  def ~ (r: String) = SEQ(s, r)
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    46
  def $ (r: Rexp) = RECD(s, r)
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
def nullable (r: Rexp) : Boolean = r match {
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    50
  case ZERO => false
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    51
  case ONE => true
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  case CHAR(_) => false
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  case STAR(_) => true
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    56
  case RECD(_, r1) => nullable(r1)
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
def der (c: Char, r: Rexp) : Rexp = r match {
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    60
  case ZERO => ZERO
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    61
  case ONE => ZERO
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    62
  case CHAR(d) => if (c == d) ONE else ZERO
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  case SEQ(r1, r2) => 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
    else SEQ(der(c, r1), r2)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
  case STAR(r) => SEQ(der(c, r), STAR(r))
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    68
  case RECD(_, r1) => der(c, r1)
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    72
// extracts a string from value
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    73
def flatten(v: Val) : String = v match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    74
  case Empty => ""
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    75
  case Chr(c) => c.toString
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    76
  case Left(v) => flatten(v)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    77
  case Right(v) => flatten(v)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    78
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    79
  case Stars(vs) => vs.map(flatten).mkString
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    80
  case Rec(_, v) => flatten(v)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    81
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    82
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    83
// extracts an environment from a value;
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    84
// used for tokenise a string
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    85
def env(v: Val) : List[(String, String)] = v match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    86
  case Empty => Nil
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    87
  case Chr(c) => Nil
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    88
  case Left(v) => env(v)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    89
  case Right(v) => env(v)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    90
  case Sequ(v1, v2) => env(v1) ::: env(v2)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    91
  case Stars(vs) => vs.flatMap(env)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    92
  case Rec(x, v) => (x, flatten(v))::env(v)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    93
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    94
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    95
// The Injection Part of the lexer
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    96
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    97
def mkeps(r: Rexp) : Val = r match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    98
  case ONE => Empty
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    99
  case ALT(r1, r2) => 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   100
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   101
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   102
  case STAR(r) => Stars(Nil)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   103
  case RECD(x, r) => Rec(x, mkeps(r))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   104
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   105
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   106
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   107
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   108
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   109
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   110
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   111
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   112
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   113
  case (CHAR(d), Empty) => Chr(c) 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   114
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   115
  case _ => { println ("Injection error") ; sys.exit(-1) } 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   116
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   117
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   118
// some "rectification" functions for simplification
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   119
def F_ID(v: Val): Val = v
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   120
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   121
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   122
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   123
  case Right(v) => Right(f2(v))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   124
  case Left(v) => Left(f1(v))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   125
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   126
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   127
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   128
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   129
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   130
  (v:Val) => Sequ(f1(Empty), f2(v))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   131
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   132
  (v:Val) => Sequ(f1(v), f2(Empty))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   133
def F_RECD(f: Val => Val) = (v:Val) => v match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   134
  case Rec(x, v) => Rec(x, f(v))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   135
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   136
def F_ERROR(v: Val): Val = throw new Exception("error")
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   137
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   138
def simp(r: Rexp): (Rexp, Val => Val) = r match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   139
  case ALT(r1, r2) => {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   140
    val (r1s, f1s) = simp(r1)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   141
    val (r2s, f2s) = simp(r2)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   142
    (r1s, r2s) match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   143
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   144
      case (_, ZERO) => (r1s, F_LEFT(f1s))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   145
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   146
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   147
    }
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   148
  }
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   149
  case SEQ(r1, r2) => {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   150
    val (r1s, f1s) = simp(r1)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   151
    val (r2s, f2s) = simp(r2)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   152
    (r1s, r2s) match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   153
      case (ZERO, _) => (ZERO, F_ERROR)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   154
      case (_, ZERO) => (ZERO, F_ERROR)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   155
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   156
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   157
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   158
    }
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   159
  }
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   160
  case RECD(x, r1) => {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   161
    val (r1s, f1s) = simp(r1)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   162
    (RECD(x, r1s), F_RECD(f1s))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   163
  }
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   164
  case r => (r, F_ID)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   165
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   166
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   167
// lexing functions including simplification
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   168
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   169
  case Nil => if (nullable(r)) mkeps(r) else { println ("Lexing Error") ; sys.exit(-1) } 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   170
  case c::cs => {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   171
    val (r_simp, f_simp) = simp(der(c, r))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   172
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   173
  }
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   174
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   175
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   176
def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   177
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   178
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   179
// The Lexing Rules for the Fun Language
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   180
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   181
def PLUS(r: Rexp) = r ~ r.%
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   182
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   183
val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   184
          "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   185
          "w" | "x" | "y" | "z" | "T" | "_"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   186
val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
val ID = SYM ~ (SYM | DIGIT).% 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
val NUM = PLUS(DIGIT)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
val KEYWORD : Rexp = "if" | "then" | "else" | "write" | "def"
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
val SEMI: Rexp = ";"
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   191
val OP: Rexp = "=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
val WHITESPACE = PLUS(" " | "\n" | "\t")
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
val RPAREN: Rexp = ")"
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
val LPAREN: Rexp = "("
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   195
val COMMA: Rexp = ","
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
val ALL = SYM | DIGIT | OP | " " | ":" | ";" | "\"" | "=" | "," | "(" | ")"
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
val ALL2 = ALL | "\n"
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
val COMMENT = ("/*" ~ ALL2.% ~ "*/") | ("//" ~ ALL.% ~ "\n")
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   201
val WHILE_REGS = (("k" $ KEYWORD) | 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   202
                  ("i" $ ID) | 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   203
                  ("o" $ OP) | 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   204
                  ("n" $ NUM) | 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   205
                  ("s" $ SEMI) | 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   206
                  ("c" $ COMMA) |
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   207
                  ("pl" $ LPAREN) |
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   208
                  ("pr" $ RPAREN) |
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   209
                  ("w" $ (WHITESPACE | COMMENT))).%
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   210
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   211
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   212
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   213
// The tokens for the Fun language
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   214
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
abstract class Token
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
case object T_SEMI extends Token
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   217
case object T_COMMA extends Token
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
case object T_LPAREN extends Token
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
case object T_RPAREN extends Token
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
case class T_ID(s: String) extends Token
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
case class T_OP(s: String) extends Token
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   222
case class T_NUM(n: Int) extends Token
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
case class T_KWD(s: String) extends Token
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   225
val token : PartialFunction[(String, String), Token] = {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   226
  case ("k", s) => T_KWD(s)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   227
  case ("i", s) => T_ID(s)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   228
  case ("o", s) => T_OP(s)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   229
  case ("n", s) => T_NUM(s.toInt)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   230
  case ("s", _) => T_SEMI
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   231
  case ("c", _) => T_COMMA
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   232
  case ("pl", _) => T_LPAREN
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   233
  case ("pr", _) => T_RPAREN
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   234
}
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   237
def tokenise(s: String) : List[Token] = 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   238
  lexing_simp(WHILE_REGS, s).collect(token)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   239
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   240
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   242
// Parser combinators
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   243
abstract class Parser[I, T](implicit ev: I => Seq[_]) {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   244
  def parse(ts: I): Set[(T, I)]
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   245
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   246
  def parse_all(ts: I) : Set[T] =
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   247
    for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   248
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   249
  def parse_single(ts: I) : T = parse_all(ts).toList match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   250
    case List(t) => t
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   251
    case _ => { println ("Parse Error\n") ; sys.exit(-1) }
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   255
class SeqParser[I, T, S](p: => Parser[I, T], 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   256
                         q: => Parser[I, S])(implicit ev: I => Seq[_]) extends Parser[I, (T, S)] {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   257
  def parse(sb: I) = 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   258
    for ((head1, tail1) <- p.parse(sb); 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   259
         (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   260
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   261
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   262
class AltParser[I, T](p: => Parser[I, T], 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   263
                      q: => Parser[I, T])(implicit ev: I => Seq[_]) extends Parser[I, T] {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   264
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   265
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   266
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   267
class FunParser[I, T, S](p: => Parser[I, T], 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   268
                         f: T => S)(implicit ev: I => Seq[_]) extends Parser[I, S] {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   269
  def parse(sb: I) = 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   270
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   271
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   272
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   273
implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = new {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   274
  def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   275
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   276
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   277
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   278
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   279
def ListParser[I, T, S](p: => Parser[I, T], 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   280
                        q: => Parser[I, S])(implicit ev: I => Seq[_]): Parser[I, List[T]] = {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   281
  (p ~ q ~ ListParser(p, q)) ==> { case ((x, y), z) => x :: z : List[T] } ||
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   282
  (p ==> ((s) => List(s)))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   283
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   284
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   285
case class TokParser(tok: Token) extends Parser[List[Token], Token] {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   286
  def parse(ts: List[Token]) = ts match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   287
    case t::ts if (t == tok) => Set((t, ts)) 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   288
    case _ => Set ()
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   289
  }
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   290
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   291
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   292
implicit def token2tparser(t: Token) = TokParser(t)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   293
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   294
implicit def TokOps(t: Token) = new {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   295
  def || (q : => Parser[List[Token], Token]) = new AltParser[List[Token], Token](t, q)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   296
  def ==>[S] (f: => Token => S) = new FunParser[List[Token], Token, S](t, f)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   297
  def ~[S](q : => Parser[List[Token], S]) = new SeqParser[List[Token], Token, S](t, q)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   298
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   299
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   300
case object NumParser extends Parser[List[Token], Int] {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   301
  def parse(ts: List[Token]) = ts match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   302
    case T_NUM(n)::ts => Set((n, ts)) 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   303
    case _ => Set ()
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   304
  }
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   305
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   306
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   307
case object IdParser extends Parser[List[Token], String] {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   308
  def parse(ts: List[Token]) = ts match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   309
    case T_ID(s)::ts => Set((s, ts)) 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   310
    case _ => Set ()
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   311
  }
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   312
}
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   313
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   314
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   315
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   316
// Abstract syntax trees for Fun
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   317
abstract class Exp
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   318
abstract class BExp 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   319
abstract class Decl
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   320
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   321
case class Def(name: String, args: List[String], body: Exp) extends Decl
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   322
case class Main(e: Exp) extends Decl
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   323
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   324
case class Call(name: String, args: List[Exp]) extends Exp
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   325
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   326
case class Write(e: Exp) extends Exp
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   327
case class Var(s: String) extends Exp
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   328
case class Num(i: Int) extends Exp
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   329
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   330
case class Sequence(e1: Exp, e2: Exp) extends Exp
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   331
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   332
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   333
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   334
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   335
// Grammar Rules for Fun
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   336
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   337
// arithmetic expressions
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   338
lazy val Exp: Parser[List[Token], Exp] = 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   339
  (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Exp ~ T_KWD("else") ~ Exp) ==>
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   340
    { case (((((x, y), z), u), v), w) => If(y, u, w): Exp } ||
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   341
  (M ~ T_SEMI ~ Exp) ==> { case ((x, y), z) => Sequence(x, z): Exp } || M
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   342
lazy val M: Parser[List[Token], Exp] =
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   343
  (T_KWD("write") ~ L) ==> { case (x, y) => Write(y): Exp } || L
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   344
lazy val L: Parser[List[Token], Exp] = 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   345
  (T ~ T_OP("+") ~ Exp) ==> { case ((x, y), z) => Aop("+", x, z): Exp } ||
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   346
  (T ~ T_OP("-") ~ Exp) ==> { case ((x, y), z) => Aop("-", x, z): Exp } || T  
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   347
lazy val T: Parser[List[Token], Exp] = 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   348
  (F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => Aop("*", x, z): Exp } || 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   349
  (F ~ T_OP("/") ~ T) ==> { case ((x, y), z) => Aop("/", x, z): Exp } || 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   350
  (F ~ T_OP("%") ~ T) ==> { case ((x, y), z) => Aop("%", x, z): Exp } || F
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   351
lazy val F: Parser[List[Token], Exp] = 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   352
  (IdParser ~ T_LPAREN ~ ListParser(Exp, T_COMMA) ~ T_RPAREN) ==> 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   353
    { case (((x, y), z), w) => Call(x, z): Exp } ||
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   354
  (T_LPAREN ~ Exp ~ T_RPAREN) ==> { case ((x, y), z) => y: Exp } || 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   355
  IdParser ==> { case x => Var(x): Exp } || 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   356
  NumParser ==> { case x => Num(x): Exp }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   357
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   358
// boolean expressions
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   359
lazy val BExp: Parser[List[Token], BExp] = 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   360
  (Exp ~ T_OP("==") ~ Exp) ==> { case ((x, y), z) => Bop("==", x, z): BExp } || 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   361
  (Exp ~ T_OP("!=") ~ Exp) ==> { case ((x, y), z) => Bop("!=", x, z): BExp } || 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   362
  (Exp ~ T_OP("<") ~ Exp) ==> { case ((x, y), z) => Bop("<", x, z): BExp } || 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   363
  (Exp ~ T_OP(">") ~ Exp) ==> { case ((x, y), z) => Bop("<", z, x): BExp } || 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   364
  (Exp ~ T_OP("<=") ~ Exp) ==> { case ((x, y), z) => Bop("<=", x, z): BExp } || 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   365
  (Exp ~ T_OP("=>") ~ Exp) ==> { case ((x, y), z) => Bop("<=", z, x): BExp }  
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   366
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   367
lazy val Defn: Parser[List[Token], Decl] =
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   368
   (T_KWD("def") ~ IdParser ~ T_LPAREN ~ ListParser(IdParser, T_COMMA) ~ T_RPAREN ~ T_OP("=") ~ Exp) ==>
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   369
     { case ((((((x, y), z), w), u), v), r) => Def(y, w, r): Decl }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   370
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   371
lazy val Prog: Parser[List[Token], List[Decl]] =
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   372
  (Defn ~ T_SEMI ~ Prog) ==> { case ((x, y), z) => x :: z : List[Decl] } ||
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   373
  (Exp ==> ((s) => List(Main(s)) : List[Decl]))
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   374
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   375
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   376
// compiler - built-in functions 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   377
// copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   378
//
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   379
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   380
val library = """
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   381
.class public XXX.XXX
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   382
.super java/lang/Object
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   383
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   384
.method public <init>()V
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   385
        aload_0
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   386
        invokenonvirtual java/lang/Object/<init>()V
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   387
        return
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   388
.end method
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   389
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   390
.method public static write(I)V 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   391
        .limit locals 1 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   392
        .limit stack 2 
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   393
        getstatic java/lang/System/out Ljava/io/PrintStream; 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   394
        iload 0
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   395
        invokevirtual java/io/PrintStream/println(I)V 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   396
        return 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   397
.end method
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   398
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   399
"""
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   400
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   401
// calculating the maximal needed stack size
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   402
def max_stack_exp(e: Exp): Int = e match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   403
  case Call(_, args) => args.map(max_stack_exp).sum
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   404
  case If(a, e1, e2) => max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   405
  case Write(e) => max_stack_exp(e) + 1
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   406
  case Var(_) => 1
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   407
  case Num(_) => 1
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   408
  case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   409
  case Sequence(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   410
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   411
def max_stack_bexp(e: BExp): Int = e match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   412
  case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   413
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   414
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   415
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   416
// for generating new labels
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   417
var counter = -1
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   418
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   419
def Fresh(x: String) = {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   420
  counter += 1
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   421
  x ++ "_" ++ counter.toString()
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   422
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   423
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   424
// convenient string interpolations 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   425
// for instructions, labels and methods
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   426
import scala.language.implicitConversions
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   427
import scala.language.reflectiveCalls
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   428
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   429
implicit def sring_inters(sc: StringContext) = new {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   430
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   431
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   432
    def m(args: Any*): String = sc.s(args:_*) ++ "\n"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   433
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   434
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   435
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   436
type Env = Map[String, Int]
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   437
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   438
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   439
def compile_expT(a: Exp, env : Env, name: String) : String = a match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   440
  case Num(i) => i"ldc $i"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   441
  case Var(s) => i"iload ${env(s)}"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   442
  case Aop("+", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"iadd"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   443
  case Aop("-", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"isub"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   444
  case Aop("*", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"imul"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   445
  case Aop("/", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"idiv"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   446
  case Aop("%", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"irem"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   447
  case If(b, a1, a2) => {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   448
    val if_else = Fresh("If_else")
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   449
    val if_end = Fresh("If_end")
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   450
    compile_bexpT(b, env, if_else) ++
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   451
    compile_expT(a1, env, name) ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   452
    i"goto $if_end" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   453
    l"$if_else" ++
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   454
    compile_expT(a2, env, name) ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   455
    l"$if_end"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   456
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   457
  case Call(n, args) => if (name == n) { 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   458
    val stores = args.zipWithIndex.map { case (x, y) => i"istore $y" } 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   459
    args.map(a => compile_expT(a, env, "")).mkString ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   460
    stores.reverse.mkString ++ 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   461
    i"goto ${n}_Start" 
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   462
  } else {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   463
    val is = "I" * args.length
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   464
    args.map(a => compile_expT(a, env, "")).mkString ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   465
    i"invokestatic XXX/XXX/${n}(${is})I"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   466
  }
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   467
  case Sequence(a1, a2) => {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   468
    compile_expT(a1, env, "") ++ i"pop" ++ compile_expT(a2, env, name)
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   469
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   470
  case Write(a1) => {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   471
    compile_expT(a1, env, "") ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   472
    i"dup" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   473
    i"invokestatic XXX/XXX/write(I)V"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   474
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   475
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   476
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   477
def compile_bexpT(b: BExp, env : Env, jmp: String) : String = b match {
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   478
  case Bop("==", a1, a2) => 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   479
    compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"if_icmpne $jmp"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   480
  case Bop("!=", a1, a2) => 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   481
    compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"if_icmpeq $jmp"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   482
  case Bop("<", a1, a2) => 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   483
    compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"if_icmpge $jmp"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   484
  case Bop("<=", a1, a2) => 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   485
    compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"if_icmpgt $jmp"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   486
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   487
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   488
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   489
def compile_decl(d: Decl) : String = d match {
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   490
  case Def(name, args, a) => { 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   491
    val env = args.zipWithIndex.toMap
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   492
    val is = "I" * args.length
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   493
    m".method public static $name($is)I" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   494
    m".limit locals ${args.length}" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   495
    m".limit stack ${1 + max_stack_exp(a)}" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   496
    l"${name}_Start" ++   
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   497
    compile_expT(a, env, name) ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   498
    i"ireturn" ++ 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   499
    m".end method\n"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   500
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   501
  case Main(a) => {
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   502
    m".method public static main([Ljava/lang/String;)V" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   503
    m".limit locals 200" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   504
    m".limit stack 200" ++
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   505
    compile_expT(a, Map(), "") ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   506
    i"invokestatic XXX/XXX/write(I)V" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   507
    i"return\n" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   508
    m".end method\n"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   509
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   510
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   511
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   512
// main compiler functions
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   513
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   514
def time_needed[T](i: Int, code: => T) = {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   515
  val start = System.nanoTime()
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   516
  for (j <- 1 to i) code
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   517
  val end = System.nanoTime()
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   518
  (end - start)/(i * 1.0e9)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   519
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   520
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   521
def compile(class_name: String, input: String) : String = {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   522
  val tks = tokenise(input)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   523
  val ast = Prog.parse_single(tks)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   524
  val instructions = ast.map(compile_decl).mkString
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   525
  (library + instructions).replaceAllLiterally("XXX", class_name)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   526
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   527
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   528
def compile_file(class_name: String) = {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   529
  val input = io.Source.fromFile(s"${class_name}.fun").mkString
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   530
  val output = compile(class_name, input)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   531
  scala.tools.nsc.io.File(s"${class_name}.j").writeAll(output)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   532
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   533
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   534
import scala.sys.process._
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   535
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   536
def compile_run(class_name: String) : Unit = {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   537
  compile_file(class_name)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   538
  (s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!!
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   539
  println("Time: " + time_needed(2, (s"java ${class_name}/${class_name}").!))
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   540
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   542
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   543
//examples
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   544
compile_run("defs")
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   545
compile_run("fact")
548
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   546
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   547
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   548
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   549
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   550
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   551
// a problem with the parser 
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 548
diff changeset
   552
/*
548
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   553
val text = "(((((1)))))"
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   554
val tokens = tokenise(text)
548
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   555
println(tokens)
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   556
val ast = Prog.parse_single(tokens)
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   557
println(ast)
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   558
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   559
Exp.parse_single(tokens)
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   560
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   561
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   562
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   563
val text = "((((1))))"
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   564
val tokens = tokenise(text)
548
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   565
println(tokens)
c6ba1d17aaf3 updated
Christian Urban <urbanc@in.tum.de>
parents: 541
diff changeset
   566
Exp.parse(tokens)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 548
diff changeset
   567
*/