Implement part of the AST structure
This commit is contained in:
13
src/ast/Node.cpp
Normal file
13
src/ast/Node.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
//
|
||||
// Created by christoph on 20.03.23.
|
||||
//
|
||||
|
||||
#include "Node.h"
|
||||
|
||||
void Node::addChild(std::unique_ptr<Node> child) {
|
||||
children.push_back(std::move(child));
|
||||
}
|
||||
|
||||
auto Node::getChildren() const -> const std::vector<std::unique_ptr<Node>> & {
|
||||
return children;
|
||||
}
|
44
src/ast/Node.h
Normal file
44
src/ast/Node.h
Normal file
@ -0,0 +1,44 @@
|
||||
//
|
||||
// Created by christoph on 20.03.23.
|
||||
//
|
||||
|
||||
#ifndef LOGISIMASSEMBLER_NODE_H
|
||||
#define LOGISIMASSEMBLER_NODE_H
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#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<Node> child);
|
||||
|
||||
[[nodiscard]] virtual auto compile() const -> uint8_t = 0;
|
||||
|
||||
[[nodiscard]] auto getChildren() const -> const std::vector<std::unique_ptr<Node>> &;
|
||||
|
||||
protected:
|
||||
enum Operation : uint8_t {
|
||||
CONSTANT,
|
||||
ALU,
|
||||
COPY,
|
||||
CONDITION
|
||||
};
|
||||
|
||||
protected:
|
||||
std::vector<std::unique_ptr<Node>> children;
|
||||
};
|
||||
|
||||
#endif //LOGISIMASSEMBLER_NODE_H
|
15
src/ast/nodes/AluNode.cpp
Normal file
15
src/ast/nodes/AluNode.cpp
Normal file
@ -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);
|
||||
}
|
32
src/ast/nodes/AluNode.h
Normal file
32
src/ast/nodes/AluNode.h
Normal file
@ -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
|
15
src/ast/nodes/ConstNode.cpp
Normal file
15
src/ast/nodes/ConstNode.cpp
Normal file
@ -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);
|
||||
}
|
22
src/ast/nodes/ConstNode.h
Normal file
22
src/ast/nodes/ConstNode.h
Normal file
@ -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
|
22
src/ast/nodes/MovNode.cpp
Normal file
22
src/ast/nodes/MovNode.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by christoph on 21.03.23.
|
||||
//
|
||||
|
||||
#include "MovNode.h"
|
||||
|
||||
MovNode::MovNode(std::unique_ptr<Node> source, std::unique_ptr<Node> 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);
|
||||
}
|
23
src/ast/nodes/MovNode.h
Normal file
23
src/ast/nodes/MovNode.h
Normal file
@ -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<Node> source, std::unique_ptr<Node> target);
|
||||
|
||||
~MovNode() override = default;
|
||||
|
||||
auto source() const -> Node &;
|
||||
|
||||
auto target() const -> Node &;
|
||||
|
||||
[[nodiscard]] auto compile() const -> uint8_t override;
|
||||
};
|
||||
|
||||
#endif //LOGISIMASSEMBLER_MOVNODE_H
|
Reference in New Issue
Block a user