diff --git a/src/parsing.cpp b/src/parsing.cpp index 529839e..acb88b7 100644 --- a/src/parsing.cpp +++ b/src/parsing.cpp @@ -523,6 +523,18 @@ namespace parsing { token::TokenStream inner{ stream }; auto before_meta = inner.metadata(); try { + if (inner.peek().type == token::Type::BreakKeyword) { + inner.next(); + stream.m_position = inner.m_position; + auto ret = new AST::BreakStatement{ before_meta + stream.metadata() }; + return std::pair{ std::unique_ptr{ret}, true }; + } + if (inner.peek().type == token::Type::ContinueKeyword) { + inner.next(); + stream.m_position = inner.m_position; + auto ret = new AST::ContinueStatement{ before_meta + stream.metadata() }; + return std::pair{ std::unique_ptr{ret}, true }; + } if (inner.peek().type == token::Type::Symbol && inner.peek().content == "{") { inner.expect(token::Type::Symbol, "{"); std::vector> statements{}; @@ -534,10 +546,10 @@ namespace parsing { statements.push_back(std::move(res.first)); } inner.expect(token::Type::Symbol, "}"); - auto ret = new AST::CompoundStatement{ before_meta + stream.metadata(),std::move(statements) }; stream.m_position = inner.m_position; + auto ret = new AST::CompoundStatement{ before_meta + stream.metadata(),std::move(statements) }; return std::pair{ std::unique_ptr{ret}, false }; } if (inner.peek().type == token::Type::ReturnKeyword) { diff --git a/src/tokens.cpp b/src/tokens.cpp index e534e7f..089dc1d 100644 --- a/src/tokens.cpp +++ b/src/tokens.cpp @@ -63,6 +63,10 @@ namespace token { return "For"; case token::Type::WhileKeyword: return "While"; + case token::Type::BreakKeyword: + return "Break"; + case token::Type::ContinueKeyword: + return "Continue"; case token::Type::Whitespace: return "Whitespace"; @@ -257,6 +261,12 @@ namespace token { else if (content == "while") { type = token::Type::WhileKeyword; } + else if (content == "break") { + type = token::Type::BreakKeyword; + } + else if (content == "continue") { + type = token::Type::ContinueKeyword; + } tokens.push_back(token::Token{ type, content, meta + content.size() }); } else if (iswhitespace(c)) { diff --git a/src/tokens.h b/src/tokens.h index 851f7b5..8615f27 100644 --- a/src/tokens.h +++ b/src/tokens.h @@ -18,6 +18,8 @@ namespace token { ElseKeyword, ForKeyword, WhileKeyword, + BreakKeyword, + ContinueKeyword, Whitespace, Comment, diff --git a/test.c b/test.c index 38f8a9b..360eae8 100644 --- a/test.c +++ b/test.c @@ -57,12 +57,17 @@ int main() { printf("2d array: %d!\n", twod_array[0][0]); // Test for-loops - for (int counter = 0; counter < 10; counter++) + for (int counter = 0; counter < 10; counter++) { + if (counter < 5) + continue; printf("for-counter: %d\n", counter); + } // Test while-loops int counter = 0; - while (counter < 10) { + while (1) { + if (counter > 10) + break; printf("while-counter: %d\n", counter++); }