2020-11-10 14:26:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "token.hpp"
|
|
|
|
|
2020-12-06 20:40:57 +00:00
|
|
|
#include <bit>
|
2020-11-10 14:26:38 +00:00
|
|
|
#include <optional>
|
2020-11-14 13:40:21 +00:00
|
|
|
#include <unordered_map>
|
2020-11-10 14:26:38 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace hex::lang {
|
|
|
|
|
|
|
|
class ASTNode {
|
|
|
|
public:
|
|
|
|
enum class Type {
|
|
|
|
VariableDecl,
|
|
|
|
TypeDecl,
|
|
|
|
Struct,
|
2020-11-20 20:29:28 +00:00
|
|
|
Union,
|
2020-11-14 13:40:21 +00:00
|
|
|
Enum,
|
2020-11-20 19:26:19 +00:00
|
|
|
Bitfield,
|
2020-11-10 14:26:38 +00:00
|
|
|
Scope,
|
|
|
|
};
|
|
|
|
|
2020-11-27 20:20:23 +00:00
|
|
|
explicit ASTNode(Type type, u32 lineNumber) : m_type(type), m_lineNumber(lineNumber) {}
|
2020-11-11 09:47:02 +00:00
|
|
|
virtual ~ASTNode() = default;
|
2020-11-10 14:26:38 +00:00
|
|
|
|
|
|
|
Type getType() { return this->m_type; }
|
2020-11-27 20:20:23 +00:00
|
|
|
u32 getLineNumber() { return this->m_lineNumber; }
|
2020-11-10 14:26:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Type m_type;
|
2020-11-27 20:20:23 +00:00
|
|
|
u32 m_lineNumber;
|
2020-11-10 14:26:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ASTNodeVariableDecl : public ASTNode {
|
|
|
|
public:
|
2020-12-06 20:40:57 +00:00
|
|
|
explicit ASTNodeVariableDecl(u32 lineNumber, const Token::TypeToken::Type &type, const std::string &name, const std::string& customTypeName = "", std::optional<u64> offset = { }, size_t arraySize = 1, std::optional<std::string> arraySizeVariable = { }, std::optional<u8> pointerSize = { }, std::optional<std::endian> endianess = { })
|
|
|
|
: ASTNode(Type::VariableDecl, lineNumber), m_type(type), m_name(name), m_customTypeName(customTypeName), m_offset(offset), m_arraySize(arraySize), m_arraySizeVariable(arraySizeVariable), m_pointerSize(pointerSize), m_endianess(endianess) { }
|
2020-11-10 14:26:38 +00:00
|
|
|
|
|
|
|
const Token::TypeToken::Type& getVariableType() const { return this->m_type; }
|
|
|
|
const std::string& getCustomVariableTypeName() const { return this->m_customTypeName; }
|
|
|
|
const std::string& getVariableName() const { return this->m_name; };
|
2020-11-12 22:57:43 +00:00
|
|
|
std::optional<u64> getOffset() const { return this->m_offset; }
|
|
|
|
size_t getArraySize() const { return this->m_arraySize; }
|
2020-11-21 19:19:33 +00:00
|
|
|
std::optional<std::string> getArraySizeVariable() const { return this->m_arraySizeVariable; }
|
2020-11-21 22:00:09 +00:00
|
|
|
std::optional<u8> getPointerSize() const { return this->m_pointerSize; }
|
2020-12-06 20:40:57 +00:00
|
|
|
std::optional<std::endian> getEndianess() const { return this->m_endianess; }
|
2020-11-10 14:26:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Token::TypeToken::Type m_type;
|
|
|
|
std::string m_name, m_customTypeName;
|
2020-11-10 15:24:48 +00:00
|
|
|
std::optional<u64> m_offset;
|
2020-11-12 22:57:43 +00:00
|
|
|
size_t m_arraySize;
|
2020-11-21 19:19:33 +00:00
|
|
|
std::optional<std::string> m_arraySizeVariable;
|
2020-11-21 22:00:09 +00:00
|
|
|
std::optional<u8> m_pointerSize;
|
2020-12-06 20:40:57 +00:00
|
|
|
std::optional<std::endian> m_endianess = { };
|
2020-11-10 14:26:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ASTNodeScope : public ASTNode {
|
|
|
|
public:
|
2020-11-27 20:20:23 +00:00
|
|
|
explicit ASTNodeScope(u32 lineNumber, std::vector<ASTNode*> nodes) : ASTNode(Type::Scope, lineNumber), m_nodes(nodes) { }
|
2020-11-10 14:26:38 +00:00
|
|
|
|
|
|
|
std::vector<ASTNode*> &getNodes() { return this->m_nodes; }
|
|
|
|
private:
|
|
|
|
std::vector<ASTNode*> m_nodes;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ASTNodeStruct : public ASTNode {
|
|
|
|
public:
|
2020-11-27 20:20:23 +00:00
|
|
|
explicit ASTNodeStruct(u32 lineNumber, std::string name, std::vector<ASTNode*> nodes)
|
|
|
|
: ASTNode(Type::Struct, lineNumber), m_name(name), m_nodes(nodes) { }
|
2020-11-10 14:26:38 +00:00
|
|
|
|
|
|
|
const std::string& getName() const { return this->m_name; }
|
|
|
|
std::vector<ASTNode*> &getNodes() { return this->m_nodes; }
|
|
|
|
private:
|
|
|
|
std::string m_name;
|
|
|
|
std::vector<ASTNode*> m_nodes;
|
|
|
|
};
|
|
|
|
|
2020-11-20 20:29:28 +00:00
|
|
|
class ASTNodeUnion : public ASTNode {
|
|
|
|
public:
|
2020-11-27 20:20:23 +00:00
|
|
|
explicit ASTNodeUnion(u32 lineNumber, std::string name, std::vector<ASTNode*> nodes)
|
|
|
|
: ASTNode(Type::Union, lineNumber), m_name(name), m_nodes(nodes) { }
|
2020-11-20 20:29:28 +00:00
|
|
|
|
|
|
|
const std::string& getName() const { return this->m_name; }
|
|
|
|
std::vector<ASTNode*> &getNodes() { return this->m_nodes; }
|
|
|
|
private:
|
|
|
|
std::string m_name;
|
|
|
|
std::vector<ASTNode*> m_nodes;
|
|
|
|
};
|
|
|
|
|
2020-11-20 19:26:19 +00:00
|
|
|
class ASTNodeBitField : public ASTNode {
|
|
|
|
public:
|
2020-11-27 20:20:23 +00:00
|
|
|
explicit ASTNodeBitField(u32 lineNumber, std::string name, std::vector<std::pair<std::string, size_t>> fields)
|
|
|
|
: ASTNode(Type::Bitfield, lineNumber), m_name(name), m_fields(fields) { }
|
2020-11-20 19:26:19 +00:00
|
|
|
|
|
|
|
const std::string& getName() const { return this->m_name; }
|
|
|
|
std::vector<std::pair<std::string, size_t>> &getFields() { return this->m_fields; }
|
|
|
|
private:
|
|
|
|
std::string m_name;
|
|
|
|
std::vector<std::pair<std::string, size_t>> m_fields;
|
|
|
|
};
|
|
|
|
|
2020-11-10 14:26:38 +00:00
|
|
|
class ASTNodeTypeDecl : public ASTNode {
|
|
|
|
public:
|
2020-11-27 20:20:23 +00:00
|
|
|
explicit ASTNodeTypeDecl(u32 lineNumber, const Token::TypeToken::Type &type, const std::string &name, const std::string& customTypeName = "")
|
|
|
|
: ASTNode(Type::TypeDecl, lineNumber), m_type(type), m_name(name), m_customTypeName(customTypeName) { }
|
2020-11-10 14:26:38 +00:00
|
|
|
|
|
|
|
const std::string& getTypeName() const { return this->m_name; };
|
|
|
|
|
|
|
|
const Token::TypeToken::Type& getAssignedType() const { return this->m_type; }
|
|
|
|
const std::string& getAssignedCustomTypeName() const { return this->m_customTypeName; }
|
|
|
|
private:
|
|
|
|
Token::TypeToken::Type m_type;
|
|
|
|
std::string m_name, m_customTypeName;
|
|
|
|
};
|
|
|
|
|
2020-11-14 13:40:21 +00:00
|
|
|
class ASTNodeEnum : public ASTNode {
|
|
|
|
public:
|
2020-11-27 20:20:23 +00:00
|
|
|
explicit ASTNodeEnum(u32 lineNumber, const Token::TypeToken::Type &type, const std::string &name)
|
|
|
|
: ASTNode(Type::Enum, lineNumber), m_type(type), m_name(name) { }
|
2020-11-14 13:40:21 +00:00
|
|
|
|
|
|
|
const std::string& getName() const { return this->m_name; };
|
|
|
|
|
|
|
|
const Token::TypeToken::Type& getUnderlyingType() const { return this->m_type; }
|
|
|
|
std::vector<std::pair<u64, std::string>>& getValues() { return this->m_values; }
|
|
|
|
private:
|
|
|
|
Token::TypeToken::Type m_type;
|
|
|
|
std::string m_name;
|
|
|
|
std::vector<std::pair<u64, std::string>> m_values;
|
|
|
|
};
|
|
|
|
|
2020-11-10 14:26:38 +00:00
|
|
|
}
|