def visit(self, node, scope):
        self.visit(node.expression, scope)
        node_type = node.expression.expr_type

        if scope.is_defined(node.name):
            var = scope.find_variable(node.name)

            if var.name == 'self':
                error = SemanticError(read_only_, node.row, node.col)
                self.errors.append(error)
                node_type = Error_Type()
            elif not node_type.conforms_to(var.type):
                error = SemanticError(
                    incompatible_types_.replace('%s', node_type.name,
                                                1).replace(
                                                    '%s', var.type.name, 1),
                    node.row, node.col, 'TypeError')
                self.errors.append(error)
                node_type = Error_Type()
        else:
            error = SemanticError(var_not_defined_.replace('%s', node.name, 1),
                                  node.row, node.col, 'NameError')
            self.errors.append(error)
            node_type = Error_Type()

        node.expr_type = node_type
Exemple #2
0
    def visit(self, node):
        try:
            self.current_type = self.context.get_type(node.name)
            if node.parent:
                try:
                    parent = self.context.get_type(node.parent)
                except SemanticException as e:
                    error = SemanticError(e.text, node.row, node.col,
                                          'TypeError')
                    self.errors.append(error)
                    parent = Error_Type()
                    self.current_type.set_parent(parent)
                else:
                    if parent.name in ['Int', 'String', 'Bool']:
                        parent = Error_Type()
                        error = SemanticError(
                            inherits_builtin_type.replace("%s", node.name, 1),
                            node.row, node.col)
                        self.errors.append(error)
                    self.current_type.set_parent(parent)

        except SemanticException as e:
            error = SemanticError(e.text, node.row, node.col, 'TypeError')
            self.errors.append(error)

        for f in node.features:
            self.visit(f)
    def visit(self, node, scope):
        try:
            self.current_method = self.current_type.get_method(node.name)
        except SemanticException as ex:
            error = SemanticError(ex.text, node.row, node.col,
                                  'AttributeError')
            self.errors.append(error)

        method_scope = scope.create_child()

        for param in node.params:
            self.visit(param, method_scope)

        self.visit(node.expression, method_scope)

        expr_type = node.expression.expr_type

        return_type = self.current_method.return_type

        if expr_type.name == 'SELF_TYPE':
            if not self.current_type.conforms_to(return_type):
                error = SemanticError(
                    incompatible_types_.replace('%s', expr_type.name,
                                                1).replace(
                                                    '%s',
                                                    self.current_type.name, 1),
                    node.row, node.col, 'TypeError')
                self.errors.append(error)
        elif not expr_type.conforms_to(return_type):
            error = SemanticError(
                incompatible_types_.replace('%s', expr_type.name,
                                            1).replace('%s', return_type.name,
                                                       1), node.row, node.col,
                'TypeError')
            self.errors.append(error)
    def visit(self, node, scope):
        try:
            node_type = self.context.get_type(node.let_type)
            if node_type.name == 'SELF_TYPE':
                node_type = scope.find_variable('self').type
        except SemanticException as ex:
            error = SemanticError(ex.text, node.row, node.col, 'TypeError')
            self.errors.append(error)
            node_type = Error_Type()

        self.visit(node.expression, scope)
        expr_type = node.expression.expr_type

        if not expr_type.conforms_to(node_type):
            error = SemanticError(
                incompatible_types_.replace('%s', expr_type.name,
                                            1).replace('%s', node_type.name,
                                                       1), node.row, node.col,
                'TypeError')
            self.errors.append(error)

        if node.name == 'self':
            error = SemanticError(self_let_, node.row, node.col)
            self.errors.append(error)
        else:
            if scope.is_local(node.name):
                scope.remove_local(node.name)

            scope.define_variable(node.name, node_type)
    def visit(self, node, scope):
        self.visit(node.obj, scope)
        instance_type = node.obj.expr_type

        try:
            static_type = self.context.get_type(node.static_type)
        except SemanticException as ex:
            static_type = Error_Type()
            error = SemanticError(ex.text, node.row, node.col, 'TypeError')
            self.errors.append(error)

        if not instance_type.conforms_to(static_type):
            error = SemanticError(
                incompatible_types_.replace('%s', instance_type.name,
                                            1).replace('%s', static_type.name,
                                                       1), node.row, node.col,
                'TypeError')
            self.errors.append(error)

        try:
            method = static_type.get_method(node.method)

            if len(node.args) == len(method.param_types):
                for arg, param_type in zip(node.args, method.param_types):
                    self.visit(arg, scope)
                    arg_type = arg.expr_type

                    if not arg_type.conforms_to(param_type):
                        error = SemanticError(
                            incompatible_types_.replace(
                                '%s', arg_type.name,
                                1).replace('%s', param_type.name, 1), node.row,
                            node.col, 'TypeError')
                        self.errors.append(error)
            else:
                error = SemanticError(
                    incorrect_count_params_.replace(
                        '%s', method.name,
                        1).replace('%s', static_type.name,
                                   1).replace('%s',
                                              str(len(method.param_types)), 1),
                    node.row, node.col)
                self.errors.append(error)

            if method.return_type.name == 'SELF_TYPE':
                node_type = instance_type
            node_type = method.return_type

        except SemanticException as ex:
            error = SemanticError(ex.text, node.row, node.col,
                                  'AttributeError')
            self.errors.append(error)
            node_type = Error_Type()

        node.expr_type = node_type
