Line data Source code
1 : #include "../../../inc/visitors/printTree/printTree.h"
2 : #include "../../../inc/parsingAnalysis/ast/userTypes/ast_attrAccess.h"
3 : #include "../../../inc/parsingAnalysis/ast/userTypes/ast_constructorCall.h"
4 : #include "../../../inc/parsingAnalysis/ast/userTypes/ast_methodCall.h"
5 : #include "../../../inc/parsingAnalysis/ast/userTypes/ast_struct.h"
6 : #include "../../../inc/parsingAnalysis/ast/userTypes/ast_this.h"
7 : #include <ostream>
8 :
9 : namespace nicole {
10 :
11 : std::expected<std::string, Error>
12 0 : PrintTree::visit(const AST_STRUCT *node) const noexcept {
13 0 : if (!node) {
14 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_STRUCT");
15 0 : }
16 :
17 0 : std::ostringstream result;
18 0 : result << indent_ << "Struct Declaration:\n";
19 0 : increaseIndent();
20 :
21 : // Nombre de la estructura
22 0 : result << indent_ << "Name: " << node->id() << "\n";
23 0 : result << indent_ << "Generics: ";
24 0 : for (const auto &generic : node->generics()) {
25 0 : result << generic.name() << ", ";
26 0 : }
27 0 : result << "\n";
28 : // Tipo padre (si existe)
29 0 : if (node->fatherType()) {
30 0 : result << indent_ << "Father Type: " << node->fatherType()->toString()
31 0 : << "\n";
32 0 : } else {
33 0 : result << indent_ << "No Father Type\n";
34 0 : }
35 :
36 : // Atributos
37 0 : result << indent_ << "Attributes:\n";
38 0 : increaseIndent();
39 0 : for (const auto &[name, type] : node->attributes().params()) {
40 0 : result << indent_ << name << " " << type->toString() << "\n";
41 0 : }
42 0 : decreaseIndent();
43 :
44 : // Métodos
45 0 : result << indent_ << "Methods:\n";
46 0 : increaseIndent();
47 0 : for (const auto &method : node->methods()) {
48 0 : const auto methodStr = method->accept(*this);
49 0 : if (!methodStr) {
50 0 : return createError(methodStr.error());
51 0 : }
52 0 : result << *methodStr;
53 0 : }
54 0 : decreaseIndent();
55 :
56 : // Constructor
57 0 : if (node->constructor()) {
58 0 : increaseIndent();
59 0 : const auto ctorStr = node->constructor()->accept(*this);
60 0 : if (!ctorStr) {
61 0 : return createError(ctorStr.error());
62 0 : }
63 0 : result << *ctorStr;
64 0 : decreaseIndent();
65 0 : }
66 :
67 : // Destructor
68 0 : if (node->destructor()) {
69 0 : increaseIndent();
70 0 : const auto dtorStr = node->destructor()->accept(*this);
71 0 : if (!dtorStr) {
72 0 : return createError(dtorStr.error());
73 0 : }
74 0 : result << *dtorStr;
75 0 : decreaseIndent();
76 0 : }
77 :
78 0 : decreaseIndent();
79 0 : return result.str();
80 0 : }
81 :
82 : std::expected<std::string, Error>
83 0 : PrintTree::visit(const AST_ATTR_ACCESS *node) const noexcept {
84 0 : if (!node) {
85 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_ATTR_ACCESS");
86 0 : }
87 0 : std::ostringstream result;
88 0 : result << indent_ << "attr access:\n";
89 0 : increaseIndent();
90 0 : result << indent_ << node->id() << "\n";
91 0 : decreaseIndent();
92 0 : return result.str();
93 0 : }
94 :
95 : std::expected<std::string, Error>
96 0 : PrintTree::visit(const AST_METHOD_CALL *node) const noexcept {
97 0 : if (!node) {
98 0 : return createError(ERROR_TYPE::NULL_NODE, "Invalid AST_METHOD_CALL");
99 0 : }
100 :
101 0 : std::ostringstream result;
102 0 : result << indent_ << "Method Call:\n";
103 0 : increaseIndent();
104 0 : result << indent_ << "Name: " << node->id() << "\n";
105 0 : result << indent_ << "Replacement of generics:\n" << indent_;
106 0 : for (const auto &generic : node->replaceOfGenerics()) {
107 0 : result << generic->toString() << ", ";
108 0 : }
109 0 : result << "\n";
110 0 : result << indent_ << "Arguments:\n";
111 0 : increaseIndent();
112 0 : for (const auto &arg : node->parameters()) {
113 0 : const auto argStr = arg->accept(*this);
114 0 : if (!argStr) {
115 0 : return createError(argStr.error());
116 0 : }
117 0 : result << *argStr;
118 0 : }
119 0 : decreaseIndent();
120 0 : decreaseIndent();
121 :
122 0 : return result.str();
123 0 : }
124 :
125 : std::expected<std::string, Error>
126 0 : PrintTree::visit(const AST_METHOD_DECL *node) const noexcept {
127 0 : if (!node) {
128 0 : return createError(ERROR_TYPE::NULL_NODE, "Invalid AST_METHOD_DECL");
129 0 : }
130 :
131 0 : std::ostringstream result;
132 0 : result << indent_ << "Method Declaration:\n";
133 0 : increaseIndent();
134 0 : result << indent_ << "Name: " << node->id() << "\n";
135 0 : result << indent_ << "Generics: ";
136 0 : for (const auto &generic : node->generics()) {
137 0 : result << generic.name() << ", ";
138 0 : }
139 0 : result << "\n";
140 0 : result << indent_ << "Return Type: " << node->returnType()->toString()
141 0 : << "\n";
142 0 : result << indent_ << "Virtual: " << (node->isVirtual() ? "true" : "false")
143 0 : << "\n";
144 0 : result << indent_ << "Parameters:\n";
145 0 : increaseIndent();
146 0 : for (const auto ¶m : node->parameters()) {
147 0 : result << indent_ << "Param: " << param.first << " type: " << param.second->toString()
148 0 : << "\n";
149 0 : }
150 0 : decreaseIndent();
151 :
152 0 : if (node->body()) {
153 0 : const auto bodyStr{node->body()->accept(*this)};
154 0 : if (!bodyStr) {
155 0 : return createError(bodyStr.error());
156 0 : }
157 0 : result << *bodyStr;
158 0 : } else {
159 0 : result << indent_ << "(empty body)\n";
160 0 : }
161 0 : decreaseIndent();
162 :
163 0 : return result.str();
164 0 : }
165 :
166 : std::expected<std::string, Error>
167 0 : PrintTree::visit(const AST_CONSTRUCTOR_DECL *node) const noexcept {
168 0 : if (!node) {
169 0 : return createError(ERROR_TYPE::NULL_NODE, "Invalid AST_CONSTRUCTOR_DECL");
170 0 : }
171 :
172 0 : std::ostringstream result;
173 0 : result << indent_ << "Constructor Decl:\n";
174 0 : increaseIndent();
175 0 : result << indent_ << "Name: " << node->id() << "\n";
176 0 : result << indent_ << "Generics: ";
177 0 : for (const auto &generic : node->generics()) {
178 0 : result << generic.name() << ", ";
179 0 : }
180 0 : result << "\n";
181 0 : result << indent_ << "Parameters:\n";
182 0 : increaseIndent();
183 0 : for (const auto ¶m : node->parameters()) {
184 0 : result << indent_ << "Param: " << param.first
185 0 : << " type: " << param.second->toString() << "\n";
186 0 : }
187 0 : decreaseIndent();
188 :
189 0 : if (node->super()) {
190 0 : const auto super{node->super()->accept(*this)};
191 0 : if (!super) {
192 0 : return createError(super.error());
193 0 : }
194 0 : result << *super;
195 0 : }
196 :
197 0 : if (node->body()) {
198 0 : const auto bodyStr{node->body()->accept(*this)};
199 0 : if (!bodyStr) {
200 0 : return createError(bodyStr.error());
201 0 : }
202 0 : result << *bodyStr;
203 0 : } else {
204 0 : result << indent_ << "(empty body)\n";
205 0 : }
206 0 : decreaseIndent();
207 :
208 0 : return result.str();
209 0 : }
210 :
211 : std::expected<std::string, Error>
212 0 : PrintTree::visit(const AST_SUPER *node) const noexcept {
213 0 : if (!node) {
214 0 : return createError(ERROR_TYPE::NULL_NODE, "Invalid AST_SUPER");
215 0 : }
216 0 : std::ostringstream result;
217 0 : result << indent_ << "Super:\n";
218 0 : result << indent_ << "Type: " + node->fatherType()->toString() << "\n";
219 0 : result << indent_ << "Replacement of generics:\n";
220 0 : for (const auto &generic : node->replacements()) {
221 0 : result << indent_ << generic->toString() << ", ";
222 0 : }
223 0 : result << "\n";
224 0 : result << indent_ << "Arguments:\n";
225 0 : increaseIndent();
226 0 : for (const auto &arg : node->arguments()) {
227 0 : const auto argStr = arg->accept(*this);
228 0 : if (!argStr) {
229 0 : return createError(argStr.error());
230 0 : }
231 0 : result << *argStr;
232 0 : }
233 0 : decreaseIndent();
234 0 : return result.str();
235 0 : }
236 :
237 : std::expected<std::string, Error>
238 0 : PrintTree::visit(const AST_DESTRUCTOR_DECL *node) const noexcept {
239 0 : if (!node) {
240 0 : return createError(ERROR_TYPE::NULL_NODE, "Invalid AST_DESTRUCTOR_DECL");
241 0 : }
242 :
243 0 : std::ostringstream result;
244 0 : result << indent_ << "Destructor Decl:\n";
245 0 : increaseIndent();
246 0 : result << indent_ << "Name: " << node->id() << "\n";
247 0 : if (node->body()) {
248 0 : const auto bodyStr{node->body()->accept(*this)};
249 0 : if (!bodyStr) {
250 0 : return createError(bodyStr.error());
251 0 : }
252 0 : result << *bodyStr;
253 0 : } else {
254 0 : result << indent_ << "(empty body)\n";
255 0 : }
256 0 : decreaseIndent();
257 :
258 0 : return result.str();
259 0 : }
260 :
261 : std::expected<std::string, Error>
262 0 : PrintTree::visit(const AST_THIS *node) const noexcept {
263 0 : if (!node) {
264 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_THIS");
265 0 : }
266 0 : std::ostringstream result;
267 0 : result << indent_ << "this call\n";
268 0 : return result.str();
269 0 : }
270 :
271 : std::expected<std::string, Error>
272 0 : PrintTree::visit(const AST_CONSTRUCTOR_CALL *node) const noexcept {
273 0 : if (!node) {
274 0 : return createError(ERROR_TYPE::NULL_NODE, "Invalid AST_CONSTRUCTOR_CALL");
275 0 : }
276 :
277 0 : std::ostringstream result;
278 0 : result << indent_ << "Constructor Call:\n";
279 0 : increaseIndent();
280 0 : result << indent_ << "Name: " << node->id() << "\n";
281 0 : result << indent_ << "Replacement of generics:\n" << indent_;
282 0 : for (const auto &generic : node->replaceOfGenerics()) {
283 0 : result << generic->toString() << ", ";
284 0 : }
285 0 : result << "\n";
286 0 : result << indent_ << "Arguments:\n";
287 0 : increaseIndent();
288 0 : for (const auto &arg : node->parameters()) {
289 0 : const auto argStr = arg->accept(*this);
290 0 : if (!argStr) {
291 0 : return createError(argStr.error());
292 0 : }
293 0 : result << *argStr;
294 0 : }
295 0 : decreaseIndent();
296 0 : decreaseIndent();
297 :
298 0 : return result.str();
299 0 : }
300 :
301 : }
|