progs/parser-combinators/comb2.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 09 Nov 2024 06:23:35 +0000
changeset 972 ebb4a40d9bae
parent 971 51e00f223792
child 973 f25d338d16c9
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
     1
// Parser Combinators:
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
     2
// Simple Version for WHILE-language
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
//====================================
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
     4
//
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
     5
// with some added convenience for
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
     6
// map-parsers and grammar rules
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
     7
//
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
     8
// call with
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
     9
//
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
    10
//    amm comb2.sc
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    12
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
    13
// more convenience for the map parsers later on;
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
    14
// it allows writing nested patterns as
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
    15
// case x ~ y ~ z => ...
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
    16
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    17
case class ~[+A, +B](x: A, y: B)
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    18
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
972
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 971
diff changeset
    20
trait IsSeq[I] {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 971
diff changeset
    21
  extension (i: I) def isEmpty: Boolean
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 971
diff changeset
    22
}
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 971
diff changeset
    23
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 971
diff changeset
    24
given IsSeq[String] = _.isEmpty
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 971
diff changeset
    25
given [I]: IsSeq[Seq[I]] = _.isEmpty
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 971
diff changeset
    26
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 971
diff changeset
    27
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
// parser combinators
972
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 971
diff changeset
    29
abstract class Parser[I : IsSeq, T] { p =>
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    30
  def parse(in: I): Set[(T, I)]  
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
972
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 971
diff changeset
    32
  def parse_all(in: I) : Set[T] =
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    33
    for ((hd, tl) <- parse(in); 
972
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 971
diff changeset
    34
        if tl.isEmpty) yield hd
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    35
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    36
  // alternative parser
972
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 971
diff changeset
    37
  def ||(q: => Parser[I, T]) : Parser[I, T] = Parser[I, T] {
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    38
    def parse(in: I) = p.parse(in) ++ q.parse(in)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    39
  }
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
    40
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    41
  // sequence parser
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    42
  def ~[S](q: => Parser[I, S]) = new Parser[I, T ~ S] {
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    43
    def parse(in: I) = 
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    44
      for ((hd1, tl1) <- p.parse(in); 
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    45
          (hd2, tl2) <- q.parse(tl1)) yield (new ~(hd1, hd2), tl2)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    46
  }
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    48
  // map parser
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    49
  def map[S](f: => T => S) = new Parser[I, S] {
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    50
    def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
    51
  }
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
// atomic parser for (particular) strings
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
case class StrParser(s: String) extends Parser[String, String] {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
  def parse(sb: String) = {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
    val (prefix, suffix) = sb.splitAt(s.length)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
    if (prefix == s) Set((prefix, suffix)) else Set()
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
  }
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
// atomic parser for identifiers (variable names)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
case object IdParser extends Parser[String, String] {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
  val reg = "[a-z][a-z,0-9]*".r
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  def parse(sb: String) = reg.findPrefixOf(sb) match {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
    case None => Set()
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
    case Some(s) => Set(sb.splitAt(s.length))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
  }
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
948
6bb67c2dcfd3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    71
// atomic parser for numbers (transformed into Ints)
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
case object NumParser extends Parser[String, Int] {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
  val reg = "[0-9]+".r
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
  def parse(sb: String) = reg.findPrefixOf(sb) match {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
    case None => Set()
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
    case Some(s) => {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
      val (hd, tl) = sb.splitAt(s.length)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
      Set((hd.toInt, tl)) 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
    }
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  }
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
// the following string interpolation allows us to write 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
// StrParser(_some_string_) more conveniently as 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
//
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
// p"<_some_string_>" 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
    88
extension (sc: StringContext) 
965
94f5cce73a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    89
  def p(args: Any*) = StrParser(sc.s(args*))
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
    90
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
// the abstract syntax trees for the WHILE language
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
abstract class Stmt
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
abstract class AExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
abstract class BExp 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
type Block = List[Stmt]
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
case object Skip extends Stmt
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
case class While(b: BExp, bl: Block) extends Stmt
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
case class Assign(s: String, a: AExp) extends Stmt
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
case class Write(s: String) extends Stmt
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
case class Var(s: String) extends AExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
case class Num(i: Int) extends AExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
case object True extends BExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
case object False extends BExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
case class And(b1: BExp, b2: BExp) extends BExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
case class Or(b1: BExp, b2: BExp) extends BExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
// arithmetic expressions
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
lazy val AExp: Parser[String, AExp] = 
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   117
  (Te ~ p"+" ~ AExp).map[AExp]{ case x ~ _ ~ z => Aop("+", x, z) } ||
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   118
  (Te ~ p"-" ~ AExp).map[AExp]{ case x ~ _ ~ z => Aop("-", x, z) } || Te
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
lazy val Te: Parser[String, AExp] = 
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   120
  (Fa ~ p"*" ~ Te).map[AExp]{ case x ~ _ ~ z => Aop("*", x, z) } || 
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   121
  (Fa ~ p"/" ~ Te).map[AExp]{ case x ~ _ ~ z => Aop("/", x, z) } || Fa  
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
lazy val Fa: Parser[String, AExp] = 
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   123
   (p"(" ~ AExp ~ p")").map{ case _ ~ y ~ _ => y } || 
948
6bb67c2dcfd3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   124
   IdParser.map(Var(_)) || 
6bb67c2dcfd3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   125
   NumParser.map(Num(_))
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
// boolean expressions with some simple nesting
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
lazy val BExp: Parser[String, BExp] = 
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   129
  (AExp ~ p"==" ~ AExp).map[BExp]{ case x ~ _ ~ z => Bop("==", x, z) } || 
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   130
  (AExp ~ p"!=" ~ AExp).map[BExp]{ case x ~ _ ~ z => Bop("!=", x, z) } || 
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   131
  (AExp ~ p"<" ~ AExp).map[BExp]{ case x ~ _ ~ z => Bop("<", x, z) } || 
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   132
  (AExp ~ p">" ~ AExp).map[BExp]{ case x ~ _ ~ z => Bop(">", x, z) } ||
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   133
  (p"(" ~ BExp ~ p")" ~ p"&&" ~ BExp).map[BExp]{ case _ ~ y ~ _ ~ _ ~ v => And(y, v) } ||
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   134
  (p"(" ~ BExp ~ p")" ~ p"||" ~ BExp).map[BExp]{ case _ ~ y ~ _ ~ _ ~ v => Or(y, v) } ||
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   135
  (p"true".map[BExp]{ _ => True }) || 
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   136
  (p"false".map[BExp]{ _ => False }) ||
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   137
  (p"(" ~ BExp ~ p")").map[BExp]{ case _ ~ x ~ _ => x }
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   139
// Stmt: a single statement
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   140
// Stmts: multiple statements
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   141
// Block: blocks (enclosed in curly braces)
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
lazy val Stmt: Parser[String, Stmt] =
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   143
  ((p"skip".map[Stmt]{_ => Skip }) ||
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   144
  (IdParser ~ p":=" ~ AExp).map[Stmt]{ case x ~ _ ~ z => Assign(x, z) } ||
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   145
  (p"write(" ~ IdParser ~ p")").map[Stmt]{ case _ ~ y ~ _ => Write(y) } ||
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   146
  (p"if" ~ BExp ~ p"then" ~ Block ~ p"else" ~ Block)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   147
    .map[Stmt]{ case _ ~ y ~ _ ~ u ~ _ ~ w => If(y, u, w) } ||
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   148
  (p"while" ~ BExp ~ p"do" ~ Block).map[Stmt]{ case _ ~ y ~ _ ~ w => While(y, w) })  
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
lazy val Stmts: Parser[String, Block] =
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   150
  (Stmt ~ p";" ~ Stmts).map[Block]{ case x ~ _ ~ z => x :: z } ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   151
  (Stmt.map[Block]{ s => List(s) })
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
lazy val Block: Parser[String, Block] =
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   153
  ((p"{" ~ Stmts ~ p"}").map{ case _ ~ y ~ _ => y } || 
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   154
  (Stmt.map(s => List(s))))
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
// Examples
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 955
diff changeset
   158
println(AExp.parse_all("2*2*2"))
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   159
println(BExp.parse_all("5+3"))
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   160
println(Stmt.parse_all("5==3"))
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   161
println(Stmt.parse_all("x2:=5+3"))
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   162
println(Block.parse_all("{x:=5;y:=8}"))
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   163
println(Block.parse_all("if(false)then{x:=5}else{x:=10}"))
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
val fib = """n := 10;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
             minus1 := 0;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
             minus2 := 1;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
             temp := 0;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
             while (n > 0) do {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
                 temp := minus2;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
                 minus2 := minus1 + minus2;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
                 minus1 := temp;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
                 n := n - 1
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
             };
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
             result := minus2""".replaceAll("\\s+", "")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   177
println("fib testcase:")
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   178
println(Stmts.parse_all(fib))
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
// an interpreter for the WHILE language
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
type Env = Map[String, Int]
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   184
def eval_aexp(a: AExp, env: Env) : Int =
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   185
  a match {
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   186
    case Num(i) => i
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   187
    case Var(s) => env(s)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   188
    case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   189
    case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   190
    case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   191
    case Aop("/", a1, a2) => eval_aexp(a1, env) / eval_aexp(a2, env)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   192
  }
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   194
def eval_bexp(b: BExp, env: Env) : Boolean =
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   195
  b match {
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   196
    case True => true
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   197
    case False => false
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   198
    case Bop("==", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   199
    case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env))
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   200
    case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   201
    case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   202
    case And(b1, b2) => eval_bexp(b1, env) && eval_bexp(b2, env)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   203
    case Or(b1, b2) => eval_bexp(b1, env) || eval_bexp(b2, env)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   204
  }
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   206
def eval_stmt(s: Stmt, env: Env) : Env =
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   207
  s match {
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   208
    case Skip => env
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   209
    case Assign(x, a) => env + (x -> eval_aexp(a, env))
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   210
    case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env) 
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   211
    case While(b, bl) => 
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   212
      if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env))
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   213
      else env
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   214
    case Write(x) => { println(env(x)) ; env }  
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   215
  }
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   216
def eval_bl(bl: Block, env: Env) : Env =
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   217
  bl match {
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   218
    case Nil => env
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   219
    case s::bl => eval_bl(bl, eval_stmt(s, env))
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   220
  }
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
def eval(bl: Block) : Env = eval_bl(bl, Map())
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
// parse + evaluate fib program; then lookup what is
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   225
// stored under the variable "result" 
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
println(eval(Stmts.parse_all(fib).head)("result"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 965
diff changeset
   229
// more examples
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
// calculate and print all factors bigger 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
// than 1 and smaller than n
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
println("Factors")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
val factors =  
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
   """n := 12;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
      f := 2;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
      while (f < n / 2 + 1) do {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
        if ((n / f) * f == n) then  { write(f) } else { skip };
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
        f := f + 1
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
      }""".replaceAll("\\s+", "")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
println(eval(Stmts.parse_all(factors).head))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   245
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
// calculate all prime numbers up to a number 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
println("Primes")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
val primes =  
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
   """end := 100;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
      n := 2;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
      while (n < end) do {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
        f := 2;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
        tmp := 0;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
        while ((f < n / 2 + 1) && (tmp == 0)) do {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
          if ((n / f) * f == n) then  { tmp := 1 } else { skip };
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   257
          f := f + 1
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   258
        };
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   259
        if (tmp == 0) then { write(n) } else { skip };
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
        n  := n + 1
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
      }""".replaceAll("\\s+", "")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   263
println(eval(Stmts.parse_all(primes).head))