Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Parser.java @ 13200:ebdc13d9845d
replaced use of graal.compileTheWorldTest.log system property with a field to control CTW verbosity which is true by default but is set to false by CTW unit test
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Sat, 30 Nov 2013 11:07:41 +0100 |
parents | 7311354f5bf8 |
children | 7c418666c6c9 |
rev | line source |
---|---|
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
1 /* |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
4 * |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
8 * |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
13 * accompanied this code). |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
14 * |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
18 * |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
21 * questions. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
22 */ |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
23 |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
24 // The content of this file is automatically generated. DO NOT EDIT. |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
25 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
26 package com.oracle.truffle.sl.parser; |
12503
d7f8dd4fe876
minor reformatting based on 'mx eclipseformat'
Doug Simon <doug.simon@oracle.com>
parents:
11610
diff
changeset
|
27 |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
28 import java.util.*; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
29 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
30 import com.oracle.truffle.sl.*; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
31 import com.oracle.truffle.sl.nodes.*; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
32 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
33 // Checkstyle: stop |
7517
b6743d7eb8d4
disabled auto-formatting of generated code
Doug Simon <doug.simon@oracle.com>
parents:
7503
diff
changeset
|
34 // @formatter:off |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
35 public class Parser { |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
36 public static final int _EOF = 0; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
37 public static final int _identifier = 1; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
38 public static final int _stringLiteral = 2; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
39 public static final int _numericLiteral = 3; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
40 public static final int maxT = 29; |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
41 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
42 static final boolean T = true; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
43 static final boolean x = false; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
44 static final int minErrDist = 2; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
45 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
46 public Token t; // last recognized token |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
47 public Token la; // lookahead token |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
48 int errDist = minErrDist; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
49 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
50 public final Scanner scanner; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
51 public final Errors errors; |
12752
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12503
diff
changeset
|
52 private final SLNodeFactory factory; |
7848
698cd036a1ca
Added a ternary operator to simple language to test short circuit specialization.
Christian Humer <christian.humer@gmail.com>
parents:
7530
diff
changeset
|
53 |
12752
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12503
diff
changeset
|
54 public Parser(Scanner scanner, SLNodeFactory factory) { |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
55 this.scanner = scanner; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
56 this.factory = factory; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
57 errors = new Errors(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
58 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
59 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
60 void SynErr(int n) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
61 if (errDist >= minErrDist) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
62 errors.SynErr(la.line, la.col, n); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
63 errDist = 0; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
64 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
65 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
66 public void SemErr(String msg) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
67 if (errDist >= minErrDist) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
68 errors.SemErr(t.line, t.col, msg); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
69 errDist = 0; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
70 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
71 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
72 void Get() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
73 for (;;) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
74 t = la; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
75 la = scanner.Scan(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
76 if (la.kind <= maxT) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
77 ++errDist; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
78 break; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
79 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
80 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
81 la = t; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
82 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
83 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
84 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
85 void Expect(int n) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
86 if (la.kind == n) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
87 Get(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
88 else { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
89 SynErr(n); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
90 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
91 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
92 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
93 boolean StartOf(int s) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
94 return set[s][la.kind]; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
95 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
96 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
97 void ExpectWeak(int n, int follow) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
98 if (la.kind == n) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
99 Get(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
100 else { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
101 SynErr(n); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
102 while (!StartOf(follow)) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
103 Get(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
104 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
105 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
106 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
107 boolean WeakSeparator(int n, int syFol, int repFol) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
108 int kind = la.kind; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
109 if (kind == n) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
110 Get(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
111 return true; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
112 } else if (StartOf(repFol)) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
113 return false; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
114 else { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
115 SynErr(n); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
116 while (!(set[syFol][kind] || set[repFol][kind] || set[0][kind])) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
117 Get(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
118 kind = la.kind; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
119 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
120 return StartOf(syFol); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
121 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
122 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
123 |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
124 void SimpleLanguage() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
125 Function(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
126 while (la.kind == 4) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
127 Function(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
128 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
129 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
130 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
131 void Function() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
132 Expect(4); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
133 factory.startFunction(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
134 Expect(1); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
135 String name = t.val; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
136 List<String> parameterNames = new ArrayList<>(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
137 if (la.kind == 5) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
138 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
139 if (la.kind == 1) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
140 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
141 parameterNames.add(t.val); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
142 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
143 while (la.kind == 6) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
144 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
145 Expect(1); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
146 parameterNames.add(t.val); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
147 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
148 Expect(7); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
149 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
150 StatementNode body = Block(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
151 factory.createFunction(body, name, parameterNames.toArray(new String[parameterNames.size()])); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
152 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
153 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
154 StatementNode Block() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
155 StatementNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
156 List<StatementNode> statements = new ArrayList<>(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
157 Expect(8); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
158 while (StartOf(1)) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
159 StatementNode statement = Statement(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
160 statements.add(statement); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
161 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
162 Expect(9); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
163 result = factory.createBlock(statements); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
164 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
165 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
166 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
167 StatementNode Statement() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
168 StatementNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
169 result = null; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
170 if (la.kind == 13) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
171 result = WhileStatement(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
172 } else if (la.kind == 11) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
173 result = IfStatement(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
174 } else if (la.kind == 14) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
175 result = ReturnStatement(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
176 } else if (StartOf(2)) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
177 result = Expression(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
178 Expect(10); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
179 } else SynErr(30); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
180 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
181 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
182 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
183 StatementNode WhileStatement() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
184 StatementNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
185 Expect(13); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
186 Expect(5); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
187 ConditionNode condition = Expression(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
188 Expect(7); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
189 StatementNode body = Block(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
190 result = factory.createWhile(condition, body); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
191 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
192 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
193 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
194 StatementNode IfStatement() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
195 StatementNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
196 Expect(11); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
197 Expect(5); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
198 ConditionNode condition = Expression(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
199 Expect(7); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
200 StatementNode thenNode = null; StatementNode elseNode = null; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
201 thenNode = Block(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
202 if (la.kind == 12) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
203 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
204 elseNode = Block(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
205 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
206 result = factory.createIf(condition, thenNode, elseNode); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
207 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
208 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
209 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
210 StatementNode ReturnStatement() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
211 StatementNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
212 Expect(14); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
213 TypedNode value = Expression(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
214 Expect(10); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
215 result = factory.createReturn(value); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
216 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
217 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
218 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
219 TypedNode Expression() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
220 TypedNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
221 result = ValueExpression(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
222 if (StartOf(3)) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
223 switch (la.kind) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
224 case 15: { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
225 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
226 break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
227 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
228 case 16: { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
229 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
230 break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
231 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
232 case 17: { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
233 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
234 break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
235 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
236 case 18: { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
237 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
238 break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
239 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
240 case 19: { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
241 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
242 break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
243 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
244 case 20: { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
245 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
246 break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
247 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
248 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
249 String op = t.val; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
250 TypedNode right = ValueExpression(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
251 result = factory.createBinary(op, result, right); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
252 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
253 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
254 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
255 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
256 TypedNode ValueExpression() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
257 TypedNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
258 result = Term(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
259 while (la.kind == 21 || la.kind == 22) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
260 if (la.kind == 21) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
261 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
262 } else { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
263 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
264 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
265 String op = t.val; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
266 TypedNode right = Term(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
267 result = factory.createBinary(op, result, right); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
268 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
269 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
270 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
271 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
272 TypedNode Term() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
273 TypedNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
274 result = Factor(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
275 while (la.kind == 23 || la.kind == 24) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
276 if (la.kind == 23) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
277 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
278 } else { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
279 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
280 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
281 String op = t.val; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
282 TypedNode right = Factor(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
283 result = factory.createBinary(op, result, right); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
284 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
285 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
286 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
287 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
288 TypedNode Factor() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
289 TypedNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
290 result = null; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
291 if (la.kind == 1) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
292 result = VariableRefOrCall(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
293 } else if (la.kind == 2) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
294 result = StringLiteral(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
295 } else if (la.kind == 3) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
296 result = NumericLiteral(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
297 } else if (la.kind == 25) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
298 result = Ternary(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
299 } else if (la.kind == 5) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
300 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
301 result = Expression(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
302 Expect(7); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
303 } else SynErr(31); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
304 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
305 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
306 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
307 TypedNode VariableRefOrCall() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
308 TypedNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
309 result = VariableRef(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
310 if (la.kind == 5 || la.kind == 28) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
311 if (la.kind == 5) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
312 TypedNode[] parameters = Parameters(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
313 result = factory.createCall(result, parameters); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
314 } else { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
315 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
316 TypedNode assignment = Expression(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
317 result = factory.createAssignment(result, assignment); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
318 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
319 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
320 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
321 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
322 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
323 TypedNode StringLiteral() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
324 TypedNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
325 Expect(2); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
326 result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1)); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
327 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
328 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
329 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
330 TypedNode NumericLiteral() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
331 TypedNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
332 Expect(3); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
333 result = factory.createNumericLiteral(t.val); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
334 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
335 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
336 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
337 TypedNode Ternary() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
338 TypedNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
339 TypedNode condition, thenPart, elsePart; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
340 Expect(25); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
341 condition = Expression(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
342 Expect(26); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
343 thenPart = Expression(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
344 Expect(27); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
345 elsePart = Expression(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
346 result = factory.createTernary(condition, thenPart, elsePart); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
347 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
348 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
349 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
350 TypedNode VariableRef() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
351 TypedNode result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
352 Expect(1); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
353 result = factory.createLocal(t.val); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
354 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
355 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
356 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
357 TypedNode[] Parameters() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
358 TypedNode[] result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
359 Expect(5); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
360 List<TypedNode> parameters = new ArrayList<>(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
361 if (StartOf(2)) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
362 TypedNode e1 = Expression(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
363 parameters.add(e1); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
364 while (la.kind == 6) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
365 Get(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
366 TypedNode e2 = Expression(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
367 parameters.add(e2); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
368 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
369 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
370 result = parameters.toArray(new TypedNode[parameters.size()]); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
371 Expect(7); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
372 return result; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
373 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
374 |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
375 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
376 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
377 public void Parse() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
378 la = new Token(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
379 la.val = ""; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
380 Get(); |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
381 SimpleLanguage(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
382 Expect(0); |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
383 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
384 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
385 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
386 private static final boolean[][] set = { |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
387 {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}, |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
388 {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}, |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
389 {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}, |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
390 {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} |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
391 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
392 }; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
393 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
394 public String ParseErrors() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
395 java.io.PrintStream oldStream = System.out; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
396 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
397 java.io.OutputStream out = new java.io.ByteArrayOutputStream(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
398 java.io.PrintStream newStream = new java.io.PrintStream(out); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
399 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
400 errors.errorStream = newStream; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
401 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
402 Parse(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
403 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
404 String errorStream = out.toString(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
405 errors.errorStream = oldStream; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
406 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
407 return errorStream; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
408 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
409 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
410 } // end Parser |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
411 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
412 class Errors { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
413 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
414 public int count = 0; // number of errors detected |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
415 public java.io.PrintStream errorStream = System.out; // error messages go to this stream |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
416 public String errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
417 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
418 protected void printMsg(int line, int column, String msg) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
419 StringBuffer b = new StringBuffer(errMsgFormat); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
420 int pos = b.indexOf("{0}"); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
421 if (pos >= 0) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
422 b.delete(pos, pos + 3); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
423 b.insert(pos, line); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
424 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
425 pos = b.indexOf("{1}"); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
426 if (pos >= 0) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
427 b.delete(pos, pos + 3); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
428 b.insert(pos, column); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
429 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
430 pos = b.indexOf("{2}"); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
431 if (pos >= 0) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
432 b.replace(pos, pos + 3, msg); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
433 errorStream.println(b.toString()); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
434 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
435 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
436 public void SynErr(int line, int col, int n) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
437 String s; |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
438 switch (n) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
439 case 0: s = "EOF expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
440 case 1: s = "identifier expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
441 case 2: s = "stringLiteral expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
442 case 3: s = "numericLiteral expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
443 case 4: s = "\"function\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
444 case 5: s = "\"(\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
445 case 6: s = "\",\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
446 case 7: s = "\")\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
447 case 8: s = "\"{\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
448 case 9: s = "\"}\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
449 case 10: s = "\";\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
450 case 11: s = "\"if\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
451 case 12: s = "\"else\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
452 case 13: s = "\"while\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
453 case 14: s = "\"return\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
454 case 15: s = "\"<\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
455 case 16: s = "\">\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
456 case 17: s = "\"<=\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
457 case 18: s = "\">=\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
458 case 19: s = "\"==\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
459 case 20: s = "\"!=\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
460 case 21: s = "\"+\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
461 case 22: s = "\"-\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
462 case 23: s = "\"*\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
463 case 24: s = "\"/\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
464 case 25: s = "\"#\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
465 case 26: s = "\"?\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
466 case 27: s = "\":\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
467 case 28: s = "\"=\" expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
468 case 29: s = "??? expected"; break; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
469 case 30: s = "invalid Statement"; break; |
12752
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12503
diff
changeset
|
470 case 31: s = "invalid Factor"; break; |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
471 default: |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
472 s = "error " + n; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
473 break; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
474 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
475 printMsg(line, col, s); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
476 count++; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
477 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
478 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
479 public void SemErr(int line, int col, String s) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
480 printMsg(line, col, s); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
481 count++; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
482 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
483 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
484 public void SemErr(String s) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
485 errorStream.println(s); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
486 count++; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
487 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
488 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
489 public void Warning(int line, int col, String s) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
490 printMsg(line, col, s); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
491 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
492 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
493 public void Warning(String s) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
494 errorStream.println(s); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
495 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
496 } // Errors |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
497 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
498 class FatalError extends RuntimeException { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
499 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
500 public static final long serialVersionUID = 1L; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
501 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
502 public FatalError(String s) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
503 super(s); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
504 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
505 } |