solutions/cw3/parser.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 22 Nov 2024 12:42:07 +0000
changeset 974 0cb4bf2469d1
parent 961 c0600f8b6427
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// CW3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
     3
//> using file project.scala
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
     4
//> using file lexer.sc
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
     5
import lexer.*
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
     7
case class ~[+A, +B](x: A, y: B)
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
     8
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
     9
// parser combinators
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    10
//
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    11
type IsSeq[I] = I => Seq[?]
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    12
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    13
abstract class Parser[I : IsSeq, T](using is: I => Seq[?])  {
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    14
  def parse(in: I): Set[(T, I)]
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    15
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    16
  def parse_all(in: I) : Set[T] =
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    17
    for ((hd, tl) <- parse(in);
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    18
        if is(tl).isEmpty) yield hd
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    19
}
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    21
// parser combinators
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    22
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    23
// alternative parser
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    24
class AltParser[I : IsSeq, T](p: => Parser[I, T],
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    25
                              q: => Parser[I, T]) extends Parser[I, T] {
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    26
  def parse(in: I) = p.parse(in) ++ q.parse(in)
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    27
}
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    29
// sequence parser
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    30
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T],
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    31
                                 q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    32
  def parse(in: I) =
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    33
    for ((hd1, tl1) <- p.parse(in);
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    34
         (hd2, tl2) <- q.parse(tl1)) yield (new ~(hd1, hd2), tl2)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    37
