Parsing bug fix

This commit is contained in:
2025-10-07 10:15:59 +11:00
parent e4432cbe21
commit a6c3a0e39e
4 changed files with 44 additions and 18 deletions

View File

@@ -92,9 +92,18 @@ std::vector<Instruction> parse(std::string program) {
error("Expected '{' for else statement");
continue;
}
size_t else_block_end = remaining_line.find_last_of('}');
if (else_block_end == std::string::npos) {
error("Expected '}' for else statement");
size_t else_block_end = 0;
int brace_level = 0;
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;
}
std::string else_content = remaining_line.substr(else_block_start + 1, else_block_end - else_block_start - 1);

View File

@@ -2,11 +2,11 @@
// Helper to trim whitespace from the start and end of a string
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) {
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));
}

17
test.kyn Normal file
View File

@@ -0,0 +1,17 @@
fun testFn in {
println "Hi!"
if compare 1 == 1 {
println "1 == 1"
# This won't work because this is indented
}
}
fun testFn in {
println "Hi!"
if compare 1 == 1 {
println "1 == 1"
# This works now that the indentation is removed
}
}