solutions/cw3/parser.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 05 Jun 2023 10:50:36 +0100
changeset 912 e32802acf952
parent 905 15973df32613
child 919 53f08d873e09
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
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
import $file.lexer
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
import lexer._ 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
case class ~[+A, +B](_1: A, _2: B)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
type IsSeq[A] = A => Seq[_]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
abstract class Parser[I : IsSeq, T] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
  def parse(ts: I): Set[(T, I)]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
  def parse_all(ts: I) : Set[T] =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
    for ((head, tail) <- parse(ts); if tail.isEmpty) yield head
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
  def parse(sb: I) = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
    for ((head1, tail1) <- p.parse(sb); 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
         (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
class AltParser[I : IsSeq, T](p: => Parser[I, T], q: => Parser[I, T]) extends Parser[I, T] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
class FunParser[I : IsSeq, T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
  def parse(sb: I) = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
// New parser that takes as input a list of tokens
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
case class TokenListParser(ts: List[Token]) extends Parser[List[Token], List[Token]] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
    def parse(tsb: List[Token]) = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
        val (prefix, suffix) = tsb.splitAt(ts.length)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
        if (prefix == ts) Set((prefix, suffix)) else Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
// Implicit definitions to go from a token 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
// or a list of tokens to a TokenListParser
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
implicit def token2parser(t: Token) = TokenListParser(List(t))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
implicit def tokenList2parser(ts: List[Token]) = TokenListParser(ts)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
  def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
implicit def TokenOps(t: Token) = new {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
    def || (q : => Parser[List[Token], List[Token]]) = new AltParser[List[Token], List[Token]](List(t), q)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
    def || (qs : List[Token]) = new AltParser[List[Token], List[Token]](List(t), qs)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
    def ==>[S] (f: => List[Token] => S) = new FunParser[List[Token], List[Token], S](List(t), f)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
    def ~[S](q : => Parser[List[Token], S]) =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
        new SeqParser[List[Token], List[Token], S](List(t), q)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
    def ~ (qs : List[Token]) =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
        new SeqParser[List[Token], List[Token], List[Token]](List(t), qs)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
implicit def TokenListOps(ts: List[Token]) = new {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
    def || (q : => Parser[List[Token], List[Token]]) = new AltParser[List[Token], List[Token]](ts, q)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
    def || (qs : List[Token]) = new AltParser[List[Token], List[Token]](ts, qs)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
    def ==>[S] (f: => List[Token] => S) = new FunParser[List[Token], List[Token], S](ts, f)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
    def ~[S](q : => Parser[List[Token], S]) =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
        new SeqParser[List[Token], List[Token], S](ts, q)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
    def ~ (qs : List[Token]) =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
        new SeqParser[List[Token], List[Token], List[Token]](ts, qs)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
// Abstract Syntax Trees
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
abstract class Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
abstract class AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
abstract class BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
type Block = List[Stmt]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
case object Skip extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
case class While(b: BExp, bl: Block) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
case class Assign(s: String, a: AExp) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
case class Read(s: String) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
case class WriteId(s: String) extends Stmt  // for printing values of variables
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
case class WriteString(s: String) extends Stmt  // for printing words
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
case class Var(s: String) extends AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
case class Num(i: Int) extends AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
case object True extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
case object False extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
case class And(b1: BExp, b2: BExp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
case class Or(b1: BExp, b2: BExp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
case class IdParser() extends Parser[List[Token], String] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
    def parse(tsb: List[Token]) = tsb match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
        case T_ID(id) :: rest => Set((id, rest))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
        case _ => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
case class NumParser() extends Parser[List[Token], Int] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
    def parse(tsb: List[Token]) = tsb match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
        case T_NUM(n) :: rest => Set((n, rest))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
        case _ => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
case class StringParser() extends Parser[List[Token], String] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
    def parse(tsb: List[Token]) = tsb match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
        case T_STRING(s) :: rest => Set((s, rest))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
        case _ => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
// WHILE Language Parsing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
lazy val AExp: Parser[List[Token], AExp] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
  (Te ~ T_OP("+") ~ AExp) ==> { case x ~ _ ~ z => Aop("+", x, z): AExp } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
  (Te ~ T_OP("-") ~ AExp) ==> { case x ~ _ ~ z => Aop("-", x, z): AExp } || Te
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
lazy val Te: Parser[List[Token], AExp] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
  (Fa ~ T_OP("*") ~ Te) ==> { case x ~ _ ~ z => Aop("*", x, z): AExp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
  (Fa ~ T_OP("/") ~ Te) ==> { case x ~ _ ~ z => Aop("/", x, z): AExp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
  (Fa ~ T_OP("%") ~ Te) ==> { case x ~ _ ~ z => Aop("%", x, z): AExp } || Fa  
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
lazy val Fa: Parser[List[Token], AExp] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
   (T_PAREN("(") ~ AExp ~ T_PAREN(")")) ==> { case _ ~ y ~ _ => y } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
   IdParser() ==> Var  || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
   NumParser() ==> Num
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
lazy val BExp: Parser[List[Token], BExp] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
   (AExp ~ T_OP("==") ~ AExp) ==> { case x ~ _ ~ z => Bop("==", x, z): BExp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
   (AExp ~ T_OP("!=") ~ AExp) ==> { case x ~ _ ~ z => Bop("!=", x, z): BExp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
   (AExp ~ T_OP("<") ~ AExp) ==> { case x ~ _ ~ z => Bop("<", x, z): BExp } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
   (AExp ~ T_OP(">") ~ AExp) ==> { case x ~ _ ~ z => Bop(">", x, z): BExp } ||
869
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 864
diff changeset
   135
   (T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("&&") ~ BExp) ==> { case _ ~ y ~ _ ~ _ ~ v => And(y, v): BExp } ||
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 864
diff changeset
   136
   (T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("||") ~ BExp) ==> { case _ ~ y ~ _ ~ _ ~ v => Or(y, v): BExp } ||
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
   (T_KEYWORD("true") ==> (_ => True: BExp )) || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
   (T_KEYWORD("false") ==> (_ => False: BExp )) ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
   (T_PAREN("(") ~ BExp ~ T_PAREN(")")) ==> { case _ ~ x ~ _ => x }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
lazy val Stmt: Parser[List[Token], Stmt] =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
    T_KEYWORD("skip") ==> (_ => Skip: Stmt) ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
    (IdParser() ~ T_OP(":=") ~ AExp) ==> { case id ~ _ ~ z => Assign(id, z): Stmt } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
    (T_KEYWORD("if") ~ BExp ~ T_KEYWORD("then") ~ Block ~ T_KEYWORD("else") ~ Block) ==> { case _ ~ y ~ _ ~ u ~ _ ~ w => If(y, u, w): Stmt } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
    (T_KEYWORD("while") ~ BExp ~ T_KEYWORD("do") ~ Block) ==> { case _ ~ y ~ _ ~ w => While(y, w) : Stmt } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
    (T_KEYWORD("read") ~ IdParser()) ==> { case _ ~ id => Read(id): Stmt} ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
    (T_KEYWORD("write") ~ IdParser()) ==> { case _ ~ id => WriteId(id): Stmt} ||
869
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 864
diff changeset
   148
    (T_KEYWORD("write") ~ StringParser()) ==> { case _ ~ s => WriteString(s): Stmt} || 
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 864
diff changeset
   149
    (T_KEYWORD("write") ~ T_PAREN("(") ~ IdParser() ~ T_PAREN(")")) ==> { case _ ~ _ ~ id ~ _ => WriteId(id): Stmt} ||
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 864
diff changeset
   150
    (T_KEYWORD("write") ~  T_PAREN("(") ~ StringParser() ~ T_PAREN(")")) ==> { case _ ~ _ ~ s ~ _ => WriteString(s): Stmt}
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
lazy val Stmts: Parser[List[Token], Block] =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
    (Stmt ~ T_SEMI ~ Stmts) ==> { case x ~ _ ~ z => x :: z : Block } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
    (Stmt ==> (s => List(s) : Block))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
lazy val Block: Parser[List[Token], Block] =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
    (T_PAREN("{") ~ Stmts ~ T_PAREN("}")) ==> { case x ~ y ~ z => y} ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
    (Stmt ==> (s => List(s)))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
// Testing with programs 2 & 3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
905
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   162
//println("Fibonacci")
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   163
//println(Stmts.parse_all(tokenise(os.read(os.pwd / "fib.while"))))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
905
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   165
//println("Loops")
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   166
//println(Stmts.parse_all(tokenise(os.read(os.pwd / "loops.while"))))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
905
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   168
//println("Collatz")
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   169
//println(Stmts.parse_all(tokenise(os.read(os.pwd / "collatz2.while"))))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
// Interpreter
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
// Environment to store values of variables
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
type Env = Map[String, Int]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
def eval_aexp(a: AExp, env: Env) : Int = a match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
  case Num(i) => i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
  case Var(s) => env(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
  case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
  case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
  case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
  case Aop("/", a1, a2) => eval_aexp(a1, env) / eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
  case Aop("%", a1, a2) => eval_aexp(a1, env) % eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
def eval_bexp(b: BExp, env: Env) : Boolean = b match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
  case True => true
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
  case False => false
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
  case Bop("==", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
  case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
  case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
  case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
  case And(b1, b2) => eval_bexp(b1, env) && eval_bexp(b2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
  case Or(b1, b2) => eval_bexp(b1, env) || eval_bexp(b2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
// Import needed to take int as input from the user
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
import scala.io.StdIn.readInt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
def eval_stmt(s: Stmt, env: Env) : Env = s match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
  case Skip => env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
  case Assign(x, a) => env + (x -> eval_aexp(a, env))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
  case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
  case While(b, bl) => 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
    if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
    else env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
  case WriteId(x) => { print(env(x)) ; env }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
  case WriteString(x) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
        print(x.replaceAll("\"", "").replaceAll("""\\n""", "\n")) ;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
        env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
       }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
  case Read(x) => { 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
      println("Enter an integer and press ENTER:") ; 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
      val n = readInt() ; // Note: Does not work when using the REPL
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
      eval_stmt(Assign(x, Num(n)), env) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
def eval_bl(bl: Block, env: Env) : Env = bl match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
  case Nil => env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
  case s::bl => eval_bl(bl, eval_stmt(s, env))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
def eval(bl: Block) : Env = eval_bl(bl, Map())
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
905
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   229
@main
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   230
def main(file: String) = {
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   231
  val contents = os.read(os.pwd / file)
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   232
  println(s"Lex $file: ")
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   233
  println(tokenise(contents))
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   234
  println(s"Parse $file: ")
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   235
  println(Stmts.parse_all(tokenise(contents)).head)
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   236
  println(s"Eval $file: ")
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   237
  println(eval(Stmts.parse_all(tokenise(contents)).head))
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   238
}
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
/*
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
println("Loops eval")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
val start = System.nanoTime()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "loops.while"))).head))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
val end = System.nanoTime()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
println("Time taken in seconds: ")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
println((end - start)/(1.0e9))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
*/