Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Parser.java @ 12752:71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 11 Nov 2013 21:34:44 +0100 |
parents | d7f8dd4fe876 |
children | 7311354f5bf8 |
comparison
equal
deleted
inserted
replaced
12712:882a0aadfed6 | 12752:71991b7a0f14 |
---|---|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 * or visit www.oracle.com if you need additional information or have any | 20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. | 21 * questions. |
22 */ | 22 */ |
23 | 23 |
24 // The content of this file is automatically generated. DO NOT EDIT. | 24 // The content of this file is automatically generated. DO NOT EDIT. |
25 | |
25 | 26 |
26 package com.oracle.truffle.sl.parser; | 27 package com.oracle.truffle.sl.parser; |
27 | 28 |
28 import java.util.*; | 29 import java.util.*; |
29 | 30 |
35 public class Parser { | 36 public class Parser { |
36 public static final int _EOF = 0; | 37 public static final int _EOF = 0; |
37 public static final int _identifier = 1; | 38 public static final int _identifier = 1; |
38 public static final int _stringLiteral = 2; | 39 public static final int _stringLiteral = 2; |
39 public static final int _numericLiteral = 3; | 40 public static final int _numericLiteral = 3; |
40 public static final int maxT = 28; | 41 public static final int maxT = 29; |
41 | 42 |
42 static final boolean T = true; | 43 static final boolean T = true; |
43 static final boolean x = false; | 44 static final boolean x = false; |
44 static final int minErrDist = 2; | 45 static final int minErrDist = 2; |
45 | 46 |
47 public Token la; // lookahead token | 48 public Token la; // lookahead token |
48 int errDist = minErrDist; | 49 int errDist = minErrDist; |
49 | 50 |
50 public final Scanner scanner; | 51 public final Scanner scanner; |
51 public final Errors errors; | 52 public final Errors errors; |
52 private final NodeFactory factory; | 53 private final SLNodeFactory factory; |
53 | 54 |
54 public Parser(Scanner scanner, NodeFactory factory) { | 55 public Parser(Scanner scanner, SLNodeFactory factory) { |
55 this.scanner = scanner; | 56 this.scanner = scanner; |
56 this.factory = factory; | 57 this.factory = factory; |
57 errors = new Errors(); | 58 errors = new Errors(); |
58 } | 59 } |
59 | 60 |
131 void Function() { | 132 void Function() { |
132 Expect(4); | 133 Expect(4); |
133 factory.startFunction(); | 134 factory.startFunction(); |
134 Expect(1); | 135 Expect(1); |
135 String name = t.val; | 136 String name = t.val; |
137 List<String> parameterNames = new ArrayList<>(); | |
138 if (la.kind == 5) { | |
139 Get(); | |
140 if (la.kind == 1) { | |
141 Get(); | |
142 parameterNames.add(t.val); | |
143 } | |
144 while (la.kind == 6) { | |
145 Get(); | |
146 Expect(1); | |
147 parameterNames.add(t.val); | |
148 } | |
149 Expect(7); | |
150 } | |
136 StatementNode body = Block(); | 151 StatementNode body = Block(); |
137 factory.createFunction(body, name); | 152 factory.createFunction(body, name, parameterNames.toArray(new String[parameterNames.size()])); |
138 } | 153 } |
139 | 154 |
140 StatementNode Block() { | 155 StatementNode Block() { |
141 StatementNode result; | 156 StatementNode result; |
142 List<StatementNode> statements = new ArrayList<>(); | 157 List<StatementNode> statements = new ArrayList<>(); |
143 Expect(5); | 158 Expect(8); |
144 while (StartOf(1)) { | 159 while (StartOf(1)) { |
145 StatementNode statement = Statement(); | 160 StatementNode statement = Statement(); |
146 statements.add(statement); | 161 statements.add(statement); |
147 } | 162 } |
148 Expect(6); | 163 Expect(9); |
149 result = factory.createBlock(statements); | 164 result = factory.createBlock(statements); |
150 return result; | 165 return result; |
151 } | 166 } |
152 | 167 |
153 StatementNode Statement() { | 168 StatementNode Statement() { |
154 StatementNode result; | 169 StatementNode result; |
155 result = null; | 170 result = null; |
156 if (la.kind == 7) { | 171 if (la.kind == 13) { |
157 result = WhileStatement(); | 172 result = WhileStatement(); |
158 } else if (la.kind == 1) { | 173 } else if (la.kind == 11) { |
159 result = AssignmentStatement(); | 174 result = IfStatement(); |
160 } else if (la.kind == 12) { | 175 } else if (la.kind == 14) { |
161 result = OutputStatement(); | |
162 } else if (la.kind == 13) { | |
163 result = ReturnStatement(); | 176 result = ReturnStatement(); |
164 } else SynErr(29); | 177 } else if (StartOf(2)) { |
178 result = Expression(); | |
179 Expect(10); | |
180 } else SynErr(30); | |
165 return result; | 181 return result; |
166 } | 182 } |
167 | 183 |
168 StatementNode WhileStatement() { | 184 StatementNode WhileStatement() { |
169 StatementNode result; | 185 StatementNode result; |
186 Expect(13); | |
187 Expect(5); | |
188 ConditionNode condition = Expression(); | |
170 Expect(7); | 189 Expect(7); |
171 Expect(8); | |
172 ConditionNode condition = Expression(); | |
173 Expect(9); | |
174 StatementNode body = Block(); | 190 StatementNode body = Block(); |
175 result = factory.createWhile(condition, body); | 191 result = factory.createWhile(condition, body); |
176 return result; | 192 return result; |
177 } | 193 } |
178 | 194 |
179 StatementNode AssignmentStatement() { | 195 StatementNode IfStatement() { |
180 StatementNode result; | 196 StatementNode result; |
181 Expect(1); | |
182 String name = t.val; | |
183 Expect(10); | |
184 TypedNode rvalue = Expression(); | |
185 Expect(11); | 197 Expect(11); |
186 result = factory.createAssignment(name, rvalue); | 198 Expect(5); |
187 return result; | 199 ConditionNode condition = Expression(); |
188 } | 200 Expect(7); |
189 | 201 StatementNode thenNode = null; StatementNode elseNode = null; |
190 StatementNode OutputStatement() { | 202 thenNode = Block(); |
191 StatementNode result; | 203 if (la.kind == 12) { |
192 List<TypedNode> expressions = new ArrayList<>(); | 204 Get(); |
193 Expect(12); | 205 elseNode = Block(); |
194 while (StartOf(2)) { | 206 } |
195 TypedNode value = Expression(); | 207 result = factory.createIf(condition, thenNode, elseNode); |
196 expressions.add(value); | |
197 } | |
198 Expect(11); | |
199 result = factory.createPrint(expressions); | |
200 return result; | 208 return result; |
201 } | 209 } |
202 | 210 |
203 StatementNode ReturnStatement() { | 211 StatementNode ReturnStatement() { |
204 StatementNode result; | 212 StatementNode result; |
205 Expect(13); | 213 Expect(14); |
206 TypedNode value = Expression(); | 214 TypedNode value = Expression(); |
207 Expect(11); | 215 Expect(10); |
208 result = factory.createReturn(value); | 216 result = factory.createReturn(value); |
209 return result; | 217 return result; |
210 } | 218 } |
211 | 219 |
212 TypedNode Expression() { | 220 TypedNode Expression() { |
213 TypedNode result; | 221 TypedNode result; |
214 result = ValueExpression(); | 222 result = ValueExpression(); |
215 if (StartOf(3)) { | 223 if (StartOf(3)) { |
216 switch (la.kind) { | 224 switch (la.kind) { |
217 case 14: { | 225 case 15: { |
218 Get(); | 226 Get(); |
219 break; | 227 break; |
220 } | 228 } |
221 case 15: { | 229 case 16: { |
222 Get(); | 230 Get(); |
223 break; | 231 break; |
224 } | 232 } |
225 case 16: { | 233 case 17: { |
226 Get(); | 234 Get(); |
227 break; | 235 break; |
228 } | 236 } |
229 case 17: { | 237 case 18: { |
230 Get(); | 238 Get(); |
231 break; | 239 break; |
232 } | 240 } |
233 case 18: { | 241 case 19: { |
234 Get(); | 242 Get(); |
235 break; | 243 break; |
236 } | 244 } |
237 case 19: { | 245 case 20: { |
238 Get(); | 246 Get(); |
239 break; | 247 break; |
240 } | 248 } |
241 } | 249 } |
242 String op = t.val; | 250 String op = t.val; |
247 } | 255 } |
248 | 256 |
249 TypedNode ValueExpression() { | 257 TypedNode ValueExpression() { |
250 TypedNode result; | 258 TypedNode result; |
251 result = Term(); | 259 result = Term(); |
252 while (la.kind == 20 || la.kind == 21) { | 260 while (la.kind == 21 || la.kind == 22) { |
253 if (la.kind == 20) { | 261 if (la.kind == 21) { |
254 Get(); | 262 Get(); |
255 } else { | 263 } else { |
256 Get(); | 264 Get(); |
257 } | 265 } |
258 String op = t.val; | 266 String op = t.val; |
263 } | 271 } |
264 | 272 |
265 TypedNode Term() { | 273 TypedNode Term() { |
266 TypedNode result; | 274 TypedNode result; |
267 result = Factor(); | 275 result = Factor(); |
268 while (la.kind == 22 || la.kind == 23) { | 276 while (la.kind == 23 || la.kind == 24) { |
269 if (la.kind == 22) { | 277 if (la.kind == 23) { |
270 Get(); | 278 Get(); |
271 } else { | 279 } else { |
272 Get(); | 280 Get(); |
273 } | 281 } |
274 String op = t.val; | 282 String op = t.val; |
279 } | 287 } |
280 | 288 |
281 TypedNode Factor() { | 289 TypedNode Factor() { |
282 TypedNode result; | 290 TypedNode result; |
283 result = null; | 291 result = null; |
284 switch (la.kind) { | 292 if (la.kind == 1) { |
285 case 27: { | 293 result = VariableRefOrCall(); |
286 result = TimeRef(); | 294 } else if (la.kind == 2) { |
287 break; | |
288 } | |
289 case 1: { | |
290 result = VariableRef(); | |
291 break; | |
292 } | |
293 case 2: { | |
294 result = StringLiteral(); | 295 result = StringLiteral(); |
295 break; | 296 } else if (la.kind == 3) { |
296 } | |
297 case 3: { | |
298 result = NumericLiteral(); | 297 result = NumericLiteral(); |
299 break; | 298 } else if (la.kind == 25) { |
300 } | |
301 case 24: { | |
302 result = Ternary(); | 299 result = Ternary(); |
303 break; | 300 } else if (la.kind == 5) { |
304 } | |
305 case 8: { | |
306 Get(); | 301 Get(); |
307 result = Expression(); | 302 result = Expression(); |
308 Expect(9); | 303 Expect(7); |
309 break; | 304 } else SynErr(31); |
310 } | 305 return result; |
311 default: SynErr(30); break; | 306 } |
312 } | 307 |
313 return result; | 308 TypedNode VariableRefOrCall() { |
314 } | 309 TypedNode result; |
315 | 310 result = VariableRef(); |
316 TypedNode TimeRef() { | 311 if (la.kind == 5 || la.kind == 28) { |
317 TypedNode result; | 312 if (la.kind == 5) { |
313 TypedNode[] parameters = Parameters(); | |
314 result = factory.createCall(result, parameters); | |
315 } else { | |
316 Get(); | |
317 TypedNode assignment = Expression(); | |
318 result = factory.createAssignment(result, assignment); | |
319 } | |
320 } | |
321 return result; | |
322 } | |
323 | |
324 TypedNode StringLiteral() { | |
325 TypedNode result; | |
326 Expect(2); | |
327 result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1)); | |
328 return result; | |
329 } | |
330 | |
331 TypedNode NumericLiteral() { | |
332 TypedNode result; | |
333 Expect(3); | |
334 result = factory.createNumericLiteral(t.val); | |
335 return result; | |
336 } | |
337 | |
338 TypedNode Ternary() { | |
339 TypedNode result; | |
340 TypedNode condition, thenPart, elsePart; | |
341 Expect(25); | |
342 condition = Expression(); | |
343 Expect(26); | |
344 thenPart = Expression(); | |
318 Expect(27); | 345 Expect(27); |
319 result = factory.createTime(); | 346 elsePart = Expression(); |
347 result = factory.createTernary(condition, thenPart, elsePart); | |
320 return result; | 348 return result; |
321 } | 349 } |
322 | 350 |
323 TypedNode VariableRef() { | 351 TypedNode VariableRef() { |
324 TypedNode result; | 352 TypedNode result; |
325 Expect(1); | 353 Expect(1); |
326 result = factory.createLocal(t.val); | 354 result = factory.createLocal(t.val); |
327 return result; | 355 return result; |
328 } | 356 } |
329 | 357 |
330 TypedNode StringLiteral() { | 358 TypedNode[] Parameters() { |
331 TypedNode result; | 359 TypedNode[] result; |
332 Expect(2); | 360 Expect(5); |
333 result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1)); | 361 List<TypedNode> parameters = new ArrayList<>(); |
334 return result; | 362 if (StartOf(2)) { |
335 } | 363 TypedNode e1 = Expression(); |
336 | 364 parameters.add(e1); |
337 TypedNode NumericLiteral() { | 365 while (la.kind == 6) { |
338 TypedNode result; | 366 Get(); |
339 Expect(3); | 367 TypedNode e2 = Expression(); |
340 result = factory.createNumericLiteral(t.val); | 368 parameters.add(e2); |
341 return result; | 369 } |
342 } | 370 } |
343 | 371 result = parameters.toArray(new TypedNode[parameters.size()]); |
344 TypedNode Ternary() { | 372 Expect(7); |
345 TypedNode result; | |
346 TypedNode condition, thenPart, elsePart; | |
347 Expect(24); | |
348 condition = Expression(); | |
349 Expect(25); | |
350 thenPart = Expression(); | |
351 Expect(26); | |
352 elsePart = Expression(); | |
353 result = factory.createTernary(condition, thenPart, elsePart); | |
354 return result; | 373 return result; |
355 } | 374 } |
356 | 375 |
357 | 376 |
358 | 377 |
364 Expect(0); | 383 Expect(0); |
365 | 384 |
366 } | 385 } |
367 | 386 |
368 private static final boolean[][] set = { | 387 private static final boolean[][] set = { |
369 {T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x}, | 388 {T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x}, |
370 {x,T,x,x, x,x,x,T, x,x,x,x, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x}, | 389 {x,T,T,T, x,T,x,x, x,x,x,T, x,T,T,x, x,x,x,x, x,x,x,x, x,T,x,x, x,x,x}, |
371 {x,T,T,T, x,x,x,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,x,x,T, x,x}, | 390 {x,T,T,T, x,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,x,x, x,x,x}, |
372 {x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,T,T, x,x,x,x, x,x,x,x, x,x} | 391 {x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,T, T,T,T,T, T,x,x,x, x,x,x,x, x,x,x} |
373 | 392 |
374 }; | 393 }; |
375 | 394 |
376 public String ParseErrors() { | 395 public String ParseErrors() { |
377 java.io.PrintStream oldStream = System.out; | 396 java.io.PrintStream oldStream = System.out; |
421 case 0: s = "EOF expected"; break; | 440 case 0: s = "EOF expected"; break; |
422 case 1: s = "identifier expected"; break; | 441 case 1: s = "identifier expected"; break; |
423 case 2: s = "stringLiteral expected"; break; | 442 case 2: s = "stringLiteral expected"; break; |
424 case 3: s = "numericLiteral expected"; break; | 443 case 3: s = "numericLiteral expected"; break; |
425 case 4: s = "\"function\" expected"; break; | 444 case 4: s = "\"function\" expected"; break; |
426 case 5: s = "\"{\" expected"; break; | 445 case 5: s = "\"(\" expected"; break; |
427 case 6: s = "\"}\" expected"; break; | 446 case 6: s = "\",\" expected"; break; |
428 case 7: s = "\"while\" expected"; break; | 447 case 7: s = "\")\" expected"; break; |
429 case 8: s = "\"(\" expected"; break; | 448 case 8: s = "\"{\" expected"; break; |
430 case 9: s = "\")\" expected"; break; | 449 case 9: s = "\"}\" expected"; break; |
431 case 10: s = "\"=\" expected"; break; | 450 case 10: s = "\";\" expected"; break; |
432 case 11: s = "\";\" expected"; break; | 451 case 11: s = "\"if\" expected"; break; |
433 case 12: s = "\"print\" expected"; break; | 452 case 12: s = "\"else\" expected"; break; |
434 case 13: s = "\"return\" expected"; break; | 453 case 13: s = "\"while\" expected"; break; |
435 case 14: s = "\"<\" expected"; break; | 454 case 14: s = "\"return\" expected"; break; |
436 case 15: s = "\">\" expected"; break; | 455 case 15: s = "\"<\" expected"; break; |
437 case 16: s = "\"<=\" expected"; break; | 456 case 16: s = "\">\" expected"; break; |
438 case 17: s = "\">=\" expected"; break; | 457 case 17: s = "\"<=\" expected"; break; |
439 case 18: s = "\"==\" expected"; break; | 458 case 18: s = "\">=\" expected"; break; |
440 case 19: s = "\"!=\" expected"; break; | 459 case 19: s = "\"==\" expected"; break; |
441 case 20: s = "\"+\" expected"; break; | 460 case 20: s = "\"!=\" expected"; break; |
442 case 21: s = "\"-\" expected"; break; | 461 case 21: s = "\"+\" expected"; break; |
443 case 22: s = "\"*\" expected"; break; | 462 case 22: s = "\"-\" expected"; break; |
444 case 23: s = "\"/\" expected"; break; | 463 case 23: s = "\"*\" expected"; break; |
445 case 24: s = "\"#\" expected"; break; | 464 case 24: s = "\"/\" expected"; break; |
446 case 25: s = "\"?\" expected"; break; | 465 case 25: s = "\"#\" expected"; break; |
447 case 26: s = "\":\" expected"; break; | 466 case 26: s = "\"?\" expected"; break; |
448 case 27: s = "\"time\" expected"; break; | 467 case 27: s = "\":\" expected"; break; |
449 case 28: s = "??? expected"; break; | 468 case 28: s = "\"=\" expected"; break; |
450 case 29: s = "invalid Statement"; break; | 469 case 29: s = "??? expected"; break; |
451 case 30: s = "invalid Factor"; break; | 470 case 30: s = "invalid Statement"; break; |
471 case 31: s = "invalid Factor"; break; | |
452 default: | 472 default: |
453 s = "error " + n; | 473 s = "error " + n; |
454 break; | 474 break; |
455 } | 475 } |
456 printMsg(line, col, s); | 476 printMsg(line, col, s); |