1 /*****************************************************************************/
5 /* Parse a statement */
9 /* (C) 1998-2001 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 /*****************************************************************************/
64 /*****************************************************************************/
65 /* Helper functions */
66 /*****************************************************************************/
70 static void CheckTok (token_t Tok, const char* Msg, int* PendingToken)
71 /* Helper function for Statement. Will check for Tok and print Msg if not
72 * found. If PendingToken is NULL, it will the skip the token, otherwise
73 * it will store one to PendingToken.
76 if (CurTok.Tok != Tok) {
78 } else if (PendingToken) {
87 static void CheckSemi (int* PendingToken)
88 /* Helper function for Statement. Will check for a semicolon and print an
89 * error message if not found (plus some error recovery). If PendingToken is
90 * NULL, it will the skip the token, otherwise it will store one to
92 * This function is a special version of CheckTok with the addition of the
96 int HaveToken = (CurTok.Tok == TOK_SEMI);
98 Error ("`;' expected");
99 /* Try to be smart about errors */
100 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
115 static void SkipPending (int PendingToken)
116 /* Skip the pending token if we have one */
125 /*****************************************************************************/
127 /*****************************************************************************/
131 static int IfStatement (void)
132 /* Handle an 'if' statement */
140 /* Generate a jump label and parse the condition */
141 Label1 = GetLocalLabel ();
142 TestInParens (Label1, 0);
144 /* Parse the if body */
145 GotBreak = Statement (0);
147 /* Else clause present? */
148 if (CurTok.Tok != TOK_ELSE) {
150 g_defcodelabel (Label1);
152 /* Since there's no else clause, we're not sure, if the a break
153 * statement is really executed.
159 /* Generate a jump around the else branch */
160 unsigned Label2 = GetLocalLabel ();
166 /* Define the target for the first test */
167 g_defcodelabel (Label1);
169 /* Total break only if both branches had a break. */
170 GotBreak &= Statement (0);
172 /* Generate the label for the else clause */
173 g_defcodelabel (Label2);
182 static void DoStatement (void)
183 /* Handle the 'do' statement */
185 /* Get the loop control labels */
186 unsigned loop = GetLocalLabel ();
187 unsigned lab = GetLocalLabel ();
189 /* Skip the while token */
192 /* Add the loop to the loop stack */
193 AddLoop (oursp, loop, lab, 0, 0);
195 /* Define the head label */
196 g_defcodelabel (loop);
198 /* Parse the loop body */
201 /* Parse the end condition */
202 Consume (TOK_WHILE, "`while' expected");
203 TestInParens (loop, 1);
206 /* Define the break label */
207 g_defcodelabel (lab);
209 /* Remove the loop from the loop stack */
215 static void WhileStatement (void)
216 /* Handle the 'while' statement */
220 /* Get the loop control labels */
221 unsigned loop = GetLocalLabel ();
222 unsigned lab = GetLocalLabel ();
224 /* Skip the while token */
227 /* Add the loop to the loop stack */
228 AddLoop (oursp, loop, lab, 0, 0);
230 /* Define the head label */
231 g_defcodelabel (loop);
233 /* Test the loop condition */
234 TestInParens (lab, 0);
237 Statement (&PendingToken);
239 /* Jump back to loop top */
243 g_defcodelabel (lab);
245 /* Eat remaining tokens that were delayed because of line info
248 SkipPending (PendingToken);
250 /* Remove the loop from the loop stack */
256 static void ReturnStatement (void)
257 /* Handle the 'return' statement */
262 if (CurTok.Tok != TOK_SEMI) {
264 /* Check if the function has a return value declared */
265 if (F_HasVoidReturn (CurrentFunc)) {
266 Error ("Returning a value in function with return type void");
269 /* Evaluate the return expression. Result will be in primary */
272 /* Convert the return value to the type of the function result */
273 if (!F_HasVoidReturn (CurrentFunc)) {
274 assignadjust (F_GetReturnType (CurrentFunc), &lval);
277 } else if (!F_HasVoidReturn (CurrentFunc) && !F_HasOldStyleIntRet (CurrentFunc)) {
278 Error ("Function `%s' must return a value", F_GetFuncName (CurrentFunc));
281 /* Cleanup the stack in case we're inside a block with locals */
282 g_space (oursp - F_GetTopLevelSP (CurrentFunc));
284 /* Output a jump to the function exit code */
285 g_jump (F_GetRetLab (CurrentFunc));
290 static void BreakStatement (void)
291 /* Handle the 'break' statement */
298 /* Get the current loop descriptor */
301 /* Check if we are inside a loop */
303 /* Error: No current loop */
304 Error ("`break' statement not within loop or switch");
308 /* Correct the stack pointer if needed */
309 g_space (oursp - L->StackPtr);
311 /* Jump to the exit label of the loop */
317 static void ContinueStatement (void)
318 /* Handle the 'continue' statement */
322 /* Skip the continue */
325 /* Get the current loop descriptor */
328 /* Search for the correct loop */
337 /* Did we find it? */
339 Error ("`continue' statement not within a loop");
343 /* Correct the stackpointer if needed */
344 g_space (oursp - L->StackPtr);
346 /* Output the loop code */
356 static void ForStatement (void)
357 /* Handle a 'for' statement */
362 CodeMark IncExprStart;
366 /* Get several local labels needed later */
367 unsigned TestLabel = GetLocalLabel ();
368 unsigned lab = GetLocalLabel ();
369 unsigned IncLabel = GetLocalLabel ();
370 unsigned lstat = GetLocalLabel ();
372 /* Skip the FOR token */
375 /* Add the loop to the loop stack */
376 AddLoop (oursp, TestLabel, lab, IncLabel, lstat);
378 /* Skip the opening paren */
381 /* Parse the initializer expression */
382 if (CurTok.Tok != TOK_SEMI) {
387 /* Label for the test expressions */
388 g_defcodelabel (TestLabel);
390 /* Parse the test expression */
391 if (CurTok.Tok != TOK_SEMI) {
399 /* Remember the start of the increment expression */
400 IncExprStart = GetCodePos();
402 /* Label for the increment expression */
403 g_defcodelabel (IncLabel);
405 /* Parse the increment expression */
406 HaveIncExpr = (CurTok.Tok != TOK_RPAREN);
411 /* Jump to the test */
414 /* Remember the end of the increment expression */
415 IncExprEnd = GetCodePos();
417 /* Skip the closing paren */
421 g_defcodelabel (lstat);
422 Statement (&PendingToken);
424 /* If we had an increment expression, move the code to the bottom of
425 * the loop. In this case we don't need to jump there at the end of
429 MoveCode (IncExprStart, IncExprEnd, GetCodePos());
431 /* Jump back to the increment expression */
435 /* Skip a pending token if we have one */
436 SkipPending (PendingToken);
438 /* Declare the break label */
439 g_defcodelabel (lab);
441 /* Remove the loop from the loop stack */
447 static int CompoundStatement (void)
448 /* Compound statement. Allow any number of statements inside braces. The
449 * function returns true if the last statement was a break or return.
454 /* Remember the stack at block entry */
455 int OldStack = oursp;
457 /* Enter a new lexical level */
460 /* Parse local variable declarations if any */
463 /* Now process statements in this block */
465 while (CurTok.Tok != TOK_RCURLY) {
466 if (CurTok.Tok != TOK_CEOF) {
467 GotBreak = Statement (0);
473 /* Clean up the stack. */
475 g_space (oursp - OldStack);
479 /* Emit references to imports/exports for this block */
482 /* Leave the lexical level */
490 int Statement (int* PendingToken)
491 /* Statement parser. Returns 1 if the statement does a return/break, returns
492 * 0 otherwise. If the PendingToken pointer is not NULL, the function will
493 * not skip the terminating token of the statement (closing brace or
494 * semicolon), but store true if there is a pending token, and false if there
495 * is none. The token is always checked, so there is no need for the caller to
496 * check this token, it must be skipped, however. If the argument pointer is
497 * NULL, the function will skip the token.
503 /* Assume no pending token */
508 /* Check for a label */
509 if (CurTok.Tok == TOK_IDENT && NextTok.Tok == TOK_COLON) {
511 /* Special handling for a label */
516 switch (CurTok.Tok) {
520 GotBreak = CompoundStatement ();
521 CheckTok (TOK_RCURLY, "`{' expected", PendingToken);
525 return IfStatement ();
541 CheckSemi (PendingToken);
546 CheckSemi (PendingToken);
550 ContinueStatement ();
551 CheckSemi (PendingToken);
560 CheckSemi (PendingToken);
565 CheckSemi (PendingToken);
573 /* Actual statement */
575 CheckSemi (PendingToken);