Ejemplo n.º 1
0
 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()
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
    def visit(self, node: cool_ast.VarDeclarationNode, scope: Scope):
        try:
            var_type = self.context.get_type(node.typex)
        except SemanticError as error:
            self.errors.append(str(error))
            var_type = ErrorType()

        if scope.is_defined(node.id):
            self.errors.append(LOCAL_ALREADY_DEFINED %
                               (node.id, self.current_method.name))
        else:
            scope.define_variable(node.id, var_type)

        expr_type = self.visit(node.expr, scope)
        if not expr_type.conforms_to(var_type):
            self.errors.append(INCOMPATIBLE_TYPES %
                               (expr_type.name, var_type.name))