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