Line data Source code
1 : #include "../../../inc/visitors/fillSemanticInfo/fillSemanticInfo.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 <variant>
9 :
10 : namespace nicole {
11 :
12 : std::expected<std::monostate, Error>
13 0 : FillSemanticInfo::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 : const auto condition{node->condition()->accept(*this)};
18 0 : if (!condition) {
19 0 : return createError(condition.error());
20 0 : }
21 0 : pushScope();
22 0 : node->body()->setScope(currentScope_);
23 0 : const auto body{node->body()->accept(*this)};
24 0 : if (!body) {
25 0 : return createError(body.error());
26 0 : }
27 0 : popScope();
28 0 : for (const auto &elseIf : node->elseIf()) {
29 0 : const auto result{elseIf->accept(*this)};
30 0 : if (!result) {
31 0 : return createError(result.error());
32 0 : }
33 0 : }
34 0 : if (node->elseBody()) {
35 0 : pushScope();
36 0 : node->elseBody()->setScope(currentScope_);
37 0 : const auto elseBody{node->elseBody()->accept(*this)};
38 0 : if (!elseBody) {
39 0 : return createError(elseBody.error());
40 0 : }
41 0 : popScope();
42 0 : }
43 0 : return {};
44 0 : }
45 :
46 : std::expected<std::monostate, Error>
47 0 : FillSemanticInfo::visit(const AST_ELSE_IF *node) const noexcept {
48 0 : if (!node) {
49 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_ELSE_IF");
50 0 : }
51 0 : const auto condition{node->condition()->accept(*this)};
52 0 : if (!condition) {
53 0 : return createError(condition.error());
54 0 : }
55 0 : pushScope();
56 0 : node->body()->setScope(currentScope_);
57 0 : const auto body{node->body()->accept(*this)};
58 0 : if (!body) {
59 0 : return createError(body.error());
60 0 : }
61 0 : popScope();
62 0 : return {};
63 0 : }
64 :
65 : std::expected<std::monostate, Error>
66 0 : FillSemanticInfo::visit(const AST_SWITCH *node) const noexcept {
67 0 : if (!node) {
68 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_SWITCH");
69 0 : }
70 0 : const auto condition{node->condition()->accept(*this)};
71 0 : if (!condition) {
72 0 : return createError(condition.error());
73 0 : }
74 0 : for (const auto &case_ : node->cases()) {
75 0 : const auto result{case_->accept(*this)};
76 0 : if (!result) {
77 0 : return createError(result.error());
78 0 : }
79 0 : }
80 0 : if (node->defaultCase()) {
81 0 : const auto defaultCase{node->defaultCase()->accept(*this)};
82 0 : if (!defaultCase) {
83 0 : return createError(defaultCase.error());
84 0 : }
85 0 : }
86 0 : return {};
87 0 : }
88 :
89 : std::expected<std::monostate, Error>
90 0 : FillSemanticInfo::visit(const AST_CASE *node) const noexcept {
91 0 : if (!node) {
92 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_CASE");
93 0 : }
94 0 : const auto match{node->match()->accept(*this)};
95 0 : if (!match) {
96 0 : return createError(match.error());
97 0 : }
98 0 : pushScope();
99 0 : node->body()->setScope(currentScope_);
100 0 : const auto body{node->body()->accept(*this)};
101 0 : if (!body) {
102 0 : return createError(body.error());
103 0 : }
104 0 : popScope();
105 0 : return {};
106 0 : }
107 :
108 : std::expected<std::monostate, Error>
109 0 : FillSemanticInfo::visit(const AST_DEFAULT *node) const noexcept {
110 0 : if (!node) {
111 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_DEFAULT");
112 0 : }
113 0 : pushScope();
114 0 : node->body()->setScope(currentScope_);
115 0 : const auto body{node->body()->accept(*this)};
116 0 : if (!body) {
117 0 : return createError(body.error());
118 0 : }
119 0 : popScope();
120 0 : return {};
121 0 : }
122 :
123 : std::expected<std::monostate, Error>
124 0 : FillSemanticInfo::visit(const AST_TERNARY *node) const noexcept {
125 0 : if (!node) {
126 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_TERNARY");
127 0 : }
128 0 : const auto condition{node->condition()->accept(*this)};
129 0 : if (!condition) {
130 0 : return createError(condition.error());
131 0 : }
132 0 : const auto first{node->first()->accept(*this)};
133 0 : if (!first) {
134 0 : return createError(first.error());
135 0 : }
136 0 : const auto second{node->second()->accept(*this)};
137 0 : if (!second) {
138 0 : return createError(second.error());
139 0 : }
140 0 : return {};
141 0 : }
142 :
143 : std::expected<std::monostate, Error>
144 0 : FillSemanticInfo::visit(const AST_CONDITION *node) const noexcept {
145 0 : if (!node) {
146 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_CONDITION");
147 0 : }
148 0 : return node->condition()->accept(*this);
149 0 : }
150 :
151 :
152 : }
|