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

@@ -2,6 +2,7 @@
#include <ehs/Array.h>
#include "Register.h"
#include "Instruction.h"
class EHS_LIB_IO Architecture
@@ -11,6 +12,7 @@ private:
ehs::UInt_64 id;
ehs::Str_8 name;
ehs::Array<Register> registers;
ehs::Array<Instruction> instructions;
static ehs::Array<const Architecture *> architectures;
@@ -32,6 +34,16 @@ public:
ehs::Str_8 GetName() const;
bool HasRegister(const ehs::UInt_64 &id) const;
bool HasRegister(const ehs::Str_8 &name) const;
const Register *GetRegister(const ehs::UInt_64 &id) const;
const Register *GetRegister(const ehs::Str_8 &name) const;
bool AddRegister(Register reg);
bool HasInstruction(const ehs::UInt_64 &id) const;
bool HasInstruction(const ehs::Str_8 &name) const;
@@ -51,4 +63,4 @@ public:
static const Architecture *Get(const ehs::Str_8 &name);
static bool Add(Architecture arc);
};
};

View File

@@ -17,5 +17,28 @@ private:
public:
Compiler();
Compiler(const Architecture *arch, const Language *lang);
Compiler(const ehs::Str_8 &arch, const ehs::Str_8 &lang);
Compiler(Compiler &&other) noexcept;
Compiler(const Compiler &other);
Compiler &operator=(Compiler &&other) noexcept;
Compiler &operator=(const Compiler &other);
ehs::Array<ehs::Byte> Compile(const ehs::Str_8 &code) const;
private:
static bool IsSeparator(const ehs::Array<ehs::Char_8> &separators, const ehs::Char_8 *c);
static bool IsPrimitive(const ehs::Array<Primitive> &primitives, const ehs::Str_8 &value);
static bool IsKeyword(const ehs::Array<ehs::Str_8> &keywords, const ehs::Str_8 &value);
static const Operator *IsOperator(const ehs::Array<Operator> &operators, const ehs::Str_8 &value);
Token ParseValue(const ehs::Array<Primitive> &primitives, const ehs::Array<ehs::Str_8> &keywords, const ehs::Array<Operator> &operators, const ehs::Str_8 &value) const;
ehs::Vector<Token> Parse(const ehs::Str_8 &code) const;
};

View File

@@ -1,9 +1,18 @@
#pragma once
#include <ehs/Array.h>
#include <ehs/Serializer.h>
#include <ehs/Str.h>
#include <ehs/Types.h>
#include "Token.h"
class Instruction;
class Architecture;
class Compiler;
class Language;
typedef ehs::Serializer<ehs::UInt_64> (*TranslateIns)(const Instruction *, const ehs::Byte *);
class EHS_LIB_IO Instruction
{
@@ -13,23 +22,12 @@ private:
Architecture *arch;
ehs::UInt_64 id;
ehs::Str_8 name;
ehs::UInt_8 size;
ehs::Byte *code;
TranslateIns translation;
public:
~Instruction();
Instruction();
Instruction(ehs::Str_8 name, const ehs::UInt_8 &size, ehs::Byte *code);
Instruction(ehs::Str_8 name, const ehs::UInt_64 &code);
Instruction(ehs::Str_8 name, const ehs::UInt_32 &code);
Instruction(ehs::Str_8 name, const ehs::UInt_16 &code);
Instruction(ehs::Str_8 name, const ehs::UInt_8 &code);
Instruction(ehs::Str_8 name);
Instruction(Instruction &&ins) noexcept;
@@ -45,7 +43,32 @@ public:
ehs::Str_8 GetName() const;
ehs::UInt_8 GetSize() const;
void SetTranslation(TranslateIns newTranslation);
ehs::Byte *GetCode() const;
ehs::Serializer<ehs::UInt_64> Translate(const ehs::Byte *data) const;
};
struct AssignBase
{
const ehs::UInt_8 type = 0;
};
struct AssignRR
{
AssignBase base = {
1
};
bool isDstAddress = false;
ehs::UInt_64 dstReg = 0;
bool isSrcAddress = false;
ehs::UInt_64 srcReg = 0;
};
struct AssignRI
{
AssignBase base = {
2
};
ehs::UInt_64 dstReg = 0;
ehs::UInt_64 srcImm = 0;
};

View File

