comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Parser.java @ 7503:31da1716950f

Updated truffle-sl for the changed operation code generation.
author Christian Humer <christian.humer@gmail.com>
date Fri, 18 Jan 2013 13:29:14 +0100
parents 213c1297a814
children b6743d7eb8d4
comparison
equal deleted inserted replaced
7502:6343a09b2ec1 7503:31da1716950f
26 package com.oracle.truffle.sl.parser; 26 package com.oracle.truffle.sl.parser;
27 import java.util.*; 27 import java.util.*;
28 28
29 import com.oracle.truffle.sl.*; 29 import com.oracle.truffle.sl.*;
30 import com.oracle.truffle.sl.nodes.*; 30 import com.oracle.truffle.sl.nodes.*;
31 import com.oracle.truffle.sl.types.*;
32 31
33 // Checkstyle: stop 32 // Checkstyle: stop
34 public class Parser { 33 public class Parser {
35 public static final int _EOF = 0; 34 public static final int _EOF = 0;
36 public static final int _identifier = 1; 35 public static final int _identifier = 1;
47 int errDist = minErrDist; 46 int errDist = minErrDist;
48 47
49 public final Scanner scanner; 48 public final Scanner scanner;
50 public final Errors errors; 49 public final Errors errors;
51 private final NodeFactory factory; 50 private final NodeFactory factory;
52 51
53 public Parser(Scanner scanner, NodeFactory factory) { 52 public Parser(Scanner scanner, NodeFactory factory) {
54 this.scanner = scanner; 53 this.scanner = scanner;
55 this.factory = factory; 54 this.factory = factory;
56 errors = new Errors(); 55 errors = new Errors();
57 } 56 }
127 } 126 }
128 } 127 }
129 128
130 void Function() { 129 void Function() {
131 Expect(4); 130 Expect(4);
132 factory.startFunction(); 131 factory.startFunction();
133 Expect(1); 132 Expect(1);
134 String name = t.val; 133 String name = t.val;
135 StatementNode body = Block(); 134 StatementNode body = Block();
136 factory.createFunction(body, name); 135 factory.createFunction(body, name);
137 } 136 }
138 137
139 StatementNode Block() { 138 StatementNode Block() {
140 StatementNode result; 139 StatementNode result;
141 List<StatementNode> statements = new ArrayList<>(); 140 List<StatementNode> statements = new ArrayList<>();
142 Expect(5); 141 Expect(5);
143 while (StartOf(1)) { 142 while (StartOf(1)) {
144 StatementNode statement = Statement(); 143 StatementNode statement = Statement();
145 statements.add(statement); 144 statements.add(statement);
146 } 145 }
147 Expect(6); 146 Expect(6);
148 result = factory.createBlock(statements); 147 result = factory.createBlock(statements);
149 return result; 148 return result;
150 } 149 }
151 150
152 StatementNode Statement() { 151 StatementNode Statement() {
153 StatementNode result; 152 StatementNode result;
154 result = null; 153 result = null;
155 if (la.kind == 7) { 154 if (la.kind == 7) {
156 result = WhileStatement(); 155 result = WhileStatement();
157 } else if (la.kind == 1) { 156 } else if (la.kind == 1) {
158 result = AssignmentStatement(); 157 result = AssignmentStatement();
159 } else if (la.kind == 12) { 158 } else if (la.kind == 12) {
169 Expect(7); 168 Expect(7);
170 Expect(8); 169 Expect(8);
171 ConditionNode condition = Expression(); 170 ConditionNode condition = Expression();
172 Expect(9); 171 Expect(9);
173 StatementNode body = Block(); 172 StatementNode body = Block();
174 result = factory.createWhile(condition, body); 173 result = factory.createWhile(condition, body);
175 return result; 174 return result;
176 } 175 }
177 176
178 StatementNode AssignmentStatement() { 177 StatementNode AssignmentStatement() {
179 StatementNode result; 178 StatementNode result;
180 Expect(1); 179 Expect(1);
181 String name = t.val; 180 String name = t.val;
182 Expect(10); 181 Expect(10);
183 TypedNode rvalue = Expression(); 182 TypedNode rvalue = Expression();
184 Expect(11); 183 Expect(11);
185 result = factory.createAssignment(name, rvalue); 184 result = factory.createAssignment(name, rvalue);
186 return result; 185 return result;
187 } 186 }
188 187
189 StatementNode OutputStatement() { 188 StatementNode OutputStatement() {
190 StatementNode result; 189 StatementNode result;
191 List<TypedNode> expressions = new ArrayList<>(); 190 List<TypedNode> expressions = new ArrayList<>();
192 Expect(12); 191 Expect(12);
193 while (StartOf(2)) { 192 while (StartOf(2)) {
194 TypedNode value = Expression(); 193 TypedNode value = Expression();
195 expressions.add(value); 194 expressions.add(value);
196 } 195 }
197 Expect(11); 196 Expect(11);
198 result = factory.createPrint(expressions); 197 result = factory.createPrint(expressions);
199 return result; 198 return result;
200 } 199 }
201 200
202 StatementNode ReturnStatement() { 201 StatementNode ReturnStatement() {
203 StatementNode result; 202 StatementNode result;
204 Expect(13); 203 Expect(13);
205 TypedNode value = Expression(); 204 TypedNode value = Expression();
206 Expect(11); 205 Expect(11);
207 result = factory.createReturn(value); 206 result = factory.createReturn(value);
208 return result; 207 return result;
209 } 208 }
210 209
211 TypedNode Expression() { 210 TypedNode Expression() {
212 TypedNode result; 211 TypedNode result;
236 case 19: { 235 case 19: {
237 Get(); 236 Get();
238 break; 237 break;
239 } 238 }
240 } 239 }
241 String op = t.val; 240 String op = t.val;
242 TypedNode right = ValueExpression(); 241 TypedNode right = ValueExpression();
243 result = factory.createBinary(op, result, right); 242 result = factory.createBinary(op, result, right);
244 } 243 }
245 return result; 244 return result;
246 } 245 }
247 246
248 TypedNode ValueExpression() { 247 TypedNode ValueExpression() {
252 if (la.kind == 20) { 251 if (la.kind == 20) {
253 Get(); 252 Get();
254 } else { 253 } else {
255 Get(); 254 Get();
256 } 255 }
257 String op = t.val; 256 String op = t.val;
258 TypedNode right = Term(); 257 TypedNode right = Term();
259 result = factory.createBinary(op, result, right); 258 result = factory.createBinary(op, result, right);
260 } 259 }
261 return result; 260 return result;
262 } 261 }
263 262
264 TypedNode Term() { 263 TypedNode Term() {
268 if (la.kind == 22) { 267 if (la.kind == 22) {
269 Get(); 268 Get();
270 } else { 269 } else {
271 Get(); 270 Get();
272 } 271 }
273 String op = t.val; 272 String op = t.val;
274 TypedNode right = Factor(); 273 TypedNode right = Factor();
275 result = factory.createBinary(op, result, right); 274 result = factory.createBinary(op, result, right);
276 } 275 }
277 return result; 276 return result;
278 } 277 }
279 278
280 TypedNode Factor() { 279 TypedNode Factor() {
281 TypedNode result; 280 TypedNode result;
282 result = null; 281 result = null;
283 if (la.kind == 24) { 282 if (la.kind == 24) {
284 result = TimeRef(); 283 result = TimeRef();
285 } else if (la.kind == 1) { 284 } else if (la.kind == 1) {
286 result = VariableRef(); 285 result = VariableRef();
287 } else if (la.kind == 2) { 286 } else if (la.kind == 2) {
297 } 296 }
298 297
299 TypedNode TimeRef() { 298 TypedNode TimeRef() {
300 TypedNode result; 299 TypedNode result;
301 Expect(24); 300 Expect(24);
302 result = factory.createTime(); 301 result = factory.createTime();
303 return result; 302 return result;
304 } 303 }
305 304
306 TypedNode VariableRef() { 305 TypedNode VariableRef() {
307 TypedNode result; 306 TypedNode result;
308 Expect(1); 307 Expect(1);
309 result = factory.createLocal(t.val); 308 result = factory.createLocal(t.val);
310 return result; 309 return result;
311 } 310 }
312 311
313 TypedNode StringLiteral() { 312 TypedNode StringLiteral() {
314 TypedNode result; 313 TypedNode result;
315 Expect(2); 314 Expect(2);
316 result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1)); 315 result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1));
317 return result; 316 return result;
318 } 317 }
319 318
320 TypedNode NumericLiteral() { 319 TypedNode NumericLiteral() {
321 TypedNode result; 320 TypedNode result;
322 Expect(3); 321 Expect(3);
323 result = factory.createNumericLiteral(t.val); 322 result = factory.createNumericLiteral(t.val);
324 return result; 323 return result;
325 } 324 }
326 325
327 326
328 327