Skip to content

Commit 5427dcf

Browse files
committed
fix if-else-endif crash
1 parent 5234c70 commit 5427dcf

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

shyll/compiler.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,13 @@ bool Compiler::Instruction()
368368
case Token::Type::If:
369369
{
370370
Token ifStart = *CurrentToken();
371+
Token elseStart;
371372
currentToken++;
373+
if (!CurrentToken())
374+
{
375+
ErrorAt(*PreviousToken(), "Unterminated if statement");
376+
break;
377+
}
372378
uint16_t ifOffset = EmitJump(OpCode::JumpIfFalse);
373379
uint16_t elseOffset = 0;
374380

@@ -395,13 +401,19 @@ bool Compiler::Instruction()
395401
}
396402
else
397403
{
404+
elseStart = *CurrentToken();
398405
elseOffset = EmitJump(OpCode::Jump);
399406
PatchJump(ifOffset);
400407
ifPatched = true;
401408
currentToken++;
402409
}
403410
}
404411
}
412+
if (!CurrentToken())
413+
{
414+
ErrorAt((ifPatched ? elseStart : ifStart), (ifPatched ? "Unterminated else statement" : "Unterminated if statement"));
415+
break;
416+
}
405417
PatchJump(ifPatched ? elseOffset : ifOffset);
406418
break;
407419
}

0 commit comments

Comments
 (0)