Skip to content

Commit

Permalink
Do not evaluate an untaken ELIF's condition
Browse files Browse the repository at this point in the history
Fixes gbdev#764
  • Loading branch information
Rangi42 committed Mar 22, 2021
1 parent 5406674 commit bd3d438
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/asm/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,8 @@ static char const *reportGarbageChar(unsigned char firstByte)

/* Lexer core */

static int yylex_SKIP_TO_ENDC(void); // forward declaration for yylex_NORMAL

static int yylex_NORMAL(void)
{
dbgPrint("Lexing in normal mode, line=%" PRIu32 ", col=%" PRIu32 "\n",
Expand Down Expand Up @@ -2085,6 +2087,12 @@ static int yylex_NORMAL(void)
if (startsIdentifier(c)) {
int tokenType = readIdentifier(c);

/* An ELIF after a taken IF needs to not evaluate its condition */
if (tokenType == T_POP_ELIF && lexerState->lastToken == T_NEWLINE
&& lexer_GetIFDepth() > 0 && lexer_RanIFBlock()
&& !lexer_ReachedELSEBlock())
return yylex_SKIP_TO_ENDC();

/* If a keyword, don't try to expand */
if (tokenType != T_ID && tokenType != T_LOCAL_ID)
return tokenType;
Expand Down
23 changes: 23 additions & 0 deletions test/asm/elif-after-taken-if.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
if 1
println "taken if"
elif 2 / 0 ; avoided fatal "Division by zero" error
println "untaken elif"
elif 3 / 0 ; avoided fatal "Division by zero" error
println "untaken after untaken"
endc

if 0
println "untaken if"
elif 1
println "taken elif"
elif !@#$ ; avoided fatal syntax error
println "untaken elif"
elif %^&* ; avoided fatal syntax error
println "untaken after untaken"
endc

if 0
println "untaken if"
elif 1 / 0 ; fatal "Division by zero" error
println "unreached elif"
endc
2 changes: 2 additions & 0 deletions test/asm/elif-after-taken-if.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FATAL: elif-after-taken-if.asm(21):
Division by zero
2 changes: 2 additions & 0 deletions test/asm/elif-after-taken-if.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
taken if
taken elif

0 comments on commit bd3d438

Please # to comment.