Format with clang

This commit is contained in:
2021-11-09 19:41:10 +01:00
parent ee016636f7
commit 61906b3c80
38 changed files with 2277 additions and 2364 deletions

View File

@@ -2,24 +2,19 @@
#include <string>
#include <token/token.hpp>
#include <unordered_map>
#include <vector>
#include <utility>
#include <vector>
namespace Compile
{
class Compiler
{
private:
std::unordered_map<std::string, std::size_t> jumpLabelLocations;
std::vector<std::pair<Token::Token, std::size_t>> unresolvedJumpLabels;
class Compiler {
private:
std::unordered_map<std::string, std::size_t> jumpLabelLocations;
std::vector<std::pair<Token::Token, std::size_t>> unresolvedJumpLabels;
void InsertAsBytes(
Token::Token const & token,
std::vector<std::uint8_t> & bytes);
void InsertAsBytes(Token::Token const & token, std::vector<std::uint8_t> & bytes);
public:
bool Compile(
std::vector<Token::Token> const & tokens,
std::vector<std::uint8_t> & bytes);
};
public:
bool Compile(std::vector<Token::Token> const & tokens, std::vector<std::uint8_t> & bytes);
};
}

View File

@@ -4,25 +4,24 @@
namespace Compile
{
class CompilationError
{
public:
Token::Token errorToken;
class CompilationError {
public:
Token::Token errorToken;
CompilationError(std::string const & message, Token::Token const & token);
CompilationError(std::string const & message, Token::Token const & token);
static CompilationError CreateExpectedArgumentError(Token::Token const & token);
static CompilationError CreateExpectedLabelError(Token::Token const & token);
static CompilationError CreateExpectedImmediateError(Token::Token const & token);
static CompilationError CreateExpectedImmediateOrRegisterOrMemory(Token::Token const & token);
static CompilationError CreateExpectedRegisterError(Token::Token const & token);
static CompilationError CreateExpectedRegisterOrMemoryError(Token::Token const & token);
static CompilationError CreateExpectedArgumentError(Token::Token const & token);
static CompilationError CreateExpectedLabelError(Token::Token const & token);
static CompilationError CreateExpectedImmediateError(Token::Token const & token);
static CompilationError CreateExpectedImmediateOrRegisterOrMemory(Token::Token const & token);
static CompilationError CreateExpectedRegisterError(Token::Token const & token);
static CompilationError CreateExpectedRegisterOrMemoryError(Token::Token const & token);
static CompilationError CreateExpectedOperandError(Token::Token const & token);
static CompilationError CreateTooManyArgumentsError(Token::Token const & token);
static CompilationError CreateTooFewArgumentsError(Token::Token const & token);
static CompilationError CreateExpectedEndOfStatementError(Token::Token const & token);
static CompilationError CreateDuplicateLabelError(Token::Token const & token);
static CompilationError CreateNonExistingLabelError(Token::Token const & token);
};
static CompilationError CreateExpectedOperandError(Token::Token const & token);
static CompilationError CreateTooManyArgumentsError(Token::Token const & token);
static CompilationError CreateTooFewArgumentsError(Token::Token const & token);
static CompilationError CreateExpectedEndOfStatementError(Token::Token const & token);
static CompilationError CreateDuplicateLabelError(Token::Token const & token);
static CompilationError CreateNonExistingLabelError(Token::Token const & token);
};
}

View File

@@ -1,37 +1,34 @@
#pragma once
#include <variant>
#include <execute/bytecode.hpp>
#include <execute/error.hpp>
#include <execute/state.hpp>
#include <stdexcept>
#include <variant>
#include <vector>
namespace Execute
{
enum class ArgumentType
{
Immediate,
Register,
Memory
};
enum class ArgumentType
{
Immediate,
Register,
Memory
};
class ArgumentValue
{
private:
ArgumentType type;
ArgumentType memoryValueType;
std::variant<RegisterByte, int> data;
class ArgumentValue {
private:
ArgumentType type;
ArgumentType memoryValueType;
std::variant<RegisterByte, int> data;
int & GetRegister(State & state) const;
std::uint8_t * GetMemory(State & state) const;
int & GetRegister(State & state) const;
std::uint8_t * GetMemory(State & state) const;
public:
void Write(int const value, State & state) const;
int Read(State & state) const;
public:
void Write(int const value, State & state) const;
int Read(State & state) const;
// Returns the size of the argument in bytes
std::size_t Parse(
std::vector<std::uint8_t> const & memory,
std::size_t const pos);
};
// Returns the size of the argument in bytes
std::size_t Parse(std::vector<std::uint8_t> const & memory, std::size_t const pos);
};
}

