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