Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SimpleLanguage.atg @ 13821:b16ec83edc73
Documentation and more refactoring of Simple Language
author | Christian Wimmer <christian.wimmer@oracle.com> |
---|---|
date | Wed, 29 Jan 2014 20:45:43 -0800 |
parents | 7c418666c6c9 |
children | 64c77f0577bb |
comparison
equal
deleted
inserted
replaced
13820:20e7727588e8 | 13821:b16ec83edc73 |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 | |
1 COMPILER SimpleLanguage | 24 COMPILER SimpleLanguage |
2 | 25 |
3 CHARACTERS | 26 CHARACTERS |
4 | 27 |
5 letter = 'A'..'Z' + 'a'..'z'. | 28 letter = 'A'..'Z' + 'a'..'z'. |
35 | 58 |
36 | 59 |
37 Function | 60 Function |
38 = | 61 = |
39 "function" | 62 "function" |
40 identifier (. String name = t.val; .) | 63 identifier (. factory.startFunction(t); .) |
41 "(" (. List<String> parameters = new ArrayList<>(); .) | 64 "(" |
42 [ | 65 [ |
43 identifier (. parameters.add(t.val); .) | 66 identifier (. factory.addFormalParameter(t); .) |
44 { | 67 { |
45 "," | 68 "," |
46 identifier (. parameters.add(t.val); .) | 69 identifier (. factory.addFormalParameter(t); .) |
47 } | 70 } |
48 ] | 71 ] |
49 ")" (. factory.startFunction(name, parameters); .) | 72 ")" |
50 Block<out SLStatementNode body> (. factory.finishFunction(body); .) | 73 Block<out SLStatementNode body> (. factory.finishFunction(body); .) |
51 . | 74 . |
52 | 75 |
53 | 76 |
54 | 77 |
55 Block<out SLStatementNode result> | 78 Block<out SLStatementNode result> |
56 = (. factory.startBlock(); | 79 = (. factory.startBlock(); |
57 List<SLStatementNode> statements = new ArrayList<>(); .) | 80 List<SLStatementNode> body = new ArrayList<>(); .) |
58 "{" | 81 "{" |
59 { | 82 { |
60 Statement<out SLStatementNode statement> (. statements.add(statement); .) | 83 Statement<out SLStatementNode s> (. body.add(s); .) |
61 } | 84 } |
62 "}" (. result = factory.finishBlock(statements); .) | 85 "}" (. result = factory.finishBlock(body); .) |
63 . | 86 . |
64 | 87 |
65 | 88 |
66 Statement<out SLStatementNode result> | 89 Statement<out SLStatementNode result> |
67 = (. result = null; .) | 90 = (. result = null; .) |
68 ( | 91 ( |
69 WhileStatement<out result> | 92 WhileStatement<out result> |
70 | | 93 | |
71 "break" (. result = factory.createBreak(); .) | 94 "break" (. result = factory.createBreak(t); .) |
72 ";" | 95 ";" |
73 | | 96 | |
74 "continue" (. result = factory.createContinue(); .) | 97 "continue" (. result = factory.createContinue(t); .) |
75 ";" | 98 ";" |
76 | | 99 | |
77 IfStatement<out result> | 100 IfStatement<out result> |
78 | | 101 | |
79 ReturnStatement<out result> | 102 ReturnStatement<out result> |
81 Expression<out result> ";" | 104 Expression<out result> ";" |
82 ) | 105 ) |
83 . | 106 . |
84 | 107 |
85 | 108 |
109 WhileStatement<out SLStatementNode result> | |
110 = | |
111 "while" | |
112 "(" (. Token whileToken = t; .) | |
113 Expression<out SLExpressionNode condition> | |
114 ")" | |
115 Block<out SLStatementNode body> (. result = factory.createWhile(whileToken, condition, body); .) | |
116 . | |
117 | |
118 | |
86 IfStatement<out SLStatementNode result> | 119 IfStatement<out SLStatementNode result> |
87 = | 120 = |
88 "if" | 121 "if" |
89 "(" | 122 "(" (. Token ifToken = t; .) |
90 Expression<out SLExpressionNode condition> | 123 Expression<out SLExpressionNode condition> |
91 ")" | 124 ")" |
92 Block<out SLStatementNode thenPart> (. SLStatementNode elsePart = null; .) | 125 Block<out SLStatementNode thenPart> (. SLStatementNode elsePart = null; .) |
93 [ | 126 [ |
94 "else" | 127 "else" |
95 Block<out elsePart> | 128 Block<out elsePart> |
96 ] (. result = factory.createIf(condition, thenPart, elsePart); .) | 129 ] (. result = factory.createIf(ifToken, condition, thenPart, elsePart); .) |
97 . | |
98 | |
99 | |
100 WhileStatement<out SLStatementNode result> | |
101 = | |
102 "while" | |
103 "(" | |
104 Expression<out SLExpressionNode condition> | |
105 ")" | |
106 Block<out SLStatementNode body> (. result = factory.createWhile(condition, body); .) | |
107 . | 130 . |
108 | 131 |
109 | 132 |
110 ReturnStatement<out SLStatementNode result> | 133 ReturnStatement<out SLStatementNode result> |
111 = | 134 = |
112 "return" | 135 "return" (. Token returnToken = t; |
113 Expression<out SLExpressionNode value> ";" (. result = factory.createReturn(value); .) | 136 SLExpressionNode value = null; .) |
137 [ | |
138 Expression<out value> | |
139 ] (. result = factory.createReturn(returnToken, value); .) | |
140 ";" | |
114 . | 141 . |
115 | 142 |
116 | 143 |
117 Expression<out SLExpressionNode result> | 144 Expression<out SLExpressionNode result> |
118 = | 145 = |
119 LogicTerm<out result> | 146 LogicTerm<out result> |
120 { | 147 { |
121 "||" (. String op = t.val; .) | 148 "||" (. Token op = t; .) |
122 LogicTerm<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) | 149 LogicTerm<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) |
123 } | 150 } |
124 . | 151 . |
125 | 152 |
126 | 153 |
127 LogicTerm<out SLExpressionNode result> | 154 LogicTerm<out SLExpressionNode result> |
128 = | 155 = |
129 LogicFactor<out result> | 156 LogicFactor<out result> |
130 { | 157 { |
131 "&&" (. String op = t.val; .) | 158 "&&" (. Token op = t; .) |
132 LogicFactor<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) | 159 LogicFactor<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) |
133 } | 160 } |
134 . | 161 . |
135 | 162 |
136 | 163 |
137 LogicFactor<out SLExpressionNode result> | 164 LogicFactor<out SLExpressionNode result> |
138 = | 165 = |
139 Arithmetic<out result> | 166 Arithmetic<out result> |
140 [ | 167 [ |
141 ("<" | "<=" | "==" | "!=" ) (. String op = t.val; .) | 168 ("<" | "<=" | ">" | ">=" | "==" | "!=" ) (. Token op = t; .) |
142 Arithmetic<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) | 169 Arithmetic<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) |
143 ] | 170 ] |
144 . | 171 . |
145 | 172 |
146 | 173 |
147 Arithmetic<out SLExpressionNode result> | 174 Arithmetic<out SLExpressionNode result> |
148 = | 175 = |
149 Term<out result> | 176 Term<out result> |
150 { | 177 { |
151 ("+" | "-") (. String op = t.val; .) | 178 ("+" | "-") (. Token op = t; .) |
152 Term<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) | 179 Term<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) |
153 } | 180 } |
154 . | 181 . |
155 | 182 |
156 | 183 |
157 Term<out SLExpressionNode result> | 184 Term<out SLExpressionNode result> |
158 = | 185 = |
159 Factor<out result> | 186 Factor<out result> |
160 { | 187 { |
161 ("*" | "/") (. String op = t.val; .) | 188 ("*" | "/") (. Token op = t; .) |
162 Factor<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) | 189 Factor<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) |
163 } | 190 } |
164 . | 191 . |
165 | 192 |
166 | 193 |
167 Factor<out SLExpressionNode result> | 194 Factor<out SLExpressionNode result> |
168 = (. result = null; .) | 195 = (. result = null; .) |
169 ( | 196 ( |
170 identifier (. String name = t.val; .) | 197 identifier (. Token nameToken = t; .) |
171 ( | 198 ( |
172 "(" (. List<SLExpressionNode> parameters = new ArrayList<>(); | 199 "(" (. List<SLExpressionNode> parameters = new ArrayList<>(); |
173 SLExpressionNode parameter; .) | 200 SLExpressionNode parameter; .) |
174 [ | 201 [ |
175 Expression<out parameter> (. parameters.add(parameter); .) | 202 Expression<out parameter> (. parameters.add(parameter); .) |
176 { | 203 { |
177 "," | 204 "," |
178 Expression<out parameter> (. parameters.add(parameter); .) | 205 Expression<out parameter> (. parameters.add(parameter); .) |
179 } | 206 } |
180 ] (. result = factory.createCall(factory.createRead(name), parameters); .) | 207 ] (. result = factory.createCall(nameToken, parameters); .) |
181 ")" | 208 ")" |
182 | | 209 | |
183 "=" | 210 "=" |
184 Expression<out SLExpressionNode value> (. result = factory.createAssignment(name, value); .) | 211 Expression<out SLExpressionNode value> (. result = factory.createAssignment(nameToken, value); .) |
185 | | 212 | |
186 (. result = factory.createRead(name); .) | 213 (. result = factory.createRead(nameToken); .) |
187 ) | 214 ) |
188 | | 215 | |
189 stringLiteral (. result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1)); .) | 216 stringLiteral (. result = factory.createStringLiteral(t); .) |
190 | | 217 | |
191 numericLiteral (. result = factory.createNumericLiteral(t.val); .) | 218 numericLiteral (. result = factory.createNumericLiteral(t); .) |
192 | | 219 | |
193 "(" Expression<out result> ")" | 220 "(" Expression<out result> ")" |
194 ) | 221 ) |
195 . | 222 . |
196 | 223 |