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