def operator(mod): op = ast.Add() if mod == 'or': op = ast.Or() if mod == '|': op = ast.Or() if mod == '||': op = ast.Or() if mod == 'and': op = ast.And() if mod == '&': op = ast.And() if mod == '&&': op = ast.And() if mod == 'plus': op = ast.Add() if mod == '+': op = ast.Add() if mod == '-': op = ast.Sub() if mod == 'minus': op = ast.Sub() if mod == 'times': op = ast.Mult() if mod == '*': op = ast.Mult() if mod == '**': op = ast.Pow() if mod == 'divide': op = ast.Div() if mod == 'divided': op = ast.Div() if mod == 'divided by': op = ast.Div() if mod == '/': op = ast.Div() if mod == '//': op = ast.FloorDiv() if mod == 'floor div': op = ast.FloorDiv() if mod == '%': op = ast.Mod() if mod == 'mod': op = ast.Mod() if mod == 'modulus': op = ast.Mod() if mod == 'modulo': op = ast.Mod() if mod == '^': op = ast.BitXor() if mod == 'xor': op = ast.BitXor() if mod == '<<': op = ast.LShift() if mod == '>>': op = ast.RShift() return op
def parse_rule_node(self, node): tag = node.tag if tag == 'Or': return self.parse_bool_op(node, ast.Or()) elif tag == 'And': return self.parse_bool_op(node, ast.And()) elif tag == 'Not': expr = self.parse_bool_op(node, ast.Or()) return ast.UnaryOp(ast.Not(), expr) if expr else None elif tag == 'All': return _ast_const('True') elif tag == 'Category': category = node.text return ast.Compare(left=ast.Str(category), ops=[ast.In()], comparators=[ ast.Attribute(value=ast.Name( id='menuentry', ctx=ast.Load()), attr='Categories', ctx=ast.Load()) ]) elif tag == 'Filename': filename = node.text return ast.Compare(left=ast.Str(filename), ops=[ast.Eq()], comparators=[ ast.Attribute(value=ast.Name( id='menuentry', ctx=ast.Load()), attr='DesktopFileID', ctx=ast.Load()) ])
def __parseRuleNode(node): tag = node.tagName if tag == 'Or': return __parseBoolOp(node, ast.Or()) elif tag == 'And': return __parseBoolOp(node, ast.And()) elif tag == 'Not': expr = __parseBoolOp(node, ast.Or()) return ast.UnaryOp(ast.Not(), expr) if expr else None elif tag == 'All': return ast.Name('True', ast.Load()) elif tag == 'Category': category = _get_node_text(node) return ast.Compare(left=ast.Str(category), ops=[ast.In()], comparators=[ ast.Attribute(value=ast.Name(id='menuentry', ctx=ast.Load()), attr='Categories', ctx=ast.Load()) ]) elif tag == 'Filename': filename = _get_node_text(node) return ast.Compare(left=ast.Str(filename), ops=[ast.Eq()], comparators=[ ast.Attribute(value=ast.Name(id='menuentry', ctx=ast.Load()), attr='DesktopFileID', ctx=ast.Load()) ])
def visit_If(self, node): if node.orelse and not self.is_body_covered(node.orelse): # Remove the `else` part of the `if` if it's not covered node.orelse = [] elif not self.is_body_covered(node.body): # The main part of the `if` is not covered if node.orelse and len(node.orelse) == 1 and \ isinstance(node.orelse[0], ast.If): # This is probably an elif clause node.orelse[0] = self.visit_If(node.orelse[0]) # This hack fixes bad indentation of the `else` part orelse_pasta = node.orelse[0].__pasta__ if orelse_pasta.get( 'is_elif') and 'elseprefix' in orelse_pasta: node.__pasta__['elseprefix'] = orelse_pasta['elseprefix'] new_test = ast.BoolOp(op=ast.Or(), values=[node.test, node.orelse[0].test]) node.test = new_test node.body = node.orelse[0].body node.orelse = node.orelse[0].orelse elif node.orelse: # Only the `else` part is covered. Move the `else` part to the # main part removing the previous uncovered content. Make sure # the `test` stills being executed to maintain the software # behavior, even when the `test` has side effects. # Because the main part of the `if` is uncovered, we can assume # that the `test` always evaluates to False. new_test = ast.BoolOp(op=ast.Or(), values=[node.test, ast.Name(id='True')]) node.test = new_test node.body = node.orelse node.orelse = [] else: # The `if` doesn't have an `else` block # This would be better, but causes indentation errors in some # conditions. # return [ast.Expr(node.test)] new_test = ast.BoolOp(op=ast.Or(), values=[node.test, ast.Name(id='True')]) node.test = new_test node.body = [ast.copy_location(ast.Pass(), node.body[0])] node.orelse = [] super(IfRemover, self).generic_visit(node) return node
def ast(self): """Python AST of this predicate (construct transitively for all indirect children as well). :return: AST of describing all children predicates """ return ast.BoolOp(ast.Or(), [ast.Expr(value=x.ast()) for x in self._children])
def visit_BoolOp(self, node): if isinstance(node.op, ast.And): self.counter += 1 if self.counter == self.nodeToMutate: new_node = ast.BoolOp() new_node.values = node.values new_node.op = ast.Or() print('changing and {} to or.'.format(self.counter)) return ast.copy_location(new_node, node) # helps debugging if isinstance(node.op, ast.Or): self.counter += 1 if self.counter == self.nodeToMutate: new_node = ast.BoolOp() new_node.values = node.values new_node.op = ast.And() print('changing or {} to and.'.format(self.counter)) return ast.copy_location(new_node, node) # helps debugging return self.generic_visit(node)
class Infix(Symbol): rightAssoc = False _logical_map = {"and": ast.And(), "or": ast.Or()} def led(self, left): self.first = left rbp = self.lbp - int(self.rightAssoc) self.second = self.parser.expression(rbp) return self def __repr__(self): return "<'%s'>(%s, %s)" % (self.value, self.first, self.second) def ast(self, context=None): lhs = self.first.ast(context) if self.second: rhs = self.second.ast(context) if self.value in self._logical_map: return ast.BoolOp(op=self._logical_map[self.value], values=[lhs, rhs]) else: path = list(context.stack) path.append(self.first.value) return ast.Call( func=ast.Name(id="filter_field", ctx=ast.Load()), args=[ ast.Name(id="obj", ctx=ast.Load()), ast.List(elts=[ast.Str(s=i) for i in path], ctx=ast.Load()), ast.Str(s=self.value), rhs, ], keywords=[], ) return ast.Name(id="not", ctx=ast.Load())
def create_expression_node(self, expression): if len(expression.logical_terms) == 1: return self.to_node(expression.logical_terms[0]) logical_terms_nodes = [] for t in expression.logical_terms: logical_terms_nodes.append(self.to_node(t)) return ast.BoolOp(ast.Or(), logical_terms_nodes)
def visit_BoolOper(self, node: BoolOper, *args, **kwargs) -> C.boolop: if node == BoolOper.And: return C.And() elif node == BoolOper.Or: return C.Or() else: raise Exception(f'unknown BoolOper {node!r}')
def translateStmtCase(n): global i name = '_CaseExp_' + str(i) i += 1 e = ast.Assign([ast.NameConstant(name)], translateExpr(n.children[0])) cases = n.children[1] ifs = [e] for case in cases: assert case.type == 's-case-match' matches = case.children[0] caseStmt = case.children[1] q = ast.BoolOp(ast.Or(), [ ast.Compare(ast.NameConstant(name), [ast.Eq()], [translateExpr(m)]) for m in matches ]) ee = ast.If(q, translateStmtOrBlock(caseStmt), []) if len(ifs) > 1: z = ifs[1].orelse while len(z) > 0: z = z[0].orelse z.append(ee) else: ifs.append(ee) if n.children[2]: z = ifs[1].orelse while len(z) > 0: z = z[0].orelse z += translateStmtOrBlock(n.children[2]) return ifs
def single_compare(self, node): rhs = node.comparators[0] if is_obj(node.left.type): node = self.single_compare_objects(node) elif node.left.type.is_pointer and rhs.type.is_pointer: # Coerce pointers to integer values before comparing node.left = nodes.CoercionNode(node.left, Py_uintptr_t) node.comparators = [nodes.CoercionNode(rhs, Py_uintptr_t)] elif node.left.type.is_complex and rhs.type.is_complex: real1, imag1 = extract(node.left) real2, imag2 = extract(rhs) op = type(node.ops[0]) if op == ast.Eq: lhs = compare(real1, ast.Eq(), real2) rhs = compare(imag1, ast.Eq(), imag2) result = ast.BoolOp(ast.And(), [lhs, rhs]) elif op == ast.NotEq: lhs = compare(real1, ast.NotEq(), real2) rhs = compare(imag1, ast.NotEq(), imag2) result = ast.BoolOp(ast.Or(), [lhs, rhs]) else: raise NotImplementedError("ordered comparisons are not " "implemented for complex numbers") node = nodes.typednode(result, bool_) return node
def preprocess_boolean(expr, inv): if type(expr) is ast.BoolOp: if type(expr.op) is ast.And or type(expr.op) is ast.Or: new_vs = [] pos_child = None for v in expr.values: nv, is_inv = preprocess_boolean(v, inv) new_vs.append(nv) pos_child = pos_child if is_inv else nv pos_child = pos_child or new_vs[0] if (type(expr.op) is ast.And and inv) or (type(expr.op) is ast.Or and not inv): return ast.BoolOp(ast.Or(), new_vs), False new_vs.remove(pos_child) new_vs2 = [] for v in new_vs: nv, _ = preprocess_boolean(v, True) new_vs2.append(nv) expr = ast.BoolOp(ast.And(), new_vs2) expr.pos_child = pos_child return expr, False else: raise Babeception( str(type(expr.op)) + ' is not supported in boolean!') elif type(expr) is ast.UnaryOp: if type(expr.op) is ast.Not: return preprocess_boolean(expr.operand, False) if inv else preprocess_boolean( expr.operand, True) else: raise Babeception( str(type(expr.op)) + ' is not supported in boolean!') elif type(expr) is ast.Compare: if type(expr.ops[0]) is ast.NotEq or type( expr.ops[0]) is ast.Lt or type(expr.ops[0]) is ast.Gt: return preprocess_child(expr, inv) elif type(expr.ops[0]) is ast.Eq: return preprocess_boolean( ast.UnaryOp( ast.Not(), ast.Compare(expr.left, [ast.NotEq()], expr.comparators)), inv) elif type(expr.ops[0]) is ast.LtE: return preprocess_boolean( ast.UnaryOp( ast.Not(), ast.Compare(expr.left, [ast.Gt()], expr.comparators)), inv) elif type(expr.ops[0]) is ast.GtE: return preprocess_boolean( ast.UnaryOp( ast.Not(), ast.Compare(expr.left, [ast.Lt()], expr.comparators)), inv) else: raise Babeception( str(type(expr.ops[0])) + ' is not supported in boolean!') elif type(expr) is ast.Call or type(expr) is ast.Name: return preprocess_child(expr, inv) else: raise Babeception(str(type(expr)) + ' is not supported in boolean!')
def operator_equals(mod): op = ast.Add() if mod == '|=': op = ast.Or() if mod == '||=': op = ast.Or() if mod == '&=': op = ast.And() if mod == '&&=': op = ast.And() if mod == '+=': op = ast.Add() if mod == '-=': op = ast.Sub() if mod == '*=': op = ast.Mult() if mod == '**=': op = ast.Pow() if mod == '/=': op = ast.Div() if mod == '//=': op = ast.FloorDiv() if mod == '%=': op = ast.Mod() if mod == '^=': op = ast.BitXor() if mod == '<<': op = ast.LShift() if mod == '>>': op = ast.RShift() return op
def BoolOp(draw, expression) -> ast.BoolOp: op = draw(sampled_from([ ast.And(), ast.Or(), ])) le = draw(lists(expression, min_size=2, max_size=3)) return ast.BoolOp(op, values=le)
def to_node(self): if len(self.right) == 0: return self.left.to_node() else: nodes = [self.left.to_node()] for node in self.right: nodes.append(node.to_node()) return ast.BoolOp(op=ast.Or(), values=nodes)
def boolean_operations(p): token_type, left, right = p[1].gettokentype(), p[0], p[2] if token_type == "and": return ast.And(left, right) elif token_type == "or": return ast.Or(left, right) else: assert False, "Something went wrong"
def __or__(self, other) -> Column: ''' Bitwise and becomes a logical and. ''' from .utils import _term_to_ast return Column( type(bool), ast.BoolOp( op=ast.Or(), values=[_term_to_ast(self, self), _term_to_ast(other, self)]))
def visit_Name(self, name): # Display the repr of the name if it's a local variable or # _should_repr_global_name() thinks it's acceptable. locs = ast_Call(self.builtin("locals"), [], []) inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs]) dorepr = self.helper("should_repr_global_name", name) test = ast.BoolOp(ast.Or(), [inlocs, dorepr]) expr = ast.IfExp(test, self.display(name), ast.Str(name.id)) return name, self.explanation_param(expr)
def or_(self) -> ast.expr: left = self.and_() if self.match_(TokenType.PIPE_PIPE): values = [left, self.and_()] while self.match_(TokenType.PIPE_PIPE): values.append(self.and_()) return ast.BoolOp(ast.Or(), values, **self.get_loc(left, values[-1])) return left
def test_UnaryOp(self): self.verify(ast.UnaryOp(ast.Invert(), ast.Num(42)), '~42') self.verify(ast.UnaryOp(ast.Not(), ast.Num(42)), 'not 42') self.verify(ast.UnaryOp(ast.UAdd(), ast.Num(42)), '+42') self.verify(ast.UnaryOp(ast.USub(), ast.Num(42)), '-42') precedence = ast.UnaryOp( ast.Not(), ast.BoolOp(ast.Or(), [ast.Num(n=1), ast.Num(2)])) self.verify(precedence, 'not (1 or 2)')
def visit_TryExcept(self, node): if node.orelse: raise NotImplementedError('Try-except else handler not implemented') self.indent() self.output('try') self.block(node.body, context=BlockContext(self.stack[-1])) self.output(' catch($e) ') self.push(BlockContext(self.stack[-1])) if_start = None if_end = None for handler in node.handlers: if handler.type is not None: if handler.name is not None: body = handler.body[:] body.insert(0, ast.Assign( [handler.name], ast_call(ast_load('JS'), ast.Str('$e')) )) else: body = handler.body types = [handler.type] if isinstance(handler.type, _ast.Name) else handler.type conditions = [ ast_call( ast_load('isinstance'), ast_call(ast_load('JS'), ast.Str('$e')), type_, ) for type_ in types ] _if = ast.If( ast.BoolOp(ast.Or(), conditions), body, [] ) if if_start is None: if_start = if_end = _if else: if_end.orelse, if_end = [_if], _if else: if handler is not node.handlers[-1]: raise SyntaxError("default 'except:' must be last") if if_start is None: self.block(handler.body) else: if_end.orelse = handler.body if if_start is not None: self.visit(if_start) self.pop() self.output('\n')
def missingCheck(check: ast.Compare, el: ast.Subscript, default: bool) -> ast.BoolOp: """Creates AST nodes corresponding to check whether the `el` is None, if it is returns `default`""" return ast.BoolOp(op=(ast.Or() if default else ast.And()), values=[ ast.Compare( left=el, ops=[(ast.Is() if default else ast.IsNot())], comparators=[noneCnst]), check ])
def __parseRule(node): type = Rule.TYPE_INCLUDE if node.tagName == 'Include' else Rule.TYPE_EXCLUDE tree = ast.Expression(lineno=1, col_offset=0) expr = __parseBoolOp(node, ast.Or()) if expr: tree.body = expr else: tree.body = ast.Name('False', ast.Load()) ast.fix_missing_locations(tree) return Rule(type, tree)
def parse_rule(self, node): type = Rule.TYPE_INCLUDE if node.tag == 'Include' else Rule.TYPE_EXCLUDE tree = ast.Expression(lineno=1, col_offset=0) expr = self.parse_bool_op(node, ast.Or()) if expr: tree.body = expr else: tree.body = _ast_const('False') ast.fix_missing_locations(tree) return Rule(type, tree)
def visit_Compare(self, node): # cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn print(" in MyTransformer.visit_Compare()") print(" node =", node) print(" op =", node.ops[0]) curr_op = node.ops[0] comp_negate = curr_op rand_num = random.randint(1, 10) if rand_num >= 7: print(" negating...") if isinstance(curr_op, ast.Eq): comp_negate = ast.NotEq() elif isinstance(curr_op, ast.NotEq): comp_negate = ast.Eq() elif isinstance(curr_op, ast.Lt): comp_negate = ast.GtE() elif isinstance(curr_op, ast.LtE): comp_negate = ast.Gt() elif isinstance(curr_op, ast.Gt): comp_negate = ast.LtE() elif isinstance(curr_op, ast.GtE): comp_negate = ast.Lt() elif isinstance(curr_op, ast.Is): comp_negate = ast.IsNot() elif isinstance(curr_op, ast.IsNot): comp_negate = ast.Is() elif isinstance(curr_op, ast.In): comp_negate = ast.NotIn() elif isinstance(curr_op, ast.NotIn): comp_negate = ast.In() else: comp_negate = ast.Eq() else: print(" mixing up...") if isinstance(curr_op, ast.Lt): comp_negate = ast.LtE() elif isinstance(curr_op, ast.LtE): comp_negate = ast.And() elif isinstance(curr_op, ast.Gt): comp_negate = ast.Or() elif isinstance(curr_op, ast.GtE): comp_negate = ast.Gt() elif isinstance(curr_op, ast.Is): comp_negate = ast.Gt() elif isinstance(curr_op, ast.IsNot): comp_negate = ast.Lt() elif isinstance(curr_op, ast.In): comp_negate = ast.In( ) #leave the same for for loops (for x in this) elif isinstance(curr_op, ast.NotIn): comp_negate = ast.Lt() else: comp_negate = ast.Eq() print(" new comparator =", comp_negate) # create negated node | Compare(expr left, cmpop* ops, expr* comparators) new_node = node new_node.ops = [comp_negate] ast.copy_location(new_node, node) ast.fix_missing_locations(new_node) return new_node
def _dnf(self, node): if type(node) != ast.BoolOp: return node values = [self._dnf(child) for child in node.values] if type(node.op) == ast.Or: return ast.BoolOp(op=ast.Or(), values=values) for index, value in enumerate(values): if (type(value) == ast.BoolOp) and (type(value.op) == ast.Or): maxterm = values[index] values = values[:index] + values[index + 1:] values = [ ast.BoolOp(op=ast.And(), values=[v] + values) for v in maxterm.values ] return self._dnf(ast.BoolOp(op=ast.Or(), values=values)) return node
def stop_iteration(node): cmp_break = ast.Compare(left=mk_name(self.flag_id), ops=[ast.Eq()], comparators=[mk_str(self.BREAK)]) cmp_return = ast.Compare(left=mk_name(self.flag_id), ops=[ast.Eq()], comparators=[mk_str(self.RETURN)]) test = ast.BoolOp(op=ast.Or(), values=[cmp_break, cmp_return]) break_stmt = ast.Break() ifstmt = ast.If(test=test, body=[break_stmt], orelse=[]) node.body.append(ifstmt)
def __or__(self, other) -> Column: ''' Bitwise and becomes a logical and. ''' self._test_for_extension('operator or') from .utils import _term_to_ast return Column( type(bool), ast.BoolOp(op=ast.Or(), values=[ ast.Name('p', ctx=ast.Load()), _term_to_ast(other, self) ]))
def visit_BoolOp(self, node): if self.randomize(): if self.randomize(): op = ast.And() else: op = ast.Or() values = [] for value in node.values: values.append(self.visit(value)) return ast.copy_location(_ast.BoolOp(op=op, values=values), node) else: return node
def test_BoolOp(self): and_op = ast.BoolOp(ast.And(), [ast.Num(2), ast.Num(3)]) self.verify(and_op, '2 and 3') or_op = ast.BoolOp(ast.Or(), [ast.Num(2), ast.Num(3)]) self.verify(or_op, '2 or 3') many_args = ast.BoolOp(ast.And(), [ast.Num(1), ast.Num(2), ast.Num(3)]) self.verify(many_args, '1 and 2 and 3') no_precedence = ast.BoolOp( ast.Or(), [ast.BoolOp(ast.And(), [ast.Num(2), ast.Num(3)]), ast.Num(1)]) self.verify(no_precedence, '2 and 3 or 1') no_precedence2 = ast.BoolOp( ast.Or(), [ast.Num(2), ast.BoolOp(ast.And(), [ast.Num(3), ast.Num(1)])]) self.verify(no_precedence2, '2 or 3 and 1') precedence = ast.BoolOp( ast.And(), [ast.Num(1), ast.BoolOp(ast.Or(), [ast.Num(2), ast.Num(3)])]) self.verify(precedence, '1 and (2 or 3)')