def visit(self, node: cool_ast.VariableNode, scope: Scope) -> Type: var_info = scope.find_variable(node.lex) if var_info is not None: return var_info.type var_type = self.attributes.get_attribute(self.current_type, node.lex) if var_type is None: return TypeVariable() return var_type
def visit(self, node: cool_ast.VariableNode, scope: Scope): if scope.is_defined(node.lex): var = scope.find_variable(node.lex) return var.type try: att = self.current_type.get_attribute(node.lex) if att.type.name == 'SELF_TYPE': return self.current_type return att.type except SemanticError: self.errors.append(VARIABLE_NOT_DEFINED % (node.lex, self.current_type.name)) return ErrorType()
def visit(self, node: cool_ast.AssignNode, scope: Scope) -> Type: expr_type = self.visit(node.expr, scope) var_info = scope.find_variable(node.id) if var_info is None: var_type = self.attributes.get_attribute(self.current_type, node.id) if var_type is None: return TypeVariable() else: var_type = var_info.type self.unify(var_type, expr_type) return expr_type
def visit(self, node: cool_ast.AssignNode, scope: Scope): expr_type = self.visit(node.expr, scope) if scope.is_defined(node.id): var_type = scope.find_variable(node.id).type else: try: att = self.current_type.get_attribute(node.id) var_type = att.type except SemanticError: self.errors.append(VARIABLE_NOT_DEFINED % (node.id, self.current_method.name)) var_type = ErrorType() if not expr_type.conforms_to(var_type): self.errors.append(INCOMPATIBLE_TYPES % (expr_type.name, var_type.name)) return expr_type
def visit(self, node: cool_ast.VarDeclarationNode, scope: Scope, index: int) -> int: if node.typex == 'AUTO_TYPE': var_info = scope.find_variable(node.id) if isinstance(var_info.type, TypeVariable): try: node.typex = self.subst[var_info.type.name].name self.change = True except KeyError: pass else: node.typex = var_info.type.name if node.expr is not None: index = self.visit(node.expr, scope, index) return index