Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Parser.java @ 7848:698cd036a1ca
Added a ternary operator to simple language to test short circuit specialization.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Tue, 19 Feb 2013 17:27:02 +0100 |
parents | 5e3d1a68664e |
children | 5f532ea846fb |
comparison
equal
deleted
inserted
replaced
7847:06a7cd6aaf00 | 7848:698cd036a1ca |
---|---|
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 package com.oracle.truffle.sl.parser; | 26 package com.oracle.truffle.sl.parser; |
27 | |
28 import java.util.*; | 27 import java.util.*; |
29 | 28 |
30 import com.oracle.truffle.sl.*; | 29 import com.oracle.truffle.sl.*; |
31 import com.oracle.truffle.sl.nodes.*; | 30 import com.oracle.truffle.sl.nodes.*; |
32 | 31 |
35 public class Parser { | 34 public class Parser { |
36 public static final int _EOF = 0; | 35 public static final int _EOF = 0; |
37 public static final int _identifier = 1; | 36 public static final int _identifier = 1; |
38 public static final int _stringLiteral = 2; | 37 public static final int _stringLiteral = 2; |
39 public static final int _numericLiteral = 3; | 38 public static final int _numericLiteral = 3; |
40 public static final int maxT = 25; | 39 public static final int maxT = 28; |
41 | 40 |
42 static final boolean T = true; | 41 static final boolean T = true; |
43 static final boolean x = false; | 42 static final boolean x = false; |
44 static final int minErrDist = 2; | 43 static final int minErrDist = 2; |
45 | 44 |
48 int errDist = minErrDist; | 47 int errDist = minErrDist; |
49 | 48 |
50 public final Scanner scanner; | 49 public final Scanner scanner; |
51 public final Errors errors; | 50 public final Errors errors; |
52 private final NodeFactory factory; | 51 private final NodeFactory factory; |
53 | 52 |
54 public Parser(Scanner scanner, NodeFactory factory) { | 53 public Parser(Scanner scanner, NodeFactory factory) { |
55 this.scanner = scanner; | 54 this.scanner = scanner; |
56 this.factory = factory; | 55 this.factory = factory; |
57 errors = new Errors(); | 56 errors = new Errors(); |
58 } | 57 } |
128 } | 127 } |
129 } | 128 } |
130 | 129 |
131 void Function() { | 130 void Function() { |
132 Expect(4); | 131 Expect(4); |
133 factory.startFunction(); | 132 factory.startFunction(); |
134 Expect(1); | 133 Expect(1); |
135 String name = t.val; | 134 String name = t.val; |
136 StatementNode body = Block(); | 135 StatementNode body = Block(); |
137 factory.createFunction(body, name); | 136 factory.createFunction(body, name); |
138 } | 137 } |
139 | 138 |
140 StatementNode Block() { | 139 StatementNode Block() { |
141 StatementNode result; | 140 StatementNode result; |
142 List<StatementNode> statements = new ArrayList<>(); | 141 List<StatementNode> statements = new ArrayList<>(); |
143 Expect(5); | 142 Expect(5); |
144 while (StartOf(1)) { | 143 while (StartOf(1)) { |
145 StatementNode statement = Statement(); | 144 StatementNode statement = Statement(); |
146 statements.add(statement); | 145 statements.add(statement); |
147 } | 146 } |
148 Expect(6); | 147 Expect(6); |
149 result = factory.createBlock(statements); | 148 result = factory.createBlock(statements); |
150 return result; | 149 return result; |
151 } | 150 } |
152 | 151 |
153 StatementNode Statement() { | 152 StatementNode Statement() { |
154 StatementNode result; | 153 StatementNode result; |
155 result = null; | 154 result = null; |
156 if (la.kind == 7) { | 155 if (la.kind == 7) { |
157 result = WhileStatement(); | 156 result = WhileStatement(); |
158 } else if (la.kind == 1) { | 157 } else if (la.kind == 1) { |
159 result = AssignmentStatement(); | 158 result = AssignmentStatement(); |
160 } else if (la.kind == 12) { | 159 } else if (la.kind == 12) { |
161 result = OutputStatement(); | 160 result = OutputStatement(); |
162 } else if (la.kind == 13) { | 161 } else if (la.kind == 13) { |
163 result = ReturnStatement(); | 162 result = ReturnStatement(); |
164 } else SynErr(26); | 163 } else SynErr(29); |
165 return result; | 164 return result; |
166 } | 165 } |
167 | 166 |
168 StatementNode WhileStatement() { | 167 StatementNode WhileStatement() { |
169 StatementNode result; | 168 StatementNode result; |
170 Expect(7); | 169 Expect(7); |
171 Expect(8); | 170 Expect(8); |
172 ConditionNode condition = Expression(); | 171 ConditionNode condition = Expression(); |
173 Expect(9); | 172 Expect(9); |
174 StatementNode body = Block(); | 173 StatementNode body = Block(); |
175 result = factory.createWhile(condition, body); | 174 result = factory.createWhile(condition, body); |
176 return result; | 175 return result; |
177 } | 176 } |
178 | 177 |
179 StatementNode AssignmentStatement() { | 178 StatementNode AssignmentStatement() { |
180 StatementNode result; | 179 StatementNode result; |
181 Expect(1); | 180 Expect(1); |
182 String name = t.val; | 181 String name = t.val; |
183 Expect(10); | 182 Expect(10); |
184 TypedNode rvalue = Expression(); | 183 TypedNode rvalue = Expression(); |
185 Expect(11); | 184 Expect(11); |
186 result = factory.createAssignment(name, rvalue); | 185 result = factory.createAssignment(name, rvalue); |
187 return result; | 186 return result; |
188 } | 187 } |
189 | 188 |
190 StatementNode OutputStatement() { | 189 StatementNode OutputStatement() { |
191 StatementNode result; | 190 StatementNode result; |
192 List<TypedNode> expressions = new ArrayList<>(); | 191 List<TypedNode> expressions = new ArrayList<>(); |
193 Expect(12); | 192 Expect(12); |
194 while (StartOf(2)) { | 193 while (StartOf(2)) { |
195 TypedNode value = Expression(); | 194 TypedNode value = Expression(); |
196 expressions.add(value); | 195 expressions.add(value); |
197 } | 196 } |
198 Expect(11); | 197 Expect(11); |
199 result = factory.createPrint(expressions); | 198 result = factory.createPrint(expressions); |
200 return result; | 199 return result; |
201 } | 200 } |
202 | 201 |
203 StatementNode ReturnStatement() { | 202 StatementNode ReturnStatement() { |
204 StatementNode result; | 203 StatementNode result; |
205 Expect(13); | 204 Expect(13); |
206 TypedNode value = Expression(); | 205 TypedNode value = Expression(); |
207 Expect(11); | 206 Expect(11); |
208 result = factory.createReturn(value); | 207 result = factory.createReturn(value); |
209 return result; | 208 return result; |
210 } | 209 } |
211 | 210 |
212 TypedNode Expression() { | 211 TypedNode Expression() { |
213 TypedNode result; | 212 TypedNode result; |
237 case 19: { | 236 case 19: { |
238 Get(); | 237 Get(); |
239 break; | 238 break; |
240 } | 239 } |
241 } | 240 } |
242 String op = t.val; | 241 String op = t.val; |
243 TypedNode right = ValueExpression(); | 242 TypedNode right = ValueExpression(); |
244 result = factory.createBinary(op, result, right); | 243 result = factory.createBinary(op, result, right); |
245 } | 244 } |
246 return result; | 245 return result; |
247 } | 246 } |
248 | 247 |
249 TypedNode ValueExpression() { | 248 TypedNode ValueExpression() { |
253 if (la.kind == 20) { | 252 if (la.kind == 20) { |
254 Get(); | 253 Get(); |
255 } else { | 254 } else { |
256 Get(); | 255 Get(); |
257 } | 256 } |
258 String op = t.val; | 257 String op = t.val; |
259 TypedNode right = Term(); | 258 TypedNode right = Term(); |
260 result = factory.createBinary(op, result, right); | 259 result = factory.createBinary(op, result, right); |
261 } | 260 } |
262 return result; | 261 return result; |
263 } | 262 } |
264 | 263 |
265 TypedNode Term() { | 264 TypedNode Term() { |
269 if (la.kind == 22) { | 268 if (la.kind == 22) { |
270 Get(); | 269 Get(); |
271 } else { | 270 } else { |
272 Get(); | 271 Get(); |
273 } | 272 } |
274 String op = t.val; | 273 String op = t.val; |
275 TypedNode right = Factor(); | 274 TypedNode right = Factor(); |
276 result = factory.createBinary(op, result, right); | 275 result = factory.createBinary(op, result, right); |
277 } | 276 } |
278 return result; | 277 return result; |
279 } | 278 } |
280 | 279 |
281 TypedNode Factor() { | 280 TypedNode Factor() { |
282 TypedNode result; | 281 TypedNode result; |
283 result = null; | 282 result = null; |
284 if (la.kind == 24) { | 283 switch (la.kind) { |
284 case 27: { | |
285 result = TimeRef(); | 285 result = TimeRef(); |
286 } else if (la.kind == 1) { | 286 break; |
287 } | |
288 case 1: { | |
287 result = VariableRef(); | 289 result = VariableRef(); |
288 } else if (la.kind == 2) { | 290 break; |
291 } | |
292 case 2: { | |
289 result = StringLiteral(); | 293 result = StringLiteral(); |
290 } else if (la.kind == 3) { | 294 break; |
295 } | |
296 case 3: { | |
291 result = NumericLiteral(); | 297 result = NumericLiteral(); |
292 } else if (la.kind == 8) { | 298 break; |
299 } | |
300 case 24: { | |
301 result = Ternary(); | |
302 break; | |
303 } | |
304 case 8: { | |
293 Get(); | 305 Get(); |
294 result = Expression(); | 306 result = Expression(); |
295 Expect(9); | 307 Expect(9); |
296 } else SynErr(27); | 308 break; |
309 } | |
310 default: SynErr(30); break; | |
311 } | |
297 return result; | 312 return result; |
298 } | 313 } |
299 | 314 |
300 TypedNode TimeRef() { | 315 TypedNode TimeRef() { |
301 TypedNode result; | 316 TypedNode result; |
317 Expect(27); | |
318 result = factory.createTime(); | |
319 return result; | |
320 } | |
321 | |
322 TypedNode VariableRef() { | |
323 TypedNode result; | |
324 Expect(1); | |
325 result = factory.createLocal(t.val); | |
326 return result; | |
327 } | |
328 | |
329 TypedNode StringLiteral() { | |
330 TypedNode result; | |
331 Expect(2); | |
332 result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1)); | |
333 return result; | |
334 } | |
335 | |
336 TypedNode NumericLiteral() { | |
337 TypedNode result; | |
338 Expect(3); | |
339 result = factory.createNumericLiteral(t.val); | |
340 return result; | |
341 } | |
342 | |
343 TypedNode Ternary() { | |
344 TypedNode result; | |
345 TypedNode condition, thenPart, elsePart; | |
302 Expect(24); | 346 Expect(24); |
303 result = factory.createTime(); | 347 condition = Expression(); |
304 return result; | 348 Expect(25); |
305 } | 349 thenPart = Expression(); |
306 | 350 Expect(26); |
307 TypedNode VariableRef() { | 351 elsePart = Expression(); |
308 TypedNode result; | 352 result = factory.createTernary(condition, thenPart, elsePart); |
309 Expect(1); | |
310 result = factory.createLocal(t.val); | |
311 return result; | |
312 } | |
313 | |
314 TypedNode StringLiteral() { | |
315 TypedNode result; | |
316 Expect(2); | |
317 result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1)); | |
318 return result; | |
319 } | |
320 | |
321 TypedNode NumericLiteral() { | |
322 TypedNode result; | |
323 Expect(3); | |
324 result = factory.createNumericLiteral(t.val); | |
325 return result; | 353 return result; |
326 } | 354 } |
327 | 355 |
328 | 356 |
329 | 357 |
335 Expect(0); | 363 Expect(0); |
336 | 364 |
337 } | 365 } |
338 | 366 |
339 private static final boolean[][] set = { | 367 private static final boolean[][] set = { |
340 {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}, | 368 {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}, |
341 {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}, | 369 {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}, |
342 {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}, | 370 {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}, |
343 {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} | 371 {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} |
344 | 372 |
345 }; | 373 }; |
346 | 374 |
347 public String ParseErrors() { | 375 public String ParseErrors() { |
348 java.io.PrintStream oldStream = System.out; | 376 java.io.PrintStream oldStream = System.out; |
411 case 19: s = "\"!=\" expected"; break; | 439 case 19: s = "\"!=\" expected"; break; |
412 case 20: s = "\"+\" expected"; break; | 440 case 20: s = "\"+\" expected"; break; |
413 case 21: s = "\"-\" expected"; break; | 441 case 21: s = "\"-\" expected"; break; |
414 case 22: s = "\"*\" expected"; break; | 442 case 22: s = "\"*\" expected"; break; |
415 case 23: s = "\"/\" expected"; break; | 443 case 23: s = "\"/\" expected"; break; |
416 case 24: s = "\"time\" expected"; break; | 444 case 24: s = "\"#\" expected"; break; |
417 case 25: s = "??? expected"; break; | 445 case 25: s = "\"?\" expected"; break; |
418 case 26: s = "invalid Statement"; break; | 446 case 26: s = "\":\" expected"; break; |
419 case 27: s = "invalid Factor"; break; | 447 case 27: s = "\"time\" expected"; break; |
448 case 28: s = "??? expected"; break; | |
449 case 29: s = "invalid Statement"; break; | |
450 case 30: s = "invalid Factor"; break; | |
420 default: | 451 default: |
421 s = "error " + n; | 452 s = "error " + n; |
422 break; | 453 break; |
423 } | 454 } |
424 printMsg(line, col, s); | 455 printMsg(line, col, s); |