Exemple #6
0
 def visit(self, node):
     try:
         attr_type = self.context.get_type(node.attr_type)
     except SemanticException:
         attr_type = Error_Type()
         error = SemanticError(
             attr_not_exist_.replace("%s", node.name,
                                     1).replace("%s",
                                                self.current_type.name, 1),
             node.row, node.col, 'TypeError')
         self.errors.append(error)
     try:
         self.current_type.define_attribute(node.name, attr_type)
     except SemanticException as e:
         error = SemanticError(e.text, node.row, node.col)
         self.errors.append(error)
 def visit(self, node, scope):
     try:
         self.current_type.get_attribute(node.name)
     except SemanticException as ex:
         error = SemanticError(ex.text, node.row, node.col,
                               'AttributeError')
         self.errors.append(error)
    def visit(self, node, scope):
        try:
            node_type = self.context.get_type(node.let_type)
            if node_type.name == 'SELF_TYPE':
                node_type = scope.find_variable('self').type
        except SemanticException as ex:
            error = SemanticError(ex.text, node.row, node.col, 'TypeError')
            self.errors.append(error)
            node_type = Error_Type()

        if node.name == 'self':
            error = SemanticError(self_let_, node.row, node.col)
            self.errors.append(error)
        else:
            if scope.is_local(node.name):
                scope.remove_local(node.name)

            scope.define_variable(node.name, node_type)
    def visit(self, node, scope):
        if scope.is_defined(node.name):
            node_type = scope.find_variable(node.name).type
        else:
            error = SemanticError(var_not_defined_.replace('%s', node.name, 1),
                                  node.row, node.col, 'NameError')
            self.errors.append(error)
            node_type = Error_Type()

        node.expr_type = node_type
Exemple #10
0
 def visit(self, node):
     self.topological_sort()
     for t in self.sort:
         if t not in ['Object', 'Int', 'String', 'Bool', 'IO']:
             try:
                 class_node = self.context.classes[t]
             except KeyError:
                 pass
             else:
                 self.visit(class_node)
     if not self.context.types.__contains__('Main'):
         error = SemanticError(Main_not_defined_, 0, 0, 'TypeError')
         self.errors.append(error)
     else:
         if not self.context.types['Main'].methods.__contains__('main'):
             main_node = self.context.classes['Main']
             error = SemanticError(main_method_not_exist_, main_node.row,
                                   main_node.col, 'AttributeError')
             self.errors.append(error)
