Line data Source code
1 : #include "../../../inc/parsingAnalysis/ast/loops/ast_doWhile.h"
2 : #include "../../../inc/parsingAnalysis/ast/loops/ast_for.h"
3 : #include "../../../inc/parsingAnalysis/ast/loops/ast_pass.h"
4 : #include "../../../inc/parsingAnalysis/ast/loops/ast_stop.h"
5 : #include "../../../inc/parsingAnalysis/ast/loops/ast_while.h"
6 : #include "../../../inc/visitors/typeAnalysis/typeAnalysis.h"
7 : #include <memory>
8 :
9 : namespace nicole {
10 :
11 : /*
12 : - condtion debe retornar bool
13 : - si body encuentra algo diferente a NoPropagate lo propaga si no retorna
14 : NoPropagate
15 : */
16 : std::expected<std::shared_ptr<Type>, Error>
17 0 : TypeAnalysis::visit(const AST_WHILE *node) const noexcept {
18 0 : if (!node) {
19 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_WHILE");
20 0 : }
21 0 : auto condition = node->condition()->accept(*this);
22 0 : if (!condition)
23 0 : return createError(condition.error());
24 0 : auto condType = condition.value();
25 :
26 0 : if (auto constCond = std::dynamic_pointer_cast<ConstType>(condType))
27 0 : condType = constCond->baseType();
28 :
29 0 : bool isGeneric{false};
30 0 : if (insideDeclWithGenerics &&
31 0 : typeTable_->isGenericType(condType, currentGenericList_)) {
32 0 : isGeneric = true;
33 : // return condType;
34 0 : }
35 :
36 0 : if (!isGeneric) {
37 0 : if (!typeTable_->areSameType(condType, typeTable_->boolType()))
38 0 : return createError(ERROR_TYPE::TYPE, "a condition must be boolean");
39 0 : }
40 :
41 0 : auto body = node->body()->accept(*this);
42 0 : if (!body)
43 0 : return createError(body.error());
44 0 : auto bodyType = body.value();
45 :
46 0 : if (!typeTable_->areSameType(bodyType, typeTable_->noPropagateType()) &&
47 0 : !typeTable_->areSameType(bodyType, typeTable_->breakType())) {
48 0 : node->setReturnedFromAnalysis(bodyType);
49 0 : return bodyType;
50 0 : }
51 0 : node->setReturnedFromAnalysis(typeTable_->noPropagateType());
52 0 : return typeTable_->noPropagateType();
53 0 : }
54 :
55 : /*
56 : - init ???
57 : - condtion reotrna bool
58 : - update ???
59 : - body lo mismo que los otros
60 : */
61 : std::expected<std::shared_ptr<Type>, Error>
62 0 : TypeAnalysis::visit(const AST_FOR *node) const noexcept {
63 0 : if (!node) {
64 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_FOR");
65 0 : }
66 0 : if (!node->init().empty()) {
67 0 : for (const auto &expr : node->init()) {
68 0 : auto initResult = expr->accept(*this);
69 0 : if (!initResult)
70 0 : return createError(initResult.error());
71 0 : }
72 0 : }
73 :
74 0 : auto condition = node->condition()->accept(*this);
75 0 : if (!condition)
76 0 : return createError(condition.error());
77 0 : auto condType = condition.value();
78 :
79 0 : if (auto constCond = std::dynamic_pointer_cast<ConstType>(condType))
80 0 : condType = constCond->baseType();
81 :
82 0 : bool isGeneric{false};
83 0 : if (insideDeclWithGenerics &&
84 0 : typeTable_->isGenericType(condType, currentGenericList_)) {
85 0 : isGeneric = true;
86 : // return condType;
87 0 : }
88 :
89 0 : if (!isGeneric) {
90 0 : if (!typeTable_->areSameType(condType, typeTable_->boolType()))
91 0 : return createError(ERROR_TYPE::TYPE, "a condition must be boolean");
92 0 : }
93 :
94 0 : if (!node->update().empty()) {
95 0 : for (const auto &expr : node->update()) {
96 0 : auto updateResult = expr->accept(*this);
97 0 : if (!updateResult)
98 0 : return createError(updateResult.error());
99 0 : }
100 0 : }
101 :
102 0 : auto body = node->body()->accept(*this);
103 0 : if (!body)
104 0 : return createError(body.error());
105 0 : auto bodyType = body.value();
106 :
107 0 : if (!typeTable_->areSameType(bodyType, typeTable_->noPropagateType()) &&
108 0 : !typeTable_->areSameType(bodyType, typeTable_->breakType())) {
109 0 : node->setReturnedFromAnalysis(bodyType);
110 0 : return bodyType;
111 0 : }
112 0 : node->setReturnedFromAnalysis(typeTable_->noPropagateType());
113 0 : return typeTable_->noPropagateType();
114 0 : }
115 :
116 : /*
117 : - si body encuentra algo diferente a NoPropagate lo propaga si no retorna
118 : NoPropagate
119 : - condtion debe retornar bool
120 : */
121 : std::expected<std::shared_ptr<Type>, Error>
122 0 : TypeAnalysis::visit(const AST_DO_WHILE *node) const noexcept {
123 0 : if (!node) {
124 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_DO_WHILE");
125 0 : }
126 0 : auto body = node->body()->accept(*this);
127 0 : if (!body)
128 0 : return createError(body.error());
129 0 : auto bodyType = body.value();
130 :
131 0 : auto condition = node->condition()->accept(*this);
132 0 : if (!condition)
133 0 : return createError(condition.error());
134 0 : auto condType = condition.value();
135 :
136 0 : if (auto constCond = std::dynamic_pointer_cast<ConstType>(condType))
137 0 : condType = constCond->baseType();
138 :
139 0 : bool isGeneric{false};
140 0 : if (insideDeclWithGenerics &&
141 0 : typeTable_->isGenericType(condType, currentGenericList_)) {
142 0 : isGeneric = true;
143 : // return condType;
144 0 : }
145 :
146 0 : if (!isGeneric) {
147 0 : if (!typeTable_->areSameType(condType, typeTable_->boolType()))
148 0 : return createError(ERROR_TYPE::TYPE, "a condition must be boolean");
149 0 : }
150 :
151 0 : if (!typeTable_->areSameType(bodyType, typeTable_->noPropagateType()) &&
152 0 : !typeTable_->areSameType(bodyType, typeTable_->breakType())) {
153 0 : node->setReturnedFromAnalysis(bodyType);
154 0 : return bodyType;
155 0 : }
156 0 : node->setReturnedFromAnalysis(typeTable_->breakType());
157 0 : return typeTable_->noPropagateType();
158 0 : }
159 :
160 : /*
161 : - retorna BreakType
162 : */
163 : std::expected<std::shared_ptr<Type>, Error>
164 0 : TypeAnalysis::visit(const AST_PASS *node) const noexcept {
165 0 : if (!node) {
166 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_PASS");
167 0 : }
168 0 : node->setReturnedFromAnalysis(typeTable_->breakType());
169 0 : return typeTable_->breakType();
170 0 : }
171 :
172 : /*
173 : - retorna BreakType
174 : */
175 : std::expected<std::shared_ptr<Type>, Error>
176 0 : TypeAnalysis::visit(const AST_STOP *node) const noexcept {
177 0 : if (!node) {
178 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_STOP");
179 0 : }
180 0 : node->setReturnedFromAnalysis(typeTable_->breakType());
181 0 : return typeTable_->breakType();
182 0 : }
183 :
184 : } // namespace nicole
|