Mercurial > hg > truffle
diff graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Parser.java @ 12752:71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 11 Nov 2013 21:34:44 +0100 |
parents | d7f8dd4fe876 |
children | 7311354f5bf8 |
line wrap: on
line diff
--- a/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Parser.java Thu Nov 07 20:55:13 2013 +0100 +++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Parser.java Mon Nov 11 21:34:44 2013 +0100 @@ -21,7 +21,8 @@ * questions. */ -// The content of this file is automatically generated. DO NOT EDIT. + // The content of this file is automatically generated. DO NOT EDIT. + package com.oracle.truffle.sl.parser; @@ -33,11 +34,11 @@ // Checkstyle: stop // @formatter:off public class Parser { - public static final int _EOF = 0; - public static final int _identifier = 1; - public static final int _stringLiteral = 2; - public static final int _numericLiteral = 3; - public static final int maxT = 28; + public static final int _EOF = 0; + public static final int _identifier = 1; + public static final int _stringLiteral = 2; + public static final int _numericLiteral = 3; + public static final int maxT = 29; static final boolean T = true; static final boolean x = false; @@ -49,9 +50,9 @@ public final Scanner scanner; public final Errors errors; - private final NodeFactory factory; + private final SLNodeFactory factory; - public Parser(Scanner scanner, NodeFactory factory) { + public Parser(Scanner scanner, SLNodeFactory factory) { this.scanner = scanner; this.factory = factory; errors = new Errors(); @@ -121,255 +122,273 @@ } } - void SimpleLanguage() { - Function(); - while (la.kind == 4) { - Function(); - } - } - - void Function() { - Expect(4); - factory.startFunction(); - Expect(1); - String name = t.val; - StatementNode body = Block(); - factory.createFunction(body, name); - } - - StatementNode Block() { - StatementNode result; - List<StatementNode> statements = new ArrayList<>(); - Expect(5); - while (StartOf(1)) { - StatementNode statement = Statement(); - statements.add(statement); - } - Expect(6); - result = factory.createBlock(statements); - return result; - } - - StatementNode Statement() { - StatementNode result; - result = null; - if (la.kind == 7) { - result = WhileStatement(); - } else if (la.kind == 1) { - result = AssignmentStatement(); - } else if (la.kind == 12) { - result = OutputStatement(); - } else if (la.kind == 13) { - result = ReturnStatement(); - } else SynErr(29); - return result; - } - - StatementNode WhileStatement() { - StatementNode result; - Expect(7); - Expect(8); - ConditionNode condition = Expression(); - Expect(9); - StatementNode body = Block(); - result = factory.createWhile(condition, body); - return result; - } - - StatementNode AssignmentStatement() { - StatementNode result; - Expect(1); - String name = t.val; - Expect(10); - TypedNode rvalue = Expression(); - Expect(11); - result = factory.createAssignment(name, rvalue); - return result; - } - - StatementNode OutputStatement() { - StatementNode result; - List<TypedNode> expressions = new ArrayList<>(); - Expect(12); - while (StartOf(2)) { - TypedNode value = Expression(); - expressions.add(value); - } - Expect(11); - result = factory.createPrint(expressions); - return result; - } - - StatementNode ReturnStatement() { - StatementNode result; - Expect(13); - TypedNode value = Expression(); - Expect(11); - result = factory.createReturn(value); - return result; - } - - TypedNode Expression() { - TypedNode result; - result = ValueExpression(); - if (StartOf(3)) { - switch (la.kind) { - case 14: { - Get(); - break; - } - case 15: { - Get(); - break; - } - case 16: { - Get(); - break; - } - case 17: { - Get(); - break; - } - case 18: { - Get(); - break; - } - case 19: { - Get(); - break; - } - } - String op = t.val; - TypedNode right = ValueExpression(); - result = factory.createBinary(op, result, right); - } - return result; - } - - TypedNode ValueExpression() { - TypedNode result; - result = Term(); - while (la.kind == 20 || la.kind == 21) { - if (la.kind == 20) { - Get(); - } else { - Get(); - } - String op = t.val; - TypedNode right = Term(); - result = factory.createBinary(op, result, right); - } - return result; - } - - TypedNode Term() { - TypedNode result; - result = Factor(); - while (la.kind == 22 || la.kind == 23) { - if (la.kind == 22) { - Get(); - } else { - Get(); - } - String op = t.val; - TypedNode right = Factor(); - result = factory.createBinary(op, result, right); - } - return result; - } - - TypedNode Factor() { - TypedNode result; - result = null; - switch (la.kind) { - case 27: { - result = TimeRef(); - break; - } - case 1: { - result = VariableRef(); - break; - } - case 2: { - result = StringLiteral(); - break; - } - case 3: { - result = NumericLiteral(); - break; - } - case 24: { - result = Ternary(); - break; - } - case 8: { - Get(); - result = Expression(); - Expect(9); - break; - } - default: SynErr(30); break; - } - return result; - } - - TypedNode TimeRef() { - TypedNode result; - Expect(27); - result = factory.createTime(); - return result; - } - - TypedNode VariableRef() { - TypedNode result; - Expect(1); - result = factory.createLocal(t.val); - return result; - } - - TypedNode StringLiteral() { - TypedNode result; - Expect(2); - result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1)); - return result; - } - - TypedNode NumericLiteral() { - TypedNode result; - Expect(3); - result = factory.createNumericLiteral(t.val); - return result; - } - - TypedNode Ternary() { - TypedNode result; - TypedNode condition, thenPart, elsePart; - Expect(24); - condition = Expression(); - Expect(25); - thenPart = Expression(); - Expect(26); - elsePart = Expression(); - result = factory.createTernary(condition, thenPart, elsePart); - return result; - } - + void SimpleLanguage() { + Function(); + while (la.kind == 4) { + Function(); + } + } + + void Function() { + Expect(4); + factory.startFunction(); + Expect(1); + String name = t.val; + List<String> parameterNames = new ArrayList<>(); + if (la.kind == 5) { + Get(); + if (la.kind == 1) { + Get(); + parameterNames.add(t.val); + } + while (la.kind == 6) { + Get(); + Expect(1); + parameterNames.add(t.val); + } + Expect(7); + } + StatementNode body = Block(); + factory.createFunction(body, name, parameterNames.toArray(new String[parameterNames.size()])); + } + + StatementNode Block() { + StatementNode result; + List<StatementNode> statements = new ArrayList<>(); + Expect(8); + while (StartOf(1)) { + StatementNode statement = Statement(); + statements.add(statement); + } + Expect(9); + result = factory.createBlock(statements); + return result; + } + + StatementNode Statement() { + StatementNode result; + result = null; + if (la.kind == 13) { + result = WhileStatement(); + } else if (la.kind == 11) { + result = IfStatement(); + } else if (la.kind == 14) { + result = ReturnStatement(); + } else if (StartOf(2)) { + result = Expression(); + Expect(10); + } else SynErr(30); + return result; + } + + StatementNode WhileStatement() { + StatementNode result; + Expect(13); + Expect(5); + ConditionNode condition = Expression(); + Expect(7); + StatementNode body = Block(); + result = factory.createWhile(condition, body); + return result; + } + + StatementNode IfStatement() { + StatementNode result; + Expect(11); + Expect(5); + ConditionNode condition = Expression(); + Expect(7); + StatementNode thenNode = null; StatementNode elseNode = null; + thenNode = Block(); + if (la.kind == 12) { + Get(); + elseNode = Block(); + } + result = factory.createIf(condition, thenNode, elseNode); + return result; + } + + StatementNode ReturnStatement() { + StatementNode result; + Expect(14); + TypedNode value = Expression(); + Expect(10); + result = factory.createReturn(value); + return result; + } + + TypedNode Expression() { + TypedNode result; + result = ValueExpression(); + if (StartOf(3)) { + switch (la.kind) { + case 15: { + Get(); + break; + } + case 16: { + Get(); + break; + } + case 17: { + Get(); + break; + } + case 18: { + Get(); + break; + } + case 19: { + Get(); + break; + } + case 20: { + Get(); + break; + } + } + String op = t.val; + TypedNode right = ValueExpression(); + result = factory.createBinary(op, result, right); + } + return result; + } + + TypedNode ValueExpression() { + TypedNode result; + result = Term(); + while (la.kind == 21 || la.kind == 22) { + if (la.kind == 21) { + Get(); + } else { + Get(); + } + String op = t.val; + TypedNode right = Term(); + result = factory.createBinary(op, result, right); + } + return result; + } + + TypedNode Term() { + TypedNode result; + result = Factor(); + while (la.kind == 23 || la.kind == 24) { + if (la.kind == 23) { + Get(); + } else { + Get(); + } + String op = t.val; + TypedNode right = Factor(); + result = factory.createBinary(op, result, right); + } + return result; + } + + TypedNode Factor() { + TypedNode result; + result = null; + if (la.kind == 1) { + result = VariableRefOrCall(); + } else if (la.kind == 2) { + result = StringLiteral(); + } else if (la.kind == 3) { + result = NumericLiteral(); + } else if (la.kind == 25) { + result = Ternary(); + } else if (la.kind == 5) { + Get(); + result = Expression(); + Expect(7); + } else SynErr(31); + return result; + } + + TypedNode VariableRefOrCall() { + TypedNode result; + result = VariableRef(); + if (la.kind == 5 || la.kind == 28) { + if (la.kind == 5) { + TypedNode[] parameters = Parameters(); + result = factory.createCall(result, parameters); + } else { + Get(); + TypedNode assignment = Expression(); + result = factory.createAssignment(result, assignment); + } + } + return result; + } + + TypedNode StringLiteral() { + TypedNode result; + Expect(2); + result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1)); + return result; + } + + TypedNode NumericLiteral() { + TypedNode result; + Expect(3); + result = factory.createNumericLiteral(t.val); + return result; + } + + TypedNode Ternary() { + TypedNode result; + TypedNode condition, thenPart, elsePart; + Expect(25); + condition = Expression(); + Expect(26); + thenPart = Expression(); + Expect(27); + elsePart = Expression(); + result = factory.createTernary(condition, thenPart, elsePart); + return result; + } + + TypedNode VariableRef() { + TypedNode result; + Expect(1); + result = factory.createLocal(t.val); + return result; + } + + TypedNode[] Parameters() { + TypedNode[] result; + Expect(5); + List<TypedNode> parameters = new ArrayList<>(); + if (StartOf(2)) { + TypedNode e1 = Expression(); + parameters.add(e1); + while (la.kind == 6) { + Get(); + TypedNode e2 = Expression(); + parameters.add(e2); + } + } + result = parameters.toArray(new TypedNode[parameters.size()]); + Expect(7); + return result; + } + public void Parse() { la = new Token(); la.val = ""; Get(); - SimpleLanguage(); - Expect(0); + SimpleLanguage(); + Expect(0); } private static final boolean[][] set = { - {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,T,x,x, x,x,x,T, x,x,x,x, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x}, - {x,T,T,T, x,x,x,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,x,x,T, x,x}, - {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} + {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}, + {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}, + {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}, + {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} }; @@ -417,38 +436,39 @@ public void SynErr(int line, int col, int n) { String s; - switch (n) { - case 0: s = "EOF expected"; break; - case 1: s = "identifier expected"; break; - case 2: s = "stringLiteral expected"; break; - case 3: s = "numericLiteral expected"; break; - case 4: s = "\"function\" expected"; break; - case 5: s = "\"{\" expected"; break; - case 6: s = "\"}\" expected"; break; - case 7: s = "\"while\" expected"; break; - case 8: s = "\"(\" expected"; break; - case 9: s = "\")\" expected"; break; - case 10: s = "\"=\" expected"; break; - case 11: s = "\";\" expected"; break; - case 12: s = "\"print\" expected"; break; - case 13: s = "\"return\" expected"; break; - case 14: s = "\"<\" expected"; break; - case 15: s = "\">\" expected"; break; - case 16: s = "\"<=\" expected"; break; - case 17: s = "\">=\" expected"; break; - case 18: s = "\"==\" expected"; break; - case 19: s = "\"!=\" expected"; break; - case 20: s = "\"+\" expected"; break; - case 21: s = "\"-\" expected"; break; - case 22: s = "\"*\" expected"; break; - case 23: s = "\"/\" expected"; break; - case 24: s = "\"#\" expected"; break; - case 25: s = "\"?\" expected"; break; - case 26: s = "\":\" expected"; break; - case 27: s = "\"time\" expected"; break; - case 28: s = "??? expected"; break; - case 29: s = "invalid Statement"; break; - case 30: s = "invalid Factor"; break; + switch (n) { + case 0: s = "EOF expected"; break; + case 1: s = "identifier expected"; break; + case 2: s = "stringLiteral expected"; break; + case 3: s = "numericLiteral expected"; break; + case 4: s = "\"function\" expected"; break; + case 5: s = "\"(\" expected"; break; + case 6: s = "\",\" expected"; break; + case 7: s = "\")\" expected"; break; + case 8: s = "\"{\" expected"; break; + case 9: s = "\"}\" expected"; break; + case 10: s = "\";\" expected"; break; + case 11: s = "\"if\" expected"; break; + case 12: s = "\"else\" expected"; break; + case 13: s = "\"while\" expected"; break; + case 14: s = "\"return\" expected"; break; + case 15: s = "\"<\" expected"; break; + case 16: s = "\">\" expected"; break; + case 17: s = "\"<=\" expected"; break; + case 18: s = "\">=\" expected"; break; + case 19: s = "\"==\" expected"; break; + case 20: s = "\"!=\" expected"; break; + case 21: s = "\"+\" expected"; break; + case 22: s = "\"-\" expected"; break; + case 23: s = "\"*\" expected"; break; + case 24: s = "\"/\" expected"; break; + case 25: s = "\"#\" expected"; break; + case 26: s = "\"?\" expected"; break; + case 27: s = "\":\" expected"; break; + case 28: s = "\"=\" expected"; break; + case 29: s = "??? expected"; break; + case 30: s = "invalid Statement"; break; + case 31: s = "invalid Factor"; break; default: s = "error " + n; break;