comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Parser.java @ 13836:64c77f0577bb

More documentation and improvements of Simple Language
author Christian Wimmer <christian.wimmer@oracle.com>
date Thu, 30 Jan 2014 17:53:27 -0800
parents b16ec83edc73
children 915ebb306fcc
comparison
equal deleted inserted replaced
13835:67e4e7f56911 13836:64c77f0577bb
143 Expect(1); 143 Expect(1);
144 factory.addFormalParameter(t); 144 factory.addFormalParameter(t);
145 } 145 }
146 } 146 }
147 Expect(7); 147 Expect(7);
148 SLStatementNode body = Block(); 148 SLStatementNode body = Block(false);
149 factory.finishFunction(body); 149 factory.finishFunction(body);
150 } 150 }
151 151
152 SLStatementNode Block() { 152 SLStatementNode Block(boolean inLoop) {
153 SLStatementNode result; 153 SLStatementNode result;
154 factory.startBlock(); 154 factory.startBlock();
155 List<SLStatementNode> body = new ArrayList<>(); 155 List<SLStatementNode> body = new ArrayList<>();
156 Expect(8); 156 Expect(8);
157 while (StartOf(1)) { 157 while (StartOf(1)) {
158 SLStatementNode s = Statement(); 158 SLStatementNode s = Statement(inLoop);
159 body.add(s); 159 body.add(s);
160 } 160 }
161 Expect(9); 161 Expect(9);
162 result = factory.finishBlock(body); 162 result = factory.finishBlock(body);
163 return result; 163 return result;
164 } 164 }
165 165
166 SLStatementNode Statement() { 166 SLStatementNode Statement(boolean inLoop) {
167 SLStatementNode result; 167 SLStatementNode result;
168 result = null; 168 result = null;
169 switch (la.kind) { 169 switch (la.kind) {
170 case 13: { 170 case 13: {
171 result = WhileStatement(); 171 result = WhileStatement();
172 break; 172 break;
173 } 173 }
174 case 10: { 174 case 10: {
175 Get(); 175 Get();
176 result = factory.createBreak(t); 176 if (inLoop) { result = factory.createBreak(t); } else { SemErr("break used outside of loop"); }
177 Expect(11); 177 Expect(11);
178 break; 178 break;
179 } 179 }
180 case 12: { 180 case 12: {
181 Get(); 181 Get();
182 result = factory.createContinue(t); 182 if (inLoop) { result = factory.createContinue(t); } else { SemErr("continue used outside of loop"); }
183 Expect(11); 183 Expect(11);
184 break; 184 break;
185 } 185 }
186 case 14: { 186 case 14: {
187 result = IfStatement(); 187 result = IfStatement(inLoop);
188 break; 188 break;
189 } 189 }
190 case 16: { 190 case 16: {
191 result = ReturnStatement(); 191 result = ReturnStatement();
192 break; 192 break;
206 Expect(13); 206 Expect(13);
207 Expect(5); 207 Expect(5);
208 Token whileToken = t; 208 Token whileToken = t;
209 SLExpressionNode condition = Expression(); 209 SLExpressionNode condition = Expression();
210 Expect(7); 210 Expect(7);
211 SLStatementNode body = Block(); 211 SLStatementNode body = Block(true);
212 result = factory.createWhile(whileToken, condition, body); 212 result = factory.createWhile(whileToken, condition, body);
213 return result; 213 return result;
214 } 214 }
215 215
216 SLStatementNode IfStatement() { 216 SLStatementNode IfStatement(boolean inLoop) {
217 SLStatementNode result; 217 SLStatementNode result;
218 Expect(14); 218 Expect(14);
219 Expect(5); 219 Expect(5);
220 Token ifToken = t; 220 Token ifToken = t;
221 SLExpressionNode condition = Expression(); 221 SLExpressionNode condition = Expression();
222 Expect(7); 222 Expect(7);
223 SLStatementNode thenPart = Block(); 223 SLStatementNode thenPart = Block(inLoop);
224 SLStatementNode elsePart = null; 224 SLStatementNode elsePart = null;
225 if (la.kind == 15) { 225 if (la.kind == 15) {
226 Get(); 226 Get();
227 elsePart = Block(); 227 elsePart = Block(inLoop);
228 } 228 }
229 result = factory.createIf(ifToken, condition, thenPart, elsePart); 229 result = factory.createIf(ifToken, condition, thenPart, elsePart);
230 return result; 230 return result;
231 } 231 }
232 232