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"); 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);

View File

@@ -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));
} }

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
}
}

View File

@@ -9,37 +9,37 @@ fun toInt in {
power = (math $power - 1) power = (math $power - 1)
if compare $char == "1" { if compare $char == "1" {
retint = (math $retint + (math 1 * 10 ^ $power)) retint = (math $retint + (math 1 * 10 ^ $power))
} }
if compare $char == "2" { if compare $char == "2" {
retint = (math $retint + (math 2 * 10 ^ $power)) retint = (math $retint + (math 2 * 10 ^ $power))
} }
if compare $char == "3" { if compare $char == "3" {
retint = (math $retint + (math 3 * 10 ^ $power)) retint = (math $retint + (math 3 * 10 ^ $power))
} }
if compare $char == "4" { if compare $char == "4" {
retint = (math $retint + (math 4 * 10 ^ $power)) retint = (math $retint + (math 4 * 10 ^ $power))
} }
if compare $char == "5" { if compare $char == "5" {
retint = (math $retint + (math 5 * 10 ^ $power)) retint = (math $retint + (math 5 * 10 ^ $power))
} }
if compare $char == "6" { if compare $char == "6" {
retint = (math $retint + (math 6 * 10 ^ $power)) retint = (math $retint + (math 6 * 10 ^ $power))
} }
if compare $char == "7" { if compare $char == "7" {
retint = (math $retint + (math 7 * 10 ^ $power)) retint = (math $retint + (math 7 * 10 ^ $power))
} }
if compare $char == "8" { if compare $char == "8" {
retint = (math $retint + (math 8 * 10 ^ $power)) retint = (math $retint + (math 8 * 10 ^ $power))
} }
if compare $char == "9" { if compare $char == "9" {
retint = (math $retint + (math 9 * 10 ^ $power)) retint = (math $retint + (math 9 * 10 ^ $power))
} }
if compare $char == "0" { if compare $char == "0" {
# whole lotta nothing # whole lotta nothing
} }
counter = (math $counter + 1) counter = (math $counter + 1)
} }
return $retint return $retint
} }
let myInt = (toInt "4738927643289") let myInt = (toInt "4738927643289")