author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Mon, 17 Nov 2014 08:38:52 +0000 | |
changeset 308 | 3703ade9b17c |
parent 93 | 4794759139ea |
child 470 | 8df654e9eb4e |
child 471 | 9476086849ad |
permissions | -rw-r--r-- |
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
1 |
// A parser and evaluator for teh while language |
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
2 |
// |
85
1a4065f965fb
added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
3 |
import matcher._ |
1a4065f965fb
added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
4 |
import parser._ |
1a4065f965fb
added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
5 |
|
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
// some regular expressions |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
8 |
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_") |
68
a09ecb1e7384
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
66
diff
changeset
|
9 |
val DIGIT = RANGE("0123456789") |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
10 |
val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT))) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
11 |
val NUM = PLUS(DIGIT) |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
12 |
val KEYWORD = ALTS("skip", "while", "do", "if", "then", "else", "true", "false") |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
13 |
val SEMI: Rexp = ";" |
74
8f85d1f61663
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
73
diff
changeset
|
14 |
val OP: Rexp = ALTS(":=", "=", "-", "+", "*", "!=", "<", ">") |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
val WHITESPACE = PLUS(RANGE(" \n")) |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
16 |
val RPAREN: Rexp = ")" |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
17 |
val LPAREN: Rexp = "(" |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
18 |
val BEGIN: Rexp = "{" |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
19 |
val END: Rexp = "}" |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
|
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
21 |
// tokens for classifying the strings that have been recognised |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
abstract class Token |
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
case object T_WHITESPACE extends Token |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
24 |
case object T_SEMI extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
25 |
case object T_LPAREN extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
26 |
case object T_RPAREN extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
27 |
case object T_BEGIN extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
28 |
case object T_END extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
29 |
case class T_ID(s: String) extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
30 |
case class T_OP(s: String) extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
31 |
case class T_NUM(s: String) extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
32 |
case class T_KWD(s: String) extends Token |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
33 |
|
85
1a4065f965fb
added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
34 |
val lexing_rules: List[(Rexp, List[Char] => Token)] = |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
35 |
List((KEYWORD, (s) => T_KWD(s.mkString)), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
36 |
(ID, (s) => T_ID(s.mkString)), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
37 |
(OP, (s) => T_OP(s.mkString)), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
38 |
(NUM, (s) => T_NUM(s.mkString)), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
39 |
(SEMI, (s) => T_SEMI), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
40 |
(LPAREN, (s) => T_LPAREN), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
41 |
(RPAREN, (s) => T_RPAREN), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
42 |
(BEGIN, (s) => T_BEGIN), |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
43 |
(END, (s) => T_END), |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
(WHITESPACE, (s) => T_WHITESPACE)) |
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
|
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
// the tokenizer |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
47 |
val Tok = Tokenizer(lexing_rules, List(T_WHITESPACE)) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
48 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
49 |
// the abstract syntax trees |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
50 |
abstract class Stmt |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
51 |
abstract class AExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
52 |
abstract class BExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
53 |
type Block = List[Stmt] |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
54 |
case object Skip extends Stmt |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
55 |
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
56 |
case class While(b: BExp, bl: Block) extends Stmt |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
57 |
case class Assign(s: String, a: AExp) extends Stmt |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
58 |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
59 |
case class Var(s: String) extends AExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
60 |
case class Num(i: Int) extends AExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
61 |
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
62 |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
63 |
case object True extends BExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
64 |
case object False extends BExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
65 |
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
66 |
|
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
67 |
// atomic parsers |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
68 |
case class TokParser(tok: Token) extends Parser[List[Token], Token] { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
69 |
def parse(ts: List[Token]) = ts match { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
70 |
case t::ts if (t == tok) => Set((t, ts)) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
71 |
case _ => Set () |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
72 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
73 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
74 |
implicit def token2tparser(t: Token) = TokParser(t) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
75 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
76 |
case object NumParser extends Parser[List[Token], Int] { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
77 |
def parse(ts: List[Token]) = ts match { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
78 |
case T_NUM(s)::ts => Set((s.toInt, ts)) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
79 |
case _ => Set () |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
80 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
81 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
82 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
83 |
case object IdParser extends Parser[List[Token], String] { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
84 |
def parse(ts: List[Token]) = ts match { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
85 |
case T_ID(s)::ts => Set((s, ts)) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
86 |
case _ => Set () |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
87 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
88 |
} |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
|
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
|
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
91 |
// arithmetic expressions |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
92 |
lazy val AExp: Parser[List[Token], AExp] = |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
93 |
(T ~ T_OP("+") ~ AExp) ==> { case ((x, y), z) => Aop("+", x, z): AExp } || |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
94 |
(T ~ T_OP("-") ~ AExp) ==> { case ((x, y), z) => Aop("-", x, z): AExp } || T |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
95 |
lazy val T: Parser[List[Token], AExp] = |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
96 |
(F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => Aop("*", x, z): AExp } || F |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
97 |
lazy val F: Parser[List[Token], AExp] = |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
98 |
(T_LPAREN ~> AExp <~ T_RPAREN) || |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
99 |
IdParser ==> Var || |
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
100 |
NumParser ==> Num |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
101 |
|
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
102 |
// boolean expressions |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
103 |
lazy val BExp: Parser[List[Token], BExp] = |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
104 |
(AExp ~ T_OP("=") ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z): BExp } || |
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
105 |
(AExp ~ T_OP("!=") ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z): BExp } || |
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
106 |
(AExp ~ T_OP("<") ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z): BExp } || |
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
107 |
(AExp ~ T_OP(">") ~ AExp) ==> { case ((x, y), z) => Bop(">", x, z): BExp } || |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
108 |
(T_KWD("true") ==> ((_) => True)) || |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
109 |
(T_KWD("false") ==> ((_) => False: BExp)) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
110 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
111 |
lazy val Stmt: Parser[List[Token], Stmt] = |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
112 |
(T_KWD("skip") ==> ((_) => Skip: Stmt)) || |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
113 |
(IdParser ~ T_OP(":=") ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } || |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
114 |
(T_KWD("if") ~ BExp ~ T_KWD("then") ~ Block ~ T_KWD("else") ~ Block) ==> |
71
7717f20f0504
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
70
diff
changeset
|
115 |
{ case (((((x,y),z),u),v),w) => If(y, u, w): Stmt } || |
7717f20f0504
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
70
diff
changeset
|
116 |
(T_KWD("while") ~ BExp ~ T_KWD("do") ~ Block) ==> { case (((x, y), z), w) => While(y, w) } |
7717f20f0504
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
70
diff
changeset
|
117 |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
118 |
lazy val Stmts: Parser[List[Token], Block] = |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
119 |
(Stmt ~ T_SEMI ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } || |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
120 |
(Stmt ==> ((s) => List(s) : Block)) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
121 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
122 |
lazy val Block: Parser[List[Token], Block] = |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
123 |
(T_BEGIN ~> Stmts <~ T_END) || |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
124 |
(Stmt ==> ((s) => List(s))) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
125 |
|
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
126 |
|
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
127 |
// examples |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
128 |
val p1 = "x := 5" |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
129 |
val p1_toks = Tok.fromString(p1) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
130 |
val p1_ast = Block.parse_all(p1_toks) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
131 |
println(p1_toks) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
132 |
println(p1_ast) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
133 |
|
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
134 |
val p1a = "{ x := 5; y := 8}" |
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
135 |
val p1a_toks = Tok.fromString(p1a) |
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
136 |
val p1a_ast = Block.parse_all(p1a_toks) |
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
137 |
println(p1a_ast) |
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
138 |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
139 |
val p2 = "5 = 6" |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
140 |
val p2_toks = Tok.fromString(p2) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
141 |
val p2_ast = BExp.parse_all(p2_toks) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
142 |
println(p2_ast) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
143 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
144 |
val p2a = "true" |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
145 |
val p2a_toks = Tok.fromString(p2a) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
146 |
val p2a_ast = BExp.parse_all(p2a_toks) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
147 |
println(p2a_ast) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
148 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
149 |
val p3 = "if true then skip else skip" |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
150 |
val p3_toks = Tok.fromString(p3) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
151 |
val p3_ast = Stmt.parse_all(p3_toks) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
152 |
println(p3_ast) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
153 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
154 |
val p3a = "if true then x := 5 else x := 10" |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
155 |
val p3a_toks = Tok.fromString(p3a) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
156 |
val p3a_ast = Stmt.parse_all(p3a_toks) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
157 |
println(p3a_ast) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
158 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
159 |
val p3b = "if false then x := 5 else x := 10" |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
160 |
val p3b_toks = Tok.fromString(p3b) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
161 |
val p3b_ast = Stmt.parse_all(p3b_toks) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
162 |
println(p3b_ast) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
163 |
|
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
164 |
// multiplication |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
165 |
val p4 = """{ x := 5; |
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
166 |
y := 4; |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
167 |
r := 0; |
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
168 |
while y > 0 do { |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
169 |
r := r + x; |
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
170 |
y := y - 1 |
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
171 |
} |
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
172 |
}""" |
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
173 |
val p4_toks = Tok.fromString(p4) |
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
174 |
val p4_ast = Block.parse_all(p4_toks) |
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
175 |
println(p4_ast) |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
176 |
|
73
27469183da75
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
72
diff
changeset
|
177 |
val p5 = """ |
27469183da75
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
72
diff
changeset
|
178 |
n := 9; |
27469183da75
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
72
diff
changeset
|
179 |
minus1 := 0; |
27469183da75
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
72
diff
changeset
|
180 |
minus2 := 1; |
27469183da75
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
72
diff
changeset
|
181 |
temp := 0; |
27469183da75
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
72
diff
changeset
|
182 |
while n > 0 do { |
27469183da75
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
72
diff
changeset
|
183 |
temp := minus2; |
27469183da75
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
72
diff
changeset
|
184 |
minus2 := minus1 + minus2; |
27469183da75
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
72
diff
changeset
|
185 |
minus1 := temp; |
27469183da75
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
72
diff
changeset
|
186 |
n := n - 1 |
27469183da75
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
72
diff
changeset
|
187 |
}; |
27469183da75
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
72
diff
changeset
|
188 |
fib_res := minus2 |
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
189 |
""" |
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
190 |
val p5_toks = Tok.fromString(p5) |
73
27469183da75
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
72
diff
changeset
|
191 |
val p5_ast = Stmts.parse_all(p5_toks) |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
192 |
|
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
193 |
// interpreter |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
194 |
type Env = Map[String, Int] |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
195 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
196 |
def eval_bexp(b: BExp, env: Env) : Boolean = b match { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
197 |
case True => true |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
198 |
case False => false |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
199 |
case Bop("=", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env) |
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
200 |
case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env)) |
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
201 |
case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env) |
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
202 |
case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env) |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
203 |
} |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
204 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
205 |
def eval_aexp(a: AExp, env : Env) : Int = a match { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
206 |
case Num(i) => i |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
207 |
case Var(s) => env(s) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
208 |
case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
209 |
case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
210 |
case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
211 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
212 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
213 |
def eval_stmt(s: Stmt, env: Env) : Env = s match { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
214 |
case Skip => env |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
215 |
case Assign(x, a) => env + (x -> eval_aexp(a, env)) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
216 |
case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env) |
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
217 |
case While(b, bl) => |
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
218 |
if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env)) |
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
219 |
else env |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
220 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
221 |
|
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
222 |
def eval_bl(bl: Block, env: Env) : Env = bl match { |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
223 |
case Nil => env |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
224 |
case s::bl => eval_bl(bl, eval_stmt(s, env)) |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
225 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
226 |
|
72
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
227 |
//examples |
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
228 |
println(eval_stmt(p3a_ast.head, Map.empty)) |
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
229 |
println(eval_stmt(p3b_ast.head, Map.empty)) |
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
230 |
println(eval_bl(p4_ast.head, Map.empty)) |
d65525aeca08
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
71
diff
changeset
|
231 |
println(eval_bl(p5_ast.head, Map.empty)) |