Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SimpleLanguage.atg @ 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 | b6a87711eca0 |
children | 7c418666c6c9 |
comparison
equal
deleted
inserted
replaced
12712:882a0aadfed6 | 12752:71991b7a0f14 |
---|---|
33 . | 33 . |
34 | 34 |
35 Function | 35 Function |
36 = | 36 = |
37 "function" (. factory.startFunction(); .) | 37 "function" (. factory.startFunction(); .) |
38 identifier (. String name = t.val; .) | 38 identifier (. String name = t.val; |
39 Block<out StatementNode body> (. factory.createFunction(body, name); .) | 39 List<String> parameterNames = new ArrayList<>(); .) |
40 ["(" [identifier (. parameterNames.add(t.val); .) | |
41 ] {"," identifier (. parameterNames.add(t.val); .) | |
42 } ")"] | |
43 Block<out StatementNode body> (. factory.createFunction(body, name, parameterNames.toArray(new String[parameterNames.size()])); .) | |
40 . | 44 . |
41 | 45 |
42 Block<out StatementNode result> | 46 Block<out StatementNode result> |
43 = (. List<StatementNode> statements = new ArrayList<>(); .) | 47 = (. List<StatementNode> statements = new ArrayList<>(); .) |
44 "{" | 48 "{" |
50 | 54 |
51 Statement<out StatementNode result> | 55 Statement<out StatementNode result> |
52 = (. result = null; .) | 56 = (. result = null; .) |
53 ( | 57 ( |
54 WhileStatement<out result> | 58 WhileStatement<out result> |
55 | | 59 | |
56 AssignmentStatement<out result> | 60 IfStatement<out result> |
57 | | |
58 OutputStatement<out result> | |
59 | | 61 | |
60 ReturnStatement<out result> | 62 ReturnStatement<out result> |
63 | | |
64 Expression<out result> ";" | |
61 ) | 65 ) |
66 . | |
67 | |
68 IfStatement<out StatementNode result> | |
69 = | |
70 "if" "(" Expression<out ConditionNode condition> ")" (. StatementNode thenNode = null; StatementNode elseNode = null; .) | |
71 Block<out thenNode> | |
72 ["else" Block<out elseNode>] (. result = factory.createIf(condition, thenNode, elseNode); .) | |
62 . | 73 . |
63 | 74 |
64 WhileStatement<out StatementNode result> | 75 WhileStatement<out StatementNode result> |
65 = | 76 = |
66 "while" | 77 "while" |
68 Expression<out ConditionNode condition> | 79 Expression<out ConditionNode condition> |
69 ")" | 80 ")" |
70 Block<out StatementNode body> (. result = factory.createWhile(condition, body); .) | 81 Block<out StatementNode body> (. result = factory.createWhile(condition, body); .) |
71 . | 82 . |
72 | 83 |
73 AssignmentStatement<out StatementNode result> | |
74 = | |
75 identifier (. String name = t.val; .) | |
76 "=" | |
77 Expression<out TypedNode rvalue> | |
78 ";" (. result = factory.createAssignment(name, rvalue); .) | |
79 . | |
80 | |
81 OutputStatement<out StatementNode result> | |
82 = (. List<TypedNode> expressions = new ArrayList<>(); .) | |
83 "print" | |
84 { | |
85 Expression<out TypedNode value> (. expressions.add(value); .) | |
86 } | |
87 ";" (. result = factory.createPrint(expressions); .) | |
88 . | |
89 | 84 |
90 ReturnStatement<out StatementNode result> | 85 ReturnStatement<out StatementNode result> |
91 = | 86 = |
92 "return" | 87 "return" |
93 Expression<out TypedNode value> ";" (. result = factory.createReturn(value); .) | 88 Expression<out TypedNode value> ";" (. result = factory.createReturn(value); .) |
121 . | 116 . |
122 | 117 |
123 Factor<out TypedNode result> | 118 Factor<out TypedNode result> |
124 = (. result = null; .) | 119 = (. result = null; .) |
125 ( | 120 ( |
126 TimeRef<out result> | 121 VariableRefOrCall<out result> |
127 | | |
128 VariableRef<out result> | |
129 | | 122 | |
130 StringLiteral<out result> | 123 StringLiteral<out result> |
131 | | 124 | |
132 NumericLiteral<out result> | 125 NumericLiteral<out result> |
133 | | 126 | |
134 Ternary<out result> | 127 Ternary<out result> |
135 | | 128 | |
136 "(" Expression<out result> ")" | 129 "(" Expression<out result> ")" |
137 ) | 130 ) |
138 . | 131 . |
139 | 132 |
141 = | 134 = |
142 "#" Expression<out condition> "?" Expression<out thenPart> ":" Expression<out elsePart> | 135 "#" Expression<out condition> "?" Expression<out thenPart> ":" Expression<out elsePart> |
143 (. result = factory.createTernary(condition, thenPart, elsePart); .) | 136 (. result = factory.createTernary(condition, thenPart, elsePart); .) |
144 . | 137 . |
145 | 138 |
146 TimeRef<out TypedNode result> | 139 VariableRefOrCall<out TypedNode result> |
147 = | 140 = |
148 "time" (. result = factory.createTime(); .) | 141 VariableRef<out result> |
142 [ | |
143 (Parameters<out TypedNode[] parameters>) (. result = factory.createCall(result, parameters); .) | |
144 | ("=" Expression<out TypedNode assignment>) (. result = factory.createAssignment(result, assignment); .) | |
145 ] | |
146 . | |
147 | |
148 Parameters<out TypedNode[] result> | |
149 = | |
150 "(" (. List<TypedNode> parameters = new ArrayList<>(); .) | |
151 [Expression<out TypedNode e1> (. parameters.add(e1); .) | |
152 {"," Expression<out TypedNode e2> (. parameters.add(e2); .) | |
153 } | |
154 ] (. result = parameters.toArray(new TypedNode[parameters.size()]); .) | |
155 ")" | |
149 . | 156 . |
150 | 157 |
151 VariableRef<out TypedNode result> | 158 VariableRef<out TypedNode result> |
152 = | 159 = |
153 identifier (. result = factory.createLocal(t.val); .) | 160 identifier (. result = factory.createLocal(t.val); .) |