parser: Add continue & break

This commit is contained in:
augustin64 2023-12-15 10:37:44 +01:00
parent 1a7b240cb0
commit 4172071dba
3 changed files with 23 additions and 4 deletions

View File

@ -67,9 +67,9 @@ AnalysisResult analyze(Node &ast, Memory &memory) {
} else if (holds_alternative<double>(token.data)) {
return Type::Double;
}
}
throw exception();
break;
}
case TokenType::Identifier: {
string identifier = get<string>(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;
}
case TokenType::Break:
case TokenType::Continue:
return {};
default:
throw exception();
}

View File

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

View File

@ -163,6 +163,20 @@ ParseReturn parse_statement(vector<Token> 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;