View File

@@ -3,41 +3,41 @@
namespace Execute
{
enum class InstructionByte : std::uint8_t
{
NOOP = 0,
/* Integer functions */
ADD_INTEGER,
SUBTRACT_INTEGER,
DIVIDE_INTEGER,
MULTIPLY_INTEGER,
SHIFT_LEFT_INTEGER,
SHIFT_RIGHT_INTEGER,
SET_INTEGER,
/* Control flow */
JUMP,
INTERRUPT,
CALL,
RETURN,
EXIT,
LESS_THAN_INTEGER,
GREATER_THAN_INTEGER,
EQUALS_INTEGER,
/* Memory */
POP_INTEGER,
PUSH_INTEGER,
/* Values */
IMMEDIATE_INTEGER,
REGISTER,
MEMORY_OP,
LABEL,
};
enum class InstructionByte : std::uint8_t
{
NOOP = 0,
/* Integer functions */
ADD_INTEGER,
SUBTRACT_INTEGER,
DIVIDE_INTEGER,
MULTIPLY_INTEGER,
SHIFT_LEFT_INTEGER,
SHIFT_RIGHT_INTEGER,
SET_INTEGER,
/* Control flow */
JUMP,
INTERRUPT,
CALL,
RETURN,
EXIT,
LESS_THAN_INTEGER,
GREATER_THAN_INTEGER,
EQUALS_INTEGER,
/* Memory */
POP_INTEGER,
PUSH_INTEGER,
/* Values */
IMMEDIATE_INTEGER,
REGISTER,
MEMORY_OP,
LABEL,
};
enum class RegisterByte : std::uint8_t
{
A = 1,
B,
C,
D
};
enum class RegisterByte : std::uint8_t
{
A = 1,
B,
C,
D
};
}

View File

@@ -3,48 +3,40 @@
namespace Execute
{
class RuntimeError
{
protected:
std::string message;
std::size_t byteLocation;
class RuntimeError {
protected:
std::string message;
std::size_t byteLocation;
public:
std::string const & GetMessage() const;
public:
std::string const & GetMessage() const;
RuntimeError();
RuntimeError(std::string const & what, std::size_t const byteLocation);
};
RuntimeError();
RuntimeError(std::string const & what, std::size_t const byteLocation);
};
class InterruptIndexOutOfRange : public RuntimeError
{
public:
InterruptIndexOutOfRange(std::size_t const location, int const index);
};
class InterruptIndexOutOfRange : public RuntimeError {
public:
InterruptIndexOutOfRange(std::size_t const location, int const index);
};
class AttemptedWriteToImmediate : public RuntimeError
{
public:
AttemptedWriteToImmediate(std::size_t const location);
};
class AttemptedWriteToImmediate : public RuntimeError {
public:
AttemptedWriteToImmediate(std::size_t const location);
};
class NonExecutableInstruction : public RuntimeError
{
public:
NonExecutableInstruction(std::size_t const location);
};
class NonExecutableInstruction : public RuntimeError {
public:
NonExecutableInstruction(std::size_t const location);
};
class NonArgumentByte : public RuntimeError
{
public:
NonArgumentByte(std::size_t const location);
};
class NonArgumentByte : public RuntimeError {
public:
NonArgumentByte(std::size_t const location);
};
class OutOfMemory : public RuntimeError
{
public:
OutOfMemory(
std::size_t const requiredMemorySize,
std::size_t const actualMemorySize);
};
class OutOfMemory : public RuntimeError {
public:
OutOfMemory(std::size_t const requiredMemorySize, std::size_t const actualMemorySize);
};
}

View File

@@ -5,8 +5,5 @@
namespace Execute
{
void ExecuteInterrupt(
int const id,
Execute::Registers & registers,
std::vector<std::uint8_t> & memory);
void ExecuteInterrupt(int const id, Execute::Registers & registers, std::vector<std::uint8_t> & memory);
}

View File

@@ -3,12 +3,12 @@
namespace Execute
{
struct Registers
{
int A, B, C, D;
std::size_t programCounter;
std::size_t stackPointer;
struct Registers
{
int A, B, C, D;
std::size_t programCounter;
std::size_t stackPointer;
// TODO status registers?
};
// TODO status registers?
};
}

View File

