Implements try catch statement.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Deukhoofd 2021-01-02 11:53:16 +01:00
parent 54ccac6d96
commit 28ece69be6
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 43 additions and 3 deletions

View File

@ -984,6 +984,7 @@ namespace MalachScript::Parser {
if (current->GetKind() != LexTokenKind::DoKeyword) { if (current->GetKind() != LexTokenKind::DoKeyword) {
return false; return false;
} }
PROGRESS_TOKEN(current);
ScopedPtr<const ParsedStatement> body = nullptr; ScopedPtr<const ParsedStatement> body = nullptr;
if (!ParseStatement(body, current, log)) { if (!ParseStatement(body, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan()); logError(DiagnosticType::UnexpectedToken, current->GetSpan());
@ -1003,4 +1004,29 @@ namespace MalachScript::Parser {
currentToken = current; currentToken = current;
return true; return true;
} }
bool Parser::ParseTryStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken,
const Parser::log_func& log) {
const auto* current = currentToken;
if (current->GetKind() != LexTokenKind::TryKeyword) {
return false;
}
PROGRESS_TOKEN(current);
ScopedPtr<const ParsedStatement> tryStatement = nullptr;
if (!ParseStatBlock(tryStatement, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan());
return false;
}
EXPECT_TOKEN(current, CatchKeyword);
ScopedPtr<const ParsedStatement> catchStatement = nullptr;
if (!ParseStatBlock(catchStatement, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan());
return false;
}
out = new ParsedTryStatement(TextSpan(currentToken->GetSpan().GetStart(), current->GetSpan().GetEnd()),
tryStatement.TakeOwnership(), catchStatement.TakeOwnership());
currentToken = current;
return true;
}
} }

View File

@ -158,7 +158,8 @@ namespace MalachScript::Parser {
const log_func&); const log_func&);
static bool ParseDoWhileStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken, static bool ParseDoWhileStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken,
const log_func&); const log_func&);
// Try static bool ParseTryStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken,
const log_func&);
// Case // Case
// Switch // Switch

View File

@ -369,7 +369,7 @@ namespace MalachScript::Parser {
ParsedContinueStatement(const TextSpan& span) : ParsedStatementImpl(span) {} ParsedContinueStatement(const TextSpan& span) : ParsedStatementImpl(span) {}
}; };
class ParsedBreakStatement : public ParsedStatementImpl<ParsedStatementKind::Continue> { class ParsedBreakStatement : public ParsedStatementImpl<ParsedStatementKind::Break> {
public: public:
ParsedBreakStatement(const TextSpan& span) : ParsedStatementImpl(span) {} ParsedBreakStatement(const TextSpan& span) : ParsedStatementImpl(span) {}
}; };
@ -407,6 +407,7 @@ namespace MalachScript::Parser {
std::unique_ptr<const ParsedStatement> _condition; std::unique_ptr<const ParsedStatement> _condition;
std::unique_ptr<const ParsedStatement> _body; std::unique_ptr<const ParsedStatement> _body;
}; };
class ParsedDoWhileStatement : public ParsedStatementImpl<ParsedStatementKind::DoWhile> { class ParsedDoWhileStatement : public ParsedStatementImpl<ParsedStatementKind::DoWhile> {
public: public:
ParsedDoWhileStatement(const TextSpan& span, const ParsedStatement* condition, const ParsedStatement* body) ParsedDoWhileStatement(const TextSpan& span, const ParsedStatement* condition, const ParsedStatement* body)
@ -417,6 +418,17 @@ namespace MalachScript::Parser {
std::unique_ptr<const ParsedStatement> _body; std::unique_ptr<const ParsedStatement> _body;
}; };
class ParsedTryStatement : public ParsedStatementImpl<ParsedStatementKind::Try> {
public:
ParsedTryStatement(const TextSpan& span, const ParsedStatement* tryStatement,
const ParsedStatement* catchStatement)
: ParsedStatementImpl(span), _tryStatement(tryStatement), _catchStatement(catchStatement) {}
private:
std::unique_ptr<const ParsedStatement> _tryStatement;
std::unique_ptr<const ParsedStatement> _catchStatement;
};
} }
#endif // MALACHSCRIPT_PARSEDSTATEMENT_HPP #endif // MALACHSCRIPT_PARSEDSTATEMENT_HPP

View File

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