Exemple #11
0
 def visit(self, node):
     self.context = Context()
     self.context.create_builtin_types()
     for dec in node.classes:
         if dec.name in ['Object', 'Int', 'String', 'Bool', 'IO']:
             error = SemanticError("Is an error redefine a builint type",
                                   dec.row, dec.col)
             self.errors.append(error)
         else:
             self.visit(dec)
    def visit(self, node, scope):
        try:
            node_type = self.current_type.get_attribute(node.name).type
        except SemanticException as ex:
            node_type = Error_Type()
            error = SemanticError(ex.text, node.row, node.col,
                                  'AttributeError')
            self.errors.append(error)

        self.visit(node.expression, scope)
        expr_type = node.expression.expr_type

        if not expr_type.conforms_to(node_type):
            error = SemanticError(
                incompatible_types_.replace('%s', expr_type.name,
                                            1).replace('%s', node_type.name,
                                                       1), node.row, node.col,
                'TypeError')
            self.errors.append(error)
    def visit(self, node, scope):
        try:
            node_type = self.context.get_type(node.new_type)
            if node_type.name == 'SELF_TYPE':
                node_type = scope.find_variable('self').type
        except SemanticException as ex:
            error = SemanticError(ex.text, node.row, node.col, 'TypeError')
            self.errors.append(error)
            node_type = Error_Type()

        node.expr_type = node_type
    def visit(self, node, scope):
        self.visit(node.predicate, scope)
        predicate_type = node.predicate.expr_type

        if predicate_type.name != 'Bool':
            error = SemanticError(wrong_type_.replace('%s', 'Bool', 1),
                                  node.row, node.col, 'TypeError')
            self.errors.append(error)

        self.visit(node.expression, scope)

        node.expr_type = self.context.get_type('Object')
    def visit(self, node, scope):
        try:
            action_type = self.context.get_type(node.act_type)
        except SemanticException as ex:
            error = SemanticError(ex.text, node.row, node.col, 'TypeError')
            self.errors.append(error)
            action_type = Error_Type()

        scope.define_variable(node.name, action_type)

        self.visit(node.body, scope)
        node.expr_type = node.body.expr_type
    def visit(self, node, scope):
        node_type = self.context.get_type('Bool')

        self.visit(node.left, scope)
        left_type = node.left.expr_type

        if left_type.name != 'Int':
            error = SemanticError(wrong_type_.replace('%s', 'Int', 1),
                                  node.row, node.col, 'TypeError')
            self.errors.append(error)
            node_type = Error_Type()

        self.visit(node.right, scope)
        right_type = node.right.expr_type

        if right_type.name != 'Int':
            error = SemanticError(wrong_type_.replace('%s', 'Int', 1),
                                  node.row, node.col, 'TypeError')
            self.errors.append(error)
            node_type = Error_Type()

        node.expr_type = node_type
    def visit(self, node, scope):
        node_type = self.context.get_type('Int')

        self.visit(node.expression, scope)
        expr_type = node.expression.expr_type

        if expr_type.name != 'Int':
            error = SemanticError(wrong_type_.replace('%s', 'Int', 1),
                                  node.row, node.col, 'TypeError')
            self.errors.append(error)
            node_type = Error_Type()

        node.expr_type = node_type
    def visit(self, node, scope):
        try:
            node_type = self.context.get_type(node.param_type)
            if node_type.name == 'SELF_TYPE':
                node_type = Error_Type()
                error = SemanticError(invalid_SELFTYPE, node.row, node.col)
                self.errors.append(error)

        except SemanticException as ex:
            node_type = Error_Type()
            error = SemanticError(ex.text, node.row, node.col, 'TypeError')
            self.errors.append(error)

        if node.name == 'self':
            error = SemanticError(self_name, node.row, node.col)
            self.errors.append(error)
        elif not scope.is_local(node.name):
            scope.define_variable(node.name, node_type)
        else:
            error = SemanticError(
                param_wrong_signature.replace('%s', node.name, 1).replace(
                    '%s', self.current_method.name, 1), node.row, node.col)
            self.errors.append(error)
    def visit(self, node, scope):
        self.visit(node.predicate, scope)
        predicate_type = node.predicate.expr_type

        if predicate_type.name != 'Bool':
            error = SemanticError(wrong_type_.replace('%s', 'Bool', 1),
                                  node.row, node.col, 'TypeError')
            self.errors.append(error)

        self.visit(node.then_expr, scope)
        then_type = node.then_expr.expr_type
        self.visit(node.else_expr, scope)
        else_type = node.else_expr.expr_type

        node.expr_type = then_type.join(else_type)
