Line data Source code
1 : #include "../../../inc/visitors/printTree/printTree.h"
2 : #include "../../../inc/parsingAnalysis/ast/loops/ast_doWhile.h"
3 : #include "../../../inc/parsingAnalysis/ast/loops/ast_for.h"
4 : #include "../../../inc/parsingAnalysis/ast/loops/ast_pass.h"
5 : #include "../../../inc/parsingAnalysis/ast/loops/ast_stop.h"
6 : #include "../../../inc/parsingAnalysis/ast/loops/ast_while.h"
7 : #include <ostream>
8 :
9 : namespace nicole {
10 :
11 : std::expected<std::string, Error>
12 0 : PrintTree::visit(const AST_WHILE *node) const noexcept {
13 0 : if (!node) {
14 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_WHILE");
15 0 : }
16 0 : std::ostringstream result;
17 0 : result << indent_ << "While Loop:\n";
18 0 : increaseIndent();
19 :
20 0 : increaseIndent();
21 0 : const auto condition{node->condition()->accept(*this)};
22 0 : if (!condition) {
23 0 : return createError(condition.error());
24 0 : }
25 0 : result << *condition;
26 0 : decreaseIndent();
27 :
28 0 : for (const auto &statement : node->body()->body()) {
29 0 : const auto val{statement->accept(*this)};
30 0 : if (!val) {
31 0 : return createError(val.error());
32 0 : }
33 0 : result << *val;
34 0 : }
35 :
36 0 : decreaseIndent();
37 0 : return result.str();
38 0 : }
39 :
40 : std::expected<std::string, Error>
41 0 : PrintTree::visit(const AST_FOR *node) const noexcept {
42 0 : if (!node) {
43 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_FOR");
44 0 : }
45 0 : std::ostringstream result;
46 0 : result << indent_ << "For Loop:\n";
47 0 : increaseIndent();
48 :
49 0 : result << indent_ << "Init:\n";
50 0 : increaseIndent();
51 0 : for (const auto &initExpr : node->init()) {
52 0 : const auto initStr{initExpr->accept(*this)};
53 0 : if (!initStr) {
54 0 : return createError(initStr.error());
55 0 : }
56 0 : result << *initStr;
57 0 : }
58 0 : decreaseIndent();
59 :
60 0 : const auto condition{node->condition()->accept(*this)};
61 0 : if (!condition) {
62 0 : return createError(condition.error());
63 0 : }
64 0 : result << *condition;
65 :
66 0 : result << indent_ << "Update:\n";
67 0 : increaseIndent();
68 0 : for (const auto &updateExpr : node->update()) {
69 0 : const auto updateStr{updateExpr->accept(*this)};
70 0 : if (!updateStr) {
71 0 : return createError(updateStr.error());
72 0 : }
73 0 : result << *updateStr;
74 0 : }
75 0 : decreaseIndent();
76 :
77 0 : for (const auto &statement : node->body()->body()) {
78 0 : const auto val{statement->accept(*this)};
79 0 : if (!val) {
80 0 : return createError(val.error());
81 0 : }
82 0 : result << *val;
83 0 : }
84 :
85 0 : decreaseIndent();
86 0 : return result.str();
87 0 : }
88 :
89 : std::expected<std::string, Error>
90 0 : PrintTree::visit(const AST_DO_WHILE *node) const noexcept {
91 0 : if (!node) {
92 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_DO_WHILE");
93 0 : }
94 0 : std::ostringstream result;
95 0 : result << indent_ << "Do While Loop:\n";
96 0 : increaseIndent();
97 :
98 0 : for (const auto &statement : node->body()->body()) {
99 0 : const auto val{statement->accept(*this)};
100 0 : if (!val) {
101 0 : return createError(val.error());
102 0 : }
103 0 : result << *val;
104 0 : }
105 0 : const auto condition{node->condition()->accept(*this)};
106 0 : if (!condition) {
107 0 : return createError(condition.error());
108 0 : }
109 0 : result << *condition;
110 0 : decreaseIndent();
111 0 : return result.str();
112 0 : }
113 :
114 : std::expected<std::string, Error>
115 0 : PrintTree::visit(const AST_PASS *node) const noexcept {
116 0 : if (!node) {
117 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_PASS");
118 0 : }
119 0 : std::ostringstream result;
120 0 : increaseIndent();
121 0 : result << indent_ << "Pass Statement:\n";
122 0 : decreaseIndent();
123 0 : return result.str();
124 0 : }
125 :
126 : std::expected<std::string, Error>
127 0 : PrintTree::visit(const AST_STOP *node) const noexcept {
128 0 : if (!node) {
129 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_STOP");
130 0 : }
131 0 : std::ostringstream result;
132 0 : increaseIndent();
133 0 : result << indent_ << "Stop Statement:\n";
134 0 : decreaseIndent();
135 0 : return result.str();
136 0 : }
137 :
138 : }
|