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