Huge update, backup commit.

This commit is contained in:
2025-07-27 23:50:41 -07:00
parent a0cd2e00b8
commit c4011152b6
21 changed files with 775 additions and 200 deletions

View File

@@ -1,6 +1,5 @@
#include "arctyx/compiler/Compiler.h"
#include <iterator>
#include <ehs/io/Console.h>
Compiler::Compiler()
@@ -8,7 +7,7 @@ Compiler::Compiler()
{
}
Compiler::Compiler(const ehs::Str_8& arch, const ehs::Str_8& lang)
Compiler::Compiler(const ehs::Str_8& arch, const ehs::Str_8& lang, ehs::Str_8 entryPoint)
{
architecture = Architecture::Get(arch);
if (!architecture)
@@ -24,18 +23,24 @@ Compiler::Compiler(const ehs::Str_8& arch, const ehs::Str_8& lang)
return;
}
entryPointId = entryPoint.Hash_64();
entryPointName = (ehs::Str_8 &&)entryPoint;
EHS_LOG_SUCCESS();
}
Compiler::Compiler(Compiler&& other) noexcept
: architecture(other.architecture), language(other.language), symbols((ehs::Array<Symbol> &&)other.symbols)
: architecture(other.architecture), language(other.language), entryPointId(other.entryPointId),
entryPointName((ehs::Str_8 &&)other.entryPointName), symbols((ehs::Array<Symbol> &&)other.symbols)
{
other.architecture = nullptr;
other.language = nullptr;
other.entryPointId = 0;
}
Compiler::Compiler(const Compiler& other)
: architecture(other.architecture), language(other.language), symbols(other.symbols)
: architecture(other.architecture), language(other.language), entryPointId(other.entryPointId),
entryPointName(other.entryPointName), symbols(other.symbols)
{
}
@@ -46,10 +51,13 @@ Compiler& Compiler::operator=(Compiler&& other) noexcept
architecture = other.architecture;
language = other.language;
entryPointId = other.entryPointId;
entryPointName = (ehs::Str_8 &&)other.entryPointName;
symbols = (ehs::Array<Symbol> &&)other.symbols;
other.architecture = nullptr;
other.language = nullptr;
other.entryPointId = 0;
return *this;
}
@@ -61,24 +69,66 @@ Compiler& Compiler::operator=(const Compiler& other)
architecture = other.architecture;
language = other.language;
entryPointId = other.entryPointId;
entryPointName = other.entryPointName;
symbols = other.symbols;
return *this;
}
ehs::Array<ehs::Byte> Compiler::Compile(const ehs::Str_8 &code) const
bool Compiler::HasSymbol(const ehs::UInt_64 &id) const
{
for (ehs::UInt_64 i = 0; i < symbols.Size(); ++i)
if (symbols[i].GetId() == id)
return true;
return false;
}
bool Compiler::HasSymbol(const ehs::Str_8 &name) const
{
return HasSymbol(name.Hash_64());
}
bool Compiler::AddSymbol(Symbol symbol)
{
if (HasSymbol(symbol.GetId()))
return false;
symbols.Push((Symbol &&)symbol);
return true;
}
Symbol *Compiler::GetSymbol(const ehs::UInt_64 &id) const
{
for (ehs::UInt_64 i = 0; i < symbols.Size(); ++i)
if (symbols[i].GetId() == id)
return &symbols[i];
return nullptr;
}
Symbol *Compiler::GetSymbol(const ehs::Str_8 &name) const
{
return GetSymbol(name.Hash_64());
}
ehs::Vector<ehs::Byte> Compiler::Compile(const ehs::Str_8 &code)
{
ehs::Console::Write_8("Code:");
ehs::Console::Write_8(code);
ehs::Console::Write_8("Tokens:");
ehs::Vector<Token> tokens = Parse(code);
ehs::Vector<Token> tokens = language->CompileIntoTokens(this, code);
for (ehs::UInt_64 i = 0; i < tokens.Size(); ++i)
{
if (tokens[i].GetType() == TokenT::UNKNOWN)
ehs::Console::Write_8("UNKNOWN, ", false);
else if (tokens[i].GetType() == TokenT::SEPARATOR)
ehs::Console::Write_8("SEPARATOR, ", false);
else if (tokens[i].GetType() == TokenT::BOOLEAN)
ehs::Console::Write_8("BOOLEAN, ", false);
else if (tokens[i].GetType() == TokenT::NUMBER)
ehs::Console::Write_8("NUMBER, ", false);
else if (tokens[i].GetType() == TokenT::STRING)
@@ -95,144 +145,36 @@ ehs::Array<ehs::Byte> Compiler::Compile(const ehs::Str_8 &code) const
ehs::Console::Write_8("UNARY_OPERATOR, ", false);
else if (tokens[i].GetType() == TokenT::COMPOUND_OPERATOR)
ehs::Console::Write_8("COMPOUND_OPERATOR, ", false);
else if (tokens[i].GetType() == TokenT::ENCAPSULATOR)
ehs::Console::Write_8("ENCAPSULATOR, ", false);
else if (tokens[i].GetType() == TokenT::EOL)
ehs::Console::Write_8("EOL");
}
ehs::Array<ehs::Byte> machineCode;
ehs::Vector<ehs::Byte> machineCode = language->CompileIntoMachineCode(this, tokens);
return machineCode;
}
bool Compiler::IsEOL(const ehs::Array<ehs::Char_8>& eols, const ehs::Char_8* c)
{
for (ehs::UInt_64 s = 0; s < eols.Size(); ++s)
if (*c == eols[s])
return true;
return false;
}
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;
}
ehs::Vector<Token> Compiler::Parse(const ehs::Str_8 &code) const
{
ehs::Vector<Token> tokens;
const ehs::Array<ehs::Char_8> eols = language->GetEOLs();
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)
ehs::Console::Write_8("\nSymbols:");
for (ehs::UInt_64 i = 0; i < symbols.Size(); ++i)
{
if (*start == '\"')
{
if (*i == '\"')
{
++start;
if (symbols[i].GetType() == SymbolType::VARIABLE)
ehs::Console::Write_8("Variable ", false);
else if (symbols[i].GetType() == SymbolType::FUNCTION)
ehs::Console::Write_8("Function ", false);
else if (symbols[i].GetType() == SymbolType::CLASS)
ehs::Console::Write_8("Class ", false);
else if (symbols[i].GetType() == SymbolType::MEMBER)
ehs::Console::Write_8("Member ", false);
else if (symbols[i].GetType() == SymbolType::METHOD)
ehs::Console::Write_8("Method ", false);
else
ehs::Console::Write_8("Unknown ", false);
tokens.Push({TokenT::STRING, ehs::Str_8(start, i - 1 - start)});
if (i + 1 < &code[code.Size()])
start = i + 1;
}
}
else if (*start == '\'')
{
if (*i == '\'')
{
++start;
const ehs::UInt_64 size = i - 1 - start;
if (size > 1)
{
EHS_LOG(ehs::LogType::ERR, 0, "Characters cannot extend to more than one.");
return {};
}
tokens.Push({TokenT::CHARACTER, {start, size}});
if (i + 1 < &code[code.Size()])
start = i + 1;
}
}
else if (const bool isEOL = IsEOL(eols, i), isSep = IsSeparator(separators, i); *i == ' ' || *i == '\t' || isEOL || isSep)
{
if ((*start == ' ' || *start == '\t') && i + 1 < &code[code.Size()])
{
start = i + 1;
continue;
}
const ehs::Str_8 value(start, i - start);
if (value.Size())
{
if (IsPrimitive(primitives, value))
tokens.Push({TokenT::TYPE, value});
else if (IsKeyword(keywords, value))
tokens.Push({TokenT::KEYWORD, value});
else if (const Operator *op = IsOperator(operators, value); op)
{
if (op->IsUnary())
tokens.Push({TokenT::UNARY_OPERATOR, value});
else
tokens.Push({TokenT::COMPOUND_OPERATOR, value});
}
else if (value[0] >= '0' && value[0] <= '9')
tokens.Push({TokenT::NUMBER, value});
else
tokens.Push({TokenT::IDENTIFIER, value});
}
if (isEOL)
tokens.Push({TokenT::EOL, {i, 1}});
else if (isSep)
tokens.Push({TokenT::SEPARATOR, {i, 1}});
start = i + 1;
}
ehs::Console::Write_8(symbols[i].GetName());
}
EHS_LOG_SUCCESS();
Symbol *entryPoint = GetSymbol(entryPointId);
if (!entryPoint || entryPoint->GetType() != SymbolType::FUNCTION)
ehs::Console::Write_8("\nCompiler Error: Function symbol, \"" + entryPointName + "\", for the entry point was not found!");
return tokens;
}
return machineCode;
}

