progs/fun.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 02 Dec 2013 03:57:48 +0000
changeset 220 141041fc76b5
child 221 824ffbf66ab4
permissions -rw-r--r--
added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
import scala.language.implicitConversions    
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
import scala.language.reflectiveCalls 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
import scala.util._
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
import scala.annotation.tailrec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
import scala.sys.process._
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
abstract class Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
case object NULL extends Rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
case object EMPTY extends Rexp
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 RANGE(cs: List[Char]) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
case class PLUS(r: Rexp) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
case class STAR(r: Rexp) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
case class NTIMES(r: Rexp, n: Int) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
case class NUPTOM(r: Rexp, n: Int, m: Int) extends Rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
object RANGE {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
  def apply(s: String) : RANGE = RANGE(s.toList)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
def NMTIMES(r: Rexp, n: Int, m: Int) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
  if(m < n) throw new IllegalArgumentException("the number m cannot be smaller than n.")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
  else NUPTOM(r, n, m - n)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
case class NOT(r: Rexp) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
case class OPT(r: Rexp) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
// some convenience for typing in regular expressions
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
def charlist2rexp(s : List[Char]) : Rexp = s match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  case Nil => EMPTY
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  case c::Nil => CHAR(c)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
implicit def RexpOps (r: Rexp) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  def | (s: Rexp) = ALT(r, s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  def % = STAR(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  def ~ (s: Rexp) = SEQ(r, s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
implicit def stringOps (s: String) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  def | (r: Rexp) = ALT(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  def | (r: String) = ALT(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  def % = STAR(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  def ~ (r: Rexp) = SEQ(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  def ~ (r: String) = SEQ(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
// nullable function: tests whether the regular 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
// expression can recognise the empty string
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
def nullable (r: Rexp) : Boolean = r match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
  case NULL => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  case EMPTY => true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
  case CHAR(_) => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
  case STAR(_) => true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  case PLUS(r) => nullable(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  case NTIMES(r, i) => if (i == 0) true else nullable(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  case NUPTOM(r, i, j) => if (i == 0) true else nullable(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
  case RANGE(_) => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
  case NOT(r) => !(nullable(r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
  case OPT(_) => true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
// derivative of a regular expression w.r.t. a character
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
def der (c: Char, r: Rexp) : Rexp = r match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
  case NULL => NULL
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
  case EMPTY => NULL
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
  case CHAR(d) => if (c == d) EMPTY else NULL
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
  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
    76
  case SEQ(r1, r2) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
    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
    78
    else SEQ(der(c, r1), r2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
  case STAR(r) => SEQ(der(c, r), STAR(r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
  case PLUS(r) => SEQ(der(c, r), STAR(r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
  case NTIMES(r, i) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
    if (i == 0) NULL else der(c, SEQ(r, NTIMES(r, i - 1)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  case NUPTOM(r, i, j) =>
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
    if (i == 0 && j == 0) NULL else 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
    if (i == 0) ALT(der(c, NTIMES(r, j)), der(c, NUPTOM(r, 0, j - 1)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
    else der(c, SEQ(r, NUPTOM(r, i - 1, j)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
  case RANGE(cs) => if (cs contains c) EMPTY else NULL
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
  case NOT(r) => NOT(der (c, r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
  case OPT(r) => der(c, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
def zeroable (r: Rexp) : Boolean = r match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
  case NULL => true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
  case EMPTY => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
  case CHAR(_) => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
  case ALT(r1, r2) => zeroable(r1) && zeroable(r2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
  case SEQ(r1, r2) => zeroable(r1) || zeroable(r2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
  case STAR(_) => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
  case PLUS(r) => zeroable(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
  case NTIMES(r, i) => if (i == 0) false else zeroable(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
  case NUPTOM(r, i, j) => if (i == 0) false else zeroable(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  case RANGE(_) => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
  case NOT(r) => !(zeroable(r))     // bug: incorrect definition for NOT
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
  case OPT(_) => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
// derivative w.r.t. a string (iterates der)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
def ders (s: List[Char], r: Rexp) : Rexp = s match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
  case Nil => r
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
  case c::s => ders(s, der(c, r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
// regular expressions for the While language
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
val DIGIT = RANGE("0123456789")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
val ID = SYM ~ (SYM | DIGIT).% 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
val NUM = PLUS(DIGIT)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
val KEYWORD : Rexp = "if" | "then" | "else" | "read" | "write" | "def"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
val SEMI: Rexp = ";"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
val COMMA: Rexp = ","
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "%" | "=" | "/"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
val WHITESPACE = PLUS(" " | "\n" | "\t")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
val RPAREN: Rexp = ")"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
val LPAREN: Rexp = "("
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
val BEGIN: Rexp = "{"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
val END: Rexp = "}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
val ALL = SYM | DIGIT | OP | " " | ":" | ";" | "\"" | "=" | "," | "(" | ")"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
val ALL2 = ALL | "\n"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
val COMMENT2 = ("/*" ~ NOT(ALL.% ~ "*/" ~ ALL.%) ~ "*/")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
val COMMENT = ("/*" ~ ALL2.% ~ "*/") | ("//" ~ ALL.% ~ "\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
val STRING = "\"" ~ ALL.% ~ "\""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
// token for While languag
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
abstract class Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
case object T_WHITESPACE extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
case object T_SEMI extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
case object T_COMMA extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
case object T_LPAREN extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
case object T_RPAREN extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
case object T_BEGIN extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
case object T_END extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
case object T_COMMENT extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
case class T_ID(s: String) extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
case class T_OP(s: String) extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
case class T_NUM(s: String) extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
case class T_KWD(s: String) extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
case class T_STRING(s: String) extends Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
case class T_ERR(s: String) extends Token // special error token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
type TokenFun = String => Token
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
type LexRules = List[(Rexp, TokenFun)]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
val While_lexing_rules: LexRules = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
  List((KEYWORD, (s) => T_KWD(s)),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
       (ID, (s) => T_ID(s)),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
       (COMMENT, (s) => T_COMMENT),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
       (OP, (s) => T_OP(s)),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
       (NUM, (s) => T_NUM(s)),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
       (SEMI, (s) => T_SEMI),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
       (COMMA, (s) => T_COMMA),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
       (LPAREN, (s) => T_LPAREN),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
       (RPAREN, (s) => T_RPAREN),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
       (BEGIN, (s) => T_BEGIN),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
       (END, (s) => T_END),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
       (STRING, (s) => T_STRING(s.drop(1).dropRight(1))),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
       (WHITESPACE, (s) => T_WHITESPACE))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
// calculates derivatives until all of them are zeroable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
@tailrec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
def munch(s: List[Char], 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
          pos: Int, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
          rs: LexRules, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
          last: Option[(Int, TokenFun)]): Option[(Int, TokenFun)] = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
  rs match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
  case Nil => last
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
  case rs if (s.length <= pos) => last
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
  case rs => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
    val ders = rs.map({case (r, tf) => (der(s(pos), r), tf)})
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
    val rs_nzero = ders.filterNot({case (r, _) => zeroable(r)})
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
    val rs_nulls = ders.filter({case (r, _) => nullable(r)})
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
    val new_last = if (rs_nulls != Nil) Some((pos, rs_nulls.head._2)) else last
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
    munch(s, 1 + pos, rs_nzero, new_last)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
// iterates the munching function and returns a Token list
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
def tokenize(s: String, rs: LexRules) : List[Token] = munch(s.toList, 0, rs, None) match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
  case None if (s == "") => Nil
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
  case None => List(T_ERR(s"Lexing error: $s"))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
  case Some((n, tf)) => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
    val (head, tail) = s.splitAt(n + 1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
    tf(head)::tokenize(tail, rs)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
def tokenizer(s:String) : List[Token] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
  tokenize(s, While_lexing_rules).filter {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
    case T_ERR(s) => { println(s); sys.exit(-1) }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
    case T_WHITESPACE => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
    case T_COMMENT => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
    case _ => true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
  } 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
def fromFile(name: String) : String = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
  io.Source.fromFile(name).mkString
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
// tokenizer tests
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
//println(tokenizer(fromFile("loops.while")).mkString("\n"))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
//println(tokenizer(fromFile("fib.while")).mkString("\n"))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
//println(tokenizer(fromFile("collatz.while")).mkString("\n"))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
//println(tokenizer(fromFile("defs.rec")).mkString("\n"))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
// Parser - Abstract syntax trees
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   217
abstract class Exp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
abstract class BExp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
abstract class Decl
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
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
   222
case class Main(e: Exp) extends Decl
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
case class Call(name: String, args: List[Exp]) extends Exp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
case class Read(s: String) extends Exp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
case class Write(s: String) extends Exp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
case class WriteS(s: String) extends Exp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   229
case class Var(s: String) extends Exp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   230
case class Num(i: Int) extends Exp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   231
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   232
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   233
case object True extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   234
case object False extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
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
   236
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
// Parser combinators
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
abstract class Parser[I <% Seq[_], T] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
  def parse(ts: I): Set[(T, I)]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
  def parse_all(ts: I) : Set[T] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
    for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
  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
   245
    case List(t) => t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
    case _ => { println ("Parse Error") ; sys.exit(-1) }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
  }
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
class SeqParser[I <% Seq[_], T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, (T, S)] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
    for ((head1, tail1) <- p.parse(sb); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
         (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   256
class AltParser[I <% Seq[_], T](p: => Parser[I, T], q: => Parser[I, T]) extends Parser[I, T] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   257
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   260
class FunParser[I <% Seq[_], T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   261
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   262
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   263
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   264
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
case class TokParser(tok: Token) extends Parser[List[Token], Token] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   266
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   267
    case t::ts if (t == tok) => Set((t, ts)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   268
    case _ => Set ()
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
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   271
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   272
implicit def token2tparser(t: Token) = TokParser(t)
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
case object NumParser extends Parser[List[Token], Int] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   275
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   276
    case T_NUM(s)::ts => Set((s.toInt, ts)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   277
    case _ => Set ()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   279
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   280
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   281
case object IdParser extends Parser[List[Token], String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   282
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   283
    case T_ID(s)::ts => Set((s, ts)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   284
    case _ => Set ()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   285
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   286
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   287
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   288
case object StringParser extends Parser[List[Token], String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   289
  def parse(ts: List[Token]) = ts match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   290
    case T_STRING(s)::ts => Set((s, ts)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   291
    case _ => Set ()
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   295
implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   296
  def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   297
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   298
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   299
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   300
implicit def TokOps(t: Token) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   301
  def || (q : => Parser[List[Token], Token]) = new AltParser[List[Token], Token](t, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   302
  def ==>[S] (f: => Token => S) = new FunParser[List[Token], Token, S](t, f)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   303
  def ~[S](q : => Parser[List[Token], S]) = new SeqParser[List[Token], Token, S](t, q)
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   306
def ListParser[I <% Seq[_], T, S](p: => Parser[I, T], q: => Parser[I, S]): Parser[I, List[T]] = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   307
  (p ~ q ~ ListParser(p, q)) ==> { case ((x, y), z) => x :: z : List[T] } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   308
  (p ==> ((s) => List(s)))
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   311
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   312
// arithmetic expressions
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   313
lazy val Exp: Parser[List[Token], Exp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   314
  (IdParser ~ T_LPAREN ~ ListParser(Exp, T_COMMA) ~ T_RPAREN) ==> 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   315
    { case (((x, y), z), w) => Call(x, z): Exp } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   316
  (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
   317
    { case (((((x, y), z), u), v), w) => If(y, u, w): Exp } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   318
  (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
   319
  (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
   320
lazy val T: Parser[List[Token], Exp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   321
  (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
   322
  (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
   323
  (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
   324
lazy val F: Parser[List[Token], Exp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   325
  (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
   326
  IdParser ==> { case x => Var(x): Exp } || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   327
  NumParser ==> { case x => Num(x): Exp }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   328
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   329
// boolean expressions
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   330
lazy val BExp: Parser[List[Token], BExp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   331
  (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
   332
  (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
   333
  (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
   334
  (Exp ~ T_OP(">") ~ Exp) ==> { case ((x, y), z) => Bop("<", z, x): BExp } || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   335
  (T_KWD("true") ==> ((_) => True)) || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   336
  (T_KWD("false") ==> ((_) => False: BExp))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   337
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   338
lazy val Defn: Parser[List[Token], Decl] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   339
   (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
   340
     { 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
   341
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   342
lazy val Prog: Parser[List[Token], List[Decl]] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   343
  (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
   344
  (Exp ==> ((s) => List(Main(s)) : List[Decl]))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   345
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   346
// parser examples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   347
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   348
val p11 = """def zero(x) = 0"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   349
val p11_toks = tokenizer(p11) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   350
val p11_ast = Defn.parse_all(p11_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   351
//println(p11_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   352
//println(p11_ast)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   353
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   354
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   355
val p12_toks = tokenizer(fromFile("defs.rec"))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   356
val p12_ast = Prog.parse_all(p12_toks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   357
//println(p12_toks.mkString(","))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   358
//println(p12_ast)
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   361
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   362
// compiler - built-in functions 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   363
// 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
   364
//
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   365
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   366
val beginning = """
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   367
.class public XXX.XXX
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   368
.super java/lang/Object
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   369
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   370
.method public <init>()V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   371
        aload_0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   372
        invokenonvirtual java/lang/Object/<init>()V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   373
        return
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   374
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   375
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   376
.method public static write(I)V 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   377
        .limit locals 5 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   378
        .limit stack 5 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   379
        iload 0 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   380
        getstatic java/lang/System/out Ljava/io/PrintStream; 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   381
        swap 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   382
        invokevirtual java/io/PrintStream/println(I)V 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   383
        return 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   384
.end method
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 static writes(Ljava/lang/String;)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   387
       .limit stack 2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   388
       .limit locals 2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   389
       getstatic java/lang/System/out Ljava/io/PrintStream;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   390
       aload 0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   391
       invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   392
       return
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   393
.end method
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
.method public static read()I 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   396
      .limit locals 10 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   397
      .limit stack 10
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
      ldc 0 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   400
      istore 1  ; this will hold our final integer 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   401
Label1: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   402
      getstatic java/lang/System/in Ljava/io/InputStream; 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   403
      invokevirtual java/io/InputStream/read()I 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   404
      istore 2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   405
      iload 2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   406
      ldc 10   ; the newline delimiter 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   407
      isub 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   408
      ifeq Label2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   409
      iload 2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   410
      ldc 32   ; the space delimiter 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   411
      isub 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   412
      ifeq Label2
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
      iload 2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   415
      ldc 48   ; we have our digit in ASCII, have to subtract it from 48 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   416
      isub 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   417
      ldc 10 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   418
      iload 1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   419
      imul 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   420
      iadd 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   421
      istore 1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   422
      goto Label1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   423
Label2: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   424
      ;when we come here we have our integer computed in Local Variable 1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   425
      iload 1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   426
      ireturn 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   427
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   428
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   429
.method public static main([Ljava/lang/String;)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   430
      .limit locals 200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   431
      .limit stack 200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   432
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   433
"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   434
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   435
val ending = """
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   436
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   437
      return
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   438
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   439
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   440
"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   441
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   442
// for generating new labels
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   443
var counter = -1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   444
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   445
def Fresh(x: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   446
  counter += 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   447
  x ++ "_" ++ counter.toString()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   448
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   449
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   450
type Mem = Map[String, String]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   451
type Instrs = List[String]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   452
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   453
def compile_exp(a: Exp, env : Mem) : Instrs = a match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   454
  case Num(i) => List("ldc " + i.toString + "\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   455
  case Var(s) => List("iload " + env(s) + "\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   456
  case Aop("+", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("iadd\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   457
  case Aop("-", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("isub\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   458
  case Aop("*", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("imul\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   459
  case If(b, a1, a2) => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   460
    val if_else = Fresh("If_else")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   461
    val if_end = Fresh("If_end")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   462
    compile_bexp(b, env, if_else) ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   463
    compile_exp(a1, env) ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   464
    List("goto " + if_end + "\n") ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   465
    List("\n" + if_else + ":\n\n") ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   466
    compile_exp(a2, env) ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   467
    List("\n" + if_end + ":\n\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   468
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   469
  case Call(n, args) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   470
    args.flatMap(a => compile_exp(a, env)) ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   471
    List ("invokestatic XXX/XXX/" + n + "(I)I\n")
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
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   474
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   475
def compile_bexp(b: BExp, env : Mem, jmp: String) : Instrs = b match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   476
  case True => Nil
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   477
  case False => List("goto " + jmp + "\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   478
  case Bop("=", a1, a2) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   479
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("if_icmpne " + jmp + "\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   480
  case Bop("!=", a1, a2) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   481
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("if_icmpeq " + jmp + "\n")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   482
  case Bop("<", a1, a2) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   483
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("if_icmpge " + jmp + "\n")
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   486
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   487
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   488
def compile_decl(d: Decl) : Instrs = d match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   489
  case Def(name, args, a) => Nil
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   490
  case Main(a) => compile_exp(a, Map())
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   493
def compile(class_name: String, input: String) : String = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   494
  val tks = tokenizer(input)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   495
  val ast = Prog.parse_single(tks)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   496
  val instructions = ast.flatMap(compile_decl).mkString
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   497
  (instructions).replaceAllLiterally("XXX", class_name)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   498
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   499
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   500
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   501
def compile_file(file_name: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   502
  val class_name = file_name.split('.')(0)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   503
  val output = compile(class_name, fromFile(file_name))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   504
  val fw = new java.io.FileWriter(class_name + ".j") 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   505
  fw.write(output) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   506
  fw.close()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   507
}
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
def time_needed[T](i: Int, code: => T) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   510
  val start = System.nanoTime()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   511
  for (j <- 1 to i) code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   512
  val end = System.nanoTime()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   513
  (end - start)/(i * 1.0e9)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   514
}
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   517
def compile_run(file_name: String) : Unit = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   518
  val class_name = file_name.split('.')(0)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   519
  compile_file(file_name)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   520
  println(fromFile("defs.j"))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   521
  //val test = ("java -jar jvm/jasmin-2.4/jasmin.jar " + class_name + ".j").!!
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   522
  //("java " + class_name + "/" + class_name).!
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   523
}
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
//examples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   527
//println(compile("test", p9))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   528
//compile_run("loops.while")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   529
compile_run("defs.rec")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   530
//compile_run("test.while")