Line data Source code
1 : #include "../../../inc/visitors/printTree/printTree.h"
2 : #include "../../../inc/parsingAnalysis/ast/functions/ast_funcCall.h"
3 : #include "../../../inc/parsingAnalysis/ast/functions/ast_funcDecl.h"
4 : #include "../../../inc/parsingAnalysis/ast/functions/ast_return.h"
5 : #include <ostream>
6 :
7 : namespace nicole {
8 :
9 : std::expected<std::string, Error>
10 0 : PrintTree::visit(const AST_FUNC_CALL *node) const noexcept {
11 0 : if (!node) {
12 0 : return createError(ERROR_TYPE::NULL_NODE, "Invalid AST_FUNC_CALL");
13 0 : }
14 :
15 0 : std::ostringstream result;
16 0 : result << indent_ << "Function Call:\n";
17 0 : increaseIndent();
18 0 : result << indent_ << "Name: " << node->id() << "\n";
19 0 : result << indent_ << "Replacement of generics:\n" << indent_;
20 0 : for (const auto &generic : node->replaceOfGenerics()) {
21 0 : result << generic->toString() << ", ";
22 0 : }
23 0 : result << "\n";
24 0 : result << indent_ << "Arguments:\n";
25 0 : increaseIndent();
26 0 : for (const auto &arg : node->parameters()) {
27 0 : const auto argStr = arg->accept(*this);
28 0 : if (!argStr) {
29 0 : return createError(argStr.error());
30 0 : }
31 0 : result << *argStr;
32 0 : }
33 0 : decreaseIndent();
34 0 : decreaseIndent();
35 :
36 0 : return result.str();
37 0 : }
38 :
39 : std::expected<std::string, Error>
40 0 : PrintTree::visit(const AST_FUNC_DECL *node) const noexcept {
41 0 : if (!node) {
42 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_FUNC_DECL");
43 0 : }
44 0 : std::ostringstream result;
45 0 : result << indent_ << "Function Declaration:\n";
46 0 : increaseIndent();
47 0 : result << indent_ << "Name: " << node->id() << "\n";
48 0 : result << indent_ << "Generics: ";
49 0 : for (const auto &generic : node->generics()) {
50 0 : result << generic.name() << ", ";
51 0 : }
52 0 : result << "\n";
53 0 : result << indent_ << "Return Type: " << node->returnType()->toString()
54 0 : << "\n";
55 :
56 0 : result << indent_ << "Parameters:\n";
57 0 : increaseIndent();
58 0 : for (const auto ¶m : node->parameters()) {
59 0 : result << indent_ << "Param: " << param.first << " type: " << param.second->toString()
60 0 : << "\n";
61 0 : }
62 0 : decreaseIndent();
63 :
64 0 : if (node->body()) {
65 0 : const auto bodyStr{node->body()->accept(*this)};
66 0 : if (!bodyStr) {
67 0 : return createError(bodyStr.error());
68 0 : }
69 0 : result << *bodyStr;
70 0 : } else {
71 0 : result << indent_ << "(empty body)\n";
72 0 : }
73 0 : decreaseIndent();
74 :
75 0 : return result.str();
76 0 : }
77 :
78 : std::expected<std::string, Error>
79 0 : PrintTree::visit(const AST_RETURN *node) const noexcept {
80 0 : if (!node) {
81 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_RETURN");
82 0 : }
83 0 : std::ostringstream result;
84 0 : result << indent_ << "Return:\n";
85 0 : increaseIndent();
86 0 : if (node->expression()) {
87 0 : const auto val{node->expression()->accept(*this)};
88 0 : if (!val) {
89 0 : return createError(val.error());
90 0 : }
91 0 : result << indent_ << "value:\n" << *val;
92 0 : } else {
93 0 : result << indent_ << "empty";
94 0 : }
95 0 : decreaseIndent();
96 0 : return result.str();
97 0 : }
98 :
99 : }
|