https://github.com/leozhang1/QtInterpreter ASTNode.h代码展示
时间: 2023-09-26 10:06:22 浏览: 122
下面是ASTNode.h的代码展示:
```c++
#ifndef ASTNODE_H
#define ASTNODE_H
#include <vector>
#include <string>
#include <memory>
// Forward declaration of ASTNode class
class ASTNode;
// Type definitions for shared pointer and vector of ASTNode objects
typedef std::shared_ptr<ASTNode> ASTNodePtr;
typedef std::vector<ASTNodePtr> ASTNodeList;
// Enum for different node types
enum class NodeType {
Program,
Statement,
Expression,
NumberLiteral,
StringLiteral,
Identifier,
BinaryExpression,
UnaryExpression,
AssignmentExpression,
VariableDeclaration,
FunctionDeclaration,
CallExpression,
IfStatement,
WhileStatement,
ForStatement,
ReturnStatement
};
// Class for AST nodes
class ASTNode {
public:
// Constructor
ASTNode(NodeType type);
// Destructor
virtual ~ASTNode() {}
// Getter and setter for node type
NodeType getType() const { return m_type; }
void setType(NodeType type) { m_type = type; }
// Getter and setter for node location in source code
int getLine() const { return m_line; }
void setLine(int line) { m_line = line; }
int getColumn() const { return m_column; }
void setColumn(int column) { m_column = column; }
// Add a child node to the list of children
void addChild(const ASTNodePtr& child);
// Get the list of children nodes
const ASTNodeList& getChildren() const { return m_children; }
// Accept method for visitor pattern
virtual void accept(class ASTVisitor& visitor);
private:
NodeType m_type; // Node type
int m_line; // Line number in source code
int m_column; // Column number in source code
ASTNodeList m_children; // List of child nodes
};
#endif // ASTNODE_H
```
这个头文件定义了 ASTNode 类,用于表示抽象语法树中的节点。它包括了节点的类型、位置信息、子节点列表等属性,并提供了一些方法用于添加和获取子节点、访问者模式等。此外,还定义了一些枚举类型用于表示不同的节点类型。
阅读全文