Beginnings of a working compiler.

This commit is contained in:
2025-04-27 22:53:48 -07:00
parent ba08245e02
commit 0fc10f4c76
21 changed files with 1239 additions and 71 deletions

View File

@@ -13,14 +13,15 @@ Architecture::Architecture(ehs::Str_8 name)
}
Architecture::Architecture(Architecture &&arch) noexcept
: id(arch.id), name((ehs::Str_8 &&)arch.name), instructions((ehs::Array<Instruction> &&)arch.instructions)
: id(arch.id), name((ehs::Str_8 &&)arch.name), registers((ehs::Array<Register> &&)arch.registers),
instructions((ehs::Array<Instruction> &&)arch.instructions)
{
for (ehs::Size i = 0; i < instructions.Size(); ++i)
instructions[i].arch = this;
}
Architecture::Architecture(const Architecture &arc)
: id(arc.id), name(arc.name), instructions(arc.instructions)
: id(arc.id), name(arc.name), registers(arc.registers), instructions(arc.instructions)
{
for (ehs::Size i = 0; i < instructions.Size(); ++i)
instructions[i].arch = this;
@@ -33,6 +34,7 @@ Architecture & Architecture::operator=(Architecture &&arc) noexcept
id = arc.id;
name = (ehs::Str_8 &&)arc.name;
registers = (ehs::Array<Register> &&)arc.registers;
instructions = (ehs::Array<Instruction> &&)arc.instructions;
for (ehs::Size i = 0; i < instructions.Size(); ++i)
@@ -50,6 +52,7 @@ Architecture & Architecture::operator=(const Architecture &arc)
id = arc.id;
name = arc.name;
registers = arc.registers;
instructions = arc.instructions;
for (ehs::Size i = 0; i < instructions.Size(); ++i)
@@ -68,6 +71,44 @@ ehs::Str_8 Architecture::GetName() const
return name;
}
bool Architecture::HasRegister(const ehs::UInt_64& id) const
{
for (ehs::UInt_64 i = 0; i < registers.Size(); ++i)
if (registers[i].GetId() == id)
return true;
return false;
}
bool Architecture::HasRegister(const ehs::Str_8& name) const
{
return HasRegister(name.Hash_64());
}
const Register* Architecture::GetRegister(const ehs::UInt_64& id) const
{
for (ehs::UInt_64 i = 0; i < registers.Size(); ++i)
if (registers[i].GetId() == id)
return &registers[i];
return nullptr;
}
const Register* Architecture::GetRegister(const ehs::Str_8& name) const
{
return GetRegister(name.Hash_64());
}
bool Architecture::AddRegister(Register reg)
{
if (HasRegister(reg.GetId()))
return false;
registers.Push((Register &&)reg);
return true;
}
bool Architecture::HasInstruction(const ehs::UInt_64 &id) const
{
for (ehs::UInt_64 i = 0; i < instructions.Size(); ++i)

View File

@@ -1 +1,186 @@
#include "arctyx/compiler/Compiler.h"
Compiler::Compiler()
: architecture(nullptr), language(nullptr)
{
}
Compiler::Compiler(const ehs::Str_8& arch, const ehs::Str_8& lang)
{
architecture = Architecture::Get(arch);
if (!architecture)
{
EHS_LOG(ehs::LogType::ERR, 0, "The Architecture, \"" + arch + "\" is not supported.");
return;
}
language = Language::Get(lang);
if (!language)
{
EHS_LOG(ehs::LogType::ERR, 0, "The Language, \"" + lang + "\" is not supported.");
return;
}
EHS_LOG_SUCCESS();
}
Compiler::Compiler(Compiler&& other) noexcept
: architecture(other.architecture), language(other.language), symbols((ehs::Array<Symbol> &&)other.symbols)
{
other.architecture = nullptr;
other.language = nullptr;
}
Compiler::Compiler(const Compiler& other)
: architecture(other.architecture), language(other.language), symbols(other.symbols)
{
}
Compiler& Compiler::operator=(Compiler&& other) noexcept
{
if (this == &other)
return *this;
architecture = other.architecture;
language = other.language;
symbols = (ehs::Array<Symbol> &&)other.symbols;
other.architecture = nullptr;
other.language = nullptr;
return *this;
}
Compiler& Compiler::operator=(const Compiler& other)
{
if (this == &other)
return *this;
architecture = other.architecture;
language = other.language;
symbols = other.symbols;
return *this;
}
ehs::Array<ehs::Byte> Compiler::Compile(const ehs::Str_8 &code) const
{
ehs::Vector<Token> tokens = Parse(code);
ehs::Array<ehs::Byte> machineCode;
return machineCode;
}
bool Compiler::IsSeparator(const ehs::Array<ehs::Char_8> &separators, const ehs::Char_8 *c)
{
for (ehs::UInt_64 s = 0; s < separators.Size(); ++s)
if (*c == separators[s])
return true;
return false;
}
bool Compiler::IsPrimitive(const ehs::Array<Primitive>& primitives, const ehs::Str_8& value)
{
for (ehs::UInt_64 i = 0; i < primitives.Size(); ++i)
if (value == primitives[i].GetName())
return true;
return false;
}
bool Compiler::IsKeyword(const ehs::Array<ehs::Str_8>& keywords, const ehs::Str_8& value)
{
for (ehs::UInt_64 i = 0; i < keywords.Size(); ++i)
if (value == keywords[i])
return true;
return false;
}
const Operator *Compiler::IsOperator(const ehs::Array<Operator>& operators, const ehs::Str_8& value)
{
for (ehs::UInt_64 i = 0; i < operators.Size(); ++i)
if (value == operators[i].GetDelimeter())
return &operators[i];
return nullptr;
}
Token Compiler::ParseValue(const ehs::Array<Primitive> &primitives, const ehs::Array<ehs::Str_8> &keywords, const ehs::Array<Operator> &operators, const ehs::Str_8& value) const
{
if (IsPrimitive(primitives, value))
return {TokenT::TYPE, value};
if (IsKeyword(keywords, value))
return {TokenT::KEYWORD, value};
const Operator *op = IsOperator(operators, value);
if (op)
{
if (op->IsUnary())
return {TokenT::UNARY_OPERATOR, value};
else
return {TokenT::COMPOUND_OPERATOR, value};
}
if (value[0] >= '0' && value[0] <= '9')
return {TokenT::VALUE, value};
return {TokenT::IDENTIFIER, value};
}
ehs::Vector<Token> Compiler::Parse(const ehs::Str_8 &code) const
{
ehs::Vector<Token> tokens;
const ehs::Array<ehs::Char_8> separators = language->GetSeparators();
const ehs::Array<Primitive> primitives = language->GetPrimitives();
const ehs::Array<ehs::Str_8> keywords = language->GetKeywords();
const ehs::Array<Operator> operators = language->GetOperators();
for (ehs::Char_8 *i = &code[0], *start = i; i < &code[code.Size()]; ++i)
{
if (*i == language->GetEOL())
{
if (start != i)
tokens.Push(ParseValue(primitives, keywords, operators, ehs::Str_8(start, i - start)));
tokens.Push({TokenT::EOL, {i, 1}});
start = i + 1;
continue;
}
if (*start == '\"')
{
if (i != start && *i == '\"')
{
tokens.Push({TokenT::VALUE, {start, (ehs::UInt_64)(i - (start - 1))}});
start = i + 1;
continue;
}
continue;
}
if (!IsSeparator(separators, i))
continue;
if (i - (start - 1) == 1 && IsSeparator(separators, start))
{
start = i + 1;
continue;
}
tokens.Push(ParseValue(primitives, keywords, operators, ehs::Str_8(start, i - start)));
start = i + 1;
}
return tokens;
}

View File

@@ -1,57 +1,26 @@
#include "arctyx/compiler/Instruction.h"
Instruction::~Instruction()
{
delete code;
}
Instruction::Instruction()
: arch(nullptr), id(0), size(0), code(nullptr)
: arch(nullptr), id(0), translation(nullptr)
{
}
Instruction::Instruction(ehs::Str_8 name, const ehs::UInt_8 &size, ehs::Byte *code)
: arch(nullptr), id(name.Hash_64()), name((ehs::Str_8 &&)name), size(size), code(code)
Instruction::Instruction(ehs::Str_8 name)
: arch(nullptr), id(name.Hash_64()), name((ehs::Str_8 &&)name), translation(nullptr)
{
}
Instruction::Instruction(ehs::Str_8 name, const ehs::UInt_64 &code)
: arch(nullptr), id(name.Hash_64()), name((ehs::Str_8 &&)name), size(sizeof(code)), code(new ehs::Byte[size])
{
*(ehs::UInt_64 *)this->code = code;
}
Instruction::Instruction(ehs::Str_8 name, const ehs::UInt_32 &code)
: arch(nullptr), id(name.Hash_64()), name((ehs::Str_8 &&)name), size(sizeof(code)), code(new ehs::Byte[size])
{
*(ehs::UInt_32 *)this->code = code;
}
Instruction::Instruction(ehs::Str_8 name, const ehs::UInt_16 &code)
: arch(nullptr), id(name.Hash_64()), name((ehs::Str_8 &&)name), size(sizeof(code)), code(new ehs::Byte[size])
{
*(ehs::UInt_16 *)this->code = code;
}
Instruction::Instruction(ehs::Str_8 name, const ehs::UInt_8 &code)
: arch(nullptr), id(name.Hash_64()), name((ehs::Str_8 &&)name), size(sizeof(code)), code(new ehs::Byte[size])
{
*this->code = code;
}
Instruction::Instruction(Instruction &&ins) noexcept
: arch(ins.arch), id(ins.id), name((ehs::Str_8 &&)ins.name), size(ins.size), code(ins.code)
: arch(ins.arch), id(ins.id), name((ehs::Str_8 &&)ins.name), translation(ins.translation)
{
ins.arch = nullptr;
ins.id = 0;
ins.size = 0;
ins.code = nullptr;
ins.translation = nullptr;
}
Instruction::Instruction(const Instruction &ins)
: arch(nullptr), id(ins.id), name(ins.name), size(ins.size), code(new ehs::Byte[size])
: arch(nullptr), id(ins.id), name(ins.name), translation(ins.translation)
{
ehs::Util::Copy(code, ins.code, size);
}
Instruction &Instruction::operator=(Instruction &&ins) noexcept
@@ -62,13 +31,11 @@ Instruction &Instruction::operator=(Instruction &&ins) noexcept
arch = ins.arch;
id = ins.id;
name = (ehs::Str_8 &&)ins.name;
size = ins.size;
code = ins.code;
translation = ins.translation;
ins.arch = nullptr;
ins.id = 0;
ins.size = 0;
ins.code = nullptr;
ins.translation = nullptr;
return *this;
}
@@ -81,10 +48,7 @@ Instruction &Instruction::operator=(const Instruction &ins)
arch = nullptr;
id = ins.id;
name = ins.name;
size = ins.size;
code = new ehs::Byte[size];
ehs::Util::Copy(code, ins.code, size);
translation = ins.translation;
return *this;
}
@@ -104,12 +68,15 @@ ehs::Str_8 Instruction::GetName() const
return name;
}
ehs::UInt_8 Instruction::GetSize() const
void Instruction::SetTranslation(TranslateIns newTranslation)
{
return size;
translation = newTranslation;
}
ehs::Byte *Instruction::GetCode() const
ehs::Serializer<ehs::UInt_64> Instruction::Translate(const ehs::Byte* data) const
{
return code;
if (!translation)
return {};
return translation(this, data);
}

