Implements parsing while statement

This commit is contained in:
Deukhoofd 2021-01-01 13:14:29 +01:00
parent a3a996d68a
commit a9c3a9e0c4
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 39 additions and 3 deletions

View File

@ -550,8 +550,9 @@ namespace MalachScript::Parser {
// TODO: All the other statements.
return ParseIfStatement(out, currentToken) || ParseForStatement(out, currentToken) ||
ParseReturn(out, currentToken) || ParseStatBlock(out, currentToken) || ParseBreak(out, currentToken) ||
ParseContinue(out, currentToken) || ParseExprStat(out, currentToken);
ParseWhileStatement(out, currentToken) || ParseReturn(out, currentToken) ||
ParseStatBlock(out, currentToken) || ParseBreak(out, currentToken) || ParseContinue(out, currentToken) ||
ParseExprStat(out, currentToken);
}
bool Parser::ParseVar([[maybe_unused]] ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken) {
@ -914,4 +915,28 @@ namespace MalachScript::Parser {
currentToken = current;
return true;
}
bool Parser::ParseWhileStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken) {
const auto* current = currentToken;
if (current->GetKind() != LexTokenKind::WhileKeyword) {
return false;
}
PROGRESS_TOKEN(current);
EXPECT_TOKEN(current, OpenParenthesisSymbol);
ScopedPtr<const ParsedStatement> condition = nullptr;
if (!ParseAssign(condition, current)) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, current->GetSpan());
return false;
}
EXPECT_TOKEN(current, CloseParenthesisSymbol);
ScopedPtr<const ParsedStatement> body = nullptr;
if (!ParseStatement(body, current)) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, current->GetSpan());
return false;
}
out = new ParsedWhileStatement(TextSpan(currentToken->GetSpan().GetStart(), current->GetSpan().GetEnd()),
condition.TakeOwnership(), body.TakeOwnership());
currentToken = current;
return true;
}
}

View File

@ -152,7 +152,7 @@ namespace MalachScript::Parser {
bool ParseIfStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken);
bool ParseForStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken);
// While
bool ParseWhileStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken);
// DoWhile
// Try
// Case

View File

@ -394,6 +394,16 @@ namespace MalachScript::Parser {
std::unique_ptr<const ParsedStatement> _body;
};
class ParsedWhileStatement : public ParsedStatementImpl<ParsedStatementKind::While> {
public:
ParsedWhileStatement(const TextSpan& span, const ParsedStatement* condition, const ParsedStatement* body)
: ParsedStatementImpl(span), _condition(condition), _body(body) {}
private:
std::unique_ptr<const ParsedStatement> _condition;
std::unique_ptr<const ParsedStatement> _body;
};
}
#endif // MALACHSCRIPT_PARSEDSTATEMENT_HPP

View File

@ -25,6 +25,7 @@ namespace MalachScript::Parser {
Continue,
Break,
For,
While,
};
}