progs/comb2.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 27 Oct 2019 13:03:58 +0000
changeset 673 715b46eee102
parent 628 8067d0a8ba04
child 683 c6c79d21f8a8
permissions -rw-r--r--
updated
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
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
     7
// more convenience for the semantic actions later on
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
     8
case class ~[+A, +B](_1: A, _2: B)
529
5c28e4134ee1 updated
cu
parents: 471
diff changeset
     9
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    10
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    11
type IsSeq[A] = A => Seq[_]
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    12
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    13
abstract class Parser[I : IsSeq, T] {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
  def parse(ts: I): Set[(T, I)]
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
  def parse_all(ts: I) : Set[T] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
    for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    20
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
    for ((head1, tail1) <- p.parse(sb); 
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    23
         (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
172
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
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    26
class AltParser[I : IsSeq, T](p: => Parser[I, T], q: => Parser[I, T]) extends Parser[I, T] {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
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
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    30
class FunParser[I : IsSeq, T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
}
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
case class StringParser(s: String) extends Parser[String, String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  def parse(sb: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
    val (prefix, suffix) = sb.splitAt(s.length)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
    if (prefix == s) Set((prefix, suffix)) else Set()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  }
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
case object NumParser extends Parser[String, Int] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
  val reg = "[0-9]+".r
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  def parse(sb: String) = reg.findPrefixOf(sb) match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
    case None => Set()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
    case Some(s) => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
      val (head, tail) = sb.splitAt(s.length)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
      Set((head.toInt, tail)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
    }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  }
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
implicit def string2parser(s : String) = StringParser(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    56
implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  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
    58
  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
    59
  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
    60
}
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
implicit def StringOps(s: String) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  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
    64
  def || (r: String) = new AltParser[String, String](s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
  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
    66
  def ~[S](q : => Parser[String, S]) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
    new SeqParser[String, String, S](s, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
  def ~ (r: String) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
    new SeqParser[String, String, String](s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
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
// the abstract syntax trees for the WHILE language
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
abstract class Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
abstract class AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
abstract class BExp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
type Block = List[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 object Skip extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
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
    82
case class While(b: BExp, bl: Block) extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
case class Assign(s: String, a: AExp) extends Stmt
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
    84
case class Write(s: String) extends Stmt
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
    85
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
case class Var(s: String) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
case class Num(i: Int) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
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
    90
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
case object True extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
case object False extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
    94
case class And(b1: BExp, b2: BExp) extends BExp
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
    95
case class Or(b1: BExp, b2: BExp) extends BExp
172
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
case object IdParser extends Parser[String, String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
  val reg = "[a-z][a-z,0-9]*".r
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
  def parse(sb: String) = reg.findPrefixOf(sb) match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
    case None => Set()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
    case Some(s) => Set(sb.splitAt(s.length))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  }
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
lazy val AExp: Parser[String, AExp] = 
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   106
  (Te ~ "+" ~ AExp) ==> { case x ~ y ~ z => Aop("+", x, z): AExp } ||
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   107
  (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
   108
lazy val Te: Parser[String, AExp] = 
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   109
  (Fa ~ "*" ~ Te) ==> { case x ~ y ~ z => Aop("*", x, z): AExp } || 
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   110
  (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
   111
lazy val Fa: Parser[String, AExp] = 
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   112
   ("(" ~ 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
   113
   IdParser ==> Var || 
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   114
   NumParser ==> Num
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   116
// boolean expressions with some simple nesting
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
lazy val BExp: Parser[String, BExp] = 
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   118
   (AExp ~ "==" ~ AExp) ==> { case x ~ y ~ z => Bop("==", x, z): BExp } || 
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   119
   (AExp ~ "!=" ~ AExp) ==> { case x ~ y ~ z => Bop("!=", x, z): BExp } || 
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   120
   (AExp ~ "<" ~ AExp) ==> { case x ~ y ~ z => Bop("<", x, z): BExp } || 
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   121
   (AExp ~ ">" ~ AExp) ==> { case x ~ y ~ z => Bop(">", x, z): BExp } ||
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   122
   ("(" ~ BExp ~ ")" ~ "&&" ~ BExp) ==> { case x ~ y ~ z ~ u ~ v => And(y, v): BExp } ||
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   123
   ("(" ~ BExp ~ ")" ~ "||" ~ BExp) ==> { case x ~ y ~ z ~ u ~ v => Or(y, v): BExp } ||
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   124
   ("true" ==> (_ => True: BExp )) || 
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   125
   ("false" ==> (_ => False: BExp )) ||
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   126
   ("(" ~ BExp ~ ")") ==> { case x ~ y ~ z => y}
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   128
// statement / statements
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
lazy val Stmt: Parser[String, Stmt] =
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   130
  (("skip" ==> (_ => Skip: Stmt)) ||
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   131
   (IdParser ~ ":=" ~ AExp) ==> { case x ~ y ~ z => Assign(x, z): Stmt } ||
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   132
   ("write(" ~ IdParser ~ ")") ==> { case x ~y ~ z => Write(y): Stmt } ||
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
   ("if" ~ BExp ~ "then" ~ Block ~ "else" ~ Block) ==>
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   134
    { case x ~ y ~ z ~ u ~ v ~ w => If(y, u, w): Stmt } ||
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   135
   ("while" ~ BExp ~ "do" ~ Block) ==> { case x ~ y ~ z ~ w => While(y, w) }) 
172
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
lazy val Stmts: Parser[String, Block] =
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   138
  (Stmt ~ ";" ~ Stmts) ==> { case x ~ y ~ z => x :: z : Block } ||
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   139
  (Stmt ==> ( s => List(s) : Block))
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   141
// blocks (enclosed in curly braces)
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
lazy val Block: Parser[String, Block] =
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   143
  (("{" ~ Stmts ~ "}") ==> { case x ~ y ~ z => y} || 
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   144
   (Stmt ==> (s => List(s))))
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
607
3f4fc76dab2f updated
Christian Urban <urbanc@in.tum.de>
parents: 531
diff changeset
   147
Stmts.parse_all("x2:=5+3;")
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
Block.parse_all("{x:=5;y:=8}")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
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
   150
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   151
val fib = """n := 10;
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   152
             minus1 := 0;
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   153
             minus2 := 1;
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   154
             temp := 0;
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   155
             while (n > 0) do {
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   156
                 temp := minus2;
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   157
                 minus2 := minus1 + minus2;
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   158
                 minus1 := temp;
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   159
                 n := n - 1
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   160
             };
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   161
             result := minus2""".replaceAll("\\s+", "")
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   163
Stmts.parse_all(fib)
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   164
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
// an interpreter for the WHILE language
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
type Env = Map[String, Int]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 607
diff changeset
   169
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
   170
  case Num(i) => i
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
  case Var(s) => env(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
  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
   173
  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
   174
  case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env)
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   175
  case Aop("/", a1, a2) => eval_aexp(a1, env) / eval_aexp(a2, env)
172
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
def eval_bexp(b: BExp, env: Env) : Boolean = b match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
  case True => true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
  case False => false
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   181
  case Bop("==", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
  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
   183
  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
   184
  case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   185
  case And(b1, b2) => eval_bexp(b1, env) && eval_bexp(b2, env)
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   186
  case Or(b1, b2) => eval_bexp(b1, env) || eval_bexp(b2, env)
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
def eval_stmt(s: Stmt, env: Env) : Env = s match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
  case Skip => env
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
  case Assign(x, a) => env + (x -> eval_aexp(a, env))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
  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
   193
  case While(b, bl) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
    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
   195
    else env
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   196
  case Write(x) => { println(env(x)) ; env }  
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
def eval_bl(bl: Block, env: Env) : Env = bl match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
  case Nil => env
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
  case s::bl => eval_bl(bl, eval_stmt(s, env))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
def eval(bl: Block) : Env = eval_bl(bl, Map())
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 607
diff changeset
   206
// parse + evaluate fib program; then lookup what is
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 607
diff changeset
   207
// stored under the variable result 
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   208
println(eval(Stmts.parse_all(fib).head)("result"))
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   209
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   210
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   211
// more examles
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   212
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   213
// calculate and print all factors bigger 
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   214
// than 1 and smaller than n
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   215
println("Factors")
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   216
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   217
val factors =  
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   218
   """n := 12;
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   219
      f := 2;
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   220
      while (f < n / 2 + 1) do {
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   221
        if ((n / f) * f == n) then  { write(f) } else { skip };
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   222
        f := f + 1
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   223
      }""".replaceAll("\\s+", "")
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   224
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   225
eval(Stmts.parse_all(factors).head)
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   226
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   227
// calculate all prime numbers up to a number 
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   228
println("Primes")
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   229
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   230
val primes =  
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   231
   """end := 100;
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   232
      n := 2;
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   233
      while (n < end) do {
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   234
        f := 2;
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   235
        tmp := 0;
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   236
        while ((f < n / 2 + 1) && (tmp == 0)) do {
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   237
          if ((n / f) * f == n) then  { tmp := 1 } else { skip };
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   238
          f := f + 1
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   239
        };
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   240
        if (tmp == 0) then { write(n) } else { skip };
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   241
        n  := n + 1
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   242
      }""".replaceAll("\\s+", "")
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   243
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 627
diff changeset
   244
eval(Stmts.parse_all(primes).head)