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