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