Skip to content

Commit

Permalink
fix if-else-endif crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Shylie committed Apr 4, 2020
1 parent 5234c70 commit 5427dcf
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions shyll/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,13 @@ bool Compiler::Instruction()
case Token::Type::If:
{
Token ifStart = *CurrentToken();
Token elseStart;
currentToken++;
if (!CurrentToken())
{
ErrorAt(*PreviousToken(), "Unterminated if statement");
break;
}
uint16_t ifOffset = EmitJump(OpCode::JumpIfFalse);
uint16_t elseOffset = 0;

Expand All @@ -395,13 +401,19 @@ bool Compiler::Instruction()
}
else
{
elseStart = *CurrentToken();
elseOffset = EmitJump(OpCode::Jump);
PatchJump(ifOffset);
ifPatched = true;
currentToken++;
}
}
}
if (!CurrentToken())
{
ErrorAt((ifPatched ? elseStart : ifStart), (ifPatched ? "Unterminated else statement" : "Unterminated if statement"));
break;
}
PatchJump(ifPatched ? elseOffset : ifOffset);
break;
}
Expand Down

0 comments on commit 5427dcf

Please sign in to comment.