// map parser
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    38
class MapParser[I : IsSeq, T, S](p: => Parser[I, T],
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    39
                                 f: T => S) extends Parser[I, S] {
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    40
  def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    43
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    44
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    45
/*
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    46
// atomic parser for (particular) strings
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    47
case class StrParser(s: String) extends Parser[String, String] {
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    48
  def parse(sb: String) = {
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    49
    val (prefix, suffix) = sb.splitAt(s.length)
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    50
    if (prefix == s) Set((prefix, suffix)) else Set()
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    51
  }
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    54
extension (sc: StringContext)
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    55
  def p(args: Any*) = StrParser(sc.s(args:_*))
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    56
*/
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    57
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    58
// more convenient syntax for parser combinators
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    59
extension [I : IsSeq, T](p: Parser[I, T]) {
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    60
  def ||(q : => Parser[I, T]) = AltParser[I, T](p, q)
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    61
  def ~[S] (q : => Parser[I, S]) = SeqParser[I, T, S](p, q)
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    62
  def map[S](f: => T => S) = MapParser[I, T, S](p, f)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
// New parser that takes as input a list of tokens
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    66
case class TokenParser(t: Token) extends Parser[List[Token], Token] {
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    67
    def parse(in: List[Token]) = {
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    68
      // an example of an atomic parser for characters
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    69
      if (!in.isEmpty && in.head == t) Set((t, in.tail)) else Set()
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    70
    }
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    71
}
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    72
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
case class TokenListParser(ts: List[Token]) extends Parser[List[Token], List[Token]] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
    def parse(tsb: List[Token]) = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
        val (prefix, suffix) = tsb.splitAt(ts.length)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
        if (prefix == ts) Set((prefix, suffix)) else Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    80
// Implicit definitions to go from a token
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
// or a list of tokens to a TokenListParser
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    82
implicit def token2parser(t: Token) : Parser[List[Token], Token] =
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    83
  TokenParser(t)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    85
extension (t: Token) {
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    86
    def || (q : => Parser[List[Token], Token]) = AltParser[List[Token], Token](t, q)
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    87
    def ~[S](q : => Parser[List[Token], S]) = SeqParser[List[Token], Token, S](t, q)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
// Abstract Syntax Trees
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
abstract class Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
abstract class AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
abstract class BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
type Block = List[Stmt]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
case object Skip extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
case class While(b: BExp, bl: Block) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
case class Assign(s: String, a: AExp) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
case class Read(s: String) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
case class WriteId(s: String) extends Stmt  // for printing values of variables
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
case class WriteString(s: String) extends Stmt  // for printing words
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
case class Var(s: String) extends AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
case class Num(i: Int) extends AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
case object True extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
case object False extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
case class And(b1: BExp, b2: BExp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
case class Or(b1: BExp, b2: BExp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
case class IdParser() extends Parser[List[Token], String] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
    def parse(tsb: List[Token]) = tsb match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
        case T_ID(id) :: rest => Set((id, rest))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
        case _ => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
case class NumParser() extends Parser[List[Token], Int] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
    def parse(tsb: List[Token]) = tsb match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
        case T_NUM(n) :: rest => Set((n, rest))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
        case _ => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
case class StringParser() extends Parser[List[Token], String] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
    def parse(tsb: List[Token]) = tsb match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
        case T_STRING(s) :: rest => Set((s, rest))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
        case _ => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   137
974
0cb4bf2469d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 961
diff changeset
   138
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
// WHILE Language Parsing
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   140
lazy val AExp: Parser[List[Token], AExp] =
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   141
  (Te ~ T_OP("+") ~ AExp).map{ case x ~ _ ~ z => Aop("+", x, z): AExp } ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   142
  (Te ~ T_OP("-") ~ AExp).map{ case x ~ _ ~ z => Aop("-", x, z): AExp } || Te
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   143
lazy val Te: Parser[List[Token], AExp] =
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   144
  (Fa ~ T_OP("*") ~ Te).map{ case x ~ _ ~ z => Aop("*", x, z): AExp } ||
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   145
  (Fa ~ T_OP("/") ~ Te).map{ case x ~ _ ~ z => Aop("/", x, z): AExp } ||
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   146
  (Fa ~ T_OP("%") ~ Te).map{ case x ~ _ ~ z => Aop("%", x, z): AExp } || Fa
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   147
lazy val Fa: Parser[List[Token], AExp] =
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   148
   (T_PAREN("(") ~ AExp ~ T_PAREN(")")).map{ case _ ~ y ~ _ => y } ||
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   149
   IdParser().map{Var(_)}  ||
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   150
   NumParser().map{Num(_)}
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   152
lazy val BExp: Parser[List[Token], BExp] =
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   153
   (AExp ~ T_OP("==") ~ AExp).map{ case x ~ _ ~ z => Bop("==", x, z): BExp } ||
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   154
   (AExp ~ T_OP("!=") ~ AExp).map{ case x ~ _ ~ z => Bop("!=", x, z): BExp } ||
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   155
   (AExp ~ T_OP("<") ~ AExp).map{ case x ~ _ ~ z => Bop("<", x, z): BExp } ||
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   156
   (AExp ~ T_OP(">") ~ AExp).map{ case x ~ _ ~ z => Bop(">", x, z): BExp } ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   157
   (T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("&&") ~ BExp).map{ case _ ~ y ~ _ ~ _ ~ v => And(y, v): BExp } ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   158
   (T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("||") ~ BExp).map{ case _ ~ y ~ _ ~ _ ~ v => Or(y, v): BExp } ||
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   159
   (T_KEYWORD("true").map(_ => True: BExp )) ||
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   160
   (T_KEYWORD("false").map(_ => False: BExp )) ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   161
   (T_PAREN("(") ~ BExp ~ T_PAREN(")")).map{ case _ ~ x ~ _ => x }
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
lazy val Stmt: Parser[List[Token], Stmt] =
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   164
    T_KEYWORD("skip").map(_ => Skip: Stmt) ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   165
    (IdParser() ~ T_OP(":=") ~ AExp).map{ case id ~ _ ~ z => Assign(id, z): Stmt } ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   166
    (T_KEYWORD("if") ~ BExp ~ T_KEYWORD("then") ~ Block ~ T_KEYWORD("else") ~ Block).map{ case _ ~ y ~ _ ~ u ~ _ ~ w => If(y, u, w): Stmt } ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   167
    (T_KEYWORD("while") ~ BExp ~ T_KEYWORD("do") ~ Block).map{ case _ ~ y ~ _ ~ w => While(y, w) : Stmt } ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   168
    (T_KEYWORD("read") ~ IdParser()).map{ case _ ~ id => Read(id): Stmt} ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   169
    (T_KEYWORD("write") ~ IdParser()).map{ case _ ~ id => WriteId(id): Stmt} ||
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   170
    (T_KEYWORD("write") ~ StringParser()).map{ case _ ~ s => WriteString(s): Stmt} ||
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   171
    (T_KEYWORD("write") ~ T_PAREN("(") ~ IdParser() ~ T_PAREN(")")).map{ case _ ~ _ ~ id ~ _ => WriteId(id): Stmt} ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   172
    (T_KEYWORD("write") ~  T_PAREN("(") ~ StringParser() ~ T_PAREN(")")).map{ case _ ~ _ ~ s ~ _ => WriteString(s): Stmt}
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
lazy val Stmts: Parser[List[Token], Block] =
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   175
    (Stmt ~ T_SEMI ~ Stmts).map{ case x ~ _ ~ z => x :: z : Block } ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   176
    (Stmt.map(s => List(s) : Block))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
lazy val Block: Parser[List[Token], Block] =
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   179
    (T_PAREN("{") ~ Stmts ~ T_PAREN("}")).map{ case x ~ y ~ z => y} ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   180
    (Stmt.map(s => List(s)))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
// Testing with programs 2 & 3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
905
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   184
//println("Fibonacci")
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   185
//println(Stmts.parse_all(tokenise(os.read(os.pwd / "fib.while"))))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
905
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   187
//println("Loops")
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   188
//println(Stmts.parse_all(tokenise(os.read(os.pwd / "loops.while"))))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
905
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   190
//println("Collatz")
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   191
//println(Stmts.parse_all(tokenise(os.read(os.pwd / "collatz2.while"))))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
// Interpreter
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
// Environment to store values of variables
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
type Env = Map[String, Int]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
def eval_aexp(a: AExp, env: Env) : Int = a match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
  case Num(i) => i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
  case Var(s) => env(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
  case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
  case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
  case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
  case Aop("/", a1, a2) => eval_aexp(a1, env) / eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
  case Aop("%", a1, a2) => eval_aexp(a1, env) % eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
def eval_bexp(b: BExp, env: Env) : Boolean = b match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
  case True => true
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
  case False => false
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
  case Bop("==", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
  case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
  case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
  case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
  case And(b1, b2) => eval_bexp(b1, env) && eval_bexp(b2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
  case Or(b1, b2) => eval_bexp(b1, env) || eval_bexp(b2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
// Import needed to take int as input from the user
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
import scala.io.StdIn.readInt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
def eval_stmt(s: Stmt, env: Env) : Env = s match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
  case Skip => env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
  case Assign(x, a) => env + (x -> eval_aexp(a, env))
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   226
  case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env)
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   227
  case While(b, bl) =>
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
    if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
    else env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
  case WriteId(x) => { print(env(x)) ; env }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
  case WriteString(x) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
        print(x.replaceAll("\"", "").replaceAll("""\\n""", "\n")) ;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
        env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
       }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   237
  case Read(x) => {
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   238
      println("Enter an integer and press ENTER:") ;
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
      val n = readInt() ; // Note: Does not work when using the REPL
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   240
      eval_stmt(Assign(x, Num(n)), env)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
def eval_bl(bl: Block, env: Env) : Env = bl match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
  case Nil => env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
  case s::bl => eval_bl(bl, eval_stmt(s, env))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
def eval(bl: Block) : Env = eval_bl(bl, Map())
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
959
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 921
diff changeset
   251
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 921
diff changeset
   252
//println(tokenise(os.read(os.pwd / "primes.while")))
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 921
diff changeset
   253
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 921
diff changeset
   254
//println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "primes.while"))).head))
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 921
diff changeset
   255
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 921
diff changeset
   256
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 921
diff changeset
   257
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   258
//@main
921
bb54e7aa1a3f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   259
def run(file: String) = {
905
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   260
  val contents = os.read(os.pwd / file)
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   261
  println(s"Lex $file: ")
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   262
  println(tokenise(contents))
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   263
  println(s"Parse $file: ")
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   264
  println(Stmts.parse_all(tokenise(contents)).head)
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   265
  println(s"Eval $file: ")
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   266
  println(eval(Stmts.parse_all(tokenise(contents)).head))
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   267
}
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   268
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   269
//@main
921
bb54e7aa1a3f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   270
def test(file: String) = {
bb54e7aa1a3f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   271
  val contents = os.read(os.pwd / file)
bb54e7aa1a3f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   272
  println(s"Lex $file: ")
bb54e7aa1a3f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   273
  println(tokenise(contents))
bb54e7aa1a3f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   274
  println(s"Parse $file: ")
bb54e7aa1a3f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   275
  println(Stmts.parse_all(tokenise(contents)).head)
bb54e7aa1a3f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   276
}
bb54e7aa1a3f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   277
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   278
/*
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   279
println("Loops eval")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   280
val start = System.nanoTime()
959
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 921
diff changeset
   281
println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "primes.while"))).head))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   282
val end = System.nanoTime()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   283
println("Time taken in seconds: ")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   284
println((end - start)/(1.0e9))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   285
*/
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   286
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   287
test("primes.while")
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   288
run("primes.while")