864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// CW3
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
import $file.lexer
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
import lexer._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
|
919
|
7 |
case class ~[+A, +B](x: A, y: B)
|
|
8 |
|
|
9 |
// parser combinators
|
|
10 |
|
|
11 |
abstract class Parser[I, T](using is: I => Seq[_]) {
|
|
12 |
def parse(in: I): Set[(T, I)]
|
|
13 |
|
|
14 |
def parse_all(in: I) : Set[T] =
|
|
15 |
for ((hd, tl) <- parse(in);
|
|
16 |
if is(tl).isEmpty) yield hd
|
|
17 |
}
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
|
919
|
19 |
// alternative parser
|
|
20 |
class AltParser[I, T](p: => Parser[I, T],
|
|
21 |
q: => Parser[I, T])(using I => Seq[_]) extends Parser[I, T] {
|
|
22 |
def parse(in: I) = p.parse(in) ++ q.parse(in)
|
|
23 |
}
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
|
919
|
25 |
// sequence parser
|
|
26 |
class SeqParser[I, T, S](p: => Parser[I, T],
|
|
27 |
q: => Parser[I, S])(using I => Seq[_]) extends Parser[I, ~[T, S]] {
|
|
28 |
def parse(in: I) =
|
|
29 |
for ((hd1, tl1) <- p.parse(in);
|
|
30 |
(hd2, tl2) <- q.parse(tl1)) yield (new ~(hd1, hd2), tl2)
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
|
919
|
33 |
// map parser
|
|
34 |
class MapParser[I, T, S](p: => Parser[I, T],
|
|
35 |
f: T => S)(using I => Seq[_]) extends Parser[I, S] {
|
|
36 |
def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl)
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
|
919
|
39 |
|
|
40 |
/*
|
|
41 |
// atomic parser for (particular) strings
|
|
42 |
case class StrParser(s: String) extends Parser[String, String] {
|
|
43 |
def parse(sb: String) = {
|
|
44 |
val (prefix, suffix) = sb.splitAt(s.length)
|
|
45 |
if (prefix == s) Set((prefix, suffix)) else Set()
|
|
46 |
}
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
|
919
|
49 |
extension (sc: StringContext)
|
|
50 |
def p(args: Any*) = StrParser(sc.s(args:_*))
|
|
51 |
*/
|
|
52 |
|
|
53 |
// more convenient syntax for parser combinators
|
|
54 |
extension [I, T](p: Parser[I, T])(using I => Seq[_]) {
|
|
55 |
def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
|
|
56 |
def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
|
|
57 |
def map[S](f: => T => S) = new MapParser[I, T, S](p, f)
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
// New parser that takes as input a list of tokens
|
919
|
61 |
case class TokenParser(t: Token) extends Parser[List[Token], Token] {
|
|
62 |
def parse(in: List[Token]) = {
|
|
63 |
// an example of an atomic parser for characters
|
|
64 |
if (!in.isEmpty && in.head == t) Set((t, in.tail)) else Set()
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
case class TokenListParser(ts: List[Token]) extends Parser[List[Token], List[Token]] {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
def parse(tsb: List[Token]) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
val (prefix, suffix) = tsb.splitAt(ts.length)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
if (prefix == ts) Set((prefix, suffix)) else Set()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
// Implicit definitions to go from a token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
// or a list of tokens to a TokenListParser
|
919
|
77 |
implicit def token2parser(t: Token) : Parser[List[Token], Token] =
|
|
78 |
TokenParser(t)
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
|
919
|
80 |
extension (t: Token) {
|
|
81 |
def || (q : => Parser[List[Token], Token]) =
|
|
82 |
new AltParser[List[Token], Token](t, q)
|
|
83 |
def ~[S](q : => Parser[List[Token], S]) =
|
|
84 |
new SeqParser[List[Token], Token, S](t, q)
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
// Abstract Syntax Trees
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
abstract class Stmt
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
abstract class AExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
abstract class BExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
type Block = List[Stmt]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
case object Skip extends Stmt
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
case class While(b: BExp, bl: Block) extends Stmt
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
case class Assign(s: String, a: AExp) extends Stmt
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
case class Read(s: String) extends Stmt
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
case class WriteId(s: String) extends Stmt // for printing values of variables
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
case class WriteString(s: String) extends Stmt // for printing words
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
case class Var(s: String) extends AExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
104 |
case class Num(i: Int) extends AExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
105 |
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
case object True extends BExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
case object False extends BExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
110 |
case class And(b1: BExp, b2: BExp) extends BExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
case class Or(b1: BExp, b2: BExp) extends BExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
case class IdParser() extends Parser[List[Token], String] {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
def parse(tsb: List[Token]) = tsb match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
case T_ID(id) :: rest => Set((id, rest))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
case _ => Set()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
case class NumParser() extends Parser[List[Token], Int] {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
def parse(tsb: List[Token]) = tsb match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
case T_NUM(n) :: rest => Set((n, rest))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
case _ => Set()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
125 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
case class StringParser() extends Parser[List[Token], String] {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
128 |
def parse(tsb: List[Token]) = tsb match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
case T_STRING(s) :: rest => Set((s, rest))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
case _ => Set()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
131 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
132 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
133 |
|
919
|
134 |
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
135 |
// WHILE Language Parsing
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
136 |
lazy val AExp: Parser[List[Token], AExp] =
|
919
|
137 |
(Te ~ T_OP("+") ~ AExp).map{ case x ~ _ ~ z => Aop("+", x, z): AExp } ||
|
|
138 |
(Te ~ T_OP("-") ~ AExp).map{ case x ~ _ ~ z => Aop("-", x, z): AExp } || Te
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
139 |
lazy val Te: Parser[List[Token], AExp] =
|
919
|
140 |
(Fa ~ T_OP("*") ~ Te).map{ case x ~ _ ~ z => Aop("*", x, z): AExp } ||
|
|
141 |
(Fa ~ T_OP("/") ~ Te).map{ case x ~ _ ~ z => Aop("/", x, z): AExp } ||
|
|
142 |
(Fa ~ T_OP("%") ~ Te).map{ case x ~ _ ~ z => Aop("%", x, z): AExp } || Fa
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
143 |
lazy val Fa: Parser[List[Token], AExp] =
|
919
|
144 |
(T_PAREN("(") ~ AExp ~ T_PAREN(")")).map{ case _ ~ y ~ _ => y } ||
|
|
145 |
IdParser().map{Var(_)} ||
|
|
146 |
NumParser().map{Num(_)}
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
147 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
148 |
lazy val BExp: Parser[List[Token], BExp] =
|
919
|
149 |
(AExp ~ T_OP("==") ~ AExp).map{ case x ~ _ ~ z => Bop("==", x, z): BExp } ||
|
|
150 |
(AExp ~ T_OP("!=") ~ AExp).map{ case x ~ _ ~ z => Bop("!=", x, z): BExp } ||
|
|
151 |
(AExp ~ T_OP("<") ~ AExp).map{ case x ~ _ ~ z => Bop("<", x, z): BExp } ||
|
|
152 |
(AExp ~ T_OP(">") ~ AExp).map{ case x ~ _ ~ z => Bop(">", x, z): BExp } ||
|
|
153 |
(T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("&&") ~ BExp).map{ case _ ~ y ~ _ ~ _ ~ v => And(y, v): BExp } ||
|
|
154 |
(T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("||") ~ BExp).map{ case _ ~ y ~ _ ~ _ ~ v => Or(y, v): BExp } ||
|
|
155 |
(T_KEYWORD("true").map(_ => True: BExp )) ||
|
|
156 |
(T_KEYWORD("false").map(_ => False: BExp )) ||
|
|
157 |
(T_PAREN("(") ~ BExp ~ T_PAREN(")")).map{ case _ ~ x ~ _ => x }
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
158 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
159 |
lazy val Stmt: Parser[List[Token], Stmt] =
|
919
|
160 |
T_KEYWORD("skip").map(_ => Skip: Stmt) ||
|
|
161 |
(IdParser() ~ T_OP(":=") ~ AExp).map{ case id ~ _ ~ z => Assign(id, z): Stmt } ||
|
|
162 |
(T_KEYWORD("if") ~ BExp ~ T_KEYWORD("then") ~ Block ~ T_KEYWORD("else") ~ Block).map{ case _ ~ y ~ _ ~ u ~ _ ~ w => If(y, u, w): Stmt } ||
|
|
163 |
(T_KEYWORD("while") ~ BExp ~ T_KEYWORD("do") ~ Block).map{ case _ ~ y ~ _ ~ w => While(y, w) : Stmt } ||
|
|
164 |
(T_KEYWORD("read") ~ IdParser()).map{ case _ ~ id => Read(id): Stmt} ||
|
|
165 |
(T_KEYWORD("write") ~ IdParser()).map{ case _ ~ id => WriteId(id): Stmt} ||
|
|
166 |
(T_KEYWORD("write") ~ StringParser()).map{ case _ ~ s => WriteString(s): Stmt} ||
|
|
167 |
(T_KEYWORD("write") ~ T_PAREN("(") ~ IdParser() ~ T_PAREN(")")).map{ case _ ~ _ ~ id ~ _ => WriteId(id): Stmt} ||
|
|
168 |
(T_KEYWORD("write") ~ T_PAREN("(") ~ StringParser() ~ T_PAREN(")")).map{ case _ ~ _ ~ s ~ _ => WriteString(s): Stmt}
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
169 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
170 |
lazy val Stmts: Parser[List[Token], Block] =
|
919
|
171 |
(Stmt ~ T_SEMI ~ Stmts).map{ case x ~ _ ~ z => x :: z : Block } ||
|
|
172 |
(Stmt.map(s => List(s) : Block))
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
173 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
174 |
lazy val Block: Parser[List[Token], Block] =
|
919
|
175 |
(T_PAREN("{") ~ Stmts ~ T_PAREN("}")).map{ case x ~ y ~ z => y} ||
|
|
176 |
(Stmt.map(s => List(s)))
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
177 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
178 |
// Testing with programs 2 & 3
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
179 |
|
905
|
180 |
//println("Fibonacci")
|
|
181 |
//println(Stmts.parse_all(tokenise(os.read(os.pwd / "fib.while"))))
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
182 |
|
905
|
183 |
//println("Loops")
|
|
184 |
//println(Stmts.parse_all(tokenise(os.read(os.pwd / "loops.while"))))
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
185 |
|
905
|
186 |
//println("Collatz")
|
|
187 |
//println(Stmts.parse_all(tokenise(os.read(os.pwd / "collatz2.while"))))
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
188 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
189 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
190 |
// Interpreter
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
191 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
192 |
// Environment to store values of variables
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
193 |
type Env = Map[String, Int]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
194 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
195 |
def eval_aexp(a: AExp, env: Env) : Int = a match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
196 |
case Num(i) => i
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
197 |
case Var(s) => env(s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
198 |
case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
199 |
case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
200 |
case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
201 |
case Aop("/", a1, a2) => eval_aexp(a1, env) / eval_aexp(a2, env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
202 |
case Aop("%", a1, a2) => eval_aexp(a1, env) % eval_aexp(a2, env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
203 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
204 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
205 |
def eval_bexp(b: BExp, env: Env) : Boolean = b match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
206 |
case True => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
207 |
case False => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
208 |
case Bop("==", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
209 |
case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
210 |
case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
211 |
case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
212 |
case And(b1, b2) => eval_bexp(b1, env) && eval_bexp(b2, env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
213 |
case Or(b1, b2) => eval_bexp(b1, env) || eval_bexp(b2, env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
214 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
215 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
216 |
// Import needed to take int as input from the user
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
217 |
import scala.io.StdIn.readInt
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
218 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
219 |
def eval_stmt(s: Stmt, env: Env) : Env = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
220 |
case Skip => env
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
221 |
case Assign(x, a) => env + (x -> eval_aexp(a, env))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
222 |
case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
223 |
case While(b, bl) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
224 |
if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
225 |
else env
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
226 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
227 |
case WriteId(x) => { print(env(x)) ; env }
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
228 |
case WriteString(x) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
229 |
print(x.replaceAll("\"", "").replaceAll("""\\n""", "\n")) ;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
230 |
env
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
231 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
232 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
233 |
case Read(x) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
234 |
println("Enter an integer and press ENTER:") ;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
235 |
val n = readInt() ; // Note: Does not work when using the REPL
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
236 |
eval_stmt(Assign(x, Num(n)), env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
237 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
238 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
239 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
240 |
def eval_bl(bl: Block, env: Env) : Env = bl match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
241 |
case Nil => env
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
242 |
case s::bl => eval_bl(bl, eval_stmt(s, env))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
243 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
244 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
245 |
def eval(bl: Block) : Env = eval_bl(bl, Map())
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
246 |
|
905
|
247 |
@main
|
921
|
248 |
def run(file: String) = {
|
905
|
249 |
val contents = os.read(os.pwd / file)
|
|
250 |
println(s"Lex $file: ")
|
|
251 |
println(tokenise(contents))
|
|
252 |
println(s"Parse $file: ")
|
|
253 |
println(Stmts.parse_all(tokenise(contents)).head)
|
|
254 |
println(s"Eval $file: ")
|
|
255 |
println(eval(Stmts.parse_all(tokenise(contents)).head))
|
|
256 |
}
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
257 |
|
921
|
258 |
@main
|
|
259 |
def test(file: String) = {
|
|
260 |
val contents = os.read(os.pwd / file)
|
|
261 |
println(s"Lex $file: ")
|
|
262 |
println(tokenise(contents))
|
|
263 |
println(s"Parse $file: ")
|
|
264 |
println(Stmts.parse_all(tokenise(contents)).head)
|
|
265 |
}
|
|
266 |
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
267 |
/*
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
268 |
println("Loops eval")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
269 |
val start = System.nanoTime()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
270 |
println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "loops.while"))).head))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
271 |
val end = System.nanoTime()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
272 |
println("Time taken in seconds: ")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
273 |
println((end - start)/(1.0e9))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
274 |
*/
|