1 /*****************************************************************************/
5 /* Parse a statement */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
65 /*****************************************************************************/
66 /* Helper functions */
67 /*****************************************************************************/
71 static void CheckTok (token_t Tok, const char* Msg, int* PendingToken)
72 /* Helper function for Statement. Will check for Tok and print Msg if not
73 * found. If PendingToken is NULL, it will the skip the token, otherwise
74 * it will store one to PendingToken.
77 if (CurTok.Tok != Tok) {
79 } else if (PendingToken) {
88 static void CheckSemi (int* PendingToken)
89 /* Helper function for Statement. Will check for a semicolon and print an
90 * error message if not found (plus some error recovery). If PendingToken is
91 * NULL, it will the skip the token, otherwise it will store one to
93 * This function is a special version of CheckTok with the addition of the
97 int HaveToken = (CurTok.Tok == TOK_SEMI);
99 Error ("`;' expected");
100 /* Try to be smart about errors */
101 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
116 static void SkipPending (int PendingToken)
117 /* Skip the pending token if we have one */
126 /*****************************************************************************/
128 /*****************************************************************************/
132 static int IfStatement (void)
133 /* Handle an 'if' statement */
141 /* Generate a jump label and parse the condition */
142 Label1 = GetLocalLabel ();
143 TestInParens (Label1, 0);
145 /* Parse the if body */
146 GotBreak = Statement (0);
148 /* Else clause present? */
149 if (CurTok.Tok != TOK_ELSE) {
151 g_defcodelabel (Label1);
153 /* Since there's no else clause, we're not sure, if the a break
154 * statement is really executed.
160 /* Generate a jump around the else branch */
161 unsigned Label2 = GetLocalLabel ();
167 /* Define the target for the first test */
168 g_defcodelabel (Label1);
170 /* Total break only if both branches had a break. */
171 GotBreak &= Statement (0);
173 /* Generate the label for the else clause */
174 g_defcodelabel (Label2);
183 static void DoStatement (void)
184 /* Handle the 'do' statement */
186 /* Get the loop control labels */
187 unsigned loop = GetLocalLabel ();
188 unsigned lab = GetLocalLabel ();
190 /* Skip the while token */
193 /* Add the loop to the loop stack */
194 AddLoop (oursp, loop, lab, 0, 0);
196 /* Define the head label */
197 g_defcodelabel (loop);
199 /* Parse the loop body */
202 /* Parse the end condition */
203 Consume (TOK_WHILE, "`while' expected");
204 TestInParens (loop, 1);
207 /* Define the break label */
208 g_defcodelabel (lab);
210 /* Remove the loop from the loop stack */
216 static void WhileStatement (void)
217 /* Handle the 'while' statement */
221 /* Get the loop control labels */
222 unsigned loop = GetLocalLabel ();
223 unsigned lab = GetLocalLabel ();
225 /* Skip the while token */
228 /* Add the loop to the loop stack */
229 AddLoop (oursp, loop, lab, 0, 0);
231 /* Define the head label */
232 g_defcodelabel (loop);
234 /* Test the loop condition */
235 TestInParens (lab, 0);
238 Statement (&PendingToken);
240 /* Jump back to loop top */
244 g_defcodelabel (lab);
246 /* Eat remaining tokens that were delayed because of line info
249 SkipPending (PendingToken);
251 /* Remove the loop from the loop stack */
257 static void ReturnStatement (void)
258 /* Handle the 'return' statement */
264 if (CurTok.Tok != TOK_SEMI) {
266 /* Check if the function has a return value declared */
267 if (F_HasVoidReturn (CurrentFunc)) {
268 Error ("Returning a value in function with return type void");
271 /* Evaluate the return expression */
272 k = hie0 (InitExprDesc (&Expr));
274 /* Ignore the return expression if the function returns void */
275 if (!F_HasVoidReturn (CurrentFunc)) {
277 /* Convert the return value to the type of the function result */
278 k = TypeConversion (&Expr, k, F_GetReturnType (CurrentFunc));
280 /* Load the value into the primary */
281 ExprLoad (CF_NONE, k, &Expr);
284 } else if (!F_HasVoidReturn (CurrentFunc) && !F_HasOldStyleIntRet (CurrentFunc)) {
285 Error ("Function `%s' must return a value", F_GetFuncName (CurrentFunc));
288 /* Cleanup the stack in case we're inside a block with locals */
289 g_space (oursp - F_GetTopLevelSP (CurrentFunc));
291 /* Output a jump to the function exit code */
292 g_jump (F_GetRetLab (CurrentFunc));
297 static void BreakStatement (void)
298 /* Handle the 'break' statement */
305 /* Get the current loop descriptor */
308 /* Check if we are inside a loop */
310 /* Error: No current loop */
311 Error ("`break' statement not within loop or switch");
315 /* Correct the stack pointer if needed */
316 g_space (oursp - L->StackPtr);
318 /* Jump to the exit label of the loop */
324 static void ContinueStatement (void)
325 /* Handle the 'continue' statement */
329 /* Skip the continue */
332 /* Get the current loop descriptor */
335 /* Search for the correct loop */
344 /* Did we find it? */
346 Error ("`continue' statement not within a loop");
350 /* Correct the stackpointer if needed */
351 g_space (oursp - L->StackPtr);
353 /* Output the loop code */
363 static void ForStatement (void)
364 /* Handle a 'for' statement */
369 CodeMark IncExprStart;
373 /* Get several local labels needed later */
374 unsigned TestLabel = GetLocalLabel ();
375 unsigned lab = GetLocalLabel ();
376 unsigned IncLabel = GetLocalLabel ();
377 unsigned lstat = GetLocalLabel ();
379 /* Skip the FOR token */
382 /* Add the loop to the loop stack */
383 AddLoop (oursp, TestLabel, lab, IncLabel, lstat);
385 /* Skip the opening paren */
388 /* Parse the initializer expression */
389 if (CurTok.Tok != TOK_SEMI) {
394 /* Label for the test expressions */
395 g_defcodelabel (TestLabel);
397 /* Parse the test expression */
398 if (CurTok.Tok != TOK_SEMI) {
406 /* Remember the start of the increment expression */
407 IncExprStart = GetCodePos();
409 /* Label for the increment expression */
410 g_defcodelabel (IncLabel);
412 /* Parse the increment expression */
413 HaveIncExpr = (CurTok.Tok != TOK_RPAREN);
418 /* Jump to the test */
421 /* Remember the end of the increment expression */
422 IncExprEnd = GetCodePos();
424 /* Skip the closing paren */
428 g_defcodelabel (lstat);
429 Statement (&PendingToken);
431 /* If we had an increment expression, move the code to the bottom of
432 * the loop. In this case we don't need to jump there at the end of
436 MoveCode (IncExprStart, IncExprEnd, GetCodePos());
438 /* Jump back to the increment expression */
442 /* Skip a pending token if we have one */
443 SkipPending (PendingToken);
445 /* Declare the break label */
446 g_defcodelabel (lab);
448 /* Remove the loop from the loop stack */
454 static int CompoundStatement (void)
455 /* Compound statement. Allow any number of statements inside braces. The
456 * function returns true if the last statement was a break or return.
461 /* Remember the stack at block entry */
462 int OldStack = oursp;
464 /* Enter a new lexical level */
467 /* Parse local variable declarations if any */
470 /* Now process statements in this block */
472 while (CurTok.Tok != TOK_RCURLY) {
473 if (CurTok.Tok != TOK_CEOF) {
474 GotBreak = Statement (0);
480 /* Clean up the stack. */
482 g_space (oursp - OldStack);
486 /* Emit references to imports/exports for this block */
489 /* Leave the lexical level */
497 int Statement (int* PendingToken)
498 /* Statement parser. Returns 1 if the statement does a return/break, returns
499 * 0 otherwise. If the PendingToken pointer is not NULL, the function will
500 * not skip the terminating token of the statement (closing brace or
501 * semicolon), but store true if there is a pending token, and false if there
502 * is none. The token is always checked, so there is no need for the caller to
503 * check this token, it must be skipped, however. If the argument pointer is
504 * NULL, the function will skip the token.
510 /* Assume no pending token */
515 /* Check for a label */
516 if (CurTok.Tok == TOK_IDENT && NextTok.Tok == TOK_COLON) {
518 /* Special handling for a label */
523 switch (CurTok.Tok) {
527 GotBreak = CompoundStatement ();
528 CheckTok (TOK_RCURLY, "`{' expected", PendingToken);
532 return IfStatement ();
548 CheckSemi (PendingToken);
553 CheckSemi (PendingToken);
557 ContinueStatement ();
558 CheckSemi (PendingToken);
567 CheckSemi (PendingToken);
572 CheckSemi (PendingToken);
580 /* Actual statement */
582 CheckSemi (PendingToken);