progs/parser-combinators/c.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 30 Jun 2020 01:31:38 +0100
changeset 731 90946a2eb48a
child 805 526e10d97435
permissions -rw-r--r--
update
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
731
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// A parser and interpreter for the While language
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
// 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
import scala.language.implicitConversions
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
import scala.language.reflectiveCalls
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
// more convenience for the semantic actions later on
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
case class ~[+A, +B](_1: A, _2: B)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
type IsSeq[A] = A => Seq[_]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
abstract class Parser[I : IsSeq, T] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
  def parse(ts: I): Set[(T, I)]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
  def parse_all(ts: I) : Set[T] =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
    for ((head, tail) <- parse(ts); if tail.isEmpty) yield head
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
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
    21
  def parse(sb: I) = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
    for ((head1, tail1) <- p.parse(sb); 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
         (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
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
    27
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
class MapParser[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
    31
  def parse(sb: I) = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
case class StrParser(s: String) extends Parser[String, String] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
  def parse(sb: String) = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
    val (prefix, suffix) = sb.splitAt(s.length)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
    if (prefix == s) Set((prefix, suffix)) else Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
case object NumParser extends Parser[String, Int] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
  val reg = "[0-9]+".r
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
  def parse(sb: String) = reg.findPrefixOf(sb) match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
    case None => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
    case Some(s) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
      val (head, tail) = sb.splitAt(s.length)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
      Set((head.toInt, tail)) 
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
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
implicit def parser_interpolation(sc: StringContext) = new {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
    def p(args: Any*) = StrParser(sc.s(args:_*))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
// this string interpolation allows us to write 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
// things like the following for a StrParser
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
// p"<_some_string_>" 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
// instead of StrParser(<_some_string_>)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
  def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
  def ~[S](q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
  def map[S](f: => T => S) = new MapParser[I, T, S](p, f) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
// these implicits allow us to use infic notation for
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
// sequences and alternatives; we also can write map
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
// for a parser
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
// the abstract syntax trees for the WHILE language
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
abstract class Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
abstract class AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
abstract class BExp 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
type Block = List[Stmt]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
case object Skip extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
case class While(b: BExp, bl: Block) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
case class Assign(s: String, a: AExp) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
case class Write(s: String) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
case class Var(s: String) extends AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
case class Num(i: Int) extends AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
case object True extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
case object False extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
case class And(b1: BExp, b2: BExp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
case class Or(b1: BExp, b2: BExp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
case object IdParser extends Parser[String, String] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
  val reg = "[a-z][a-z,0-9]*".r
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
  def parse(sb: String) = reg.findPrefixOf(sb) match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
    case None => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
    case Some(s) => Set(sb.splitAt(s.length))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  }
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
// arithmetic expressions
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
lazy val AExp: Parser[String, AExp] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
  (Te ~ p"+" ~ AExp).map[AExp]{ case x ~ _ ~ z => Aop("+", x, z) } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
  (Te ~ p"-" ~ AExp).map[AExp]{ case x ~ _ ~ z => Aop("-", x, z) } || Te
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
lazy val Te: Parser[String, AExp] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
  (Fa ~ p"*" ~ Te).map[AExp]{ case x ~ _ ~ z => Aop("*", x, z) } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
  (Fa ~ p"/" ~ Te).map[AExp]{ case x ~ _ ~ z => Aop("/", x, z) } || Fa  
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
lazy val Fa: Parser[String, AExp] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
   (p"(" ~ AExp ~ p")").map{ case _ ~ y ~ _ => y } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
   IdParser.map(Var) || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
   NumParser.map(Num)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
// boolean expressions with some simple nesting
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
lazy val BExp: Parser[String, BExp] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
   (AExp ~ p"==" ~ AExp).map[BExp]{ case x ~ _ ~ z => Bop("==", x, z) } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
   (AExp ~ p"!=" ~ AExp).map[BExp]{ case x ~ _ ~ z => Bop("!=", x, z) } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
   (AExp ~ p"<" ~ AExp).map[BExp]{ case x ~ _ ~ z => Bop("<", x, z) } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
   (AExp ~ p">" ~ AExp).map[BExp]{ case x ~ _ ~ z => Bop(">", x, z) } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
   (p"(" ~ BExp ~ p")" ~ p"&&" ~ BExp).map[BExp]{ case _ ~ y ~ _ ~ _ ~ v => And(y, v) } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
   (p"(" ~ BExp ~ p")" ~ p"||" ~ BExp).map[BExp]{ case _ ~ y ~ _ ~ _ ~ v => Or(y, v) } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
   (p"true".map[BExp]{ _ => True }) || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
   (p"false".map[BExp]{ _ => False }) ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
   (p"(" ~ BExp ~ p")").map[BExp]{ case _ ~ x ~ _ => x }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
// statement / statements
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
lazy val Stmt: Parser[String, Stmt] =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
  ((p"skip".map[Stmt]{_ => Skip }) ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
   (IdParser ~ p":=" ~ AExp).map[Stmt]{ case x ~ _ ~ z => Assign(x, z) } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
   (p"write(" ~ IdParser ~ p")").map[Stmt]{ case _ ~ y ~ _ => Write(y) } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
   (p"if" ~ BExp ~ p"then" ~ Block ~ p"else" ~ Block)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
     .map[Stmt]{ case _ ~ y ~ _ ~ u ~ _ ~ w => If(y, u, w) } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
   (p"while" ~ BExp ~ p"do" ~ Block).map[Stmt]{ case _ ~ y ~ _ ~ w => While(y, w) }) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
lazy val Stmts: Parser[String, Block] =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
  (Stmt ~ p";" ~ Stmts).map[Block]{ case x ~ _ ~ z => x :: z } ||
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
  (Stmt.map[Block]{ s => List(s) })
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
// blocks (enclosed in curly braces)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
lazy val Block: Parser[String, Block] =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
  ((p"{" ~ Stmts ~ p"}").map{ case _ ~ y ~ _ => y } || 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
   (Stmt.map(s => List(s))))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
Stmts.parse_all("x2:=5+3;")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
Block.parse_all("{x:=5;y:=8}")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
Block.parse_all("if(false)then{x:=5}else{x:=10}")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
val fib = """n := 10;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
             minus1 := 0;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
             minus2 := 1;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
             temp := 0;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
             while (n > 0) do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
                 temp := minus2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
                 minus2 := minus1 + minus2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
                 minus1 := temp;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
                 n := n - 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
             };
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
             result := minus2""".replaceAll("\\s+", "")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
Stmts.parse_all(fib)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
// an interpreter for the WHILE language
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
type Env = Map[String, Int]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
def eval_aexp(a: AExp, env: Env) : Int = a match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
  case Num(i) => i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
  case Var(s) => env(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
  case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
  case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
  case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env)
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
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
def eval_bexp(b: BExp, env: Env) : Boolean = b match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
  case True => true
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
  case False => false
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
  case Bop("==", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
  case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
  case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
  case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
  case And(b1, b2) => eval_bexp(b1, env) && eval_bexp(b2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
  case Or(b1, b2) => eval_bexp(b1, env) || eval_bexp(b2, env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
def eval_stmt(s: Stmt, env: Env) : Env = s match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
  case Skip => env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
  case Assign(x, a) => env + (x -> eval_aexp(a, env))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
  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
   198
  case While(b, bl) => 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
    if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
    else env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
  case Write(x) => { println(env(x)) ; env }  
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
def eval_bl(bl: Block, env: Env) : Env = bl match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
  case Nil => env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
  case s::bl => eval_bl(bl, eval_stmt(s, env))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
def eval(bl: Block) : Env = eval_bl(bl, Map())
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
// parse + evaluate fib program; then lookup what is
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
// stored under the variable result 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
println(eval(Stmts.parse_all(fib).head)("result"))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
// more examles
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
// calculate and print all factors bigger 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
// than 1 and smaller than n
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
println("Factors")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
val factors =  
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
   """n := 12;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
      f := 2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
      while (f < n / 2 + 1) do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
        if ((n / f) * f == n) then  { write(f) } else { skip };
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
        f := f + 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
      }""".replaceAll("\\s+", "")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
eval(Stmts.parse_all(factors).head)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
// calculate all prime numbers up to a number 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
println("Primes")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
val primes =  
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
   """end := 100;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
      n := 2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
      while (n < end) do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
        f := 2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
        tmp := 0;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
        while ((f < n / 2 + 1) && (tmp == 0)) do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
          if ((n / f) * f == n) then  { tmp := 1 } else { skip };
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
          f := f + 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
        };
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
        if (tmp == 0) then { write(n) } else { skip };
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
        n  := n + 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
      }""".replaceAll("\\s+", "")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
eval(Stmts.parse_all(primes).head)