本文整理汇总了Python中ast.UAdd方法的典型用法代码示例。如果您正苦于以下问题:Python ast.UAdd方法的具体用法?Python ast.UAdd怎么用?Python ast.UAdd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ast
的用法示例。
在下文中一共展示了ast.UAdd方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: visit_UnaryOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import UAdd [as 别名]
def visit_UnaryOp(self, node):
self.visit(node.operand)
op = node.op
node.obj = node.operand.obj
if isinstance(op, ast.Not):
node.obj = bool()
elif isinstance(op, ast.UAdd):
node.obj = int(-1)
elif isinstance(op, ast.USub):
node.obj = int(-1)
示例2: factor_rewrite
# 需要导入模块: import ast [as 别名]
# 或者: from ast import UAdd [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: visitUnaryOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import UAdd [as 别名]
def visitUnaryOp(self, node):
import ast
if isinstance(node.op, ast.UAdd):
return +self.visit(node.operand)
elif isinstance(node.op, ast.USub):
return -self.visit(node.operand)
else:
raise SyntaxError("Unknown unary op: %r" % node.op)
示例4: visit_UnaryOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import UAdd [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')
示例5: visit_UnaryOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import UAdd [as 别名]
def visit_UnaryOp(self, node):
'Change USub and Invert'
operand = self.visit(node.operand)
if isinstance(node.op, ast.UAdd):
return operand
if isinstance(node.op, ast.USub):
return ast.BinOp(ast.Num(-1), ast.Mult(), operand)
if isinstance(node.op, ast.Invert):
return ast.BinOp(ast.Num(-1), ast.BitXor(), operand)
assert False, 'unhandled node type: ' + ast.dump(node)
示例6: visitUnaryOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import UAdd [as 别名]
def visitUnaryOp(self, node):
import ast
if isinstance(node.op, ast.UAdd):
return +self.visit(node.operand)
elif isinstance(node.op, ast.USub):
return -self.visit(node.operand)
else:
raise SyntaxError("Unknown unary op: %r" % node.op)
示例7: __init__
# 需要导入模块: import ast [as 别名]
# 或者: from ast import UAdd [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
示例8: visit_UnaryOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import UAdd [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
示例9: visit_UnaryOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import UAdd [as 别名]
def visit_UnaryOp(self, node):
"""
Adjust operand value and discard unary operations, where possible.
This is done so that negative decimal literals are accurately represented.
"""
self.generic_visit(node)
# TODO once grammar is updated, remove this
# UAdd has no effect on the value of it's operand, so it is discarded
if isinstance(node.op, python_ast.UAdd):
return node.operand
is_sub = isinstance(node.op, python_ast.USub)
is_num = (
hasattr(node.operand, "n")
and not isinstance(node.operand.n, bool)
and isinstance(node.operand.n, (int, Decimal))
)
if is_sub and is_num:
node.operand.n = 0 - node.operand.n
node.operand.col_offset = node.col_offset
node.operand.node_source_code = node.node_source_code
return node.operand
else:
return node
示例10: unaryop
# 需要导入模块: import ast [as 别名]
# 或者: from ast import UAdd [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: is_interesting_expression
# 需要导入模块: import ast [as 别名]
# 或者: from ast import UAdd [as 别名]
def is_interesting_expression(node):
# type: (ast.AST) -> bool
"""
If this expression has a value that may not be exactly what it looks like,
return True. Put differently, return False if this is just a literal.
"""
return (isinstance(node, ast.expr) and
not (isinstance(node, (ast.Num, ast.Str, getattr(ast, 'NameConstant', ()))) or
isinstance(getattr(node, 'ctx', None),
(ast.Store, ast.Del)) or
(isinstance(node, ast.UnaryOp) and
isinstance(node.op, (ast.UAdd, ast.USub)) and
isinstance(node.operand, ast.Num)) or
(isinstance(node, (ast.List, ast.Tuple, ast.Dict)) and
not any(is_interesting_expression(n) for n in ast.iter_child_nodes(node)))))
示例12: visit_UAdd
# 需要导入模块: import ast [as 别名]
# 或者: from ast import UAdd [as 别名]
def visit_UAdd(self, node):
trailing_nodes = list(map(type, self.node_window[-4:]))
if trailing_nodes == [ast.UnaryOp, ast.UAdd, ast.UnaryOp, ast.UAdd]:
originator = self.node_window[-4]
self.errors.append(B002(originator.lineno, originator.col_offset))
self.generic_visit(node)
示例13: p_factor_1
# 需要导入模块: import ast [as 别名]
# 或者: from ast import UAdd [as 别名]
def p_factor_1(p):
'''factor : PLUS factor'''
# 1 2
op = ast.UAdd(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)