@@ -5,13 +5,13 @@
namespace Execute
{
struct State
{
bool terminated;
Registers registers;
std::vector<std::uint8_t> memory;
struct State
{
bool terminated;
Registers registers;
std::vector<std::uint8_t> memory;
void PushToStack(int const value);
int PopFromStack();
};
void PushToStack(int const value);
int PopFromStack();
};
}

View File

@@ -6,42 +6,35 @@
namespace Execute
{
class VirtualMachine
{
private:
State state;
class VirtualMachine {
private:
State state;
void DoArithmatic(
InstructionByte const instruction,
std::array<ArgumentValue, 3> & arguments);
void SetInteger(std::array<ArgumentValue, 3> & arguments);
void ExecuteJump(std::array<ArgumentValue, 3> & arguments);
void ExecuteInterrupt(std::array<ArgumentValue, 3> & arguments);
void ExecuteCall(
std::array<ArgumentValue, 3> & arguments,
std::size_t const returnByte);
void ExecuteReturn();
void DoBooleanLogic(
InstructionByte const instruction,
std::array<ArgumentValue, 3> & arguments,
std::size_t const nextInstruction);
void ExecutePop(std::array<ArgumentValue, 3> & arguments);
void ExecutePush(std::array<ArgumentValue, 3> & arguments);
void DoArithmatic(InstructionByte const instruction, std::array<ArgumentValue, 3> & arguments);
void SetInteger(std::array<ArgumentValue, 3> & arguments);
void ExecuteJump(std::array<ArgumentValue, 3> & arguments);
void ExecuteInterrupt(std::array<ArgumentValue, 3> & arguments);
void ExecuteCall(std::array<ArgumentValue, 3> & arguments, std::size_t const returnByte);
void ExecuteReturn();
void DoBooleanLogic(
InstructionByte const instruction,
std::array<ArgumentValue, 3> & arguments,
std::size_t const nextInstruction);
void ExecutePop(std::array<ArgumentValue, 3> & arguments);
void ExecutePush(std::array<ArgumentValue, 3> & arguments);
void Step();
void Step();
public:
void Run();
void SingleStep();
public:
void Run();
void SingleStep();
void SetMemorySize(std::size_t const size);
void LoadCode(
std::vector<std::uint8_t> const & byteCode,
bool const printDecodedBytes);
void SetMemorySize(std::size_t const size);
void LoadCode(std::vector<std::uint8_t> const & byteCode, bool const printDecodedBytes);
State const & GetState() const;
Execute::InstructionByte GetCurrentInstruction() const;
State const & GetState() const;
Execute::InstructionByte GetCurrentInstruction() const;
bool IsTerminated() const;
};
bool IsTerminated() const;
};
}

View File