View File

@@ -3,29 +3,32 @@
ehs::Array<const Language *> Language::languages;
Language::Language()
: id(0)
: id(0), compileIntoTokensCb(nullptr), compileIntoMachineCodeCb(nullptr)
{
}
Language::Language(ehs::Str_8 name, const ehs::Version& version)
: id(name.Hash_64()), name((ehs::Str_8 &&)name), version(version)
: id(name.Hash_64()), name((ehs::Str_8 &&)name), version(version), compileIntoTokensCb(nullptr),
compileIntoMachineCodeCb(nullptr)
{
}
Language::Language(Language&& lang) noexcept
: id(lang.id), name((ehs::Str_8 &&)lang.name), version(lang.version), eols((ehs::Array<ehs::Char_8> &&)lang.eols),
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)
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),
compileIntoTokensCb(lang.compileIntoTokensCb), compileIntoMachineCodeCb(lang.compileIntoMachineCodeCb)
{
lang.id = 0;
lang.version = {};
lang.compileIntoTokensCb = nullptr;
lang.compileIntoMachineCodeCb = nullptr;
}
Language::Language(const Language& lang)
: id(lang.id), name(lang.name), version(lang.version), eols(lang.eols), separators(lang.separators),
primitives(lang.primitives), keywords(lang.keywords), operators(lang.operators),
interpretations(lang.interpretations)
: id(lang.id), name(lang.name), version(lang.version), eols(lang.eols), primitives(lang.primitives),
keywords(lang.keywords), operators(lang.operators), interpretations(lang.interpretations),
compileIntoTokensCb(lang.compileIntoTokensCb), compileIntoMachineCodeCb(lang.compileIntoMachineCodeCb)
{
}
@@ -38,14 +41,17 @@ Language& Language::operator=(Language&& lang) noexcept
name = (ehs::Str_8 &&)lang.name;
version = lang.version;
eols = (ehs::Array<ehs::Char_8> &&)lang.eols;
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;
compileIntoTokensCb = lang.compileIntoTokensCb;
compileIntoMachineCodeCb = lang.compileIntoMachineCodeCb;
lang.id = 0;
lang.version = {};
lang.compileIntoTokensCb = nullptr;
lang.compileIntoMachineCodeCb = nullptr;
return *this;
}
@@ -59,11 +65,12 @@ Language& Language::operator=(const Language& lang)
name = lang.name;
version = lang.version;
eols = lang.eols;
separators = lang.separators;
primitives = lang.primitives;
keywords = lang.keywords;
operators = lang.operators;
interpretations = lang.interpretations;
compileIntoTokensCb = lang.compileIntoTokensCb;
compileIntoMachineCodeCb = lang.compileIntoMachineCodeCb;
return *this;
}
@@ -107,30 +114,6 @@ bool Language::AddEOL(const ehs::Char_8 &eol)
return true;
}
ehs::Array<ehs::Char_8> Language::GetSeparators() const
{
return separators;
}
bool Language::HasSeparator(const ehs::Char_8& separator) const
{
for (ehs::Size i = 0; i < separators.Size(); ++i)
if (separators[i] == separator)
return true;
return false;
}
bool Language::AddSeparator(const ehs::Char_8& separator)
{
if (HasSeparator(separator))
return false;
separators.Push(separator);
return true;
}
ehs::Array<Primitive> Language::GetPrimitives() const
{
return primitives;
@@ -264,6 +247,44 @@ bool Language::AddInterpretation(Interpretation interpretation)
return true;
}
void Language::SetCompileIntoTokensCb(const CompileIntoTokensCb cb)
{
compileIntoTokensCb = cb;
}
ehs::Vector<Token> Language::CompileIntoTokens(Compiler *compiler, const ehs::Str_8& code) const
{
if (compileIntoTokensCb)
{
EHS_LOG_SUCCESS();
return compileIntoTokensCb(compiler, this, code);
}
EHS_LOG(ehs::LogType::ERR, 0, "The " + name + " language is incomplete and does not implement a " + name + " to token compiler.");
return {};
}
void Language::SetCompileIntoMachineCodeCb(const CompileIntoMachineCodeCb cb)
{
compileIntoMachineCodeCb = cb;
}
ehs::Vector<ehs::Byte> Language::CompileIntoMachineCode(Compiler *compiler, const ehs::Vector<Token>& tokens) const
{
if (compileIntoTokensCb)
{
EHS_LOG_SUCCESS();
return compileIntoMachineCodeCb(compiler, this, tokens);
}
EHS_LOG(ehs::LogType::ERR, 0, "The " + name + " language is incomplete and does not implement a token to machine code compiler.");
return {};
}
bool Language::Has(const ehs::UInt_64& id)
{
for (ehs::UInt_64 i = 0; i < languages.Size(); ++i)
@@ -300,4 +321,4 @@ bool Language::Add(Language lang)
languages.Push(new Language((Language &&)lang));
return true;
}
}

