Line data Source code
1 : #include "../../../inc/visitors/printTree/printTree.h"
2 : #include "../../../inc/parsingAnalysis/ast/operators/ast_binary.h"
3 : #include "../../../inc/parsingAnalysis/ast/operators/ast_unary.h"
4 : #include <ostream>
5 :
6 : namespace nicole {
7 :
8 : std::expected<std::string, Error>
9 0 : PrintTree::visit(const AST_BINARY *node) const noexcept {
10 0 : if (!node) {
11 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_BINARY");
12 0 : }
13 0 : std::ostringstream result;
14 0 : result << indent_ << node->op().raw() + ":\n";
15 0 : increaseIndent();
16 0 : const auto left{node->left()->accept(*this)};
17 0 : if (!left) {
18 0 : return createError(left.error());
19 0 : }
20 0 : result << indent_ << "Left:\n" << *left;
21 0 : const auto right{node->right()->accept(*this)};
22 0 : if (!right) {
23 0 : return createError(right.error());
24 0 : }
25 0 : result << indent_ << "Right:\n" << *right;
26 0 : decreaseIndent();
27 0 : return result.str();
28 0 : }
29 :
30 : std::expected<std::string, Error>
31 0 : PrintTree::visit(const AST_UNARY *node) const noexcept {
32 0 : if (!node) {
33 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_UNARY");
34 0 : }
35 0 : std::ostringstream result;
36 0 : result << indent_ << node->op().raw() + ":\n";
37 0 : increaseIndent();
38 0 : const auto val{node->value()->accept(*this)};
39 0 : if (!val) {
40 0 : return createError(val.error());
41 0 : }
42 0 : result << indent_ << "value:\n" << *val;
43 0 : decreaseIndent();
44 0 : return result.str();
45 0 : }
46 :
47 : }
|