patterns: ASTNode and LogConsole cleanup

This commit is contained in:
WerWolv
2021-10-31 15:06:48 +01:00
parent 716d6573ca
commit cd89b55f5b
5 changed files with 91 additions and 88 deletions

View File

@@ -11,11 +11,62 @@
#include <variant>
#include <vector>
#include <hex/pattern_language/ast_node_base.hpp>
namespace hex::pl {
class ASTNode;
class ASTNodeAttribute;
class PatternData;
class Evaluator;
class Attributable {
protected:
Attributable() = default;
Attributable(const Attributable &) = default;
public:
void addAttribute(ASTNodeAttribute *attribute) {
this->m_attributes.push_back(attribute);
}
[[nodiscard]] const auto &getAttributes() const {
return this->m_attributes;
}
private:
std::vector<ASTNodeAttribute *> m_attributes;
};
class Clonable {
public:
[[nodiscard]]
virtual ASTNode* clone() const = 0;
};
class ASTNode : public Clonable {
public:
constexpr ASTNode() = default;
constexpr virtual ~ASTNode() = default;
constexpr ASTNode(const ASTNode &) = default;
[[nodiscard]] constexpr u32 getLineNumber() const { return this->m_lineNumber; }
[[maybe_unused]] constexpr void setLineNumber(u32 lineNumber) { this->m_lineNumber = lineNumber; }
[[nodiscard]] virtual ASTNode *evaluate(Evaluator *evaluator) const { return this->clone(); }
[[nodiscard]] virtual std::vector<PatternData *> createPatterns(Evaluator *evaluator) const { return {}; }
using FunctionResult = std::pair<bool, std::optional<Token::Literal>>;
virtual FunctionResult execute(Evaluator *evaluator) { throw std::pair<u32, std::string>(this->getLineNumber(), "cannot execute non-function statement"); }
private:
u32 m_lineNumber = 1;
};
class ASTNodeAttribute : public ASTNode {
public:

View File

@@ -1,65 +0,0 @@
#pragma once
#include <optional>
#include <vector>
#include <hex/pattern_language/token.hpp>
namespace hex::pl {
class ASTNode;
class ASTNodeAttribute;
class PatternData;
class Evaluator;
class Attributable {
protected:
Attributable() = default;
Attributable(const Attributable &) = default;
public:
void addAttribute(ASTNodeAttribute *attribute) {
this->m_attributes.push_back(attribute);
}
[[nodiscard]] const auto &getAttributes() const {
return this->m_attributes;
}
private:
std::vector<ASTNodeAttribute *> m_attributes;
};
class Clonable {
public:
[[nodiscard]]
virtual ASTNode* clone() const = 0;
};
class ASTNode : public Clonable {
public:
constexpr ASTNode() = default;
constexpr virtual ~ASTNode() = default;
constexpr ASTNode(const ASTNode &) = default;
[[nodiscard]] constexpr u32 getLineNumber() const { return this->m_lineNumber; }
[[maybe_unused]] constexpr void setLineNumber(u32 lineNumber) { this->m_lineNumber = lineNumber; }
[[nodiscard]] virtual ASTNode *evaluate(Evaluator *evaluator) const { return this->clone(); }
[[nodiscard]] virtual std::vector<PatternData *> createPatterns(Evaluator *evaluator) const { return {}; }
using FunctionResult = std::pair<bool, std::optional<Token::Literal>>;
virtual FunctionResult execute(Evaluator *evaluator) { throw std::pair<u32, std::string>(this->getLineNumber(), "cannot execute non-function statement"); }
private:
u32 m_lineNumber = 1;
};
}

View File

@@ -2,13 +2,12 @@
#include <hex.hpp>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <hex/pattern_language/ast_node_base.hpp>
namespace hex::pl {
class ASTNode;
@@ -27,30 +26,15 @@ namespace hex::pl {
using EvaluateError = std::pair<u32, std::string>;
void log(Level level, const std::string &message) {
switch (level) {
default:
case Level::Debug: this->m_consoleLog.emplace_back(level, "[-] " + message); break;
case Level::Info: this->m_consoleLog.emplace_back(level, "[i] " + message); break;
case Level::Warning: this->m_consoleLog.emplace_back(level, "[*] " + message); break;
case Level::Error: this->m_consoleLog.emplace_back(level, "[!] " + message); break;
}
}
void log(Level level, const std::string &message);
[[noreturn]]
static void abortEvaluation(const std::string &message) {
throw EvaluateError(0, message);
}
static void abortEvaluation(const std::string &message);
[[noreturn]]
static void abortEvaluation(const std::string &message, const auto *node) {
throw EvaluateError(static_cast<const ASTNode*>(node)->getLineNumber(), message);
}
static void abortEvaluation(const std::string &message, const ASTNode *node);
void clear() {
this->m_consoleLog.clear();
this->m_lastHardError = { };
}
void clear();
void setHardError(const EvaluateError &error) { this->m_lastHardError = error; }