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