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 /*****************************************************************************/
66 /*****************************************************************************/
70 /* Maximum count of cases */
75 /*****************************************************************************/
76 /* Helper functions */
77 /*****************************************************************************/
81 static void CheckTok (token_t Tok, const char* Msg, int* PendingToken)
82 /* Helper function for Statement. Will check for Tok and print Msg if not
83 * found. If PendingToken is NULL, it will the skip the token, otherwise
84 * it will store one to PendingToken.
87 if (CurTok.Tok != Tok) {
89 } else if (PendingToken) {
98 static void CheckSemi (int* PendingToken)
99 /* Helper function for Statement. Will call CheckTok with the parameters
103 CheckTok (TOK_SEMI, "`;' expected", PendingToken);
108 static void SkipPending (int PendingToken)
109 /* Skip the pending token if we have one */
118 /*****************************************************************************/
120 /*****************************************************************************/
124 static int IfStatement (void)
125 /* Handle an 'if' statement */
133 /* Generate a jump label and parse the condition */
134 Label1 = GetLocalLabel ();
137 /* Parse the if body */
138 GotBreak = Statement (0);
140 /* Else clause present? */
141 if (CurTok.Tok != TOK_ELSE) {
143 g_defcodelabel (Label1);
145 /* Since there's no else clause, we're not sure, if the a break
146 * statement is really executed.
152 /* Generate a jump around the else branch */
153 unsigned Label2 = GetLocalLabel ();
159 /* Define the target for the first test */
160 g_defcodelabel (Label1);
162 /* Total break only if both branches had a break. */
163 GotBreak &= Statement (0);
165 /* Generate the label for the else clause */
166 g_defcodelabel (Label2);
175 static void DoStatement (void)
176 /* Handle the 'do' statement */
178 /* Get the loop control labels */
179 unsigned loop = GetLocalLabel ();
180 unsigned lab = GetLocalLabel ();
182 /* Skip the while token */
185 /* Add the loop to the loop stack */
186 AddLoop (oursp, loop, lab, 0, 0);
188 /* Define the head label */
189 g_defcodelabel (loop);
191 /* Parse the loop body */
194 /* Parse the end condition */
195 Consume (TOK_WHILE, "`while' expected");
199 /* Define the break label */
200 g_defcodelabel (lab);
202 /* Remove the loop from the loop stack */
208 static void WhileStatement (void)
209 /* Handle the 'while' statement */
213 /* Get the loop control labels */
214 unsigned loop = GetLocalLabel ();
215 unsigned lab = GetLocalLabel ();
217 /* Skip the while token */
220 /* Add the loop to the loop stack */
221 AddLoop (oursp, loop, lab, 0, 0);
223 /* Define the head label */
224 g_defcodelabel (loop);
226 /* Test the loop condition */
230 Statement (&PendingToken);
232 /* Jump back to loop top */
236 g_defcodelabel (lab);
238 /* Eat remaining tokens that were delayed because of line info
241 SkipPending (PendingToken);
243 /* Remove the loop from the loop stack */
249 static void ReturnStatement (void)
250 /* Handle the 'return' statement */
255 if (CurTok.Tok != TOK_SEMI) {
256 if (HasVoidReturn (CurrentFunc)) {
257 Error ("Returning a value in function with return type void");
260 /* Evaluate the return expression. Result will be in primary */
263 /* Convert the return value to the type of the function result */
264 if (!HasVoidReturn (CurrentFunc)) {
265 assignadjust (GetReturnType (CurrentFunc), &lval);
267 } else if (!HasVoidReturn (CurrentFunc)) {
268 Error ("Function `%s' must return a value", GetFuncName (CurrentFunc));
271 /* Cleanup the stack in case we're inside a block with locals */
272 g_space (oursp - GetTopLevelSP (CurrentFunc));
274 /* Output a jump to the function exit code */
275 g_jump (GetRetLab (CurrentFunc));
280 static void BreakStatement (void)
281 /* Handle the 'break' statement */
288 /* Get the current loop descriptor */
291 /* Check if we are inside a loop */
293 /* Error: No current loop */
294 Error ("`break' statement not within loop or switch");
298 /* Correct the stack pointer if needed */
299 g_space (oursp - L->StackPtr);
301 /* Jump to the exit label of the loop */
307 static void ContinueStatement (void)
308 /* Handle the 'continue' statement */
312 /* Skip the continue */
315 /* Get the current loop descriptor */
318 /* Search for the correct loop */
327 /* Did we find it? */
329 Error ("`continue' statement not within a loop");
333 /* Correct the stackpointer if needed */
334 g_space (oursp - L->StackPtr);
336 /* Output the loop code */
346 static void ForStatement (void)
347 /* Handle a 'for' statement */
353 CodeMark IncExprStart;
357 /* Get several local labels needed later */
358 unsigned TestLabel = GetLocalLabel ();
359 unsigned lab = GetLocalLabel ();
360 unsigned IncLabel = GetLocalLabel ();
361 unsigned lstat = GetLocalLabel ();
363 /* Skip the FOR token */
366 /* Add the loop to the loop stack */
367 AddLoop (oursp, TestLabel, lab, IncLabel, lstat);
369 /* Skip the opening paren */
372 /* Parse the initializer expression */
373 if (CurTok.Tok != TOK_SEMI) {
378 /* Label for the test expressions */
379 g_defcodelabel (TestLabel);
381 /* Parse the test expression */
382 if (CurTok.Tok != TOK_SEMI) {
384 g_truejump (CF_NONE, lstat);
391 /* Remember the start of the increment expression */
392 IncExprStart = GetCodePos();
394 /* Label for the increment expression */
395 g_defcodelabel (IncLabel);
397 /* Parse the increment expression */
398 HaveIncExpr = (CurTok.Tok != TOK_RPAREN);
403 /* Jump to the test */
406 /* Remember the end of the increment expression */
407 IncExprEnd = GetCodePos();
409 /* Skip the closing paren */
413 g_defcodelabel (lstat);
414 Statement (&PendingToken);
416 /* If we had an increment expression, move the code to the bottom of
417 * the loop. In this case we don't need to jump there at the end of
421 MoveCode (IncExprStart, IncExprEnd, GetCodePos());
423 /* Jump back to the increment expression */
427 /* Skip a pending token if we have one */
428 SkipPending (PendingToken);
430 /* Declare the break label */
431 g_defcodelabel (lab);
433 /* Remove the loop from the loop stack */
439 static int CompoundStatement (void)
440 /* Compound statement. Allow any number of statements inside braces. The
441 * function returns true if the last statement was a break or return.
446 /* Remember the stack at block entry */
447 int OldStack = oursp;
449 /* Enter a new lexical level */
452 /* Parse local variable declarations if any */
455 /* Now process statements in this block */
457 while (CurTok.Tok != TOK_RCURLY) {
458 if (CurTok.Tok != TOK_CEOF) {
459 GotBreak = Statement (0);
465 /* Clean up the stack. */
467 g_space (oursp - OldStack);
471 /* Emit references to imports/exports for this block */
474 /* Leave the lexical level */
482 int Statement (int* PendingToken)
483 /* Statement parser. Returns 1 if the statement does a return/break, returns
484 * 0 otherwise. If the PendingToken pointer is not NULL, the function will
485 * not skip the terminating token of the statement (closing brace or
486 * semicolon), but store true if there is a pending token, and false if there
487 * is none. The token is always checked, so there is no need for the caller to
488 * check this token, it must be skipped, however. If the argument pointer is
489 * NULL, the function will skip the token.
495 /* Assume no pending token */
500 /* Check for a label */
501 if (CurTok.Tok == TOK_IDENT && NextTok.Tok == TOK_COLON) {
503 /* Special handling for a label */
508 switch (CurTok.Tok) {
512 GotBreak = CompoundStatement ();
513 CheckTok (TOK_RCURLY, "`{' expected", PendingToken);
517 return IfStatement ();
533 CheckSemi (PendingToken);
538 CheckSemi (PendingToken);
542 ContinueStatement ();
543 CheckSemi (PendingToken);
552 CheckSemi (PendingToken);
565 /* Actual statement */
567 CheckSemi (PendingToken);