From d6dc3f3af48b521ae79cf14134876b81470da4bf Mon Sep 17 00:00:00 2001 From: ChUrl Date: Tue, 21 Mar 2023 14:33:05 +0100 Subject: [PATCH] Implement part of the AST structure --- src/ast/Node.cpp | 13 +++++++++++ src/ast/Node.h | 44 +++++++++++++++++++++++++++++++++++++ src/ast/nodes/AluNode.cpp | 15 +++++++++++++ src/ast/nodes/AluNode.h | 32 +++++++++++++++++++++++++++ src/ast/nodes/ConstNode.cpp | 15 +++++++++++++ src/ast/nodes/ConstNode.h | 22 +++++++++++++++++++ src/ast/nodes/MovNode.cpp | 22 +++++++++++++++++++ src/ast/nodes/MovNode.h | 23 +++++++++++++++++++ 8 files changed, 186 insertions(+) create mode 100644 src/ast/Node.cpp create mode 100644 src/ast/Node.h create mode 100644 src/ast/nodes/AluNode.cpp create mode 100644 src/ast/nodes/AluNode.h create mode 100644 src/ast/nodes/ConstNode.cpp create mode 100644 src/ast/nodes/ConstNode.h create mode 100644 src/ast/nodes/MovNode.cpp create mode 100644 src/ast/nodes/MovNode.h diff --git a/src/ast/Node.cpp b/src/ast/Node.cpp new file mode 100644 index 0000000..85bd16b --- /dev/null +++ b/src/ast/Node.cpp @@ -0,0 +1,13 @@ +// +// Created by christoph on 20.03.23. +// + +#include "Node.h" + +void Node::addChild(std::unique_ptr child) { + children.push_back(std::move(child)); +} + +auto Node::getChildren() const -> const std::vector> & { + return children; +} diff --git a/src/ast/Node.h b/src/ast/Node.h new file mode 100644 index 0000000..4d9e99c --- /dev/null +++ b/src/ast/Node.h @@ -0,0 +1,44 @@ +// +// Created by christoph on 20.03.23. +// + +#ifndef LOGISIMASSEMBLER_NODE_H +#define LOGISIMASSEMBLER_NODE_H + +#include +#include +#include "../lexer/Token.h" + +class Node { +public: + Node() = default; + + Node(const Node ©) = default; + + auto operator=(const Node ©) -> Node & = default; + + Node(Node &&move) = default; + + auto operator=(Node &&move) -> Node & = default; + + virtual ~Node() = default; + + void addChild(std::unique_ptr child); + + [[nodiscard]] virtual auto compile() const -> uint8_t = 0; + + [[nodiscard]] auto getChildren() const -> const std::vector> &; + +protected: + enum Operation : uint8_t { + CONSTANT, + ALU, + COPY, + CONDITION + }; + +protected: + std::vector> children; +}; + +#endif //LOGISIMASSEMBLER_NODE_H diff --git a/src/ast/nodes/AluNode.cpp b/src/ast/nodes/AluNode.cpp new file mode 100644 index 0000000..93df05b --- /dev/null +++ b/src/ast/nodes/AluNode.cpp @@ -0,0 +1,15 @@ +// +// Created by christoph on 21.03.23. +// + +#include "AluNode.h" + +AluNode::AluNode(AluNode::AluOperation operation) : operation(operation) {} + +auto AluNode::compile() const -> uint8_t { + if (operation > SUB) { + throw "Compile Error: Invalid ALU Operation!"; + } + + return (ALU & 0b11) << 6 | (operation & 0b111); +} diff --git a/src/ast/nodes/AluNode.h b/src/ast/nodes/AluNode.h new file mode 100644 index 0000000..6676207 --- /dev/null +++ b/src/ast/nodes/AluNode.h @@ -0,0 +1,32 @@ +// +// Created by christoph on 21.03.23. +// + +#ifndef LOGISIMASSEMBLER_ALUNODE_H +#define LOGISIMASSEMBLER_ALUNODE_H + +#include "../Node.h" + +class AluNode : public Node { +public: + enum AluOperation : uint8_t { + AND, + OR, + NAND, + NOR, + ADD, + SUB + }; + +public: + AluNode(AluOperation operation); + + ~AluNode() override = default; + + [[nodiscard]] auto compile() const -> uint8_t override; + +private: + AluOperation operation; +}; + +#endif //LOGISIMASSEMBLER_ALUNODE_H diff --git a/src/ast/nodes/ConstNode.cpp b/src/ast/nodes/ConstNode.cpp new file mode 100644 index 0000000..93c8ba8 --- /dev/null +++ b/src/ast/nodes/ConstNode.cpp @@ -0,0 +1,15 @@ +// +// Created by christoph on 21.03.23. +// + +#include "ConstNode.h" + +ConstNode::ConstNode(uint8_t value) : value(value) {} + +auto ConstNode::compile() const -> uint8_t { + if (value > 0b00111111) { + throw "Compile Error: Constant too large!"; + } + + return (CONSTANT & 0b11) << 6 | (value & 0b111111); +} diff --git a/src/ast/nodes/ConstNode.h b/src/ast/nodes/ConstNode.h new file mode 100644 index 0000000..e4a50ce --- /dev/null +++ b/src/ast/nodes/ConstNode.h @@ -0,0 +1,22 @@ +// +// Created by christoph on 21.03.23. +// + +#ifndef LOGISIMASSEMBLER_CONSTNODE_H +#define LOGISIMASSEMBLER_CONSTNODE_H + +#include "../Node.h" + +class ConstNode : public Node { +public: + ConstNode(uint8_t value); + + ~ConstNode() override = default; + + [[nodiscard]] auto compile() const -> uint8_t override; + +private: + uint8_t value; +}; + +#endif //LOGISIMASSEMBLER_CONSTNODE_H diff --git a/src/ast/nodes/MovNode.cpp b/src/ast/nodes/MovNode.cpp new file mode 100644 index 0000000..398f1de --- /dev/null +++ b/src/ast/nodes/MovNode.cpp @@ -0,0 +1,22 @@ +// +// Created by christoph on 21.03.23. +// + +#include "MovNode.h" + +MovNode::MovNode(std::unique_ptr source, std::unique_ptr target) { + children.push_back(std::move(source)); + children.push_back(std::move(target)); +} + +auto MovNode::source() const -> Node & { + return *children[0]; +} + +auto MovNode::target() const -> Node & { + return *children[1]; +} + +auto MovNode::compile() const -> uint8_t { + return (COPY & 0b11) << 6 | (source().compile() & 0b111) << 3 | (target().compile() & 0b111); +} diff --git a/src/ast/nodes/MovNode.h b/src/ast/nodes/MovNode.h new file mode 100644 index 0000000..d56b8b3 --- /dev/null +++ b/src/ast/nodes/MovNode.h @@ -0,0 +1,23 @@ +// +// Created by christoph on 21.03.23. +// + +#ifndef LOGISIMASSEMBLER_MOVNODE_H +#define LOGISIMASSEMBLER_MOVNODE_H + +#include "../Node.h" + +class MovNode : public Node { +public: + MovNode(std::unique_ptr source, std::unique_ptr target); + + ~MovNode() override = default; + + auto source() const -> Node &; + + auto target() const -> Node &; + + [[nodiscard]] auto compile() const -> uint8_t override; +}; + +#endif //LOGISIMASSEMBLER_MOVNODE_H