Exemple #1
0
 def visit_PatMatch(self, node):
     self.generic_visit(node)
     
     if (dka.hasnodetype(node.iter, dha.Name) and
         dha.isfixset(node.iter.id)):
         elem = self.visit(node.target)
         return dha.PatMatchName(elem, node.iter.id)
     
     cont = dha.valueToPattern(node.iter, dha.P_BOUND)
     elem = self.visit(node.target)
     return dha.PatMatchName(dha.PatTuple([cont, elem]),
                             self.getMemSym(elem._t))
Exemple #2
0
 def visit_While(self, node):
     body = self.stmtlist_helper(node.body)
     orelse = self.stmtlist_helper(node.orelse)
     # Membership tests become pattern matches.
     if (isinstance(node.test, ast.Compare) and
         len(node.test.ops) == len(node.test.comparators) == 1 and
         isinstance(node.test.ops[0], ast.In)):
         left = self.visit(node.test.left)
         right = self.visit(node.test.comparators[0])
         
         if self.objectdomain:
             node = dha.PatWhile(dha.PatMatch(dha.valueToPattern(left), right),
                                 body, orelse)
         else:
             dka.assertnodetype(iter, dha.Name)
             return dha.PatWhile(dha.PatMatchName(dha.valueToPattern(left), right.id),
                                 body, orelse)
     else:
         test = self.visit(node.test)
         node = dha.While(test, body, orelse)
     
     return node
Exemple #3
0
 def visit_If(self, node):
     body = self.stmtlist_helper(node.body)
     orelse = self.stmtlist_helper(node.orelse)
     
     # Membership tests become pattern matches.
     if (isinstance(node.test, ast.Compare) and
         len(node.test.ops) == len(node.test.comparators) == 1 and
         isinstance(node.test.ops[0], ast.In)):
         left = self.visit(node.test.left)
         right = self.visit(node.test.comparators[0])
         if self.objectdomain:
             case = dha.PatCase(dha.PatMatch(dha.valueToPattern(left), right),
                                body)
         else:
             dka.assertnodetype(right, dha.Name)
             case = dha.PatCase(dha.PatMatchName(dha.valueToPattern(left, right.od)),
                                body)
     
     else:
         test = self.visit(node.test)
         case = dha.CondCase(test, body)
     
     return dha.If([case], orelse)
Exemple #4
0
 def visit_SetUpdate(self, node):
     isaddup = dha.isAddUpdate(node)
     
     if node.op.mod is not dha.UP_NONSTRICT:
         return
     
     has = dha.Match(dha.PatMatch(dha.valueToPattern(dka.copy(node.value)),
                                  dha.Name(node.target)),
                     dka.Symtab())
     if isaddup:
         test = dha.UnaryOp(dha.Not(), has)
     else:
         test = has
     
     newupdate = dka.copy(node)
     newupdate.op.mod = dha.UP_STRICT
     
     blk = dha.Block([newupdate])
     return dha.If([dha.CondCase(test, blk)])
Exemple #5
0
    def visit_SetUpdate(self, node):
        sym = node.target
        if hasattr(sym, 'needsmulti') and sym.needsmulti:
            self.multisyms.add(sym)
        else:
            return
        
        isaddup = dha.isAddUpdate(node)
        has = dha.Match(dha.PatMatchName(dha.valueToPattern(dka.copy(node.value)),
                                         node.target),
                        dka.Symtab())
        
        if isaddup:
            test1 = dha.UnaryOp(dha.Not(), has)
            inc1 = dha.RefUpdate(node.target, dka.copy(node.op), dka.copy(node.value))
            inc2 = dka.copy(inc1)
            
            body = dha.Block([dka.copy(node),
                              inc1])
            orelse = dha.Block([inc2])
            
            code = dha.If([dha.CondCase(test1, body)], orelse)
        
        else:
            test1 = has
            test2 = dha.BinOp(dha.GetRefCount(node.target, dka.copy(node.value)),
                              dha.Eq(),
                              dha.Num(0))
            body2 = dha.Block([dka.copy(node)])
            remif = dha.If([dha.CondCase(test2, body2)])
            body1 = [dha.RefUpdate(node.target, dka.copy(node.op), dka.copy(node.value)),
                     remif]
#            code = dha.If([dha.CondCase(test1, body1)])
            code = body1
        
        return code