X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcc65%2Fstmt.c;h=77b39859d1775972f743138e40e48946c9c9f4b1;hb=b3496bb343a2c93284a8669da4b52cf45b3db3dd;hp=ab091be3af230a0782f0dee9d566d6d189366e9c;hpb=89f00263af86905d08d8f0c61e105521cbe190dc;p=cc65 diff --git a/src/cc65/stmt.c b/src/cc65/stmt.c index ab091be3a..77b39859d 100644 --- a/src/cc65/stmt.c +++ b/src/cc65/stmt.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 1998-2001 Ullrich von Bassewitz */ +/* (C) 1998-2003 Ullrich von Bassewitz */ /* Wacholderweg 14 */ /* D-70597 Stuttgart */ /* EMail: uz@cc65.org */ @@ -58,6 +58,7 @@ #include "swstmt.h" #include "symtab.h" #include "stmt.h" +#include "typeconv.h" @@ -74,22 +75,40 @@ static void CheckTok (token_t Tok, const char* Msg, int* PendingToken) */ { if (CurTok.Tok != Tok) { - Error (Msg); + Error (Msg); } else if (PendingToken) { - *PendingToken = 1; + *PendingToken = 1; } else { - NextToken (); + NextToken (); } } static void CheckSemi (int* PendingToken) -/* Helper function for Statement. Will call CheckTok with the parameters - * for a semicolon. +/* Helper function for Statement. Will check for a semicolon and print an + * error message if not found (plus some error recovery). If PendingToken is + * NULL, it will the skip the token, otherwise it will store one to + * PendingToken. + * This function is a special version of CheckTok with the addition of the + * error recovery. */ { - CheckTok (TOK_SEMI, "`;' expected", PendingToken); + int HaveToken = (CurTok.Tok == TOK_SEMI); + if (!HaveToken) { + Error ("`;' expected"); + /* Try to be smart about errors */ + if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) { + HaveToken = 1; + } + } + if (HaveToken) { + if (PendingToken) { + *PendingToken = 1; + } else { + NextToken (); + } + } } @@ -238,7 +257,8 @@ static void WhileStatement (void) static void ReturnStatement (void) /* Handle the 'return' statement */ { - ExprDesc lval; + ExprDesc Expr; + int k; NextToken (); if (CurTok.Tok != TOK_SEMI) { @@ -248,12 +268,17 @@ static void ReturnStatement (void) Error ("Returning a value in function with return type void"); } - /* Evaluate the return expression. Result will be in primary */ - expression (&lval); + /* Evaluate the return expression */ + k = hie0 (InitExprDesc (&Expr)); - /* Convert the return value to the type of the function result */ + /* Ignore the return expression if the function returns void */ if (!F_HasVoidReturn (CurrentFunc)) { - assignadjust (F_GetReturnType (CurrentFunc), &lval); + + /* Convert the return value to the type of the function result */ + k = TypeConversion (&Expr, k, F_GetReturnType (CurrentFunc)); + + /* Load the value into the primary */ + ExprLoad (CF_NONE, k, &Expr); } } else if (!F_HasVoidReturn (CurrentFunc) && !F_HasOldStyleIntRet (CurrentFunc)) {