예제 #1
0
 def visit(self, node: ast.Let, scope: Scope):
     new_scope = Scope(scope.classname, scope)
     for decl in node.declarations:
         self.visit(decl, new_scope)
     b_type = self.visit(node.body, new_scope)
     node.static_type = b_type
     return b_type
예제 #2
0
 def visit(self, node: ast.Action, scope: Scope):
     scope.getType(node.action_type)
     new_scope = Scope(scope.classname, scope)
     new_scope.defineSymbol(node.name, scope.getType(node.action_type))
     return_type = self.visit(node.body, new_scope)
     node.static_type = return_type
     return return_type
예제 #3
0
 def visit(self, node:ast.Let, scope:Scope, errors):
     newScope = Scope(scope.classname, scope)
     for decl in node.init_expr :
         try:
             self.visit(decl, newScope, errors)
         except Exception as e:
             print(e)
     return self.visit(node.body, newScope, errors)
예제 #4
0
    def visit(self, node:ast.Program, scope:Scope, errors):
        for classDef in node.classes:
            attribs = filter(lambda x: type(x) is ast.ClassAttribute, classDef)
            attribs = list(attribs)
            newType = ctype(classDef.name, classDef.parent, [], [])
            try:
                scope.createType(newType)
            except Exception as e:
                print(str(e))

        for i in node.classes:
            t = self.visit(i, Scope(i.name, scope), errors)
예제 #5
0
class CheckSemantic:
    @visitor.on('AST')
    def visit(self, node, scope, errors):
        pass

    @visitor.when(ast.Program)
    def visit(self, node:ast.Program, scope:Scope, errors):
        for classDef in node.classes:
            attribs = filter(lambda x: type(x) is ast.ClassAttribute, classDef)
            attribs = list(attribs)
            newType = ctype(classDef.name, classDef.parent, [], [])
            try:
                scope.createType(newType)
            except Exception as e:
                print(str(e))

        for i in node.classes:
            t = self.visit(i, Scope(i.name, scope), errors)

    @visitor.when(ast.Class)
    def visit(self, node:ast.Class, scope:Scope, errors):
        methods = filter(lambda x: type(x) is ast.ClassMethod, classDef)
        methods = list(methods)
        attribs = filter(lambda x: type(x) is ast.ClassAttribute, classDef)
        attribs = list(attribs)
        for i in attribs:
            ta = self.visit(i, scope, errors)
            try:
                scope.getType(node.name).add_attrib({i.name: scope.getType(i.attr_type)})
            except Exception as e:
                print(e)
        for i in methods:
            try:
                scope.getType(node.name).add_method({i.name:{
                    'formal_params':{
                        t.name: scope.getType(t.param_type) for j in i.formal_params
                    },
                    'return_type': scope.getType(i.return_type),
                    'body': i.body
                }})
            exec Exception as e:
                print(e)
        for i in methods:
            tb = self.visit(i, Scope(scope.classname, scope), errors)
예제 #6
0
    def visit(self, node: ast.Program, _):
        scope_root = Scope(None, None)
        for classDef in node.classes:
            new_type = ctype(classDef.name)
            scope_root.createType(new_type)
        for classDef in node.classes:
            a = scope_root.getType(classDef.name)
            a.parent = scope_root.getType(classDef.parent)
        new_types = self.sort_type(scope_root.get_types())
        node.classes = list(
            map(lambda x: self.get_node_by_type(x, node.classes),
                list(new_types.keys())[6:]))
        scope_root.set_types(new_types)
        self.check_ciclic_inheritance(scope_root.get_types())
        scopes = []
        for j in node.classes:
            scope = Scope(j.name, scope_root)
            methods = filter(lambda x: type(x) is ast.ClassMethod, j.features)
            methods = list(methods)
            attribs = filter(lambda x: type(x) is ast.ClassAttribute,
                             j.features)
            attribs = list(attribs)
            p_type = scope.getType(j.parent)
            if p_type.name in ['Int', 'Bool', 'String']:
                raise CheckSemanticError(
                    f'Any type can\'t inheriths from {p_type}')
            for i in p_type.attributes:
                try:
                    scope.getType(j.name).add_attrib(i)
                except Exception as e:
                    raise e
                try:
                    scope.defineAttrib(list(i.keys())[0], list(i.values())[0])
                except Exception as e:
                    raise e

            for i in attribs:
                try:
                    scope.getType(j.name).add_attrib(
                        {i.name: scope.getType(i.attr_type)})
                except Exception as e:
                    raise e
                try:
                    scope.defineAttrib(i.name, scope.getType(i.attr_type))
                except Exception as e:
                    raise e
            for i in methods:
                try:
                    scope.getType(j.name).add_method({
                        i.name: {
                            'formal_params': {
                                t.name: scope.getType(t.param_type)
                                for t in i.formal_params
                            },
                            'return_type': scope.getType(i.return_type),
                            'body': i.body
                        }
                    })
                except Exception as e:
                    raise e
            scopes.append(scope)
        for i in range(len(node.classes)):
            self.visit(node.classes[i], scopes[i])
        return scope_root
예제 #7
0
 def visit(self, node: ast.Class, scope: Scope):
     for i in node.features:
         self.visit(i, Scope(scope.classname, scope))
예제 #8
0
 def visit(self, node:ast.Action, scope:Scope, errors):
     newScope = Scope(scope.classname, scope)
     newScope.defineSymbol(node.name, scope.getType(node.action_type))
     return self.visit(node.body, newScope, errors)