diff --git a/src/parser/parser.cpp b/src/parser/parser.cpp index 27a18d4..ba1a9d0 100644 --- a/src/parser/parser.cpp +++ b/src/parser/parser.cpp @@ -92,9 +92,18 @@ std::vector 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); diff --git a/src/utils/trim/trim.cpp b/src/utils/trim/trim.cpp index cd79636..7b79c23 100644 --- a/src/utils/trim/trim.cpp +++ b/src/utils/trim/trim.cpp @@ -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)); } diff --git a/test.kyn b/test.kyn new file mode 100644 index 0000000..3fb9867 --- /dev/null +++ b/test.kyn @@ -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 +} +} diff --git a/tests/toInt.kyn b/tests/toInt.kyn index e9b7d97..e2bfd0b 100644 --- a/tests/toInt.kyn +++ b/tests/toInt.kyn @@ -9,37 +9,37 @@ fun toInt in { power = (math $power - 1) if compare $char == "1" { retint = (math $retint + (math 1 * 10 ^ $power)) -} + } if compare $char == "2" { retint = (math $retint + (math 2 * 10 ^ $power)) -} + } if compare $char == "3" { retint = (math $retint + (math 3 * 10 ^ $power)) -} + } if compare $char == "4" { retint = (math $retint + (math 4 * 10 ^ $power)) -} + } if compare $char == "5" { retint = (math $retint + (math 5 * 10 ^ $power)) -} - if compare $char == "6" { + } + if compare $char == "6" { retint = (math $retint + (math 6 * 10 ^ $power)) -} + } if compare $char == "7" { retint = (math $retint + (math 7 * 10 ^ $power)) -} + } if compare $char == "8" { retint = (math $retint + (math 8 * 10 ^ $power)) -} + } if compare $char == "9" { retint = (math $retint + (math 9 * 10 ^ $power)) -} + } if compare $char == "0" { # whole lotta nothing -} + } counter = (math $counter + 1) -} + } return $retint } let myInt = (toInt "4738927643289")