本文整理匯總了Python中ast.Invert方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.Invert方法的具體用法?Python ast.Invert怎麽用?Python ast.Invert使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.Invert方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: check_not
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Invert [as 別名]
def check_not(self, target, pattern):
'Check NOT pattern node that could be in another form'
if self.is_wildcard(pattern.operand):
wkey = pattern.operand.id
if isinstance(target, ast.Num):
if wkey not in self.wildcards:
mod = 2**self.nbits
self.wildcards[wkey] = ast.Num((~target.n) % mod)
return True
else:
wilds2 = self.wildcards[pattern.operand.id]
num = ast.Num((~target.n) % 2**self.nbits)
return asttools.Comparator().visit(wilds2, num)
else:
if wkey not in self.wildcards:
self.wildcards[wkey] = ast.UnaryOp(ast.Invert(),
target)
return True
return self.check_eq_z3(target, pattern)
else:
subpattern = pattern.operand
newtarget = ast.UnaryOp(ast.Invert(), target)
return self.check_eq_z3(newtarget, subpattern)
示例2: factor_rewrite
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Invert [as 別名]
def factor_rewrite(mark: Tokenizer, factor, power):
return power if power else ast.UnaryOp(
**(loc @ mark),
op={
'~': ast.Invert,
'+': ast.UAdd,
'-': ast.USub
}[mark.value](),
operand=factor,
)
示例3: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Invert [as 別名]
def visit_UnaryOp(self, node, **kwargs):
if isinstance(node.op, (ast.Not, ast.Invert)):
return UnaryOp('~', self.visit(node.operand))
elif isinstance(node.op, ast.USub):
return self.const_type(-self.visit(node.operand).value, self.env)
elif isinstance(node.op, ast.UAdd):
raise NotImplementedError('Unary addition not supported')
示例4: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Invert [as 別名]
def visit_UnaryOp(self, node):
'Change ~x to - x - 1'
if isinstance(node.op, ast.Invert):
return ast.BinOp(ast.UnaryOp(ast.USub(), node.operand),
ast.Add(),
ast.Num(-1))
return self.generic_visit(node)
示例5: check_pattern
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Invert [as 別名]
def check_pattern(self, target, pattern):
'Try to match pattern written in different ways'
if asttools.CheckConstExpr().visit(pattern):
if isinstance(target, ast.Num):
# if pattern is only a constant, evaluate and compare
# to target
pattcopy = deepcopy(pattern)
eval_pat = asttools.ConstFolding(pattcopy,
self.nbits).visit(pattcopy)
return self.visit(target, eval_pat)
if isinstance(target, ast.Num):
# check that wildcards in pattern have not been affected
return self.get_model(target, pattern)
# deal with NOT that could have been evaluated before
notnode = (isinstance(pattern, ast.UnaryOp) and
isinstance(pattern.op, ast.Invert))
if notnode:
return self.check_not(target, pattern)
# deal with (-1)*B that could have been evaluated
negnode = (isinstance(pattern, ast.BinOp)
and isinstance(pattern.op, ast.Mult)
and isinstance(pattern.left, ast.Num)
and pattern.left.n == -1)
if negnode:
return self.check_neg(target, pattern)
# deal with 2*B
multnode = (isinstance(pattern, ast.BinOp) and
isinstance(pattern.op, ast.Mult))
if multnode:
return self.check_twomult(target, pattern)
# return self.general_check(target, pattern)
return False
示例6: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Invert [as 別名]
def visit_UnaryOp(self, node):
'Replace bitwise unaryop with function call'
self.generic_visit(node)
if isinstance(node.op, ast.Invert):
return ast.Call(ast.Name('mnot', ast.Load()),
[node.operand], [], None, None)
return node
示例7: visit_Call
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Invert [as 別名]
def visit_Call(self, node):
'Replace custom function with bitwise operators'
self.generic_visit(node)
if isinstance(node.func, ast.Name):
if len(node.args) == 2:
if node.func.id == "mand":
op = ast.BitAnd()
elif node.func.id == "mxor":
op = ast.BitXor()
elif node.func.id == "mor":
op = ast.BitOr()
elif node.func.id == "mlshift":
op = ast.LShift()
elif node.func.id == "mrshift":
op = ast.RShift()
else:
return node
return ast.BinOp(node.args[0],
op,
node.args[1])
elif len(node.args) == 1 and node.func.id == "mnot":
arg = node.args[0]
self.generic_visit(node)
return ast.UnaryOp(ast.Invert(), arg)
return self.generic_visit(node)
示例8: __init__
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Invert [as 別名]
def __init__( s, component ):
super().__init__( component )
s.loop_var_env = set()
s.tmp_var_env = set()
# opmap maps an ast operator to its RTLIR counterpart.
s.opmap = {
# Bool operators
# Note: we do not support boolean operators because Python does
# not allow overloading And and Or operators. Using them in
# expressions might lead to inconsistent semantics.
# ast.And : bir.And(), ast.Or : bir.Or(),
# Unary operators
# Note: ast.Not is disallowed because it is a boolean operator
# ast.Not : bir.Not(),
ast.Invert : bir.Invert(),
ast.UAdd : bir.UAdd(), ast.USub : bir.USub(),
# Binary operators
ast.Add : bir.Add(), ast.Sub : bir.Sub(),
ast.Mult : bir.Mult(), ast.Div : bir.Div(),
ast.Mod : bir.Mod(), ast.Pow : bir.Pow(),
ast.LShift : bir.ShiftLeft(), ast.RShift : bir.ShiftRightLogic(),
ast.BitOr : bir.BitOr(), ast.BitAnd : bir.BitAnd(),
ast.BitXor : bir.BitXor(),
# Compare bir.bir.operators
ast.Eq : bir.Eq(), ast.NotEq : bir.NotEq(),
ast.Lt : bir.Lt(), ast.LtE : bir.LtE(),
ast.Gt : bir.Gt(), ast.GtE : bir.GtE()
}
# Override
示例9: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Invert [as 別名]
def visit_UnaryOp(self, node: ast.UnaryOp) -> Any:
"""Visit the node operand and apply the operation on the result."""
if isinstance(node.op, ast.UAdd):
result = +self.visit(node=node.operand)
elif isinstance(node.op, ast.USub):
result = -self.visit(node=node.operand)
elif isinstance(node.op, ast.Not):
result = not self.visit(node=node.operand)
elif isinstance(node.op, ast.Invert):
result = ~self.visit(node=node.operand)
else:
raise NotImplementedError("Unhandled op of {}: {}".format(node, node.op))
self.recomputed_values[node] = result
return result
示例10: unaryop
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Invert [as 別名]
def unaryop(self, block, node):
operand = self.expression(block, node.operand)
if isinstance(node.op, ast.UAdd):
#No-Op
var = operand
elif isinstance(node.op, ast.USub):
if operand.value is not None:
#It's a literal, return the negation
var = self.add_literal(-operand.value)
else:
#It's an expression, subtract from zero (faster than multiplying by -1)
var = self.add_variable(None)
zero = self.add_literal(0, 'ZERO')
operation = BinaryOperation(zero, Operator.subtract, operand)
assignment = Assignment(var, operation)
block.add(assignment)
elif isinstance(node.op, ast.Invert):
#Make sure it's numeric
if operand.is_mask:
raise Exception('Unary invert requires a numeric operand')
#Bit-flip
var = self.add_variable(None)
operation = UnaryOperation(Operator.bit_not, operand)
assignment = Assignment(var, operation)
block.add(assignment)
elif isinstance(node.op, ast.Not):
#Make sure it's boolean
if not operand.is_mask:
raise Exception('Unary not requires a boolean operand')
#Invert the mask
var = self.add_variable(None, is_mask=True)
operation = UnaryOperation(Operator.bool_not, operand)
assignment = Assignment(var, operation)
block.add(assignment)
else:
raise Exception('Unexpected UnaryOp (%s)'%(node.op.__class__))
return var
#Parses a comparison operator (AST CmpOp)
示例11: p_factor_3
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Invert [as 別名]
def p_factor_3(p):
'''factor : TILDE factor'''
# 1 2
op = ast.Invert(rule=inspect.currentframe().f_code.co_name, **p[1][1])
p[0] = ast.UnaryOp(op, p[2], rule=inspect.currentframe().f_code.co_name)
inherit_lineno(p[0], op)