def test_dict_to_ast(): code = """ @public def test() -> int128: a: uint256 = 100 return 123 """ original_ast = parse_to_ast(code) out_dict = ast_to_dict(original_ast) new_ast = dict_to_ast(out_dict) assert new_ast == original_ast
def test_dict_to_ast(): code = """ @public def test() -> int128: a: uint256 = 100 b: int128 = -22 c: decimal = 3.31337 d: bytes[11] = b"oh hai mark" e: address = 0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef f: bool = False return 123 """ original_ast = parse_to_ast(code) out_dict = ast_to_dict(original_ast) new_ast = dict_to_ast(out_dict) assert new_ast == original_ast
def _mk_ast_dict(code, contract_name, interface_codes): o = { 'contract_name': contract_name, 'ast': ast_to_dict(parser.parse_to_ast(code)) } return o
def parse_for(self): # from .parser import ( # parse_body, # ) # Type 0 for, e.g. for i in list(): ... if self._is_list_iter(): return self.parse_for_list() is_invalid_for_statement = any(( not isinstance(self.stmt.iter, ast.Call), not isinstance(self.stmt.iter.func, ast.Name), not isinstance(self.stmt.target, ast.Name), self.stmt.iter.func.id != "range", len(self.stmt.iter.args) not in {1, 2}, )) if is_invalid_for_statement: raise StructureException( ("For statements must be of the form `for i in range(rounds): " "..` or `for i in range(start, start + rounds): ..`"), self.stmt.iter) block_scope_id = id(self.stmt.orelse) with self.context.make_blockscope(block_scope_id): # Get arg0 arg0 = self.stmt.iter.args[0] num_of_args = len(self.stmt.iter.args) # Type 1 for, e.g. for i in range(10): ... if num_of_args == 1: arg0_val = self._get_range_const_value(arg0) start = LLLnode.from_list(0, typ='int128', pos=getpos(self.stmt)) rounds = arg0_val # Type 2 for, e.g. for i in range(100, 110): ... elif self._check_valid_range_constant(self.stmt.iter.args[1], raise_exception=False)[0]: arg0_val = self._get_range_const_value(arg0) arg1_val = self._get_range_const_value(self.stmt.iter.args[1]) start = LLLnode.from_list(arg0_val, typ='int128', pos=getpos(self.stmt)) rounds = LLLnode.from_list(arg1_val - arg0_val, typ='int128', pos=getpos(self.stmt)) # Type 3 for, e.g. for i in range(x, x + 10): ... else: arg1 = self.stmt.iter.args[1] if not isinstance(arg1, ast.BinOp) or not isinstance( arg1.op, ast.Add): raise StructureException( ("Two-arg for statements must be of the form `for i " "in range(start, start + rounds): ...`"), arg1, ) if arg0 != arg1.left: raise StructureException( ("Two-arg for statements of the form `for i in " "range(x, x + y): ...` must have x identical in both " "places: %r %r") % (ast_to_dict(arg0), ast_to_dict(arg1.left)), self.stmt.iter, ) rounds = self._get_range_const_value(arg1.right) start = Expr.parse_value_expr(arg0, self.context) varname = self.stmt.target.id pos = self.context.new_variable(varname, BaseType('int128'), pos=getpos(self.stmt)) self.context.forvars[varname] = True o = LLLnode.from_list( [ 'repeat', pos, start, rounds, parse_body(self.stmt.body, self.context) ], typ=None, pos=getpos(self.stmt), ) del self.context.vars[varname] del self.context.forvars[varname] return o