@@ -6,6 +6,8 @@
#include <ehs/Version.h>
#include "Interpretation.h"
#include "Operator.h"
#include "Primitive.h"
class EHS_LIB_IO Language
{
@@ -17,6 +19,9 @@ private:
ehs::Version version;
ehs::Char_8 eol;
ehs::Array<ehs::Char_8> separators;
ehs::Array<Primitive> primitives;
ehs::Array<ehs::Str_8> keywords;
ehs::Array<Operator> operators;
ehs::Array<Interpretation> interpretations;
static ehs::Array<const Language *> languages;
@@ -50,6 +55,33 @@ public:
bool AddSeparator(const ehs::Char_8 &separator);
ehs::Array<Primitive> GetPrimitives() const;
bool HasPrimitive(const ehs::UInt_64 &id) const;
bool HasPrimitive(const ehs::Str_8 &name) const;
const Primitive *GetPrimitive(const ehs::UInt_64 &id) const;
const Primitive *GetPrimitive(const ehs::Str_8 &name) const;
bool AddPrimitive(Primitive primitive);
ehs::Array<ehs::Str_8> GetKeywords() const;
bool HasKeyword(const ehs::Str_8 &keyword) const;
bool AddKeyword(const ehs::Str_8 &keyword);
ehs::Array<Operator> GetOperators() const;
bool HasOperator(const ehs::Str_8 &delimeter) const;
const Operator *GetOperator(const ehs::Str_8 &delimeter) const;
bool AddOperator(Operator primitive);
ehs::Array<Interpretation> GetInterpretations() const;
bool HasInterpretation(const TokenT &type, const ehs::Str_8 &name) const;

View File

@@ -0,0 +1,33 @@
#pragma once
#include <ehs/Str.h>
#include <ehs/Types.h>
class Operator
{
private:
bool unary;
ehs::Str_8 delimeter;
ehs::UInt_64 instructionId;
ehs::Str_8 instructionName;
public:
Operator();
Operator(ehs::Str_8 delimeter, ehs::Str_8 instructionName);
Operator(Operator &&other) noexcept;
Operator(const Operator &other);
Operator &operator=(Operator &&other) noexcept;
Operator &operator=(const Operator &other);
bool IsUnary() const;
ehs::Str_8 GetDelimeter() const;
ehs::UInt_64 GetInstructionId() const;
ehs::Str_8 GetInstructionName() const;
};

View File

@@ -0,0 +1,40 @@
#pragma once
#include <cstdint>
#include <ehs/Str.h>
enum class Signedness : ehs::UInt_8
{
UNSIGNED,
SIGNED
};
class Primitive
{
private:
ehs::UInt_64 id;
ehs::Str_8 name;
ehs::UInt_8 byteDepth;
Signedness signedness;
public:
Primitive();
Primitive(ehs::Str_8 name, const ehs::UInt_8 &byteDepth, const Signedness &signedness);
Primitive(Primitive &&other) noexcept;
Primitive(const Primitive &other);
Primitive &operator=(Primitive &&other) noexcept;
Primitive &operator=(const Primitive &other);
ehs::UInt_64 GetId() const;
ehs::Str_8 GetName() const;
ehs::UInt_8 GetByteDepth() const;
Signedness GetSignedness() const;
};

View File

@@ -0,0 +1,41 @@
#pragma once
#include <ehs/Str.h>
class Register
{
private:
ehs::UInt_64 id;
ehs::Str_8 name;
ehs::UInt_32 byteDepth;
ehs::UInt_64 code;;
public:
Register();
Register(ehs::Str_8 name, const ehs::UInt_64 &byteDepth, const ehs::UInt_64 &code);
Register(Register &&other) noexcept;
Register(const Register &other);
Register &operator=(Register &&other) noexcept;
Register &operator=(const Register &other);
bool operator==(const ehs::Str_8 &otherName) const;
bool operator!=(const ehs::Str_8 &otherName) const;
bool operator==(const ehs::UInt_64 &otherId) const;
bool operator!=(const ehs::UInt_64 &otherId) const;
ehs::UInt_64 GetId() const;
ehs::Str_8 GetName() const;
ehs::UInt_8 GetByteDepth() const;
ehs::UInt_64 GetCode() const;
};

View File

@@ -7,9 +7,11 @@ enum class TokenT : ehs::UInt_8
UNKNOWN,
VALUE,
KEYWORD,
TYPE,
IDENTIFIER,
UNARY_OPERATOR,
COMPOUND_OPERATOR
COMPOUND_OPERATOR,
EOL
};
class EHS_LIB_IO Token