Implements parsing while statement
This commit is contained in:
parent
a3a996d68a
commit
a9c3a9e0c4
@ -550,8 +550,9 @@ namespace MalachScript::Parser {
|
|||||||
// TODO: All the other statements.
|
// TODO: All the other statements.
|
||||||
|
|
||||||
return ParseIfStatement(out, currentToken) || ParseForStatement(out, currentToken) ||
|
return ParseIfStatement(out, currentToken) || ParseForStatement(out, currentToken) ||
|
||||||
ParseReturn(out, currentToken) || ParseStatBlock(out, currentToken) || ParseBreak(out, currentToken) ||
|
ParseWhileStatement(out, currentToken) || ParseReturn(out, currentToken) ||
|
||||||
ParseContinue(out, currentToken) || ParseExprStat(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) {
|
bool Parser::ParseVar([[maybe_unused]] ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken) {
|
||||||
@ -914,4 +915,28 @@ namespace MalachScript::Parser {
|
|||||||
currentToken = current;
|
currentToken = current;
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
@ -152,7 +152,7 @@ namespace MalachScript::Parser {
|
|||||||
|
|
||||||
bool ParseIfStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken);
|
bool ParseIfStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken);
|
||||||
bool ParseForStatement(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
|
// DoWhile
|
||||||
// Try
|
// Try
|
||||||
// Case
|
// Case
|
||||||
|
@ -394,6 +394,16 @@ namespace MalachScript::Parser {
|
|||||||
std::unique_ptr<const ParsedStatement> _body;
|
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
|
#endif // MALACHSCRIPT_PARSEDSTATEMENT_HPP
|
||||||
|
@ -25,6 +25,7 @@ namespace MalachScript::Parser {
|
|||||||
Continue,
|
Continue,
|
||||||
Break,
|
Break,
|
||||||
For,
|
For,
|
||||||
|
While,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user