-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAST.cpp
More file actions
131 lines (111 loc) · 3.8 KB
/
Copy pathAST.cpp
File metadata and controls
131 lines (111 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// AST.cpp
// ============================================================
// Purpose: Implements toString() for every AST node type.
// These are used to PRINT THE TREE after parsing
// so the user can see the full AST structure.
#include "AST.h" // file from header file for the AST class
#include <sstream> // std::ostringstream for building strings
// Helper: create a string of 'indent' spaces for tree drawing
static std::string pad(int indent) {
return std::string(indent, ' '); // Returns " " etc.
}
// NumberNode Processing
std::string NumberNode::toString(int indent) const {
// e.g. " NumberNode(42)"
std::ostringstream ss;
ss << pad(indent) << "NumberNode(" << value << ")";
return ss.str();
}
// StringNode Processing
std::string StringNode::toString(int indent) const {
std::ostringstream ss;
ss << pad(indent) << "StringNode(\"" << value << "\")";
return ss.str();
}
// IdentifierNode Processing
std::string IdentifierNode::toString(int indent) const {
std::ostringstream ss;
ss << pad(indent) << "IdentifierNode(" << name << ")";
return ss.str();
}
// BinaryOpNode Processing
std::string BinaryOpNode::toString(int indent) const {
std::ostringstream ss;
ss << pad(indent) << "BinaryOpNode(" << op << ")\n";
// Recursively print left child with 2 more spaces of indent
ss << left->toString(indent + 2) << "\n";
// Recursively print right child
ss << right->toString(indent + 2);
return ss.str();
}
// VarDeclNode Processing
std::string VarDeclNode::toString(int indent) const {
std::ostringstream ss;
ss << pad(indent) << "VarDeclNode(" << type << " " << name << ")";
if (init) {
// If there is an initialiser, print it as a child
ss << "\n" << init->toString(indent + 2);
}
return ss.str();
}
// AssignNode Processing
std::string AssignNode::toString(int indent) const {
std::ostringstream ss;
ss << pad(indent) << "AssignNode(" << name << ")\n";
ss << expr->toString(indent + 2); // Value expression as child
return ss.str();
}
// IfNode Processing
std::string IfNode::toString(int indent) const {
std::ostringstream ss;
ss << pad(indent) << "IfNode\n";
// Print condition
ss << pad(indent + 2) << "condition:\n";
ss << condition->toString(indent + 4) << "\n";
// Print then-branch
ss << pad(indent + 2) << "then:\n";
for (const auto& s : thenBranch)
ss << s->toString(indent + 4) << "\n";
// Print else-branch only if it exists
if (!elseBranch.empty()) {
ss << pad(indent + 2) << "else:\n";
for (const auto& s : elseBranch)
ss << s->toString(indent + 4) << "\n";
}
return ss.str();
}
// WhileNode Processing
std::string WhileNode::toString(int indent) const {
std::ostringstream ss;
ss << pad(indent) << "WhileNode\n";
ss << pad(indent + 2) << "condition:\n";
ss << condition->toString(indent + 4) << "\n";
ss << pad(indent + 2) << "body:\n";
for (const auto& s : body)
ss << s->toString(indent + 4) << "\n";
return ss.str();
}
// PrintNode Processing
std::string PrintNode::toString(int indent) const {
std::ostringstream ss;
ss << pad(indent) << "PrintNode\n";
ss << expr->toString(indent + 2);
return ss.str();
}
// ReturnNode Processing
std::string ReturnNode::toString(int indent) const {
std::ostringstream ss;
ss << pad(indent) << "ReturnNode";
if (expr) {
ss << "\n" << expr->toString(indent + 2);
}
return ss.str();
}
// BlockNode Processing
std::string BlockNode::toString(int indent) const {
std::ostringstream ss;
ss << pad(indent) << "BlockNode\n";
for (const auto& s : statements)
ss << s->toString(indent + 2) << "\n";
return ss.str();
}