|
| 1 | +#ifndef NODE_H |
| 2 | +#define NODE_H |
| 3 | + |
| 4 | +#include <QString> |
| 5 | +#include <QList> |
| 6 | +#include <QStringView> |
| 7 | +#include <QLatin1StringView> |
| 8 | +#include <QDebug> |
| 9 | + |
| 10 | +#include <array> |
| 11 | + |
| 12 | + |
| 13 | +namespace Tiny::Data { |
| 14 | +class Node { |
| 15 | + |
| 16 | +public: |
| 17 | + enum class NodeType { |
| 18 | + Program, |
| 19 | + Statement, |
| 20 | + IfStatement, |
| 21 | + RepeatStatement, |
| 22 | + AssignStatement, |
| 23 | + ReadStatement, |
| 24 | + WriteStatement, |
| 25 | + Expression, |
| 26 | + Comparison, |
| 27 | + Addition, |
| 28 | + Multiplication, |
| 29 | + Factor, |
| 30 | + Identifier, |
| 31 | + Number |
| 32 | + }; // enum class NodeType |
| 33 | + |
| 34 | + |
| 35 | + explicit Node(NodeType type, QString value, Node *parent = nullptr); |
| 36 | + ~Node(); |
| 37 | + |
| 38 | + // getters |
| 39 | + QString getValue() const; |
| 40 | + NodeType getType() const; |
| 41 | + QStringView getNodeTypeString() const; |
| 42 | + Node *getParent() const; |
| 43 | + QList<Node *> getSiblings() const; |
| 44 | + QList<Node *> getChildren() const; |
| 45 | + |
| 46 | + // setters |
| 47 | + void setValue(QString value); |
| 48 | + void setType(NodeType type); |
| 49 | + void setParent(Node *parent); |
| 50 | + |
| 51 | + // add children |
| 52 | + void addChild(Node *child); |
| 53 | + |
| 54 | + // add siblings |
| 55 | + void addSibling(Node *sibling); |
| 56 | + |
| 57 | + // print tree |
| 58 | + void printTree(int depth = 0) const; |
| 59 | + |
| 60 | +private: |
| 61 | + QString value; |
| 62 | + NodeType type; |
| 63 | + Node *parent; |
| 64 | + QList<Node *> siblings; |
| 65 | + QList<Node *> children; |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + static constexpr std::array<QLatin1StringView, 14> nodeTypeStrings = { |
| 71 | + QLatin1StringView("Program"), |
| 72 | + QLatin1StringView("Statement"), |
| 73 | + QLatin1StringView("IfStatement"), |
| 74 | + QLatin1StringView("RepeatStatement"), |
| 75 | + QLatin1StringView("AssignStatement"), |
| 76 | + QLatin1StringView("ReadStatement"), |
| 77 | + QLatin1StringView("WriteStatement"), |
| 78 | + QLatin1StringView("Expression"), |
| 79 | + QLatin1StringView("Comparison"), |
| 80 | + QLatin1StringView("Addition"), |
| 81 | + QLatin1StringView("Multiplication"), |
| 82 | + QLatin1StringView("Factor"), |
| 83 | + QLatin1StringView("Identifier"), |
| 84 | + QLatin1StringView("Number") |
| 85 | + }; // nodeTypeStrings Lookup Table |
| 86 | + |
| 87 | +}; // class Node |
| 88 | + |
| 89 | +} // namespace Tiny::Data |
| 90 | + |
| 91 | +#endif // NODE_H |
0 commit comments