Line data Source code
1 : #include "../../../inc/visitors/typeAnalysis/typeAnalysis.h"
2 : #include "../../../inc/parsingAnalysis/ast/assignments/ast_assignment.h"
3 : #include <memory>
4 :
5 : namespace nicole {
6 :
7 : /*
8 : - comprueba que left no sea void ni NoPropagate, BreakType
9 : - comrpueba que right no sea void ni NoPropagate, BreakType
10 : - comprueba que sea asignable
11 : - retorna NoPropagate
12 : */
13 : std::expected<std::shared_ptr<Type>, Error>
14 0 : TypeAnalysis::visit(const AST_ASSIGNMENT *node) const noexcept {
15 0 : if (!node) {
16 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_ASSIGNMENT");
17 0 : }
18 0 : const auto left{node->left()->accept(*this)};
19 0 : if (!left) {
20 0 : return createError(left.error());
21 0 : }
22 0 : if (const auto constType{std::dynamic_pointer_cast<ConstType>(*left)}) {
23 0 : return createError(ERROR_TYPE::TYPE, "cannot reassign to a const variable");
24 0 : }
25 0 : const auto right{node->value()->accept(*this)};
26 0 : if (!right) {
27 0 : return createError(right.error());
28 0 : }
29 0 : if (!typeTable_->canAssign(left.value(), right.value()))
30 0 : return createError(ERROR_TYPE::TYPE, "incompatible types in assignment");
31 0 : node->setReturnedFromAnalysis(typeTable_->noPropagateType());
32 0 : return typeTable_->noPropagateType();
33 0 : }
34 :
35 : }
|