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)
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