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