From 4b68d19993c41da4f77342e8bc4dea74a9d4f80c Mon Sep 17 00:00:00 2001 From: Kugel Fuhr <98353208+kugelfuhr@users.noreply.github.com> Date: Sun, 1 Sep 2024 10:42:18 +0200 Subject: [PATCH] Fix issue #1663. --- src/ca65/condasm.c | 72 +++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/src/ca65/condasm.c b/src/ca65/condasm.c index 6198f4017..f872ec9ed 100644 --- a/src/ca65/condasm.c +++ b/src/ca65/condasm.c @@ -133,24 +133,26 @@ static void SetIfCond (IfDesc* ID, int C) -static void ElseClause (IfDesc* ID, const char* Directive) -/* Enter an .ELSE clause */ +static int ElseClause (IfDesc* ID, const char* Directive) +/* Enter an .ELSE clause. Return true if this was ok, zero on errors. */ { /* Check if we have an open .IF - otherwise .ELSE is not allowed */ if (ID == 0) { Error ("Unexpected %s", Directive); - return; + return 0; } /* Check for a duplicate else, then remember that we had one */ if (ID->Flags & ifElse) { /* We already had a .ELSE ! */ Error ("Duplicate .ELSE"); + return 0; } ID->Flags |= ifElse; /* Condition is inverted now */ ID->Flags ^= ifCond; + return 1; } @@ -226,46 +228,52 @@ void DoConditionals (void) D = GetCurrentIf (); /* Allow an .ELSE */ - ElseClause (D, ".ELSE"); + if (ElseClause (D, ".ELSE")) { + /* Remember the data for the .ELSE */ + if (D) { + ReleaseFullLineInfo (&D->LineInfos); + GetFullLineInfo (&D->LineInfos); + D->Name = ".ELSE"; + } - /* Remember the data for the .ELSE */ - if (D) { - ReleaseFullLineInfo (&D->LineInfos); - GetFullLineInfo (&D->LineInfos); - D->Name = ".ELSE"; + /* Calculate the new overall condition */ + CalcOverallIfCond (); + + /* Skip .ELSE */ + NextTok (); + ExpectSep (); + } else { + /* Problem with .ELSE, ignore remainder of line */ + SkipUntilSep (); } - - /* Calculate the new overall condition */ - CalcOverallIfCond (); - - /* Skip .ELSE */ - NextTok (); - ExpectSep (); break; case TOK_ELSEIF: D = GetCurrentIf (); /* Handle as if there was an .ELSE first */ - ElseClause (D, ".ELSEIF"); + if (ElseClause (D, ".ELSEIF")) { + /* Calculate the new overall if condition */ + CalcOverallIfCond (); - /* Calculate the new overall if condition */ - CalcOverallIfCond (); + /* Allocate and prepare a new descriptor */ + D = AllocIf (".ELSEIF", 0); + NextTok (); - /* Allocate and prepare a new descriptor */ - D = AllocIf (".ELSEIF", 0); - NextTok (); + /* Ignore the new condition if we are inside a false .ELSE + ** branch. This way we won't get any errors about undefined + ** symbols or similar... + */ + if (IfCond) { + SetIfCond (D, ConstExpression ()); + ExpectSep (); + } - /* Ignore the new condition if we are inside a false .ELSE - ** branch. This way we won't get any errors about undefined - ** symbols or similar... - */ - if (IfCond) { - SetIfCond (D, ConstExpression ()); - ExpectSep (); + /* Get the new overall condition */ + CalcOverallIfCond (); + } else { + /* Problem with .ELSEIF, ignore remainder of line */ + SkipUntilSep (); } - - /* Get the new overall condition */ - CalcOverallIfCond (); break; case TOK_ENDIF: