|
| 1 | +/** |
| 2 | + * @file syntax_tree.hpp |
| 3 | + * @brief Defines the classes for syntax tree nodes used by the TINY language parser. |
| 4 | + * |
| 5 | + * The syntax tree represents the hierarchical structure of the parsed program. |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef SYNTAX_TREE_HPP |
| 9 | +#define SYNTAX_TREE_HPP |
| 10 | + |
| 11 | +#include <memory> |
| 12 | +#include <vector> |
| 13 | +#include <string> |
| 14 | + |
| 15 | +namespace TINY::PARSER |
| 16 | +{ |
| 17 | + |
| 18 | + /** |
| 19 | + * @class SyntaxTreeNode |
| 20 | + * @brief Base class for all syntax tree nodes. |
| 21 | + */ |
| 22 | + class SyntaxTreeNode |
| 23 | + { |
| 24 | + public: |
| 25 | + virtual ~SyntaxTreeNode() = default; |
| 26 | + |
| 27 | + /** |
| 28 | + * @brief Converts the syntax tree node to a string representation. |
| 29 | + * @param indentation The indentation level for pretty-printing. |
| 30 | + * @return A string representing the node. |
| 31 | + */ |
| 32 | + virtual std::string toString(int indentation = 0) const = 0; |
| 33 | + }; |
| 34 | + |
| 35 | + /** |
| 36 | + * @class StatementSequenceNode |
| 37 | + * @brief Represents a sequence of statements. |
| 38 | + */ |
| 39 | + class StatementSequenceNode : public SyntaxTreeNode |
| 40 | + { |
| 41 | + public: |
| 42 | + void addStatement(std::unique_ptr<SyntaxTreeNode> statement); |
| 43 | + std::string toString(int indentation = 0) const override; |
| 44 | + |
| 45 | + private: |
| 46 | + std::vector<std::unique_ptr<SyntaxTreeNode>> statements; |
| 47 | + }; |
| 48 | + |
| 49 | + /** |
| 50 | + * @class IfStatementNode |
| 51 | + * @brief Represents an if-then-else statement. |
| 52 | + */ |
| 53 | + class IfStatementNode : public SyntaxTreeNode |
| 54 | + { |
| 55 | + public: |
| 56 | + IfStatementNode(std::unique_ptr<SyntaxTreeNode> condition, |
| 57 | + std::unique_ptr<SyntaxTreeNode> thenPart, |
| 58 | + std::unique_ptr<SyntaxTreeNode> elsePart); |
| 59 | + |
| 60 | + std::string toString(int indentation = 0) const override; |
| 61 | + |
| 62 | + private: |
| 63 | + std::unique_ptr<SyntaxTreeNode> condition; |
| 64 | + std::unique_ptr<SyntaxTreeNode> thenPart; |
| 65 | + std::unique_ptr<SyntaxTreeNode> elsePart; |
| 66 | + }; |
| 67 | + |
| 68 | + /** |
| 69 | + * @class RepeatStatementNode |
| 70 | + * @brief Represents a repeat-until loop. |
| 71 | + */ |
| 72 | + class RepeatStatementNode : public SyntaxTreeNode |
| 73 | + { |
| 74 | + public: |
| 75 | + RepeatStatementNode(std::unique_ptr<SyntaxTreeNode> body, |
| 76 | + std::unique_ptr<SyntaxTreeNode> condition); |
| 77 | + |
| 78 | + std::string toString(int indentation = 0) const override; |
| 79 | + |
| 80 | + private: |
| 81 | + std::unique_ptr<SyntaxTreeNode> body; |
| 82 | + std::unique_ptr<SyntaxTreeNode> condition; |
| 83 | + }; |
| 84 | + |
| 85 | + /** |
| 86 | + * @class AssignStatementNode |
| 87 | + * @brief Represents an assignment statement. |
| 88 | + */ |
| 89 | + class AssignStatementNode : public SyntaxTreeNode |
| 90 | + { |
| 91 | + public: |
| 92 | + AssignStatementNode(const std::string &variableName, |
| 93 | + std::unique_ptr<SyntaxTreeNode> expression); |
| 94 | + |
| 95 | + std::string toString(int indentation = 0) const override; |
| 96 | + |
| 97 | + private: |
| 98 | + std::string variableName; |
| 99 | + std::unique_ptr<SyntaxTreeNode> expression; |
| 100 | + }; |
| 101 | + |
| 102 | + /** |
| 103 | + * @class ReadStatementNode |
| 104 | + * @brief Represents a read statement. |
| 105 | + */ |
| 106 | + class ReadStatementNode : public SyntaxTreeNode |
| 107 | + { |
| 108 | + public: |
| 109 | + explicit ReadStatementNode(const std::string &variableName); |
| 110 | + |
| 111 | + std::string toString(int indentation = 0) const override; |
| 112 | + |
| 113 | + private: |
| 114 | + std::string variableName; |
| 115 | + }; |
| 116 | + |
| 117 | + /** |
| 118 | + * @class WriteStatementNode |
| 119 | + * @brief Represents a write statement. |
| 120 | + */ |
| 121 | + class WriteStatementNode : public SyntaxTreeNode |
| 122 | + { |
| 123 | + public: |
| 124 | + explicit WriteStatementNode(std::unique_ptr<SyntaxTreeNode> expression); |
| 125 | + |
| 126 | + std::string toString(int indentation = 0) const override; |
| 127 | + |
| 128 | + private: |
| 129 | + std::unique_ptr<SyntaxTreeNode> expression; |
| 130 | + }; |
| 131 | + |
| 132 | + /** |
| 133 | + * @class OpExpressionNode |
| 134 | + * @brief Represents an expression with an operator. |
| 135 | + */ |
| 136 | + class OpExpressionNode : public SyntaxTreeNode |
| 137 | + { |
| 138 | + public: |
| 139 | + OpExpressionNode(const std::string &op, |
| 140 | + std::unique_ptr<SyntaxTreeNode> left, |
| 141 | + std::unique_ptr<SyntaxTreeNode> right); |
| 142 | + |
| 143 | + std::string toString(int indentation = 0) const override; |
| 144 | + |
| 145 | + private: |
| 146 | + std::string op; |
| 147 | + std::unique_ptr<SyntaxTreeNode> left; |
| 148 | + std::unique_ptr<SyntaxTreeNode> right; |
| 149 | + }; |
| 150 | + |
| 151 | + /** |
| 152 | + * @class ConstExpressionNode |
| 153 | + * @brief Represents a constant (number) in an expression. |
| 154 | + */ |
| 155 | + class ConstExpressionNode : public SyntaxTreeNode |
| 156 | + { |
| 157 | + public: |
| 158 | + explicit ConstExpressionNode(const std::string &value); |
| 159 | + |
| 160 | + std::string toString(int indentation = 0) const override; |
| 161 | + |
| 162 | + private: |
| 163 | + std::string value; |
| 164 | + }; |
| 165 | + |
| 166 | + /** |
| 167 | + * @class VariableExpressionNode |
| 168 | + * @brief Represents a variable in an expression. |
| 169 | + */ |
| 170 | + class VariableExpressionNode : public SyntaxTreeNode |
| 171 | + { |
| 172 | + public: |
| 173 | + explicit VariableExpressionNode(const std::string &name); |
| 174 | + |
| 175 | + std::string toString(int indentation = 0) const override; |
| 176 | + |
| 177 | + private: |
| 178 | + std::string name; |
| 179 | + }; |
| 180 | + |
| 181 | +} // namespace TINY::PARSER |
| 182 | + |
| 183 | +#endif // SYNTAX_TREE_HPP |
0 commit comments