parser: Add continue & break
This commit is contained in:
parent
1a7b240cb0
commit
4172071dba
@ -67,9 +67,9 @@ AnalysisResult analyze(Node &ast, Memory &memory) {
|
||||
} else if (holds_alternative<double>(token.data)) {
|
||||
return Type::Double;
|
||||
}
|
||||
throw exception();
|
||||
break;
|
||||
}
|
||||
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;
|
||||
}
|
||||
throw exception();
|
||||
break;
|
||||
case TokenType::Break:
|
||||
case TokenType::Continue:
|
||||
return {};
|
||||
default:
|
||||
throw exception();
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user