From 5427dcfaab0da7a1c25f3b90e048b35392041a4a Mon Sep 17 00:00:00 2001 From: Shy Date: Fri, 3 Apr 2020 21:40:05 -0400 Subject: [PATCH] fix if-else-endif crash --- shyll/compiler.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/shyll/compiler.cpp b/shyll/compiler.cpp index 5dc0f39..32518e5 100644 --- a/shyll/compiler.cpp +++ b/shyll/compiler.cpp @@ -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; @@ -395,6 +401,7 @@ bool Compiler::Instruction() } else { + elseStart = *CurrentToken(); elseOffset = EmitJump(OpCode::Jump); PatchJump(ifOffset); ifPatched = true; @@ -402,6 +409,11 @@ bool Compiler::Instruction() } } } + if (!CurrentToken()) + { + ErrorAt((ifPatched ? elseStart : ifStart), (ifPatched ? "Unterminated else statement" : "Unterminated if statement")); + break; + } PatchJump(ifPatched ? elseOffset : ifOffset); break; }