def visit(self, node: ast.Case, scope: Scope): t = list(scope.get_types().keys()) t.reverse() def get_by_(tp, l: ast.Action): for i in l: if tp == i.action_type: return i k = [] for i in t: n = get_by_(i, node.actions) if n: k.append(n) node.actions = k self.visit(node.expr, scope) list_type = [] for item in node.actions: list_type.append(self.visit(item, scope)) return_type = list_type[0] for item in list_type[1:]: if return_type.name == 'SELF_TYPE' and item.name == 'SELF_TYPE': continue elif return_type.name == 'SELF_TYPE': return_type = scope.join(scope.getType(scope.classname), item) elif item.name == 'SELF_TYPE': return_type = scope.join(scope.getType(scope.classname), return_type) else: return_type = scope.join(return_type, item) node.static_type = return_type return return_type
def visit(self, node:ast.If, scope:Scope, errors): predType = self.visit(node.predicate, scope, errors) if not predType.name == "Bool": raise CheckSemanticError(f'you can\'t match {predType.name} with Bool') ifType = self.visit(node.then_body, scope, errors) elseType = self.visit(node.else_body, scope, errors) return scope.join(ifType, elseType)
def visit(self, node: ast.If, scope: Scope): pred_type = self.visit(node.predicate, scope) if not pred_type.name == "Bool": raise CheckSemanticError( f'you can\'t match {pred_type.name} with Bool') if_type = self.visit(node.then_body, scope) else_type = self.visit(node.else_body, scope) if if_type.name == 'SELF_TYPE' and if_type.name == 'SELF_TYPE': node.static_type = if_type return if_type elif if_type.name == 'SELF_TYPE': return_type = scope.join(else_type, scope.getType(scope.classname)) node.static_type = return_type return return_type elif else_type.name == 'SELF_TYPE': return_type = scope.join(if_type, scope.getType(scope.classname)) node.static_type = return_type return return_type else: return_type = scope.join(if_type, else_type) node.static_type = return_type return return_type
def visit(self, node:ast.Case, scope:Scope, errors): self.visit(node.expr, scope, errors) listType = [] for item in node.actions: try: scope.getType(item.action_type) except Exception as e: print(e) listType.append(self.visit(item, scope, errors)) returnType = listType[0] for item in listType[1:]: returnType = scope.join(returnType, item) return returnType