Line data Source code
1 : #include "../../../inc/visitors/printTree/printTree.h"
2 : #include "../../../inc/parsingAnalysis/ast/conditionals/ast_case.h"
3 : #include "../../../inc/parsingAnalysis/ast/conditionals/ast_default.h"
4 : #include "../../../inc/parsingAnalysis/ast/conditionals/ast_elseIf.h"
5 : #include "../../../inc/parsingAnalysis/ast/conditionals/ast_if.h"
6 : #include "../../../inc/parsingAnalysis/ast/conditionals/ast_switch.h"
7 : #include "../../../inc/parsingAnalysis/ast/conditionals/ast_ternary.h"
8 : #include <ostream>
9 :
10 : namespace nicole {
11 :
12 : std::expected<std::string, Error>
13 0 : PrintTree::visit(const AST_IF *node) const noexcept {
14 0 : if (!node) {
15 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_IF");
16 0 : }
17 0 : std::ostringstream result;
18 0 : result << indent_ << "If Statement:\n";
19 0 : increaseIndent();
20 0 : const auto condition{node->condition()->accept(*this)};
21 0 : if (!condition) {
22 0 : return createError(condition.error());
23 0 : }
24 0 : result << *condition;
25 :
26 0 : for (const auto &statement : node->body()->body()) {
27 0 : const auto stmt{statement->accept(*this)};
28 0 : if (!stmt) {
29 0 : return createError(stmt.error());
30 0 : }
31 0 : result << *stmt;
32 0 : }
33 :
34 0 : if (node->elseIf().size() > 0) {
35 0 : result << indent_ << "Else Ifs:\n";
36 0 : for (const auto &elseIf : node->elseIf()) {
37 0 : const auto elseIfStr{elseIf->accept(*this)};
38 0 : if (!elseIfStr) {
39 0 : return createError(elseIfStr.error());
40 0 : }
41 0 : result << *elseIfStr;
42 0 : }
43 0 : }
44 :
45 0 : if (node->elseBody()) {
46 0 : result << indent_ << "Else:\n";
47 0 : for (const auto &statement : node->elseBody()->body()) {
48 0 : const auto stmt{statement->accept(*this)};
49 0 : if (!stmt) {
50 0 : return createError(stmt.error());
51 0 : }
52 0 : result << *stmt;
53 0 : }
54 0 : }
55 :
56 0 : decreaseIndent();
57 0 : return result.str();
58 0 : }
59 :
60 : std::expected<std::string, Error>
61 0 : PrintTree::visit(const AST_ELSE_IF *node) const noexcept {
62 0 : if (!node) {
63 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_ELSE_IF");
64 0 : }
65 0 : std::ostringstream result;
66 0 : result << indent_ << "Else If:\n";
67 0 : increaseIndent();
68 0 : const auto condition{node->condition()->accept(*this)};
69 0 : if (!condition) {
70 0 : return createError(condition.error());
71 0 : }
72 0 : result << *condition;
73 :
74 0 : for (const auto &statement : node->body()->body()) {
75 0 : const auto stmt{statement->accept(*this)};
76 0 : if (!stmt) {
77 0 : return createError(stmt.error());
78 0 : }
79 0 : result << *stmt;
80 0 : }
81 :
82 0 : decreaseIndent();
83 0 : return result.str();
84 0 : }
85 :
86 : std::expected<std::string, Error>
87 0 : PrintTree::visit(const AST_SWITCH *node) const noexcept {
88 0 : if (!node) {
89 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_SWITCH");
90 0 : }
91 0 : std::ostringstream result;
92 0 : result << indent_ << "Switch Statement:\n";
93 0 : increaseIndent();
94 0 : const auto expr{node->condition()->accept(*this)};
95 0 : if (!expr) {
96 0 : return createError(expr.error());
97 0 : }
98 0 : result << *expr;
99 :
100 0 : result << indent_ << "Cases:\n";
101 0 : for (const auto &caseStmt : node->cases()) {
102 0 : const auto caseStr{caseStmt->accept(*this)};
103 0 : if (!caseStr) {
104 0 : return createError(caseStr.error());
105 0 : }
106 0 : result << *caseStr;
107 0 : }
108 0 : if (node->defaultCase()) {
109 0 : const auto defaultCase{node->defaultCase()->accept(*this)};
110 0 : if (!defaultCase) {
111 0 : return createError(defaultCase.error());
112 0 : }
113 0 : result << *defaultCase;
114 0 : }
115 0 : decreaseIndent();
116 0 : return result.str();
117 0 : }
118 :
119 : std::expected<std::string, Error>
120 0 : PrintTree::visit(const AST_CASE *node) const noexcept {
121 0 : if (!node) {
122 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_CASE");
123 0 : }
124 0 : std::ostringstream result;
125 0 : result << indent_ << "Case:\n";
126 0 : increaseIndent();
127 0 : const auto cond{node->match()->accept(*this)};
128 0 : if (!cond) {
129 0 : return createError(cond.error());
130 0 : }
131 0 : result << *cond;
132 :
133 0 : for (const auto &statement : node->body()->body()) {
134 0 : const auto stmt{statement->accept(*this)};
135 0 : if (!stmt) {
136 0 : return createError(stmt.error());
137 0 : }
138 0 : result << *stmt;
139 0 : }
140 0 : decreaseIndent();
141 0 : return result.str();
142 0 : }
143 :
144 : std::expected<std::string, Error>
145 0 : PrintTree::visit(const AST_DEFAULT *node) const noexcept {
146 0 : if (!node) {
147 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_DEFAULT");
148 0 : }
149 0 : std::ostringstream result;
150 0 : result << indent_ << "Default Case:\n";
151 0 : increaseIndent();
152 0 : for (const auto &statement : node->body()->body()) {
153 0 : const auto stmt{statement->accept(*this)};
154 0 : if (!stmt) {
155 0 : return createError(stmt.error());
156 0 : }
157 0 : result << *stmt;
158 0 : }
159 0 : decreaseIndent();
160 0 : return result.str();
161 0 : }
162 :
163 : std::expected<std::string, Error>
164 0 : PrintTree::visit(const AST_TERNARY *node) const noexcept {
165 0 : if (!node) {
166 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_TERNARY");
167 0 : }
168 0 : std::ostringstream result;
169 0 : result << indent_ << "Ternary Expression:\n";
170 0 : increaseIndent();
171 0 : const auto cond{node->condition()->accept(*this)};
172 0 : if (!cond) {
173 0 : return createError(cond.error());
174 0 : }
175 0 : result << *cond;
176 :
177 0 : result << indent_ << "True Branch:\n";
178 0 : const auto trueBranch{node->first()->accept(*this)};
179 0 : if (!trueBranch) {
180 0 : return createError(trueBranch.error());
181 0 : }
182 0 : result << *trueBranch;
183 :
184 0 : result << indent_ << "False Branch:\n";
185 0 : const auto falseBranch{node->second()->accept(*this)};
186 0 : if (!falseBranch) {
187 0 : return createError(falseBranch.error());
188 0 : }
189 0 : result << *falseBranch;
190 0 : decreaseIndent();
191 0 : return result.str();
192 0 : }
193 :
194 : std::expected<std::string, Error>
195 0 : PrintTree::visit(const AST_CONDITION *node) const noexcept {
196 0 : if (!node) {
197 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_CONDITION");
198 0 : }
199 0 : std::ostringstream result;
200 0 : result << indent_ << "Condition:\n";
201 0 : increaseIndent();
202 0 : const auto expr{node->condition()->accept(*this)};
203 0 : if (!expr) {
204 0 : return createError(expr.error());
205 0 : }
206 0 : result << *expr;
207 0 : decreaseIndent();
208 0 : return result.str();
209 0 : }
210 :
211 : }
|