def enumToClause(enum, body): """Generate a For or If clause (with the specified body) that corresponds to an enumerator.""" ### Could use a little refactoring; break off AttrEnum stuff into separate method. if dha.isPosEnum(enum): if dha.isAttrEnum(enum): code = [dha.For(dha.PatMatch(dka.copy(enum.target), dha.selectorToAttr(enum.iter)), body)] else: code = [dha.For(dha.PatMatchName(dka.copy(enum.target), enum.iter), body)] else: if dha.isAttrEnum(enum): code = [dha.If([dha.CondCase(dha.BinOp(dha.patternToValue(enum.target), dha.NotIn(), dha.selectorToAttr(enum.iter)), body)])] else: code = [dha.If([dha.CondCase(dha.BinOp(dha.patternToValue(enum.target), dha.NotIn(), dha.Name(enum.iter)), body)])] return code
def visit_Match(self, node): self.generic_visit(node) target, iter = self.patmatch_helper(node.match) mask = getPatMask(target) if maskAllBound(mask): code = dha.BinOp(dha.patternToValue(target), dha.In(), iter) else: code = dha.UnaryOp(dha.NotEmpty(), iter) return code
def visit_For(self, node): # Transform the PatMatch node first. self.generic_visit(node) target, iter = self.patmatch_helper(node.match) mask = getPatMask(target) # If fully bound, replace with an If. if maskAllBound(mask): test = dha.BinOp(dha.patternToValue(target), dha.In(), iter) code = dha.If([dha.CondCase(test, node.body)], node.orelse) # Otherwise, take no action. (The PatMatch has already been rewritten as a lookup.) else: code = node return code
def visit_PatWhile(self, node): # Transform the PatMatch node first. self.generic_visit(node) target, iter = self.patmatch_helper(node.match) mask = getPatMask(target) # If fully bound, replace with a condition While. if maskAllBound(mask): test = dha.BinOp(dha.patternToValue(target), dha.In(), iter) code = dha.While(test, node.body, node.orelse) # Otherwise, test non-emptiness and bind. else: elem = dha.UnaryOp(dha.Any(), iter) node.body.stmtlist[:0] = [dha.Assign(target, elem)] emptytest = dha.UnaryOp(dha.NotEmpty(), dka.copy(iter)) code = dha.While(emptytest, node.body, node.orelse) return code
def visit_CompatTuple(self, node): pat = self.visit(dha.patternToValue(node.pat)) value = self.visit(node.value) return ast.Call(ast.Name('compat', ast.Load()), [pat, value], [], None, None)