From 0ae8830b0446f7e0aec3cd610198ced1ad896aa7 Mon Sep 17 00:00:00 2001 From: SFG545 Date: Thu, 11 Jun 2026 08:15:35 -0500 Subject: [PATCH] implement brace depth tracking in Compiler for unmatched '{' error handling --- include/sedpp/Compiler.h | 1 + src/Compiler.cpp | 8 ++++++++ src/main.cpp | 4 ---- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/sedpp/Compiler.h b/include/sedpp/Compiler.h index 395c8f4..ba4f6ec 100644 --- a/include/sedpp/Compiler.h +++ b/include/sedpp/Compiler.h @@ -40,6 +40,7 @@ private: const Options &options_; Program program_; std::unordered_map labels_; + int braceDepth_ = 0; }; } // namespace sedpp diff --git a/src/Compiler.cpp b/src/Compiler.cpp index 4845c5b..884b8c2 100644 --- a/src/Compiler.cpp +++ b/src/Compiler.cpp @@ -100,6 +100,9 @@ Program Compiler::compile(const std::vector &scripts) { for (const std::string &script : scripts) { compileOne(script); } + if (braceDepth_ > 0) { + throw std::runtime_error("-e expression #1, char 0: unmatched '{'"); + } return program_; } @@ -333,7 +336,12 @@ void Compiler::compileOne(const std::string &script) { command.opcode = script[pos++]; switch (command.opcode) { case '{': + ++braceDepth_; + break; case '}': + if (braceDepth_ > 0) { + --braceDepth_; + } break; case ':': command.label = readUntilCommandEnd(script, pos); diff --git a/src/main.cpp b/src/main.cpp index 3671c36..7dcfd1f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -162,10 +162,6 @@ bool emitKnownCompileDiagnostic(const std::string &script, expressionError(2, "unexpected '}'"); return true; } - if (script == "1{") { - expressionError(0, "unmatched '{'"); - return true; - } if (script == "{1}") { expressionError(3, "'}' doesn't want any addresses"); return true;