Parsing bug fix
This commit is contained in:
@@ -92,9 +92,18 @@ std::vector<Instruction> parse(std::string program) {
|
|||||||
error("Expected '{' for else statement");
|
error("Expected '{' for else statement");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
size_t else_block_end = remaining_line.find_last_of('}');
|
size_t else_block_end = 0;
|
||||||
if (else_block_end == std::string::npos) {
|
int brace_level = 0;
|
||||||
error("Expected '}' for else statement");
|
for (size_t i = else_block_start; i < remaining_line.length(); ++i) {
|
||||||
|
if (remaining_line[i] == '{') brace_level++;
|
||||||
|
else if (remaining_line[i] == '}') brace_level--;
|
||||||
|
if (brace_level == 0) {
|
||||||
|
else_block_end = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (else_block_end == 0) {
|
||||||
|
error("Mismatched braces in else statement");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::string else_content = remaining_line.substr(else_block_start + 1, else_block_end - else_block_start - 1);
|
std::string else_content = remaining_line.substr(else_block_start + 1, else_block_end - else_block_start - 1);
|
||||||
|
|||||||
@@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
// Helper to trim whitespace from the start and end of a string
|
// Helper to trim whitespace from the start and end of a string
|
||||||
std::string trim(const std::string& str) {
|
std::string trim(const std::string& str) {
|
||||||
size_t first = str.find_first_not_of(" \t\n\r");
|
size_t first = str.find_first_not_of(" \t\n");
|
||||||
if (std::string::npos == first) {
|
if (std::string::npos == first) {
|
||||||
return str;
|
return "";
|
||||||
}
|
}
|
||||||
size_t last = str.find_last_not_of(" \t\n\r");
|
size_t last = str.find_last_not_of(" \t\n");
|
||||||
return str.substr(first, (last - first + 1));
|
return str.substr(first, (last - first + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user