Line data Source code
1 : #include "../../../inc/visitors/printTree/printTree.h"
2 : #include "../../../inc/parsingAnalysis/ast/tree.h"
3 : #include <ostream>
4 :
5 : namespace nicole {
6 :
7 : std::expected<std::string, Error>
8 0 : PrintTree::visit(const AST_STATEMENT *node) const noexcept {
9 0 : if (!node) {
10 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_STATEMENT");
11 0 : }
12 0 : std::ostringstream result;
13 0 : result << indent_ << "statement:\n";
14 0 : increaseIndent();
15 0 : const auto expr{node->expression()->accept(*this)};
16 0 : if (!expr) {
17 0 : return createError(expr.error());
18 0 : }
19 0 : result << *expr;
20 0 : decreaseIndent();
21 0 : return result.str();
22 0 : }
23 :
24 : std::expected<std::string, Error>
25 0 : PrintTree::visit(const AST_BODY *node) const noexcept {
26 0 : if (!node) {
27 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_BODY");
28 0 : }
29 0 : std::ostringstream result;
30 0 : result << indent_ << "Body:\n";
31 0 : increaseIndent();
32 0 : for (const auto &statement : node->body()) {
33 0 : const auto val{statement->accept(*this)};
34 0 : if (!val) {
35 0 : return createError(val.error());
36 0 : }
37 0 : result << *val;
38 0 : }
39 0 : decreaseIndent();
40 0 : return result.str();
41 0 : }
42 :
43 : std::expected<std::string, Error>
44 0 : PrintTree::visit(const Tree *tree) const noexcept {
45 0 : if (!tree) {
46 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid Tree");
47 0 : }
48 0 : std::ostringstream result;
49 0 : result << "Tree:\n";
50 0 : increaseIndent();
51 0 : const auto str{tree->root()->accept(*this)};
52 0 : if (!str) {
53 0 : return createError(str.error());
54 0 : }
55 0 : result << *str;
56 0 : decreaseIndent();
57 0 : return result.str();
58 0 : }
59 :
60 : } // namespace nicole
|