Initial work on parsing.

This commit is contained in:
2020-10-07 22:11:18 +02:00
parent f299d5183f
commit 2036f1ce43
10 changed files with 276 additions and 55 deletions

View File

@@ -10,6 +10,7 @@ namespace MalachScript::Parser {
public:
ParsedStatement(TextSpan span) : _span(span) {}
virtual ~ParsedStatement() = default;
[[nodiscard]] virtual ParsedStatementKind GetKind() const noexcept = 0;
[[nodiscard]] inline const TextSpan& GetSpan() const noexcept { return _span; }
};
@@ -17,15 +18,15 @@ namespace MalachScript::Parser {
template <ParsedStatementKind kind> class ParsedStatementImpl : public ParsedStatement {
public:
ParsedStatementImpl(TextSpan span) : ParsedStatement(span) {}
[[nodiscard]] inline ParsedStatementKind GetKind() const noexcept override { return kind; }
[[nodiscard]] inline ParsedStatementKind GetKind() const noexcept final { return kind; }
};
class ParsedBlockStatement : public ParsedStatementImpl<ParsedStatementKind::Block> {
class ParsedScriptStatement : public ParsedStatementImpl<ParsedStatementKind::Script> {
std::vector<std::unique_ptr<const ParsedStatement>> _statements;
public:
ParsedBlockStatement(TextSpan span, const std::vector<const ParsedStatement*>& statements)
: ParsedStatementImpl<ParsedStatementKind::Block>(span), _statements(statements.size()) {
ParsedScriptStatement(TextSpan span, const std::vector<const ParsedStatement*>& statements)
: ParsedStatementImpl<ParsedStatementKind::Script>(span), _statements(statements.size()) {
for (size_t i = 0; i < statements.size(); i++)
_statements[i] = std::unique_ptr<const ParsedStatement>(statements[i]);
}
@@ -35,15 +36,18 @@ namespace MalachScript::Parser {
}
};
class ParsedScriptStatement : public ParsedStatementImpl<ParsedStatementKind::Script> {
std::unique_ptr<const ParsedBlockStatement> _block;
class ParsedClassStatement : public ParsedStatementImpl<ParsedStatementKind::Class> {
std::u8string_view _identifier;
std::vector<std::u8string_view> _inherits;
std::vector<std::unique_ptr<const ParsedStatement>> _body;
public:
ParsedScriptStatement(const ParsedBlockStatement* block)
: ParsedStatementImpl<ParsedStatementKind::Script>(block->GetSpan()), _block(block) {}
[[nodiscard]] inline const std::unique_ptr<const ParsedBlockStatement>& GetBlock() const noexcept {
return _block;
ParsedClassStatement(TextSpan span, std::u8string_view identifier, std::vector<std::u8string_view> inherits,
const std::vector<const ParsedStatement*>& body)
: ParsedStatementImpl<ParsedStatementKind::Class>(span), _identifier(identifier), _inherits(inherits),
_body(body.size()) {
for (size_t i = 0; i < body.size(); i++)
_body[i] = std::unique_ptr<const ParsedStatement>(body[i]);
}
};
}