author | Christian Urban <urbanc@in.tum.de> |
Sun, 28 Jul 2019 16:15:03 +0100 | |
changeset 625 | 6709fa87410b |
parent 380 | 1e88390e81aa |
child 626 | 2d91b2107656 |
permissions | -rw-r--r-- |
625 | 1 |
// A Small Compiler for a Simple Functional Language |
2 |
// (includes a lexer and a parser) |
|
3 |
||
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
import scala.language.implicitConversions |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
import scala.language.reflectiveCalls |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
import scala.util._ |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
import scala.annotation.tailrec |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
import scala.sys.process._ |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
10 |
def fromFile(name: String) : String = |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
11 |
io.Source.fromFile(name).mkString |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
12 |
|
625 | 13 |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
abstract class Rexp |
625 | 15 |
case object ZERO extends Rexp |
16 |
case object ONE extends Rexp |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
case class CHAR(c: Char) extends Rexp |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
case class STAR(r: Rexp) extends Rexp |
625 | 21 |
case class RECD(x: String, r: Rexp) extends Rexp |
22 |
||
23 |
abstract class Val |
|
24 |
case object Empty extends Val |
|
25 |
case class Chr(c: Char) extends Val |
|
26 |
case class Sequ(v1: Val, v2: Val) extends Val |
|
27 |
case class Left(v: Val) extends Val |
|
28 |
case class Right(v: Val) extends Val |
|
29 |
case class Stars(vs: List[Val]) extends Val |
|
30 |
case class Rec(x: String, v: Val) extends Val |
|
31 |
||
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
// some convenience for typing in regular expressions |
625 | 33 |
def charlist2rexp(s : List[Char]): Rexp = s match { |
34 |
case Nil => ONE |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
case c::Nil => CHAR(c) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
} |
625 | 38 |
implicit def string2rexp(s : String) : Rexp = |
39 |
charlist2rexp(s.toList) |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
|
625 | 41 |
implicit def RexpOps(r: Rexp) = new { |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
def | (s: Rexp) = ALT(r, s) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
def % = STAR(r) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
def ~ (s: Rexp) = SEQ(r, s) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
|
625 | 47 |
implicit def stringOps(s: String) = new { |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
def | (r: Rexp) = ALT(s, r) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
def | (r: String) = ALT(s, r) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
def % = STAR(s) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
def ~ (r: Rexp) = SEQ(s, r) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
def ~ (r: String) = SEQ(s, r) |
625 | 53 |
def $ (r: Rexp) = RECD(s, r) |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
def nullable (r: Rexp) : Boolean = r match { |
625 | 57 |
case ZERO => false |
58 |
case ONE => true |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
case CHAR(_) => false |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
case STAR(_) => true |
625 | 63 |
case RECD(_, r1) => nullable(r1) |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
def der (c: Char, r: Rexp) : Rexp = r match { |
625 | 67 |
case ZERO => ZERO |
68 |
case ONE => ZERO |
|
69 |
case CHAR(d) => if (c == d) ONE else ZERO |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
case SEQ(r1, r2) => |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
else SEQ(der(c, r1), r2) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
625 | 75 |
case RECD(_, r1) => der(c, r1) |
76 |
} |
|
77 |
||
78 |
||
79 |
// extracts a string from value |
|
80 |
def flatten(v: Val) : String = v match { |
|
81 |
case Empty => "" |
|
82 |
case Chr(c) => c.toString |
|
83 |
case Left(v) => flatten(v) |
|
84 |
case Right(v) => flatten(v) |
|
85 |
case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
|
86 |
case Stars(vs) => vs.map(flatten).mkString |
|
87 |
case Rec(_, v) => flatten(v) |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
|
625 | 90 |
// extracts an environment from a value; |
91 |
// used for tokenise a string |
|
92 |
def env(v: Val) : List[(String, String)] = v match { |
|
93 |
case Empty => Nil |
|
94 |
case Chr(c) => Nil |
|
95 |
case Left(v) => env(v) |
|
96 |
case Right(v) => env(v) |
|
97 |
case Sequ(v1, v2) => env(v1) ::: env(v2) |
|
98 |
case Stars(vs) => vs.flatMap(env) |
|
99 |
case Rec(x, v) => (x, flatten(v))::env(v) |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
|
625 | 102 |
// The Injection Part of the lexer |
103 |
||
104 |
// calculates a value for how a nullable regex |
|
105 |
// matches the empty string |
|
106 |
def mkeps(r: Rexp) : Val = r match { |
|
107 |
case ONE => Empty |
|
108 |
case ALT(r1, r2) => |
|
109 |
if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
|
110 |
case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
|
111 |
case STAR(r) => Stars(Nil) |
|
112 |
case RECD(x, r) => Rec(x, mkeps(r)) |
|
113 |
} |
|
114 |
||
115 |
// injects back a character into a value |
|
116 |
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
|
117 |
case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
|
118 |
case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
|
119 |
case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
|
120 |
case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
|
121 |
case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1)) |
|
122 |
case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
|
123 |
case (CHAR(d), Empty) => Chr(c) |
|
124 |
case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
125 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
126 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
127 |
|
625 | 128 |
// some "rectification" functions for simplification |
129 |
def F_ID(v: Val): Val = v |
|
130 |
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) |
|
131 |
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) |
|
132 |
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
133 |
case Right(v) => Right(f2(v)) |
|
134 |
case Left(v) => Left(f1(v)) |
|
135 |
} |
|
136 |
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
137 |
case Sequ(v1, v2) => Sequ(f1(v1), f2(v2)) |
|
138 |
} |
|
139 |
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = |
|
140 |
(v:Val) => Sequ(f1(Empty), f2(v)) |
|
141 |
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = |
|
142 |
(v:Val) => Sequ(f1(v), f2(Empty)) |
|
143 |
def F_RECD(f: Val => Val) = (v:Val) => v match { |
|
144 |
case Rec(x, v) => Rec(x, f(v)) |
|
145 |
} |
|
146 |
def F_ERROR(v: Val): Val = throw new Exception("error") |
|
147 |
||
148 |
// simplification of regular expressions returns now also |
|
149 |
// an rectification function; no simplification under STAR |
|
150 |
def simp(r: Rexp): (Rexp, Val => Val) = r match { |
|
151 |
case ALT(r1, r2) => { |
|
152 |
val (r1s, f1s) = simp(r1) |
|
153 |
val (r2s, f2s) = simp(r2) |
|
154 |
(r1s, r2s) match { |
|
155 |
case (ZERO, _) => (r2s, F_RIGHT(f2s)) |
|
156 |
case (_, ZERO) => (r1s, F_LEFT(f1s)) |
|
157 |
case _ => if (r1s == r2s) (r1s, F_LEFT(f1s)) |
|
158 |
else (ALT (r1s, r2s), F_ALT(f1s, f2s)) |
|
159 |
} |
|
160 |
} |
|
161 |
case SEQ(r1, r2) => { |
|
162 |
val (r1s, f1s) = simp(r1) |
|
163 |
val (r2s, f2s) = simp(r2) |
|
164 |
(r1s, r2s) match { |
|
165 |
case (ZERO, _) => (ZERO, F_ERROR) |
|
166 |
case (_, ZERO) => (ZERO, F_ERROR) |
|
167 |
case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s)) |
|
168 |
case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s)) |
|
169 |
case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s)) |
|
170 |
} |
|
171 |
} |
|
172 |
case RECD(x, r1) => { |
|
173 |
val (r1s, f1s) = simp(r1) |
|
174 |
(RECD(x, r1s), F_RECD(f1s)) |
|
175 |
} |
|
176 |
case r => (r, F_ID) |
|
177 |
} |
|
178 |
||
179 |
// lexing functions including simplification |
|
180 |
def lex_simp(r: Rexp, s: List[Char]) : Val = s match { |
|
181 |
case Nil => if (nullable(r)) mkeps(r) else { println ("Lexing Error") ; sys.exit(-1) } |
|
182 |
case c::cs => { |
|
183 |
val (r_simp, f_simp) = simp(der(c, r)) |
|
184 |
inj(r, c, f_simp(lex_simp(r_simp, cs))) |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList)) |
|
189 |
||
190 |
||
191 |
// The Lexing Rules for the Fun Language |
|
192 |
||
193 |
def PLUS(r: Rexp) = r ~ r.% |
|
194 |
||
195 |
val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | |
|
196 |
"l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | |
|
197 |
"w" | "x" | "y" | "z" | "T" | "_" |
|
198 |
val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
199 |
val ID = SYM ~ (SYM | DIGIT).% |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
200 |
val NUM = PLUS(DIGIT) |
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
201 |
val KEYWORD : Rexp = "if" | "then" | "else" | "write" | "def" |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
202 |
val SEMI: Rexp = ";" |
625 | 203 |
val OP: Rexp = "=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/" |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
204 |
val WHITESPACE = PLUS(" " | "\n" | "\t") |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
205 |
val RPAREN: Rexp = ")" |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
206 |
val LPAREN: Rexp = "(" |
625 | 207 |
val COMMA: Rexp = "," |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
208 |
val ALL = SYM | DIGIT | OP | " " | ":" | ";" | "\"" | "=" | "," | "(" | ")" |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
209 |
val ALL2 = ALL | "\n" |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
210 |
val COMMENT = ("/*" ~ ALL2.% ~ "*/") | ("//" ~ ALL.% ~ "\n") |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
211 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
212 |
|
625 | 213 |
|
214 |
val WHILE_REGS = (("k" $ KEYWORD) | |
|
215 |
("i" $ ID) | |
|
216 |
("o" $ OP) | |
|
217 |
("n" $ NUM) | |
|
218 |
("s" $ SEMI) | |
|
219 |
("c" $ COMMA) | |
|
220 |
("pl" $ LPAREN) | |
|
221 |
("pr" $ RPAREN) | |
|
222 |
("w" $ (WHITESPACE | COMMENT))).% |
|
223 |
||
224 |
||
225 |
||
226 |
// The tokens for the While language |
|
227 |
||
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
228 |
abstract class Token |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
229 |
case object T_SEMI extends Token |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
230 |
case object T_COMMA extends Token |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
231 |
case object T_LPAREN extends Token |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
232 |
case object T_RPAREN extends Token |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
233 |
case class T_ID(s: String) extends Token |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
234 |
case class T_OP(s: String) extends Token |
625 | 235 |
case class T_NUM(n: Int) extends Token |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
236 |
case class T_KWD(s: String) extends Token |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
237 |
|
625 | 238 |
val token : PartialFunction[(String, String), Token] = { |
239 |
case ("k", s) => T_KWD(s) |
|
240 |
case ("i", s) => T_ID(s) |
|
241 |
case ("o", s) => T_OP(s) |
|
242 |
case ("n", s) => T_NUM(s.toInt) |
|
243 |
case ("s", _) => T_SEMI |
|
244 |
case ("c", _) => T_COMMA |
|
245 |
case ("pl", _) => T_LPAREN |
|
246 |
case ("pr", _) => T_RPAREN |
|
247 |
} |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
248 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
249 |
|
625 | 250 |
def tokenise(s: String) : List[Token] = |
251 |
lexing_simp(WHILE_REGS, s).collect(token) |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
252 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
253 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
254 |
// Parser - Abstract syntax trees |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
255 |
abstract class Exp |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
256 |
abstract class BExp |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
257 |
abstract class Decl |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
258 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
259 |
case class Def(name: String, args: List[String], body: Exp) extends Decl |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
260 |
case class Main(e: Exp) extends Decl |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
261 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
262 |
case class Call(name: String, args: List[Exp]) extends Exp |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
263 |
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp |
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
264 |
case class Write(e: Exp) extends Exp |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
265 |
case class Var(s: String) extends Exp |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
266 |
case class Num(i: Int) extends Exp |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
267 |
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp |
625 | 268 |
case class Sequence(e1: Exp, e2: Exp) extends Exp |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
269 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
270 |
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
271 |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
272 |
// calculating the maximal needed stack size |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
273 |
def max_stack_exp(e: Exp): Int = e match { |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
274 |
case Call(_, args) => args.map(max_stack_exp).sum |
225
ef573e8fc86e
bug found by Andres
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
223
diff
changeset
|
275 |
case If(a, e1, e2) => max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max) |
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
276 |
case Write(e) => max_stack_exp(e) + 1 |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
277 |
case Var(_) => 1 |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
278 |
case Num(_) => 1 |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
279 |
case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) |
625 | 280 |
case Sequence(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max |
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
281 |
} |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
282 |
def max_stack_bexp(e: BExp): Int = e match { |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
283 |
case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
284 |
} |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
285 |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
286 |
// Parser combinators |
625 | 287 |
abstract class Parser[I, T](implicit ev: I => Seq[_]) { |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
288 |
def parse(ts: I): Set[(T, I)] |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
289 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
290 |
def parse_all(ts: I) : Set[T] = |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
291 |
for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
292 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
293 |
def parse_single(ts: I) : T = parse_all(ts).toList match { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
294 |
case List(t) => t |
625 | 295 |
case _ => { println ("Parse Error\n") ; sys.exit(-1) } |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
296 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
297 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
298 |
|
625 | 299 |
class SeqParser[I, T, S](p: => Parser[I, T], |
300 |
q: => Parser[I, S])(implicit ev: I => Seq[_]) extends Parser[I, (T, S)] { |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
301 |
def parse(sb: I) = |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
302 |
for ((head1, tail1) <- p.parse(sb); |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
303 |
(head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
304 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
305 |
|
625 | 306 |
class AltParser[I, T](p: => Parser[I, T], |
307 |
q: => Parser[I, T])(implicit ev: I => Seq[_]) extends Parser[I, T] { |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
308 |
def parse(sb: I) = p.parse(sb) ++ q.parse(sb) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
309 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
310 |
|
625 | 311 |
class FunParser[I, T, S](p: => Parser[I, T], |
312 |
f: T => S)(implicit ev: I => Seq[_]) extends Parser[I, S] { |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
313 |
def parse(sb: I) = |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
314 |
for ((head, tail) <- p.parse(sb)) yield (f(head), tail) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
315 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
316 |
|
625 | 317 |
implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = new { |
318 |
def || (q : => Parser[I, T]) = new AltParser[I, T](p, q) |
|
319 |
def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f) |
|
320 |
def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q) |
|
321 |
} |
|
322 |
||
323 |
def ListParser[I, T, S](p: => Parser[I, T], |
|
324 |
q: => Parser[I, S])(implicit ev: I => Seq[_]): Parser[I, List[T]] = { |
|
325 |
(p ~ q ~ ListParser(p, q)) ==> { case ((x, y), z) => x :: z : List[T] } || |
|
326 |
(p ==> ((s) => List(s))) |
|
327 |
} |
|
328 |
||
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
329 |
case class TokParser(tok: Token) extends Parser[List[Token], Token] { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
330 |
def parse(ts: List[Token]) = ts match { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
331 |
case t::ts if (t == tok) => Set((t, ts)) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
332 |
case _ => Set () |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
333 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
334 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
335 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
336 |
implicit def token2tparser(t: Token) = TokParser(t) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
337 |
|
625 | 338 |
implicit def TokOps(t: Token) = new { |
339 |
def || (q : => Parser[List[Token], Token]) = new AltParser[List[Token], Token](t, q) |
|
340 |
def ==>[S] (f: => Token => S) = new FunParser[List[Token], Token, S](t, f) |
|
341 |
def ~[S](q : => Parser[List[Token], S]) = new SeqParser[List[Token], Token, S](t, q) |
|
342 |
} |
|
343 |
||
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
344 |
case object NumParser extends Parser[List[Token], Int] { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
345 |
def parse(ts: List[Token]) = ts match { |
625 | 346 |
case T_NUM(n)::ts => Set((n, ts)) |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
347 |
case _ => Set () |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
348 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
349 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
350 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
351 |
case object IdParser extends Parser[List[Token], String] { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
352 |
def parse(ts: List[Token]) = ts match { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
353 |
case T_ID(s)::ts => Set((s, ts)) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
354 |
case _ => Set () |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
355 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
356 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
357 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
358 |
|
625 | 359 |
// Grammar Rules |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
360 |
|
625 | 361 |
// arithmetic expressions |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
362 |
lazy val Exp: Parser[List[Token], Exp] = |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
363 |
(T_KWD("if") ~ BExp ~ T_KWD("then") ~ Exp ~ T_KWD("else") ~ Exp) ==> |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
364 |
{ case (((((x, y), z), u), v), w) => If(y, u, w): Exp } || |
625 | 365 |
(M ~ T_SEMI ~ Exp) ==> { case ((x, y), z) => Sequence(x, z): Exp } || M |
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
366 |
lazy val M: Parser[List[Token], Exp] = |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
367 |
(T_KWD("write") ~ L) ==> { case (x, y) => Write(y): Exp } || L |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
368 |
lazy val L: Parser[List[Token], Exp] = |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
369 |
(T ~ T_OP("+") ~ Exp) ==> { case ((x, y), z) => Aop("+", x, z): Exp } || |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
370 |
(T ~ T_OP("-") ~ Exp) ==> { case ((x, y), z) => Aop("-", x, z): Exp } || T |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
371 |
lazy val T: Parser[List[Token], Exp] = |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
372 |
(F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => Aop("*", x, z): Exp } || |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
373 |
(F ~ T_OP("/") ~ T) ==> { case ((x, y), z) => Aop("/", x, z): Exp } || |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
374 |
(F ~ T_OP("%") ~ T) ==> { case ((x, y), z) => Aop("%", x, z): Exp } || F |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
375 |
lazy val F: Parser[List[Token], Exp] = |
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
376 |
(IdParser ~ T_LPAREN ~ ListParser(Exp, T_COMMA) ~ T_RPAREN) ==> |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
377 |
{ case (((x, y), z), w) => Call(x, z): Exp } || |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
378 |
(T_LPAREN ~ Exp ~ T_RPAREN) ==> { case ((x, y), z) => y: Exp } || |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
379 |
IdParser ==> { case x => Var(x): Exp } || |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
380 |
NumParser ==> { case x => Num(x): Exp } |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
381 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
382 |
// boolean expressions |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
383 |
lazy val BExp: Parser[List[Token], BExp] = |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
384 |
(Exp ~ T_OP("==") ~ Exp) ==> { case ((x, y), z) => Bop("==", x, z): BExp } || |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
385 |
(Exp ~ T_OP("!=") ~ Exp) ==> { case ((x, y), z) => Bop("!=", x, z): BExp } || |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
386 |
(Exp ~ T_OP("<") ~ Exp) ==> { case ((x, y), z) => Bop("<", x, z): BExp } || |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
387 |
(Exp ~ T_OP(">") ~ Exp) ==> { case ((x, y), z) => Bop("<", z, x): BExp } || |
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
388 |
(Exp ~ T_OP("<=") ~ Exp) ==> { case ((x, y), z) => Bop("<=", x, z): BExp } || |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
389 |
(Exp ~ T_OP("=>") ~ Exp) ==> { case ((x, y), z) => Bop("<=", z, x): BExp } |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
390 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
391 |
lazy val Defn: Parser[List[Token], Decl] = |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
392 |
(T_KWD("def") ~ IdParser ~ T_LPAREN ~ ListParser(IdParser, T_COMMA) ~ T_RPAREN ~ T_OP("=") ~ Exp) ==> |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
393 |
{ case ((((((x, y), z), w), u), v), r) => Def(y, w, r): Decl } |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
394 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
395 |
lazy val Prog: Parser[List[Token], List[Decl]] = |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
396 |
(Defn ~ T_SEMI ~ Prog) ==> { case ((x, y), z) => x :: z : List[Decl] } || |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
397 |
(Exp ==> ((s) => List(Main(s)) : List[Decl])) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
398 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
399 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
400 |
// compiler - built-in functions |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
401 |
// copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
402 |
// |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
403 |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
404 |
val library = """ |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
405 |
.class public XXX.XXX |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
406 |
.super java/lang/Object |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
407 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
408 |
.method public <init>()V |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
409 |
aload_0 |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
410 |
invokenonvirtual java/lang/Object/<init>()V |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
411 |
return |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
412 |
.end method |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
413 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
414 |
.method public static write(I)V |
380
1e88390e81aa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
323
diff
changeset
|
415 |
.limit locals 1 |
1e88390e81aa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
323
diff
changeset
|
416 |
.limit stack 2 |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
417 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
380
1e88390e81aa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
323
diff
changeset
|
418 |
iload 0 |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
419 |
invokevirtual java/io/PrintStream/println(I)V |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
420 |
return |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
421 |
.end method |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
422 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
423 |
""" |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
424 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
425 |
// for generating new labels |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
426 |
var counter = -1 |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
427 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
428 |
def Fresh(x: String) = { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
429 |
counter += 1 |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
430 |
x ++ "_" ++ counter.toString() |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
431 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
432 |
|
625 | 433 |
// convenient string interpolations |
434 |
// for instructions, labels and methods |
|
435 |
import scala.language.implicitConversions |
|
436 |
import scala.language.reflectiveCalls |
|
437 |
||
438 |
implicit def sring_inters(sc: StringContext) = new { |
|
439 |
def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" |
|
440 |
def l(args: Any*): String = sc.s(args:_*) ++ ":\n" |
|
441 |
def m(args: Any*): String = sc.s(args:_*) ++ "\n" |
|
442 |
} |
|
443 |
||
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
444 |
|
625 | 445 |
type Env = Map[String, Int] |
446 |
||
447 |
// compile expressions |
|
448 |
def compile_exp(a: Exp, env : Env) : String = a match { |
|
449 |
case Num(i) => i"ldc $i" |
|
450 |
case Var(s) => i"iload ${env(s)}" |
|
451 |
case Aop("+", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"iadd" |
|
452 |
case Aop("-", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"isub" |
|
453 |
case Aop("*", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"imul" |
|
454 |
case Aop("/", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"idiv" |
|
455 |
case Aop("%", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"irem" |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
456 |
case If(b, a1, a2) => { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
457 |
val if_else = Fresh("If_else") |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
458 |
val if_end = Fresh("If_end") |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
459 |
compile_bexp(b, env, if_else) ++ |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
460 |
compile_exp(a1, env) ++ |
625 | 461 |
i"goto $if_end" ++ |
462 |
l"$if_else" ++ |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
463 |
compile_exp(a2, env) ++ |
625 | 464 |
l"$if_end" |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
465 |
} |
625 | 466 |
case Call(name, args) => { |
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
467 |
val is = "I" * args.length |
625 | 468 |
args.map(a => compile_exp(a, env)).mkString ++ |
469 |
i"invokestatic XXX/XXX/$name($is)I" |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
470 |
} |
625 | 471 |
case Sequence(a1, a2) => { |
472 |
compile_exp(a1, env) ++ i"pop" ++ compile_exp(a2, env) |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
473 |
} |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
474 |
case Write(a1) => { |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
475 |
compile_exp(a1, env) ++ |
625 | 476 |
i"dup" ++ |
477 |
i"invokestatic XXX/XXX/write(I)V" |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
478 |
} |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
479 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
480 |
|
625 | 481 |
// compile boolean expressions |
482 |
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match { |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
483 |
case Bop("==", a1, a2) => |
625 | 484 |
compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpne $jmp" |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
485 |
case Bop("!=", a1, a2) => |
625 | 486 |
compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpeq $jmp" |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
487 |
case Bop("<", a1, a2) => |
625 | 488 |
compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpge $jmp" |
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
489 |
case Bop("<=", a1, a2) => |
625 | 490 |
compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpgt $jmp" |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
491 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
492 |
|
625 | 493 |
// compile function for declarations and main |
494 |
def compile_decl(d: Decl) : String = d match { |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
495 |
case Def(name, args, a) => { |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
496 |
val env = args.zipWithIndex.toMap |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
497 |
val is = "I" * args.length |
625 | 498 |
m".method public static $name($is)I" ++ |
499 |
m".limit locals ${args.length.toString}" ++ |
|
500 |
m".limit stack ${1 + max_stack_exp(a)}" ++ |
|
501 |
l"${name}_Start" ++ |
|
223
e4b29b57f6a3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
221
diff
changeset
|
502 |
compile_exp(a, env) ++ |
625 | 503 |
i"ireturn" ++ |
504 |
m".end method\n" |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
505 |
} |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
506 |
case Main(a) => { |
625 | 507 |
m".method public static main([Ljava/lang/String;)V" ++ |
508 |
m".limit locals 200" ++ |
|
509 |
m".limit stack 200" ++ |
|
223
e4b29b57f6a3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
221
diff
changeset
|
510 |
compile_exp(a, Map()) ++ |
625 | 511 |
i"invokestatic XXX/XXX/write(I)V" ++ |
512 |
i"return" ++ |
|
513 |
m".end method\n" |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
514 |
} |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
515 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
516 |
|
625 | 517 |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
518 |
def compile(class_name: String, input: String) : String = { |
625 | 519 |
val tks = tokenise(input) |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
311
diff
changeset
|
520 |
println(Prog.parse_single(tks).mkString("\n")) |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
521 |
val ast = Prog.parse_single(tks) |
625 | 522 |
val instructions = ast.map(compile_decl).mkString |
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
523 |
(library + instructions).replaceAllLiterally("XXX", class_name) |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
524 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
525 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
526 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
527 |
def compile_file(file_name: String) = { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
528 |
val class_name = file_name.split('.')(0) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
529 |
val output = compile(class_name, fromFile(file_name)) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
530 |
val fw = new java.io.FileWriter(class_name + ".j") |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
531 |
fw.write(output) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
532 |
fw.close() |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
533 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
534 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
535 |
def time_needed[T](i: Int, code: => T) = { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
536 |
val start = System.nanoTime() |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
537 |
for (j <- 1 to i) code |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
538 |
val end = System.nanoTime() |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
539 |
(end - start)/(i * 1.0e9) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
540 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
541 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
542 |
def compile_run(file_name: String) : Unit = { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
543 |
val class_name = file_name.split('.')(0) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
544 |
compile_file(file_name) |
625 | 545 |
val test = (s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!! |
546 |
println("Time: " + time_needed(2, (s"java ${class_name}/${class_name}").!)) |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
547 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
548 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
549 |
|
625 | 550 |
// some examples |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
311
diff
changeset
|
551 |
compile_file("fact.rec") |
625 | 552 |
compile_run("defs.rec") |
553 |
compile_run("fact.rec") |