progs/comb2.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 01 Nov 2013 11:57:04 +0000
changeset 173 7cfb7a6f7c99
parent 172 47b5c91eff47
child 185 ea8b94d4755e
permissions -rw-r--r--
added slides
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
import scala.language.implicitConversions
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
import scala.language.reflectiveCalls
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
abstract class Parser[I <% Seq[_], T] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
  def parse(ts: I): Set[(T, I)]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
  def parse_all(ts: I) : Set[T] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
    for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
class SeqParser[I <% Seq[_], T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, (T, S)] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
    for ((head1, tail1) <- p.parse(sb); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
         (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
class AltParser[I <% Seq[_], T](p: => Parser[I, T], q: => Parser[I, T]) extends Parser[I, T] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
class FunParser[I <% Seq[_], T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
case class StringParser(s: String) extends Parser[String, String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  def parse(sb: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
    val (prefix, suffix) = sb.splitAt(s.length)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
    if (prefix == s) Set((prefix, suffix)) else Set()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
case object NumParser extends Parser[String, Int] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  val reg = "[0-9]+".r
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  def parse(sb: String) = reg.findPrefixOf(sb) match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
    case None => Set()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
    case Some(s) => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
      val (head, tail) = sb.splitAt(s.length)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
      Set((head.toInt, tail)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
    }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
implicit def string2parser(s : String) = StringParser(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
implicit def StringOps(s: String) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  def || (q : => Parser[String, String]) = new AltParser[String, String](s, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  def || (r: String) = new AltParser[String, String](s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
  def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  def ~[S](q : => Parser[String, S]) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
    new SeqParser[String, String, S](s, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
  def ~ (r: String) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
    new SeqParser[String, String, String](s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
lazy val E: Parser[String, Int] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  (T ~ "+" ~ E) ==> { case ((x, y), z) => x + z} || T  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
lazy val T: Parser[String, Int] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
  (F ~ "*" ~ T) ==> { case ((x, y), z) => x * z} || F
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
lazy val F: Parser[String, Int] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
  ("(" ~ E ~ ")") ==> { case ((x, y), z) => y} || NumParser
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
println(E.parse_all("1+2+3"))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
println(E.parse_all("1+2*3+1"))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
// no left-recursion allowed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
lazy val EL: Parser[String, Int] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
  ((EL ~ "+" ~ EL) ==> { case ((x, y), z) => x + z} || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
   (EL ~ "*" ~ EL) ==> { case ((x, y), z) => x * z} ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
   ("(" ~ EL ~ ")") ==> { case ((x, y), z) => y} ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
   NumParser)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
//println(E.parse_all("1+2+3"))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
// the abstract syntax trees for the WHILE language
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
abstract class Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
abstract class AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
abstract class BExp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
type Block = List[Stmt]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
case object Skip extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
case class While(b: BExp, bl: Block) extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
case class Assign(s: String, a: AExp) extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
case class Var(s: String) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
case class Num(i: Int) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
case object True extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
case object False extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
case object IdParser extends Parser[String, String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
  val reg = "[a-z][a-z,0-9]*".r
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
  def parse(sb: String) = reg.findPrefixOf(sb) match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
    case None => Set()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
    case Some(s) => Set(sb.splitAt(s.length))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
lazy val AExp: Parser[String, AExp] = 
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
   114
  ((Te ~ "+" ~ AExp) ==> { case ((x, y), z) => Aop("+", x, z):AExp } ||
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
   115
   (Te ~ "-" ~ AExp) ==> { case ((x, y), z) => Aop("-", x, z):AExp } || Te)  
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
lazy val Te: Parser[String, AExp] = 
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
   117
  (Fa ~ "*" ~ Te) ==> { case ((x, y), z) => Aop("*", x, z):AExp } || Fa
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
lazy val Fa: Parser[String, AExp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
  (("(" ~ AExp ~ ")") ==> { case ((x, y), z) => y } || 
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
   120
   IdParser ==> Var || 
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
   121
   NumParser ==> Num)
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
// boolean expressions
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
lazy val BExp: Parser[String, BExp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
  ((AExp ~ "=" ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z): BExp } || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
   (AExp ~ "!=" ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z): BExp } || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
   (AExp ~ "<" ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z): BExp } || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
   (AExp ~ ">" ~ AExp) ==> { case ((x, y), z) => Bop(">", x, z): BExp } || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
   ("true" ==> ((_) => True: BExp)) || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
   ("false" ==> ((_) => False: BExp)) ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
   ("(" ~ BExp ~ ")") ==> { case ((x, y), z) => y})
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
lazy val Stmt: Parser[String, Stmt] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
  (("skip" ==> ((_) => Skip: Stmt)) ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
   (IdParser ~ ":=" ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
   ("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block) ==>
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
    { case (((((x,y),z),u),v),w) => If(y, u, w): Stmt } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
   ("while" ~ BExp ~ "do" ~ Block) ==> { case (((x, y), z), w) => While(y, w) }) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
lazy val Stmts: Parser[String, Block] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
  (Stmt ~ ";" ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
  (Stmt ==> ((s) => List(s) : Block))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
lazy val Block: Parser[String, Block] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
  (("{" ~ Stmts ~ "}") ==> { case ((x, y), z) => y} || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
   (Stmt ==> ((s) => List(s))))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
Block.parse_all("x2:=5")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
Block.parse_all("{x:=5;y:=8}")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
Block.parse_all("if(false)then{x:=5}else{x:=10}")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
val fib = """{n:=10;minus1:=0;minus2:=1;temp:=0;while(n>0)do{temp:=minus2;minus2:=minus1+minus2;minus1:=temp;n:=n-1};result:=minus2}"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
Block.parse_all(fib)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
// an interpreter for the WHILE language
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
type Env = Map[String, Int]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
def eval_aexp(a: AExp, env : Env) : Int = a match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
  case Num(i) => i
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
  case Var(s) => env(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
  case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
  case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
  case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
def eval_bexp(b: BExp, env: Env) : Boolean = b match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
  case True => true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
  case False => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
  case Bop("=", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
  case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
  case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
  case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
def eval_stmt(s: Stmt, env: Env) : Env = s match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
  case Skip => env
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
  case Assign(x, a) => env + (x -> eval_aexp(a, env))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
  case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
  case While(b, bl) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
    if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
    else env
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
def eval_bl(bl: Block, env: Env) : Env = bl match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
  case Nil => env
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
  case s::bl => eval_bl(bl, eval_stmt(s, env))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
def eval(bl: Block) : Env = eval_bl(bl, Map())
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
eval(Block.parse_all(fib).head)("result")