progs/comb2.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 28 Jul 2019 14:24:46 +0100
changeset 624 8d0af38389bc
parent 607 3f4fc76dab2f
child 627 f5214da1976e
permissions -rw-r--r--
updated to Scala 2.13
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 607
diff changeset
     1
// A parser and interpreter for the While language
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
     2
// 
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
     3
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
import scala.language.implicitConversions
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
import scala.language.reflectiveCalls
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
529
5c28e4134ee1 updated
cu
parents: 471
diff changeset
     7
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 607
diff changeset
     8
abstract class Parser[I, T](implicit ev: I => Seq[_]) {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
  def parse(ts: I): Set[(T, I)]
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
  def parse_all(ts: I) : Set[T] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
    for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 607
diff changeset
    15
class SeqParser[I, T, S](p: => Parser[I, T], q: => Parser[I, S])(implicit ev: I => Seq[_]) extends Parser[I, (T, S)] {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
    for ((head1, tail1) <- p.parse(sb); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
         (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
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
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 607
diff changeset
    21
class AltParser[I, T](p: => Parser[I, T], q: => Parser[I, T])(implicit ev: I => Seq[_]) extends Parser[I, T] {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 607
diff changeset
    25
class FunParser[I, T, S](p: => Parser[I, T], f: T => S)(implicit ev: I => Seq[_]) extends Parser[I, S] {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
case class StringParser(s: String) extends Parser[String, String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  def parse(sb: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
    val (prefix, suffix) = sb.splitAt(s.length)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
    if (prefix == s) Set((prefix, suffix)) else Set()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
case object NumParser extends Parser[String, Int] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  val reg = "[0-9]+".r
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  def parse(sb: String) = reg.findPrefixOf(sb) match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
    case None => Set()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
    case Some(s) => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
      val (head, tail) = sb.splitAt(s.length)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
      Set((head.toInt, tail)) 
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
  }
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
implicit def string2parser(s : String) = StringParser(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 607
diff changeset
    51
implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = new {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  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
    53
  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
    54
  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
    55
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
implicit def StringOps(s: String) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
  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
    59
  def || (r: String) = new AltParser[String, String](s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
  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
    61
  def ~[S](q : => Parser[String, S]) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
    new SeqParser[String, String, S](s, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  def ~ (r: String) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
    new SeqParser[String, String, String](s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
// the abstract syntax trees for the WHILE language
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
abstract class Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
abstract class AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
abstract class BExp 
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
type Block = List[Stmt]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
case object Skip extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
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
    77
case class While(b: BExp, bl: Block) extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
case class Assign(s: String, a: AExp) extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
case class Var(s: String) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
case class Num(i: Int) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
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
    83
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
case object True extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
case object False extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
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
    87
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
case object IdParser extends Parser[String, String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
  val reg = "[a-z][a-z,0-9]*".r
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
  def parse(sb: String) = reg.findPrefixOf(sb) match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
    case None => Set()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
    case Some(s) => Set(sb.splitAt(s.length))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
  }
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
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
    98
  ((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
    99
   (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
   100
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
   101
  (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
   102
lazy val Fa: Parser[String, AExp] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
  (("(" ~ 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
   104
   IdParser ==> Var || 
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
   105
   NumParser ==> Num)
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
// boolean expressions
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
lazy val BExp: Parser[String, BExp] = 
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   109
  ((AExp ~ "=" ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z):BExp } || 
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   110
   (AExp ~ "!=" ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z):BExp } || 
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   111
   (AExp ~ "<" ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z):BExp } || 
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   112
   (AExp ~ ">" ~ AExp) ==> { case ((x, y), z) => Bop(">", x, z):BExp } || 
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   113
   ("true" ==> ((_) => True:BExp )) || 
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   114
   ("false" ==> ((_) => False:BExp )) ||
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
   ("(" ~ BExp ~ ")") ==> { case ((x, y), z) => y})
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
lazy val Stmt: Parser[String, Stmt] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
  (("skip" ==> ((_) => Skip: Stmt)) ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
   (IdParser ~ ":=" ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
   ("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block) ==>
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
    { 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
   122
   ("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
   123
 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
lazy val Stmts: Parser[String, Block] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
  (Stmt ~ ";" ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
  (Stmt ==> ((s) => List(s) : Block))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
lazy val Block: Parser[String, Block] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
  (("{" ~ Stmts ~ "}") ==> { case ((x, y), z) => y} || 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
   (Stmt ==> ((s) => List(s))))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
607
3f4fc76dab2f updated
Christian Urban <urbanc@in.tum.de>
parents: 531
diff changeset
   133
Stmts.parse_all("x2:=5+3;")
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
Block.parse_all("{x:=5;y:=8}")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
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
   136
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
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
   138
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
Block.parse_all(fib)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
// an interpreter for the WHILE language
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
type Env = Map[String, Int]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 607
diff changeset
   144
def eval_aexp(a: AExp, env: Env) : Int = a match {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
  case Num(i) => i
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
  case Var(s) => env(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
  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
   148
  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
   149
  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
   150
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
def eval_bexp(b: BExp, env: Env) : Boolean = b match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
  case True => true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
  case False => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
  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
   156
  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
   157
  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
   158
  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
   159
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
def eval_stmt(s: Stmt, env: Env) : Env = s match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
  case Skip => env
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
  case Assign(x, a) => env + (x -> eval_aexp(a, env))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
  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
   165
  case While(b, bl) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
    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
   167
    else env
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
def eval_bl(bl: Block, env: Env) : Env = bl match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
  case Nil => env
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
  case s::bl => eval_bl(bl, eval_stmt(s, env))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
def eval(bl: Block) : Env = eval_bl(bl, Map())
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 607
diff changeset
   177
// parse + evaluate fib program; then lookup what is
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 607
diff changeset
   178
// stored under the variable result 
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   179
println(eval(Block.parse_all(fib).head)("result"))