progs/funt.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 20 Nov 2019 17:10:03 +0000
changeset 695 484b74bc057e
parent 628 8067d0a8ba04
permissions -rw-r--r--
deleted init function from boilerplate code
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
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   255
// convenience for matching later on
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   256
case class ~[+A, +B](_1: A, _2: B)
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   257
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   258
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   259
class SeqParser[I, T, S](p: => Parser[I, T], 
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   260
                         q: => Parser[I, S])(implicit ev: I => Seq[_]) extends Parser[I, ~[T, S]] {
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   261
  def parse(sb: I) = 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   262
    for ((head1, tail1) <- p.parse(sb); 
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   263
         (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   264
}
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
class AltParser[I, T](p: => Parser[I, T], 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   267
                      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
   268
  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
   269
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   270
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   271
class FunParser[I, T, S](p: => Parser[I, T], 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   272
                         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
   273
  def parse(sb: I) = 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   274
    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
   275
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   276
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   277
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
   278
  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
   279
  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
   280
  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
   281
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   282
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   283
def ListParser[I, T, S](p: => Parser[I, T], 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   284
                        q: => Parser[I, S])(implicit ev: I => Seq[_]): Parser[I, List[T]] = {
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   285
  (p ~ q ~ ListParser(p, q)) ==> { case x ~ _ ~ z => x :: z : List[T] } ||
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   286
  (p ==> ((s) => List(s)))
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   287
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   288
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   289
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
   290
  def parse(ts: List[Token]) = ts match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   291
    case t::ts if (t == tok) => Set((t, ts)) 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   292
    case _ => Set ()
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
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   295
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   296
implicit def token2tparser(t: Token) = TokParser(t)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   297
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   298
implicit def TokOps(t: Token) = new {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   299
  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
   300
  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
   301
  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
   302
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   303
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   304
case object NumParser extends Parser[List[Token], Int] {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   305
  def parse(ts: List[Token]) = ts match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   306
    case T_NUM(n)::ts => Set((n, ts)) 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   307
    case _ => Set ()
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   308
  }
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   309
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   310
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   311
case object IdParser extends Parser[List[Token], String] {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   312
  def parse(ts: List[Token]) = ts match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   313
    case T_ID(s)::ts => Set((s, ts)) 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   314
    case _ => Set ()
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   315
  }
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   316
}
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   317
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   318
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   319
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   320
// Abstract syntax trees for Fun
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   321
abstract class Exp
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   322
abstract class BExp 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   323
abstract class Decl
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   324
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   325
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
   326
case class Main(e: Exp) extends Decl
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   327
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   328
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
   329
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
   330
case class Write(e: Exp) extends Exp
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   331
case class Var(s: String) extends Exp
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   332
case class Num(i: Int) extends Exp
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   333
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
   334
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
   335
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
   336
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   337
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   338
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   339
// Grammar Rules for Fun
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   340
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   341
// arithmetic expressions
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   342
lazy val Exp: Parser[List[Token], Exp] = 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   343
  (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Exp ~ T_KWD("else") ~ Exp) ==>
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   344
    { case _ ~ y ~ _ ~ u ~ _ ~ w => If(y, u, w): Exp } ||
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   345
  (M ~ T_SEMI ~ Exp) ==> { case x ~ _ ~ z => Sequence(x, z): Exp } || M
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   346
lazy val M: Parser[List[Token], Exp] =
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   347
  (T_KWD("write") ~ L) ==> { case _ ~ y => Write(y): Exp } || L
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   348
lazy val L: Parser[List[Token], Exp] = 
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   349
  (T ~ T_OP("+") ~ Exp) ==> { case x ~ _ ~ z => Aop("+", x, z): Exp } ||
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   350
  (T ~ T_OP("-") ~ Exp) ==> { case x ~ _ ~ z => Aop("-", x, z): Exp } || T  
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   351
lazy val T: Parser[List[Token], Exp] = 
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   352
  (F ~ T_OP("*") ~ T) ==> { case x ~ _ ~ z => Aop("*", x, z): Exp } || 
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   353
  (F ~ T_OP("/") ~ T) ==> { case x ~ _ ~ z => Aop("/", x, z): Exp } || 
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   354
  (F ~ T_OP("%") ~ T) ==> { case x ~ _ ~ z => Aop("%", x, z): Exp } || F
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   355
lazy val F: Parser[List[Token], Exp] = 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   356
  (IdParser ~ T_LPAREN ~ ListParser(Exp, T_COMMA) ~ T_RPAREN) ==> 
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   357
    { case x ~ _ ~ z ~ _ => Call(x, z): Exp } ||
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   358
  (T_LPAREN ~ Exp ~ T_RPAREN) ==> { case _ ~ y ~ _ => y: Exp } || 
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   359
  IdParser ==> { case x => Var(x): Exp } || 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   360
  NumParser ==> { case x => Num(x): Exp }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   361
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   362
// boolean expressions
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   363
lazy val BExp: Parser[List[Token], BExp] = 
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   364
  (Exp ~ T_OP("==") ~ Exp) ==> { case x ~ _ ~ z => Bop("==", x, z): BExp } || 
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   365
  (Exp ~ T_OP("!=") ~ Exp) ==> { case x ~ _ ~ z => Bop("!=", x, z): BExp } || 
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   366
  (Exp ~ T_OP("<") ~ Exp) ==> { case x ~ _ ~ z => Bop("<", x, z): BExp } || 
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   367
  (Exp ~ T_OP(">") ~ Exp) ==> { case x ~ _ ~ z => Bop("<", z, x): BExp } || 
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   368
  (Exp ~ T_OP("<=") ~ Exp) ==> { case x ~ _ ~ z => Bop("<=", x, z): BExp } || 
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   369
  (Exp ~ T_OP("=>") ~ Exp) ==> { case x ~ _ ~ z => Bop("<=", z, x): BExp }  
541
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 Defn: Parser[List[Token], Decl] =
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   372
   (T_KWD("def") ~ IdParser ~ T_LPAREN ~ ListParser(IdParser, T_COMMA) ~ T_RPAREN ~ T_OP("=") ~ Exp) ==>
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   373
     { case x ~ y ~ z ~ w ~ u ~ v ~ r => Def(y, w, r): Decl }
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   374
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   375
lazy val Prog: Parser[List[Token], List[Decl]] =
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
   376
  (Defn ~ T_SEMI ~ Prog) ==> { case x ~ _ ~ z => x :: z : List[Decl] } ||
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   377
  (Exp ==> ((s) => List(Main(s)) : List[Decl]))
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   378
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   379
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   380
// compiler - built-in functions 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   381
// 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
   382
//
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
val library = """
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   385
.class public XXX.XXX
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   386
.super java/lang/Object
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   387
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   388
.method public static write(I)V 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   389
        .limit locals 1 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   390
        .limit stack 2 
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   391
        getstatic java/lang/System/out Ljava/io/PrintStream; 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   392
        iload 0
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   393
        invokevirtual java/io/PrintStream/println(I)V 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   394
        return 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   395
.end method
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   396
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   397
"""
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   398
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   399
// calculating the maximal needed stack size
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   400
def max_stack_exp(e: Exp): Int = e match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   401
  case Call(_, args) => args.map(max_stack_exp).sum
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   402
  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
   403
  case Write(e) => max_stack_exp(e) + 1
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   404
  case Var(_) => 1
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   405
  case Num(_) => 1
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   406
  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
   407
  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
   408
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   409
def max_stack_bexp(e: BExp): Int = e match {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   410
  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
   411
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   412
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   413
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   414
// for generating new labels
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   415
var counter = -1
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   416
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   417
def Fresh(x: String) = {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   418
  counter += 1
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   419
  x ++ "_" ++ counter.toString()
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   420
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   421
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   422
// convenient string interpolations 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   423
// for instructions, labels and methods
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   424
import scala.language.implicitConversions
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   425
import scala.language.reflectiveCalls
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   426
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   427
implicit def sring_inters(sc: StringContext) = new {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   428
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   429
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   430
    def m(args: Any*): String = sc.s(args:_*) ++ "\n"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   431
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   432
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   433
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   434
type Env = Map[String, Int]
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   435
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   436
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   437
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
   438
  case Num(i) => i"ldc $i"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   439
  case Var(s) => i"iload ${env(s)}"
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   440
  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
   441
  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
   442
  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
   443
  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
   444
  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
   445
  case If(b, a1, a2) => {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   446
    val if_else = Fresh("If_else")
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   447
    val if_end = Fresh("If_end")
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   448
    compile_bexpT(b, env, if_else) ++
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   449
    compile_expT(a1, env, name) ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   450
    i"goto $if_end" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   451
    l"$if_else" ++
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   452
    compile_expT(a2, env, name) ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   453
    l"$if_end"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   454
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   455
  case Call(n, args) => if (name == n) { 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   456
    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
   457
    args.map(a => compile_expT(a, env, "")).mkString ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   458
    stores.reverse.mkString ++ 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   459
    i"goto ${n}_Start" 
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   460
  } else {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   461
    val is = "I" * args.length
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   462
    args.map(a => compile_expT(a, env, "")).mkString ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   463
    i"invokestatic XXX/XXX/${n}(${is})I"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   464
  }
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   465
  case Sequence(a1, a2) => {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   466
    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
   467
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   468
  case Write(a1) => {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   469
    compile_expT(a1, env, "") ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   470
    i"dup" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   471
    i"invokestatic XXX/XXX/write(I)V"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   472
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   473
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   474
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   475
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
   476
  case Bop("==", a1, a2) => 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   477
    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
   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_icmpeq $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_icmpge $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_icmpgt $jmp"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   484
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   485
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   486
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   487
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
   488
  case Def(name, args, a) => { 
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   489
    val env = args.zipWithIndex.toMap
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   490
    val is = "I" * args.length
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   491
    m".method public static $name($is)I" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   492
    m".limit locals ${args.length}" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   493
    m".limit stack ${1 + max_stack_exp(a)}" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   494
    l"${name}_Start" ++   
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   495
    compile_expT(a, env, name) ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   496
    i"ireturn" ++ 
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   497
    m".end method\n"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   498
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   499
  case Main(a) => {
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   500
    m".method public static main([Ljava/lang/String;)V" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   501
    m".limit locals 200" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   502
    m".limit stack 200" ++
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   503
    compile_expT(a, Map(), "") ++
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   504
    i"invokestatic XXX/XXX/write(I)V" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   505
    i"return\n" ++
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   506
    m".end method\n"
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   507
  }
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   508
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   509
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   510
// main compiler functions
541
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   511
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   512
def time_needed[T](i: Int, code: => T) = {
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   513
  val start = System.nanoTime()
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   514
  for (j <- 1 to i) code
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   515
  val end = System.nanoTime()
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   516
  (end - start)/(i * 1.0e9)
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   517
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   518
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   519
def compile(class_name: String, input: String) : String = {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   520
  val tks = tokenise(input)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   521
  val ast = Prog.parse_single(tks)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   522
  val instructions = ast.map(compile_decl).mkString
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   523
  (library + instructions).replaceAllLiterally("XXX", class_name)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   524
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   525
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   526
def compile_file(class_name: String) = {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   527
  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
   528
  val output = compile(class_name, input)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   529
  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
   530
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   531
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   532
import scala.sys.process._
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
def compile_run(class_name: String) : Unit = {
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   535
  compile_file(class_name)
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   536
  (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
   537
  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
   538
}
885cf83ebce3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   539
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
//examples
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   542
compile_run("defs")
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   543
compile_run("fact")