View File

@@ -14,7 +14,8 @@ Language::Language(ehs::Str_8 name, const ehs::Version& version)
Language::Language(Language&& lang) noexcept
: id(lang.id), name((ehs::Str_8 &&)lang.name), version(lang.version), eol(lang.eol),
separators((ehs::Array<ehs::Char_8> &&)lang.separators),
separators((ehs::Array<ehs::Char_8> &&)lang.separators), primitives((ehs::Array<Primitive> &&)lang.primitives),
keywords((ehs::Array<ehs::Str_8> &&)lang.keywords), operators((ehs::Array<Operator> &&)lang.operators),
interpretations((ehs::Array<Interpretation> &&)lang.interpretations)
{
lang.id = 0;
@@ -24,6 +25,7 @@ Language::Language(Language&& lang) noexcept
Language::Language(const Language& lang)
: id(lang.id), name(lang.name), version(lang.version), eol(lang.eol), separators(lang.separators),
primitives(lang.primitives), keywords(lang.keywords), operators(lang.operators),
interpretations(lang.interpretations)
{
}
@@ -38,6 +40,9 @@ Language& Language::operator=(Language&& lang) noexcept
version = lang.version;
eol = lang.eol;
separators = (ehs::Array<ehs::Char_8> &&)lang.separators;
primitives = (ehs::Array<Primitive> &&)lang.primitives;
keywords = (ehs::Array<ehs::Str_8> &&)lang.keywords;
operators = (ehs::Array<Operator> &&)lang.operators;
interpretations = (ehs::Array<Interpretation> &&)lang.interpretations;
lang.id = 0;
@@ -57,6 +62,9 @@ Language& Language::operator=(const Language& lang)
version = lang.version;
eol = lang.eol;
separators = lang.separators;
primitives = lang.primitives;
keywords = lang.keywords;
operators = lang.operators;
interpretations = lang.interpretations;
return *this;
@@ -111,6 +119,106 @@ bool Language::AddSeparator(const ehs::Char_8& separator)
return true;
}
ehs::Array<Primitive> Language::GetPrimitives() const
{
return primitives;
}
bool Language::HasPrimitive(const ehs::UInt_64 &id) const
{
for (ehs::Size i = 0; i < primitives.Size(); ++i)
if (primitives[i].GetId() == id)
return true;
return false;
}
bool Language::HasPrimitive(const ehs::Str_8 &name) const
{
return HasPrimitive(name.Hash_64());
}
const Primitive *Language::GetPrimitive(const ehs::UInt_64 &id) const
{
for (ehs::Size i = 0; i < primitives.Size(); ++i)
if (primitives[i].GetId() == id)
return &primitives[i];
return nullptr;
}
const Primitive* Language::GetPrimitive(const ehs::Str_8 &name) const
{
return GetPrimitive(name.Hash_64());
}
bool Language::AddPrimitive(Primitive primitive)
{
if (HasPrimitive(primitive.GetId()))
return false;
primitives.Push((Primitive &&)primitive);
return true;
}
ehs::Array<ehs::Str_8> Language::GetKeywords() const
{
return keywords;
}
bool Language::HasKeyword(const ehs::Str_8& keyword) const
{
for (ehs::UInt_64 i = 0; i < keywords.Size(); ++i)
if (keywords[i] == keyword)
return true;
return false;
}
bool Language::AddKeyword(const ehs::Str_8& keyword)
{
if (HasKeyword(keyword))
return false;
keywords.Push((ehs::Str_8 &&)keyword);
return true;
}
ehs::Array<Operator> Language::GetOperators() const
{
return operators;
}
bool Language::HasOperator(const ehs::Str_8 &delimeter) const
{
for (ehs::UInt_64 i = 0; i < operators.Size(); ++i)
if (operators[i].GetDelimeter() == delimeter)
return true;
return false;
}
const Operator* Language::GetOperator(const ehs::Str_8 &delimeter) const
{
for (ehs::UInt_64 i = 0; i < operators.Size(); ++i)
if (operators[i].GetDelimeter() == delimeter)
return &operators[i];
return nullptr;
}
bool Language::AddOperator(Operator primitive)
{
if (HasOperator(primitive.GetDelimeter()))
return false;
operators.Push((Operator &&)primitive);
return true;
}
ehs::Array<Interpretation> Language::GetInterpretations() const
{
return interpretations;

75
src/compiler/Operator.cpp Normal file
View File

@@ -0,0 +1,75 @@
#include "arctyx/compiler/Operator.h"
Operator::Operator()
: instructionId(0)
{
}
Operator::Operator(ehs::Str_8 delimeter, ehs::Str_8 instructionName)
: unary(delimeter.Size() == 1), delimeter((ehs::Str_8 &&)delimeter), instructionId(instructionName.Hash_64()),
instructionName((ehs::Str_8 &&)instructionName)
{
}
Operator::Operator(Operator&& other) noexcept
: unary(other.unary), delimeter((ehs::Str_8 &&)other.delimeter), instructionId(other.instructionId),
instructionName((ehs::Str_8 &&)other.instructionName)
{
other.unary = false;
other.instructionId = 0;
}
Operator::Operator(const Operator& other)
: unary(other.unary), delimeter(other.delimeter), instructionId(other.instructionId),
instructionName(other.instructionName)
{
}
Operator& Operator::operator=(Operator&& other) noexcept
{
if (this == &other)
return *this;
unary = other.unary;
delimeter = (ehs::Str_8 &&)other.delimeter;
instructionId = other.instructionId;
instructionName = (ehs::Str_8 &&)other.instructionName;
other.unary = false;
other.instructionId = 0;
return *this;
}
Operator& Operator::operator=(const Operator& other)
{
if (this == &other)
return *this;
unary = other.unary;
delimeter = other.delimeter;
instructionId = other.instructionId;
instructionName = other.instructionName;
return *this;
}
bool Operator::IsUnary() const
{
return unary;
}
ehs::Str_8 Operator::GetDelimeter() const
{
return delimeter;
}
ehs::UInt_64 Operator::GetInstructionId() const
{
return instructionId;
}
ehs::Str_8 Operator::GetInstructionName() const
{
return instructionName;
}

View File

@@ -0,0 +1,74 @@
#include "arctyx/compiler/Primitive.h"
Primitive::Primitive()
: id(0), byteDepth(0), signedness(Signedness::UNSIGNED)
{
}
Primitive::Primitive(ehs::Str_8 name, const ehs::UInt_8& byteDepth, const Signedness &signedness)
: id(name.Hash_64()), name((ehs::Str_8 &&)name), byteDepth(byteDepth), signedness(signedness)
{
}
Primitive::Primitive(Primitive&& other) noexcept
: id(other.id), name((ehs::Str_8 &&)other.name), byteDepth(other.byteDepth), signedness(other.signedness)
{
other.id = 0;
other.byteDepth = 0;
other.signedness = Signedness::UNSIGNED;
}
Primitive::Primitive(const Primitive& other)
: id(other.id), name(other.name), byteDepth(other.byteDepth), signedness(other.signedness)
{
}
Primitive& Primitive::operator=(Primitive&& other) noexcept
{
if (this == &other)
return *this;
id = other.id;
name = (ehs::Str_8 &&)other.name;
byteDepth = other.byteDepth;
signedness = other.signedness;
other.id = 0;
other.byteDepth = 0;
other.signedness = Signedness::UNSIGNED;
return *this;
}
Primitive& Primitive::operator=(const Primitive& other)
{
if (this == &other)
return *this;
id = other.id;
name = other.name;
byteDepth = other.byteDepth;
signedness = other.signedness;
return *this;
}
ehs::UInt_64 Primitive::GetId() const
{
return id;
}
ehs::Str_8 Primitive::GetName() const
{
return name;
}
ehs::UInt_8 Primitive::GetByteDepth() const
{
return byteDepth;
}
Signedness Primitive::GetSignedness() const
{
return signedness;
}

94
src/compiler/Register.cpp Normal file
View File

@@ -0,0 +1,94 @@
#include "arctyx/compiler/Register.h"
Register::Register()
: id(0), byteDepth(0)
{
}
Register::Register(ehs::Str_8 name, const ehs::UInt_64 &byteDepth, const ehs::UInt_64 &code)
: id(name.Hash_64()), name((ehs::Str_8 &&)name), byteDepth(byteDepth), code(code)
{
}
Register::Register(Register&& other) noexcept
: id(other.id), name((ehs::Str_8 &&)other.name), byteDepth(other.byteDepth), code(other.code)
{
other.id = 0;
other.byteDepth = 0;
other.code = 0;
}
Register::Register(const Register& other)
: id(other.id), name(other.name), byteDepth(other.byteDepth), code(other.code)
{
}
Register& Register::operator=(Register&& other) noexcept
{
if (this == &other)
return *this;
id = other.id;
name = (ehs::Str_8 &&)other.name;
byteDepth = other.byteDepth;
code = other.code;
other.id = 0;
other.byteDepth = 0;
other.code = 0;
return *this;
}
Register& Register::operator=(const Register& other)
{
if (this == &other)
return *this;
id = other.id;
name = other.name;
byteDepth = other.byteDepth;
code = other.code;
return *this;
}
bool Register::operator==(const ehs::Str_8& otherName) const
{
return name == otherName;
}
bool Register::operator!=(const ehs::Str_8& otherName) const
{
return name != otherName;
}
bool Register::operator==(const ehs::UInt_64& otherId) const
{
return id == otherId;
}
bool Register::operator!=(const ehs::UInt_64& otherId) const
{
return id != otherId;
}
ehs::UInt_64 Register::GetId() const
{
return id;
}
ehs::Str_8 Register::GetName() const
{
return name;
}
ehs::UInt_8 Register::GetByteDepth() const
{
return byteDepth;
}
ehs::UInt_64 Register::GetCode() const
{
return code;
}