Exemple #20
0
    def visit(self, node):
        try:
            param_names = []
            param_types = []
            for p in node.params:
                param_names.append(p.name)
                try:
                    param_type = self.context.get_type(p.param_type)
                except SemanticException:
                    param_type = Error_Type()
                    error = SemanticError(
                        param_not_exist_.replace("%s", p.name, 1).replace(
                            "%s", node.name,
                            1).replace("%s", self.current_type.name, 1),
                        node.row, node.col, 'TypeError')
                    self.errors.append(error)

                param_types.append(param_type)

            try:
                return_type = self.context.get_type(node.return_type)
            except SemanticException:
                return_type = Error_Type()
                error = SemanticError(
                    invalid_return_type_.replace(
                        "%s", node.return_type,
                        1).replace("%s", node.name,
                                   1).replace("%s", self.current_type.name, 1),
                    node.row, node.col, 'TypeError')
                self.errors.append(error)

            self.current_type.define_method(node.name, param_names,
                                            param_types, return_type)
        except SemanticException as e:
            error = SemanticError(e.text, node.row, node.col)
            self.errors.append(error)
    def visit(self, node, scope):
        node_type = self.context.get_type('Bool')

        self.visit(node.left, scope)
        left_type = node.left.expr_type

        self.visit(node.right, scope)
        right_type = node.right.expr_type

        if (left_type.name in ['Int', 'Bool', 'String'] or right_type.name in [
                'Int', 'Bool', 'String'
        ]) and left_type.name != right_type.name:
            error = SemanticError(wrong_type_.replace('%s', left_type.name, 1),
                                  node.row, node.col, 'TypeError')
            self.errors.append(error)
            node_type = Error_Type()

        node.expr_type = node_type
    def visit(self, node, scope):
        self.visit(node.expression, scope)
        action_expr_types = []
        var_declared = []

        for action in node.act_list:
            var_type = action.act_type
            if not var_type in var_declared:
                var_declared.append(var_type)
            else:
                error = SemanticError(
                    other_branch_declared_.replace("%s", var_type, 1),
                    action.row, action.col)
                self.errors.append(error)
            self.visit(action, scope.create_child())
            action_expr_types.append(action.expr_type)

        t_0 = action_expr_types.pop(0)
        node_type = t_0.multiple_join(action_expr_types)

        node.expr_type = node_type
Exemple #23
0
    def topological_sort(self):
        indeg = {key: 0 for key in self.context.graph.keys()}
        for u in self.context.graph.keys():
            for v in self.context.graph[u]:
                indeg[v] += 1

        roots = [key for key in indeg.keys() if indeg[key] == 0]

        for v in roots:
            self.dfs(v)

        visited = [x for x in self.visited]
        visited.reverse()
        for t in visited:
            if not self.visited[t] and not t in [
                    'Object', 'Int', 'String', 'Bool', 'IO'
            ]:
                class_node = self.context.classes[t]
                error = SemanticError(circular_dependency_.replace('%s', t, 1),
                                      class_node.row, class_node.col)
                self.errors.append(error)
                break
Exemple #24
0
 def visit(self, node):
     try:
         self.context.create_type(node)
     except SemanticException as e:
         error = SemanticError(e.text, node.row, node.col)
         self.errors.append(error)