Implements parsing dowhile statements
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-01-01 13:21:59 +01:00
parent 037470a0ce
commit dd2722d485
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 36 additions and 1 deletions

View File

@ -555,6 +555,7 @@ namespace MalachScript::Parser {
case LexTokenKind::ReturnKeyword: return ParseReturn(out, currentToken);
case LexTokenKind::BreakKeyword: return ParseBreak(out, currentToken);
case LexTokenKind::ContinueKeyword: return ParseContinue(out, currentToken);
case LexTokenKind::DoKeyword: return ParseDoWhileStatement(out, currentToken);
default: return ParseStatBlock(out, currentToken) || ParseExprStat(out, currentToken);
}
return false;
@ -944,4 +945,28 @@ namespace MalachScript::Parser {
currentToken = current;
return true;
}
bool Parser::ParseDoWhileStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken) {
const auto* current = currentToken;
if (current->GetKind() != LexTokenKind::DoKeyword) {
return false;
}
ScopedPtr<const ParsedStatement> body = nullptr;
if (!ParseStatement(body, current)) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, current->GetSpan());
return false;
}
EXPECT_TOKEN(current, WhileKeyword);
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);
EXPECT_TOKEN(current, SemicolonSymbol);
out = new ParsedDoWhileStatement(TextSpan(currentToken->GetSpan().GetStart(), current->GetSpan().GetEnd()),
condition.TakeOwnership(), body.TakeOwnership());
currentToken = current;
return true;
}
}

View File

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

View File

@ -403,6 +403,15 @@ namespace MalachScript::Parser {
std::unique_ptr<const ParsedStatement> _condition;
std::unique_ptr<const ParsedStatement> _body;
};
class ParsedDoWhileStatement : public ParsedStatementImpl<ParsedStatementKind::DoWhile> {
public:
ParsedDoWhileStatement(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;
};
}

View File

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