def getretcond(self, expr): if isinstance(expr, Op) and expr.name == 'ite': icond = expr.args[0] ct = self.getretcond(expr.args[1]) cf = self.getretcond(expr.args[2]) cond = [] if ct is None and cf is None: return None if ct is True and cf is True: return True if ct: if ct is True: cond.append(icond.copy()) else: cond.append(Op(self.ANDOP, icond.copy(), ct.copy())) if cf: nicond = Op(self.NOTOP, icond) if cf is True: cond.append(nicond.copy()) else: cond.append(Op(self.ANDOP, nicond.copy(), cf.copy())) if len(cond) == 1: return cond[0] else: return Op(self.OROP, cond[0], cond[1]) elif isinstance(expr, Var) and expr.name == VAR_RET: return None else: return True
def visit_For(self, node): if node.orelse: raise NotSupported("For-Else not supported", line=self.getline(node.orelse)) # Iterated expression it = self.visit_expr(node.iter) # Targets of iteration if isinstance(node.target, ast.Name): targets = [self.visit_expr(node.target)] elif isinstance(node.target, ast.Tuple): targets = map(self.visit_expr, node.target.elts) else: raise NotSupported('For loop with {} as target'.format( node.target.__class__.__name__), line=node.lineno) hiddenvar = self.hiddenvarcnt self.hiddenvarcnt += 1 # Set up the iterated variable iter_name = 'iter#{}'.format(hiddenvar) it_var = Var(iter_name) self.addtype(iter_name, '*') # Set up the iteration index ind_name = 'ind#{}'.format(hiddenvar) ind_var = Var(ind_name) self.addtype(ind_name, 'int') # Add assignments to iterators self.addexpr(it_var.name, it) self.addexpr(ind_var.name, Const(str(0), line=node.lineno)) # Condition is ind_var < len(iter_var) cond = Op('Lt', ind_var.copy(), Op('len', it_var.copy()), line=node.iter.lineno) # Assignments to iterated variable(s) prebody = [] el = Op('GetElement', it_var.copy(), ind_var.copy(), line=node.target.lineno) if len(targets) == 1: prebody.append((targets[0].name, el.copy())) else: for i, t in enumerate(targets): eli = Op('GetElement', el.copy(), Const(str(i)), line=node.target.lineno) prebody.append((t.name, eli)) # Add index variable increment prebody.append((ind_var.name, Op('Add', ind_var.copy(), Const(str(1)), line=node.iter.lineno))) self.visit_loop(node, None, cond, None, node.body, False, 'for', prebody=prebody)
def visit_For(self, node): if node.orelse: raise NotSupported("For-Else not supported", line=self.getline(node.orelse)) # Iterated expression it = self.visit_expr(node.iter) # Targets of iteration if isinstance(node.target, ast.Name): self.addtype(node.target.id, '*') targets = [self.visit_expr(node.target)] elif isinstance(node.target, ast.Tuple): for el in node.target.elts: if isinstance(el, ast.Name): self.addtype(el.id, '*') targets = map(self.visit_expr, node.target.elts) else: raise NotSupported( 'For loop with {} as target'.format( node.target.__class__.__name__), line=node.lineno) hiddenvar = self.hiddenvarcnt self.hiddenvarcnt += 1 # Set up the iterated variable iter_name = 'iter#{}'.format(hiddenvar) it_var = Var(iter_name) self.addtype(iter_name, '*') # Set up the iteration index ind_name = 'ind#{}'.format(hiddenvar) ind_var = Var(ind_name) self.addtype(ind_name, 'int') # Add assignments to iterators self.addexpr(it_var.name, it) self.addexpr(ind_var.name, Const(str(0), line=node.lineno)) # Condition is ind_var < len(iter_var) cond = Op('Lt', ind_var.copy(), Op('len', it_var.copy()), line=node.iter.lineno) # Assignments to iterated variable(s) prebody = [] el = Op('GetElement', it_var.copy(), ind_var.copy(), line=node.target.lineno) if len(targets) == 1: prebody.append((targets[0].name, el.copy())) else: for i, t in enumerate(targets): eli = Op('GetElement', el.copy(), Const(str(i)), line=node.target.lineno) prebody.append((t.name, eli)) # Add index variable increment prebody.append((ind_var.name, Op('Add', ind_var.copy(), Const(str(1)), line=node.iter.lineno))) self.visit_loop(node, None, cond, None, node.body, False, 'for', prebody=prebody)