49
src/compiler/Stack.cpp Normal file
View File

@@ -0,0 +1,49 @@
#include "arctyx/compiler/Stack.h"
Stack::Stack()
{
}
Stack::Stack(const ehs::UInt_64& offset)
{
}
Stack::Stack(Stack&& other) noexcept
{
}
Stack::Stack(const Stack& other)
{
}
Stack& Stack::operator=(Stack&& other) noexcept
{
}
Stack& Stack::operator=(const Stack& other)
{
}
ehs::UInt_64 Stack::GetOffset() const
{
}
bool Stack::HasItem(const ehs::UInt_64& id) const
{
}
bool Stack::HasItem(const ehs::Str_8& name) const
{
}
bool Stack::AddItem(StackItem item)
{
}
StackItem* Stack::GetItem(const ehs::UInt_64& id) const
{
}
StackItem* Stack::GetItem(const ehs::Str_8& name) const
{
}

View File

@@ -0,0 +1,74 @@
#include "arctyx/compiler/StackItem.h"
StackItem::StackItem()
: id(0), address(0), size(0)
{
}
StackItem::StackItem(ehs::Str_8 name, const ehs::UInt_64& address, const ehs::UInt_64 &size)
: id(name.Hash_64()), name((ehs::Str_8 &&)name), address(address), size(size)
{
}
StackItem::StackItem(StackItem&& other) noexcept
: id(other.id), name((ehs::Str_8 &&)other.name), address(other.address), size(other.size)
{
other.id = 0;
other.address = 0;
other.size = 0;
}
StackItem::StackItem(const StackItem &other)
: id(other.id), name(other.name), address(other.address), size(other.size)
{
}
StackItem &StackItem::operator=(StackItem &&other) noexcept
{
if (this == &other)
return *this;
name = (ehs::Str_8 &&)other.name;
id = other.id;
address = other.address;
size = other.size;
other.id = 0;
other.address = 0;
other.size = 0;
return *this;
}
StackItem &StackItem::operator=(const StackItem& other)
{
if (this == &other)
return *this;
name = other.name;
id = other.id;
address = other.address;
size = other.size;
return *this;
}
ehs::Str_8 StackItem::GetName() const
{
return name;
}
ehs::UInt_64 StackItem::GetId() const
{
return id;
}
ehs::UInt_64 StackItem::GetAddress() const
{
return address;
}
ehs::UInt_64 StackItem::GetSize() const
{
return size;
}

