我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用ast.Num()。
def __init__(self, expr, context): self.expr = expr self.context = context self.expr_table = { LLLnode: self.get_expr, ast.Num: self.number, ast.Str: self.string, ast.NameConstant: self.constants, ast.Name: self.variables, ast.Attribute: self.attribute, ast.Subscript: self.subscript, ast.BinOp: self.arithmetic, ast.Compare: self.compare, ast.BoolOp: self.boolean_operations, ast.UnaryOp: self.unary_operations, ast.Call: self.call, ast.List: self.list_literals, ast.Dict: self.struct_literals, ast.Tuple: self.tuple_literals, } expr_type = self.expr.__class__ if expr_type in self.expr_table: self.lll_node = self.expr_table[expr_type]() else: raise Exception("Unsupported operator: %r" % ast.dump(self.expr))
def visit_BinOp(self, node): node = self.generic_visit(node) left = node.left right = node.right if all(isinstance(value, ast.Num) for value in (left, right)): if isinstance(node.op, tuple(self._operators.keys())): val = self._operators[type(node.op)](left.n, right.n) node = ast.copy_location(ast.Num(n = val), node) return node elif all(isinstance(value, ast.Str) for value in (left, right)): if isinstance(node.op, ast.Add): val = left.s + right.s node = ast.copy_location(ast.Str(s = val), node) return node return self.fold(node) #def visit_GeneratorExp(self, node): # return self.comprehension(node)
def visit_BinOp(self, node: ast.BinOp) -> ast.BinOp: node = self.generic_visit(node) if self._is_numeric_mult(node): if isinstance(node.right, ast.Num): if node.right.n == 0: node = ast.copy_location(ast.Num(n = 0), node) elif node.right.n == 1: node = node.left elif node.right.n == 2: node.op = ast.copy_location(ast.Add(), node.op) node.right = copy(node.left) elif isinstance(node.left , ast.Num): if node.left.n == 0: node = ast.copy_location(ast.Num(n = 0), node) elif node.left.n == 1: node = node.right elif node.left.n == 2: node.op = ast.copy_location(ast.Add(), node.op) node.left = copy(node.right) return node
def visit_Module(self, node): for expr in node.body: if not isinstance(expr, ast.Assign): continue if not isinstance(expr.value, (ast.Num, ast.Str)): continue if len(expr.targets) != 1: continue name = expr.targets[0] if not isinstance(name, ast.Name): continue name = name.id if not self.is_const_name(name): continue if name in self._constants: self._constants[name] = None else: self._constants[name] = expr.value return self.generic_visit(node)
def eval_expr(expr): """ Eval and expression inside a #define using a suppart of python grammar """ def _eval(node): if isinstance(node, ast.Num): return node.n elif isinstance(node, ast.BinOp): return OPERATORS[type(node.op)](_eval(node.left), _eval(node.right)) elif isinstance(node, ast.UnaryOp): return OPERATORS[type(node.op)](_eval(node.operand)) elif isinstance(node, ast.BoolOp): values = [_eval(x) for x in node.values] return OPERATORS[type(node.op)](**values) else: raise TypeError(node) return _eval(ast.parse(expr, mode='eval').body)
def basicTypeSpecialFunction(cv): """If you're in a number or string (which has no metadata), move up to the AST to make the special functions work.""" if isinstance(cv, SwapVector) or isinstance(cv, MoveVector): return cv if (cv.path[0] in [('n', 'Number'), ('s', 'String'), ('id', 'Name'), ('arg', 'Argument'), ('value', 'Name Constant'), ('s', 'Bytes'), ('name', 'Alias')]): cvCopy = cv.deepcopy() cv.oldSubtree = deepcopy(cvCopy.traverseTree(cv.start)) if cv.path[0] == ('n', 'Number'): cv.newSubtree = ast.Num(cv.newSubtree) elif cv.path[0] == ('s', 'String'): cv.newSubtree = ast.Str(cv.newSubtree) elif cv.path[0] == ('id', 'Name'): cv.newSubtree = ast.Name(cv.newSubtree, cv.oldSubtree.ctx) elif cv.path[0] == ('arg', 'Argument'): cv.newSubtree = ast.arg(cv.newSubtree, cv.oldSubtree.annotation) elif cv.path[0] == ('value', 'Name Constant'): cv.newSubtree = ast.NameConstant(cv.newSubtree) elif cv.path[0] == ('s', 'Bytes'): cv.newSubtree = ast.Bytes(cv.newSubtree) elif cv.path[0] == ('name', 'Alias'): cv.newSubtree = ast.alias(cv.newSubtree, cv.oldSubtree.asname) cv.path = cv.path[1:] return cv
def cleanupRanges(a): """Remove any range shenanigans, because Python lets you include unneccessary values""" if not isinstance(a, ast.AST): return a if type(a) == ast.Call: if type(a.func) == ast.Name: if a.func.id in ["range"]: if len(a.args) == 3: # The step defaults to 1! if type(a.args[2]) == ast.Num and a.args[2].n == 1: a.args = a.args[:-1] if len(a.args) == 2: # The start defaults to 0! if type(a.args[0]) == ast.Num and a.args[0].n == 0: a.args = a.args[1:] return applyToChildren(a, cleanupRanges)
def cleanupSlices(a): """Remove any slice shenanigans, because Python lets you include unneccessary values""" if not isinstance(a, ast.AST): return a if type(a) == ast.Subscript: if type(a.slice) == ast.Slice: # Lower defaults to 0 if a.slice.lower != None and type(a.slice.lower) == ast.Num and a.slice.lower.n == 0: a.slice.lower = None # Upper defaults to len(value) if a.slice.upper != None and type(a.slice.upper) == ast.Call and \ type(a.slice.upper.func) == ast.Name and a.slice.upper.func.id == "len": if compareASTs(a.value, a.slice.upper.args[0], checkEquality=True) == 0: a.slice.upper = None # Step defaults to 1 if a.slice.step != None and type(a.slice.step) == ast.Num and a.slice.step.n == 1: a.slice.step = None return applyToChildren(a, cleanupSlices)
def num_negate(op): top = type(op) neg = not op.num_negated if hasattr(op, "num_negated") else True if top == ast.Add: newOp = ast.Sub() elif top == ast.Sub: newOp = ast.Add() elif top in [ast.Mult, ast.Div, ast.Mod, ast.Pow, ast.LShift, ast.RShift, ast.BitOr, ast.BitXor, ast.BitAnd, ast.FloorDiv]: return None # can't negate this elif top in [ast.Num, ast.Name]: # this is a normal value, so put a - in front of it newOp = ast.UnaryOp(ast.USub(addedNeg=True), op) else: log("astTools\tnum_negate\tUnusual type: " + str(top), "bug") transferMetaData(op, newOp) newOp.num_negated = neg return newOp
def isDefault(a): """Our programs have a default setting of return 42, so we should detect that""" if type(a) == ast.Module and len(a.body) == 1: a = a.body[0] else: return False if type(a) != ast.FunctionDef: return False if len(a.body) == 0: return True elif len(a.body) == 1: if type(a.body[0]) == ast.Return: if a.body[0].value == None or \ type(a.body[0].value) == ast.Num and a.body[0].value.n == 42: return True return False
def get_version(): with open(os.path.join('settei', 'version.py')) as f: tree = ast.parse(f.read(), f.name) for node in ast.walk(tree): if not (isinstance(node, ast.Assign) and len(node.targets) == 1): continue target, = node.targets value = node.value if not (isinstance(target, ast.Name) and target.id == 'VERSION_INFO' and isinstance(value, ast.Tuple)): continue elts = value.elts if any(not isinstance(elt, ast.Num) for elt in elts): continue return '.'.join(str(elt.n) for elt in elts)
def test_increment_lineno(self): src = ast.parse('1 + 1', mode='eval') self.assertEqual(ast.increment_lineno(src, n=3), src) self.assertEqual(ast.dump(src, include_attributes=True), 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' 'col_offset=0))' ) # issue10869: do not increment lineno of root twice src = ast.parse('1 + 1', mode='eval') self.assertEqual(ast.increment_lineno(src.body, n=3), src.body) self.assertEqual(ast.dump(src, include_attributes=True), 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' 'col_offset=0))' )
def parse_args(args): arg_list = [] for arg in args: if isinstance(arg, ast.Str): arg_list.append("%s" % arg.s) elif isinstance(arg, ast.Name): value = arg.id if value == "None": arg_list.append(None) else: arg_list.append(value) elif isinstance(arg, ast.Num): arg_list.append(arg.n) elif isinstance(arg, ast.List): arg_list.append(parse_args(arg.elts)) elif isinstance(arg, ast.Tuple): arg_list.append(tuple(parse_args(arg.elts))) elif isinstance(arg, ast.Attribute): arg_list.append(str(arg.value.id) + "." + str(arg.attr)) else: print(arg, type(arg)) return arg_list
def get_type(self, node): if isinstance(node, ast.Num): return Type.NUMBER elif isinstance(node, ast.Str): return Type.STRING elif isinstance(node, ast.Name): if self.variables[node.id] is not None: return self.variables[node.id].var_type else: return Type.VOID elif isinstance(node, ast.BinOp): if self.get_type(node.left).is_number and self.get_type(node.right).is_number: return Type.NUMBER elif self.get_type(node.left).is_string or self.get_type(node.right).is_string: return Type.STRING elif isinstance(node, ast.Call): return self.functions[node.func.id].return_type else: return Type.VOID
def _UnaryOp(self, t): self.write("(") self.write(self.unop[t.op.__class__.__name__]) self.write(" ") # If we're applying unary minus to a number, parenthesize the number. # This is necessary: -2147483648 is different from -(2147483648) on # a 32-bit machine (the first is an int, the second a long), and # -7j is different from -(7j). (The first has real part 0.0, the second # has real part -0.0.) if isinstance(t.op, ast.USub) and isinstance(t.operand, ast.Num): self.write("(") self.dispatch(t.operand) self.write(")") else: self.dispatch(t.operand) self.write(")")
def visit_For(self, node): """ for i in 5: pass # range(5) for i in 2^10: pass # range(2, 10) for i in 1^100^5: pass # range(2, 10, 5) """ self.generic_visit(node) it = node.iter if isinstance(it, ast.Num): node.iter = self.make_Call('range', [it]) elif self._is_range_op(it): # two cases, 2^10 and 1^100^5 range_args = [] left = it.left if self._is_range_op(left): range_args.append(left.left) range_args.append(left.right) else: range_args.append(left) range_args.append(it.right) node.iter = self.make_Call('range', range_args) return node
def test_subscript(self): sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)), ast.Load()) self.expr(sub, "must have Load context") x = ast.Name("x", ast.Load()) sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())), ast.Load()) self.expr(sub, "must have Load context") s = ast.Name("x", ast.Store()) for args in (s, None, None), (None, s, None), (None, None, s): sl = ast.Slice(*args) self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context") sl = ast.ExtSlice([]) self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice") sl = ast.ExtSlice([ast.Index(s)]) self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
def specialize_constant(node, value): if value is None or isinstance(value, bool): new_node = ast.NameConstant(value=value) elif isinstance(value, (int, float, complex)): new_node = ast.Num(n=value) elif isinstance(value, str): new_node = ast.Str(s=value) elif isinstance(value, bytes): new_node = ast.Bytes(s=value) elif isinstance(value, tuple): elts = [specialize_constant(node, elt) for elt in value] new_node = ast.Tuple(elts=elts, ctx=ast.Load()) else: raise ValueError("unknown constant: %r" % value) fatoptimizer.tools.copy_lineno(node, new_node) return new_node
def parse_array_slice_py_ast(array_node): if hasattr(array_node.slice, 'value'): getitem_tuple = array_node.slice.value elif hasattr(array_node.slice, 'dims'): getitem_tuple = array_node.slice.dims elif isinstance(array_node.slice, ast.Slice): getitem_tuple = array_node.slice if hasattr(getitem_tuple, 'elts'): getitem_tuple = getitem_tuple.elts if isinstance(getitem_tuple, ast.Name) or isinstance(getitem_tuple, ast.Slice) or isinstance(getitem_tuple, ast.Num): getitem_tuple = [getitem_tuple] try: getitem_strs = [py_ast.dump_ast(x) for x in getitem_tuple] except: getitem_strs = [py_ast.dump_ast(x) for x in getitem_tuple.elts] return (getitem_tuple, getitem_strs)
def make_subscript(varname, idx, ctx=None, lineno=0, col_offset=0): ctx = ctx or ast.Load() return ast.Subscript( value = ast.Name( id = varname, ctx = ast.Load(), lineno = lineno, col_offset = col_offset, ), slice = ast.Index( value = ast.Num( n = idx, lineno = lineno, col_offset = col_offset, ), lineno = lineno, col_offset = col_offset, ), ctx = ctx, lineno = lineno, col_offset = col_offset, )
def convert_to_value(item): if isinstance(item, ast.Str): return item.s elif hasattr(ast, 'Bytes') and isinstance(item, ast.Bytes): return item.s elif isinstance(item, ast.Tuple): return tuple(convert_to_value(i) for i in item.elts) elif isinstance(item, ast.Num): return item.n elif isinstance(item, ast.Name): result = VariableKey(item=item) constants_lookup = { 'True': True, 'False': False, 'None': None, } return constants_lookup.get( result.name, result, ) elif (not PY33) and isinstance(item, ast.NameConstant): # None, True, False are nameconstants in python3, but names in 2 return item.value else: return UnhandledKeyType()
def eval_numeric_constexpr(node: ast.AST) -> int: if isinstance(node, ast.Num): return node.n if isinstance(node, ast.UnaryOp): if isinstance(node.op, ast.UAdd): return +eval_numeric_constexpr(node.operand) elif isinstance(node.op, ast.USub): return -eval_numeric_constexpr(node.operand) else: return None if isinstance(node, ast.BinOp): if isinstance(node.op, ast.Add): return eval_numeric_constexpr(node.left) + eval_numeric_constexpr(node.right) if isinstance(node.op, ast.Sub): return eval_numeric_constexpr(node.left) - eval_numeric_constexpr(node.right) if isinstance(node.op, ast.Mult): return eval_numeric_constexpr(node.left) * eval_numeric_constexpr(node.right) if isinstance(node.op, ast.Div): return eval_numeric_constexpr(node.left) / eval_numeric_constexpr(node.right) return None
def parse_decorator(node: ast.AST): if isinstance(node, ast.Name): ret = Decorator() ret.name = node.id return ret elif isinstance(node, ast.Call): ret = Decorator() ret.name = node.func.id for arg in node.args: if isinstance(arg, ast.Num): ret.args.append(str(arg.n)) elif isinstance(arg, ast.Str): ret.args.append(str(arg.n)) elif isinstance(arg, ast.Name): ret.args.append(arg.id) else: v = eval_numeric_constexpr(arg) if v: ret.args.append(str(v)) else: error(loc(node), "Unsupported decorator type") return ret else: error(loc(node), "Supported decorators are Name and Call") return None
def PYSL_tl_decl(node: ast.AnnAssign): """Parses a specific top-level declaration""" if not isinstance(node.annotation, ast.Call) or (node.annotation.func.id != 'register' or len(node.annotation.args) != 2 or not isinstance(node.annotation.args[0], ast.Name) or not isinstance(node.annotation.args[1], ast.Num)): error(loc(node), "Invalid top level resource declaration, see docs. <name> : register(<type>, <slot>) = (...)") res_name = node.target.id res_type = node.annotation.args[0].id res_slot = node.annotation.args[1].n if res_type in pysl.Language.Sampler.TYPES: emitter.sampler(parse_sampler(node, res_name, res_type[7:], res_slot, node.value)) else: error((node), "Unrecognized top-level resource declaration {0} : {1}".format(res_name, res_type))
def get_type(expr): """Find the type of an expression. Args: expr: The expression to check. Returns: The type of the expression. """ if isinstance(expr, ast.Num): return build_pb2.Attribute.INTEGER elif isinstance(expr, ast.Str): return build_pb2.Attribute.STRING elif isinstance(expr, ast.List): return build_pb2.Attribute.STRING_LIST elif isinstance(expr, ast.Name) and (expr.id == "True" or expr.id == "False"): return build_pb2.Attribute.BOOLEAN else: return build_pb2.Attribute.UNKNOWN
def subs(root, **kwargs): '''Substitute ast.Name nodes for numbers in root using the mapping in kwargs. Returns a new copy of root. ''' root = copy.deepcopy(root) class Transformer(ast.NodeTransformer): def visit_FunctionDef(self, node): return node def visit_Name(self, node): if node.id in kwargs and not isinstance(node.ctx, ast.Store): replacement = kwargs[node.id] if isinstance(replacement, int): return ast.copy_location(ast.Num(n=replacement), node) else: return copy.copy(replacement) else: return node return Transformer().visit(root)
def parse_factor_expression(call_or_name_node): if isinstance(call_or_name_node, ast.Name): # a.set_to(b) is shorthand for a.set_to(Copy(b)) name_node = call_or_name_node return None, [name_node.id] elif isinstance(call_or_name_node, ast.Call): # a.set_to(f(b)) call_node = call_or_name_node return call_node.func.id, [name_or_number(node) for node in call_node.args] elif isinstance(call_or_name_node, ast.Num): # a.observe_value(0) num_node = call_or_name_node return None, [int(num_node.n)] elif isinstance(call_or_name_node, ast.Subscript): print ast.dump(call_or_name_node) pdb.set_trace() else: assert False, "Can't parse factor " + ast.dump(call_or_name_node)
def unfold_list(_list): if any(isinstance(x, ast.Call) for x in _list): new_list = list() for e in _list: if isinstance(e, ast.Call): name = e.func.id if name == 'r': try: lower = e.args[0].n upper = e.args[1].n r_list = r(lower, upper) for elem in r_list: new_list.append(ast.Num(elem)) except Exception: raise AttributeError( "function 'r' needs 2 integer arguments") else: return _list else: new_list.append(e) return new_list else: return _list
def eval_expr(expr): import ast import operator as op op = { ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul, ast.Div: op.truediv, ast.Pow: op.pow, ast.BitXor: op.xor, ast.USub: op.neg, } def eval_(node): if isinstance(node, ast.Num): return fractions.Fraction(node.n) elif isinstance(node, ast.BinOp): return op[type(node.op)](eval_(node.left), eval_(node.right)) elif isinstance(node, ast.UnaryOp): return op[type(node.op)](eval_(node.operand)) raise TypeError(node) return eval_(ast.parse(str(expr), mode='eval').body)
def get_info(filename): info = {} with open(filename) as _file: data = ast.parse(_file.read()) for node in data.body: if type(node) != ast.Assign: continue if type(node.value) not in [ast.Str, ast.Num]: continue name = None for target in node.targets: name = target.id if type(node.value) == ast.Str: info[name] = node.value.s elif type(node.value) == ast.Num: info[name] = node.value.n return info
def buildexec(self): if isinstance(self.schema, Null): if sys.version_info[0] <= 2: return ast.Name("None", ast.Load()) else: return ast.NameConstant(None) elif isinstance(self.schema, Boolean): if sys.version_info[0] <= 2: if self.value: return ast.Name("True", ast.Load()) else: return ast.Name("False", ast.Load()) else: if self.value: return ast.NameConstant(True) else: return ast.NameConstant(False) elif isinstance(self.schema, Number): return ast.Num(self.value) else: raise NotImplementedError # have to think about this when the case comes up
def version(): path = 'pypika/__init__.py' with open(path, 'rU') as file: t = compile(file.read(), path, 'exec', ast.PyCF_ONLY_AST) for node in (n for n in t.body if isinstance(n, ast.Assign)): if len(node.targets) == 1: name = node.targets[0] if isinstance(name, ast.Name) and \ name.id in ('__version__', '__version_info__', 'VERSION'): v = node.value if isinstance(v, ast.Str): return v.s if isinstance(v, ast.Tuple): r = [] for e in v.elts: if isinstance(e, ast.Str): r.append(e.s) elif isinstance(e, ast.Num): r.append(str(e.n)) return '.'.join(r)
def reify(kvalue, gen_sym): value = kvalue.value if value is True or value is False or value is None: return ast.NameConstant(value=value), gen_sym, {} elif type(value) == str: return ast.Str(s=value), gen_sym, {} elif type(value) == bytes: return ast.Bytes(s=value), gen_sym, {} elif type(value) in (int, float, complex): return ast.Num(n=value), gen_sym, {} else: if kvalue.preferred_name is None: name, gen_sym = gen_sym('temp') else: name = kvalue.preferred_name return ast.Name(id=name, ctx=ast.Load()), gen_sym, {name: value}
def test_simple_reify(): check_reify(True, ast.NameConstant(value=True)) check_reify(False, ast.NameConstant(value=False)) check_reify(None, ast.NameConstant(value=None)) class Dummy(): pass x = Dummy() check_reify( x, ast.Name(id='__peval_temp_1', ctx=ast.Load()), expected_binding=dict(__peval_temp_1=x)) check_reify( x, ast.Name(id='y', ctx=ast.Load()), preferred_name='y', expected_binding=dict(y=x)) check_reify(1, ast.Num(n=1)) check_reify(2.3, ast.Num(n=2.3)) check_reify(3+4j, ast.Num(n=3+4j)) check_reify('abc', ast.Str(s='abc')) s = bytes('abc', encoding='ascii') check_reify(s, ast.Bytes(s=s))
def test_walker(): @ast_walker def process_numbers(state, node, **kwds): if isinstance(node, ast.Num): return state.update(numbers=state.numbers.add(node.n)), ast.Num(n=node.n + 1) else: return state, node node = get_ast(dummy) state, new_node = process_numbers(dict(numbers=immutableset()), node) assert state.numbers == set([1, 4]) assert_ast_equal(new_node, get_ast(""" def dummy(x, y): c = 5 a = 2 """)) # Transformations
def test_walk_field_transform(): @ast_transformer def increment(node, walk_field, **kwds): if isinstance(node, ast.Assign): return replace_fields(node, targets=node.targets, value=walk_field(node.value)) elif isinstance(node, ast.Num): return ast.Num(n=node.n + 1) else: return node node = get_ast(dummy) new_node = increment(node) assert_ast_equal(new_node, get_ast( """ def dummy(x, y): c = 5 a = 2 """))
def test_walk_field_transform_inspect(): @ast_walker def names_and_incremented_nums(state, node, walk_field, **kwds): if isinstance(node, ast.Assign): state, value_node = walk_field(state, node.value) new_node = replace_fields(node, targets=node.targets, value=value_node) new_state = state.update(objs=state.objs.add(node.targets[0].id)) return new_state, new_node elif isinstance(node, ast.Num): return state.update(objs=state.objs.add(node.n)), ast.Num(n=node.n + 1) else: return state, node node = get_ast(dummy) state, new_node = names_and_incremented_nums(dict(objs=immutableset()), node) assert state.objs == set(['a', 'c', 1, 4]) assert_ast_equal(new_node, get_ast( """ def dummy(x, y): c = 5 a = 2 """))
def test_skip_fields(): @ast_transformer def increment(node, skip_fields, **kwds): if isinstance(node, ast.Assign) and node.targets[0].id == 'c': skip_fields() if isinstance(node, ast.Num): return ast.Num(n=node.n + 1) else: return node node = get_ast(dummy) new_node = increment(node) assert_ast_equal(new_node, get_ast( """ def dummy(x, y): c = 4 a = 2 """))
def _read_label(key): if isinstance(key, ast.Name): return key.id elif isinstance(key, ast.Num): n = key.n if isinstance(n, (int, long)) and n >= 0: return n else: raise _errors.TyError("Invalid numeric label.", key) elif isinstance(key, ast.Str): s = key.s if s != "": return s else: raise _errors.TyError("Invalid string label.", key) else: raise _errors.TyError("Invalid label", key)
def translate_pat_Call_constructor(self, ctx, pat, scrutinee_trans): lbl = pat.func.id tag_loc = ast.Subscript( value=scrutinee_trans, slice=ast.Index(value=ast.Num(n=0))) lbl_condition = ast.Compare( left=tag_loc, ops=[ast.Eq()], comparators=[ast.Str(s=lbl)]) arg = pat.args[0] arg_scrutinee = ast.Subscript( value=scrutinee_trans, slice=ast.Index(value=ast.Num(n=1))) arg_condition, binding_translations = ctx.translate_pat(arg, arg_scrutinee) condition = ast.BoolOp( op=ast.And(), values=[lbl_condition, arg_condition]) return condition, binding_translations
def parse_unit(item): if isinstance(item, ast.Name): if item.id not in valid_units: raise InvalidTypeException("Invalid base unit", item) return {item.id: 1} elif isinstance(item, ast.Num) and item.n == 1: return {} elif not isinstance(item, ast.BinOp): raise InvalidTypeException("Invalid unit expression", item) elif isinstance(item.op, ast.Mult): left, right = parse_unit(item.left), parse_unit(item.right) return combine_units(left, right) elif isinstance(item.op, ast.Div): left, right = parse_unit(item.left), parse_unit(item.right) return combine_units(left, right, div=True) elif isinstance(item.op, ast.Pow): if not isinstance(item.left, ast.Name): raise InvalidTypeException("Can only raise a base type to an exponent", item) if not isinstance(item.right, ast.Num) or not isinstance(item.right.n, int) or item.right.n <= 0: raise InvalidTypeException("Exponent must be positive integer", item) return {item.left.id: item.right.n} else: raise InvalidTypeException("Invalid unit expression", item) # Parses an expression representing a type. Annotation refers to whether # the type is to be located in memory or storage
def resolve_negative_literals(_ast): class RewriteUnaryOp(ast.NodeTransformer): def visit_UnaryOp(self, node): if isinstance(node.op, ast.USub) and isinstance(node.operand, ast.Num): node.operand.n = 0 - node.operand.n return node.operand else: return node return RewriteUnaryOp().visit(_ast) # Make a getter for a variable. This function gives an output that # contains lists of 4-tuples: # (i) the tail of the function name for the getter # (ii) the code for the arguments that the function takes # (iii) the code for the return # (iv) the output type # # Here is an example: # # Input: my_variable: {foo: num, bar: decimal[5]} # # Output: # # [('__foo', '', '.foo', 'num'), # ('__bar', 'arg0: num, ', '.bar[arg0]', 'decimal')] # # The getters will have code: # def get_my_variable__foo() -> num: return self.foo # def get_my_variable__bar(arg0: nun) -> decimal: return self.bar[arg0]
def visit_BoolOp(self, boolop): res_var = self.variable() expl_list = self.assign(ast.List([], ast.Load())) app = ast.Attribute(expl_list, "append", ast.Load()) is_or = int(isinstance(boolop.op, ast.Or)) body = save = self.statements fail_save = self.on_failure levels = len(boolop.values) - 1 self.push_format_context() # Process each operand, short-circuting if needed. for i, v in enumerate(boolop.values): if i: fail_inner = [] # cond is set in a prior loop iteration below self.on_failure.append(ast.If(cond, fail_inner, [])) # noqa self.on_failure = fail_inner self.push_format_context() res, expl = self.visit(v) body.append(ast.Assign([ast.Name(res_var, ast.Store())], res)) expl_format = self.pop_format_context(ast.Str(expl)) call = ast_Call(app, [expl_format], []) self.on_failure.append(ast.Expr(call)) if i < levels: cond = res if is_or: cond = ast.UnaryOp(ast.Not(), cond) inner = [] self.statements.append(ast.If(cond, inner, [])) self.statements = body = inner self.statements = save self.on_failure = fail_save expl_template = self.helper("format_boolop", expl_list, ast.Num(is_or)) expl = self.pop_format_context(expl_template) return ast.Name(res_var, ast.Load()), self.explanation_param(expl)
def _add_value(self, module, name, alias): module = __import__(module) value = getattr(module, name) if isinstance(value, (int, float)): self._values[alias] = ast.Num(n = value)
def _is_numeric_mult(self, node: ast.BinOp) -> bool: if isinstance(node.op, ast.Mult): if isinstance(node.left , (ast.Name, ast.Num)) \ and isinstance(node.right, ast.Num): return True if isinstance(node.right, (ast.Name, ast.Num)) \ and isinstance(node.left , ast.Num): return True return False
def visit_BinOp(self, node: ast.BinOp): node = self.generic_visit(node) if self._is_numeric_pow(node): left, right = node.left, node.right degree = ( right.n if isinstance(right, ast.Num) else -right.operand.n if isinstance(right.op, ast.USub) else right.operand.n ) degree = int(degree) if abs(degree) == 0: node = ast.copy_location(ast.Num(n = 1), node) elif abs(degree) == 1: node = node.left elif 2 <= abs(degree) <= self.MAX_DEGREE: for _ in range(1, abs(degree)): new_node = ast.BinOp\ ( left = left , op = ast.Mult() , right = copy(node.left) ) left = new_node = ast.copy_location(new_node, node) node = new_node else: return node if degree < 0: new_node = ast.BinOp\ ( left = ast.Num(n = 1) , op = ast.Div() , right = node ) node = ast.copy_location(new_node, node) return node