From 4172071dba5071d1a06899d35435026517c37153 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Fri, 15 Dec 2023 10:37:44 +0100 Subject: [PATCH] parser: Add continue & break --- src/analysis.cpp | 11 +++++++---- src/include/types.h | 2 ++ src/parser.cpp | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/analysis.cpp b/src/analysis.cpp index e4916ac..a8daae7 100644 --- a/src/analysis.cpp +++ b/src/analysis.cpp @@ -67,9 +67,9 @@ AnalysisResult analyze(Node &ast, Memory &memory) { } else if (holds_alternative(token.data)) { return Type::Double; } + throw exception(); + break; } - throw exception(); - break; case TokenType::Identifier: { string identifier = get(token.data); @@ -77,9 +77,12 @@ AnalysisResult analyze(Node &ast, Memory &memory) { throw RuntimeError("Unknown identifier \""+identifier+"\"", token.pos); return memory.get(identifier).type; + throw exception(); + break; } - throw exception(); - break; + case TokenType::Break: + case TokenType::Continue: + return {}; default: throw exception(); } diff --git a/src/include/types.h b/src/include/types.h index 7b3fbb9..9718b5c 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -39,6 +39,8 @@ Statement -> | If (Expr) Instruction Else Instruction | While (Expr) Instruction | For (Expr | ExprStatement; Expr; Expr) Instruction + | Continue ; + | Break ; ExprStatement -> | Type ParIdentifier = Expr // AssignedDeclaration diff --git a/src/parser.cpp b/src/parser.cpp index 7b9c65e..892f7fc 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -163,6 +163,20 @@ ParseReturn parse_statement(vector tokens) { throw ParseException(); switch (tokens.back().type) { + case TokenType::Break: + case TokenType::Continue: { + Token token = tokens.back(); + tokens.pop_back(); + + if (tokens.back().type != TokenType::Semicolon) + throw SyntaxError("Expected ';'", tokens.back().pos); + + tokens.pop_back(); + return { + .node=token, + .tokens=tokens + }; + } case TokenType::While: case TokenType::If: { CodePosition pos = tokens.back().pos;