View File

@@ -0,0 +1 @@
#include "arctyx/compiler/StackParam.h"

View File

@@ -1,22 +1,26 @@
#include "arctyx/compiler/Symbol.h"
Symbol::Symbol()
: type(SymbolType::UNKNOWN), id(0), address(0)
: type(SymbolType::UNKNOWN), id(0), address(0), size(0)
{
}
Symbol::Symbol(const SymbolType& type, ehs::Str_8 name, const ehs::UInt_64& address)
: type(type), id(name.Hash_64()), name((ehs::Str_8 &&)name), address(address)
Symbol::Symbol(const SymbolType& type, ehs::Str_8 name, const ehs::UInt_64& address, const ehs::UInt_64 &size)
: type(type), id(name.Hash_64()), name((ehs::Str_8 &&)name), address(address), size(size)
{
}
Symbol::Symbol(Symbol&& other) noexcept
: type(other.type), id(other.id), name((ehs::Str_8 &&)other.name), address(other.address)
: type(other.type), id(other.id), name((ehs::Str_8 &&)other.name), address(other.address), size(other.size)
{
other.type = SymbolType::UNKNOWN;
other.id = 0;
other.address = 0;
other.size = 0;
}
Symbol::Symbol(const Symbol &other)
: type(other.type), id(other.id), name(other.name), address(other.address)
: type(other.type), id(other.id), name(other.name), address(other.address), size(other.size)
{
}
@@ -29,10 +33,12 @@ Symbol &Symbol::operator=(Symbol &&other) noexcept
name = (ehs::Str_8 &&)other.name;
id = other.id;
address = other.address;
size = other.size;
other.type = SymbolType::UNKNOWN;
other.id = 0;
other.address = 0;
other.size = 0;
return *this;
}
@@ -46,6 +52,7 @@ Symbol &Symbol::operator=(const Symbol& other)
name = other.name;
id = other.id;
address = other.address;
size = other.size;
return *this;
}
@@ -69,3 +76,8 @@ ehs::UInt_64 Symbol::GetAddress() const
{
return address;
}
ehs::UInt_64 Symbol::GetSize() const
{
return size;
}

View File

@@ -45,6 +45,11 @@ Token & Token::operator=(const Token &token)
return *this;
}
void Token::SetType(const TokenT& newType)
{
type = newType;
}
TokenT Token::GetType() const
{
return type;