@@ -2,20 +2,17 @@
#include <string>
#include <vector>
class Preprocessor
{
class Preprocessor {
private:
std::vector<std::string> substitutionIdentifiers;
std::vector<std::string> substitutionValues;
std::vector<std::string> substitutionIdentifiers;
std::vector<std::string> substitutionValues;
void extractComment(std::string & line,
std::size_t const lineNumber,
std::size_t const lineColumn);
void extractComment(std::string & line, std::size_t const lineNumber, std::size_t const lineColumn);
void processLine(std::string & line, std::size_t const lineNumber);
void processLine(std::string & line, std::size_t const lineNumber);
public:
void process(std::vector<std::string> & lines);
void process(std::vector<std::string> & lines);
void printSubstitutions() const;
void printSubstitutions() const;
};

View File

@@ -4,10 +4,10 @@
namespace Token
{
struct TokenizationError : public std::exception
{
Token errorToken;
std::string errorMsg;
TokenizationError(Token const & token, std::string const & msg);
};
struct TokenizationError : public std::exception
{
Token errorToken;
std::string errorMsg;
TokenizationError(Token const & token, std::string const & msg);
};
}

View File

@@ -3,27 +3,27 @@
namespace Token
{
enum class OperandType : int
{
Unknown = -1,
AddInteger = 0,
SubtractInteger,
DivideInteger,
MultiplyInteger,
ShiftIntegerLeft,
ShiftIntegerRight,
Jump,
CallFunction,
ReturnFromFunction,
ExitProgram,
LessThanInteger,
GreaterThanInteger,
EqualInteger,
SetInteger,
Interrupt,
PushInteger,
PopInteger
};
enum class OperandType : int
{
Unknown = -1,
AddInteger = 0,
SubtractInteger,
DivideInteger,
MultiplyInteger,
ShiftIntegerLeft,
ShiftIntegerRight,
Jump,
CallFunction,
ReturnFromFunction,
ExitProgram,
LessThanInteger,
GreaterThanInteger,
EqualInteger,
SetInteger,
Interrupt,
PushInteger,
PopInteger
};
OperandType GetOperandType(std::string const & op);
OperandType GetOperandType(std::string const & op);
}

View File

@@ -3,14 +3,14 @@
namespace Token
{
enum class RegisterType
{
Unknown = -1,
A = 0,
B,
C,
D
};
enum class RegisterType
{
Unknown = -1,
A = 0,
B,
C,
D
};
RegisterType GetRegisterType(std::string const & reg);
RegisterType GetRegisterType(std::string const & reg);
}

View File

@@ -7,47 +7,58 @@
namespace Token
{
enum class TokenValueType
{
None = 0,
Integer,
Operand,
Register,
String
};
enum class TokenValueType
{
None = 0,
Integer,
Operand,
Register,
String
};
struct Token
{
private:
Token(TokenType type, bool validness, int const lineNumber, int const lineColumn);
Token(TokenType type, std::string const & string, bool validness, int const lineNumber, int const lineColumn);
Token(TokenType type, int value, bool validness, int const lineNumber, int const lineColumn);
Token(TokenType type, RegisterType const registerType, bool validness, int const lineNumber, int const lineColumn);
Token(TokenType type, OperandType const OperandType, bool validness, int const lineNumber, int const lineColumn);
struct Token
{
private:
Token(TokenType type, bool validness, int const lineNumber, int const lineColumn);
Token(TokenType type, std::string const & string, bool validness, int const lineNumber, int const lineColumn);
Token(TokenType type, int value, bool validness, int const lineNumber, int const lineColumn);
Token(
TokenType type,
RegisterType const registerType,
bool validness,
int const lineNumber,
int const lineColumn);
Token(
TokenType type,
OperandType const OperandType,
bool validness,
int const lineNumber,
int const lineColumn);
public:
int const lineNumber;
int const lineColumn;
TokenType type;
TokenValueType const valueType;
bool isValid;
std::variant<OperandType, RegisterType, int, std::string> data;
std::string errorMessage;
public:
int const lineNumber;
int const lineColumn;
TokenType type;
TokenValueType const valueType;
bool isValid;
std::variant<OperandType, RegisterType, int, std::string> data;
std::string errorMessage;
Token(Token const & other);
Token(Token const & other);
static Token CreateEmptyToken(int const lineNumber, int const lineColumn);
static Token CreateErrorToken(std::string const & message, TokenType const type, int const lineNumber, int const lineColumn);
static Token CreateStatementEndToken(int const lineNumber, int const lineColumn);
static Token CreateLabelDefinitionToken(std::string const & string, int const lineNumber, int const lineColumn);
static Token CreateLabelArgumentToken(std::string const & string, int const lineNumber, int const lineColumn);
static Token CreateImmediateValueToken(int const value, int const lineNumber, int const lineColumn);
static Token CreateRegisterToken(RegisterType const registerType, int const lineNumber, int const lineColumn);
static Token CreateOperandToken(OperandType const operandType, int const lineNumber, int const lineColumn);
static Token CreateMemoryToken(RegisterType const registerType, int const lineNumber, int const lineColumn);
static Token CreateMemoryToken(int const value, int const lineNumber, int const lineColumn);
static Token CreateEmptyToken(int const lineNumber, int const lineColumn);
static Token
CreateErrorToken(std::string const & message, TokenType const type, int const lineNumber, int const lineColumn);
static Token CreateStatementEndToken(int const lineNumber, int const lineColumn);
static Token CreateLabelDefinitionToken(std::string const & string, int const lineNumber, int const lineColumn);
static Token CreateLabelArgumentToken(std::string const & string, int const lineNumber, int const lineColumn);
static Token CreateImmediateValueToken(int const value, int const lineNumber, int const lineColumn);
static Token CreateRegisterToken(RegisterType const registerType, int const lineNumber, int const lineColumn);
static Token CreateOperandToken(OperandType const operandType, int const lineNumber, int const lineColumn);
static Token CreateMemoryToken(RegisterType const registerType, int const lineNumber, int const lineColumn);
static Token CreateMemoryToken(int const value, int const lineNumber, int const lineColumn);
std::string GetName() const;
void Print() const;
};
std::string GetName() const;
void Print() const;
};
}

View File

@@ -5,19 +5,13 @@
namespace Token
{
class Tokenizer
{
private:
// Argument for string should never be of length zero
Token ExtractToken(
std::string const & string,
std::size_t const lineNumber,
std::size_t const lineColumn) const;
class Tokenizer {
private:
// Argument for string should never be of length zero
Token
ExtractToken(std::string const & string, std::size_t const lineNumber, std::size_t const lineColumn) const;
public:
void Tokenize(
std::string const & line,
std::size_t const lineNumber,
std::vector<Token> & tokens);
};
public:
void Tokenize(std::string const & line, std::size_t const lineNumber, std::vector<Token> & tokens);
};
}

View File

@@ -2,15 +2,15 @@
namespace Token
{
enum class TokenType
{
Unknown = -1,
Operand = 0,
ImmediateInteger,
Register,
StatementEnd,
LabelDefinition,
LabelArgument,
Memory
};
enum class TokenType
{
Unknown = -1,
Operand = 0,
ImmediateInteger,
Register,
StatementEnd,
LabelDefinition,
LabelArgument,
Memory
};
}

View File

@@ -6,25 +6,18 @@
namespace Utils
{
bool isWhitespaceCharacter(char const c);
bool isWhitespaceCharacter(char const c);
// Returns nullopt in case the value is missing its terminator character
std::optional<std::string> getValueSurroundedBy(
std::string const & src,
std::size_t const pos,
char const surroundingCharacter);
// Returns nullopt in case the value is missing its terminator character
std::optional<std::string>
getValueSurroundedBy(std::string const & src, std::size_t const pos, char const surroundingCharacter);
std::string getValueSurroundedByWhitespace(
std::string const & src,
std::size_t const pos);
std::string getValueSurroundedByWhitespace(std::string const & src, std::size_t const pos);
namespace Bytes
{
void Write(
int const value,
std::vector<std::uint8_t> & vec,
std::size_t const pos);
namespace Bytes
{
void Write(int const value, std::vector<std::uint8_t> & vec, std::size_t const pos);
int Read(std::vector<std::uint8_t> const & vec, std::size_t const pos);
}
int Read(std::vector<std::uint8_t> const & vec, std::size_t const pos);
}
}

View File

@@ -3,42 +3,33 @@
#include <execute/virtualmachine.hpp>
#include <token/tokenizer.hpp>
class Wassembler
{
class Wassembler {
private:
Execute::VirtualMachine vm;
bool printSubstitutions;
bool printTokens;
bool printTranslatedBytes;
Execute::VirtualMachine vm;
bool printSubstitutions;
bool printTokens;
bool printTranslatedBytes;
bool LoadTextFile(
std::string const & filePath,
std::vector<std::string> & lines) const;
bool Preprocess(std::vector<std::string> & lines) const;
bool Tokenize(
std::vector<std::string> const & lines,
std::vector<Token::Token> & tokens) const;
bool CompileToBytes(
std::vector<Token::Token> const & tokens,
std::vector<std::string> const & lines,
std::vector<std::uint8_t> & bytes) const;
void ExecuteCode(std::vector<std::uint8_t> const & bytes);
bool LoadTextFile(std::string const & filePath, std::vector<std::string> & lines) const;
bool Preprocess(std::vector<std::string> & lines) const;
bool Tokenize(std::vector<std::string> const & lines, std::vector<Token::Token> & tokens) const;
bool CompileToBytes(
std::vector<Token::Token> const & tokens,
std::vector<std::string> const & lines,
std::vector<std::uint8_t> & bytes) const;
void ExecuteCode(std::vector<std::uint8_t> const & bytes);
bool CompileFile(
std::string const & filePath,
std::vector<std::uint8_t> & bytes) const;
bool CompileFile(std::string const & filePath, std::vector<std::uint8_t> & bytes) const;
public:
void SetMemorySize(unsigned const size);
void SetMemorySize(unsigned const size);
void EnableSubstitutionsLogging();
void EnableTokensLogging();
void EnableByteTranslationLogging();
void EnableSubstitutionsLogging();
void EnableTokensLogging();
void EnableByteTranslationLogging();
bool CompileAndRun(std::string const & filePath);
bool CompileToFile(
std::string const & inputFilePath,
std::string const & outputFilePath);
bool CompileAndRun(std::string const & filePath);
bool CompileToFile(std::string const & inputFilePath, std::string const & outputFilePath);
Wassembler() = default;
Wassembler() = default;
};