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 parse_line(code): parsed_ast = parse_to_ast(code)[0] return parsed_ast
def parse_to_lll(kode, runtime_only=False, interface_codes=None): code = parse_to_ast(kode) return parse_tree_to_lll(code, kode, runtime_only=runtime_only, interface_codes=interface_codes)
def process_arg(index, arg, expected_arg_typelist, function_name, context): if isinstance(expected_arg_typelist, Optional): expected_arg_typelist = expected_arg_typelist.typ if not isinstance(expected_arg_typelist, tuple): expected_arg_typelist = (expected_arg_typelist, ) vsub = None for expected_arg in expected_arg_typelist: if expected_arg == 'num_literal': if context.constants.is_constant_of_base_type( arg, ('uint256', 'int128')): return context.constants.get_constant(arg.id, None).value if isinstance(arg, ast.Num) and get_original_if_0_prefixed( arg, context) is None: return arg.n elif expected_arg == 'str_literal': if isinstance(arg, ast.Str) and get_original_if_0_prefixed( arg, context) is None: bytez = b'' for c in arg.s: if ord(c) >= 256: raise InvalidLiteralException( f"Cannot insert special character {c} into byte array", arg, ) bytez += bytes([ord(c)]) return bytez elif expected_arg == 'bytes_literal': if isinstance(arg, ast.Bytes): return arg.s elif expected_arg == 'name_literal': if isinstance(arg, ast.Name): return arg.id elif isinstance(arg, ast.Subscript) and arg.value.id == 'bytes': return f'bytes[{arg.slice.value.n}]' elif expected_arg == '*': return arg elif expected_arg == 'bytes': sub = Expr(arg, context).lll_node if isinstance(sub.typ, ByteArrayType): return sub elif expected_arg == 'string': sub = Expr(arg, context).lll_node if isinstance(sub.typ, StringType): return sub else: # Does not work for unit-endowed types inside compound types, e.g. timestamp[2] parsed_expected_type = context.parse_type( parse_to_ast(expected_arg)[0].value, 'memory', ) if isinstance(parsed_expected_type, BaseType): vsub = vsub or Expr.parse_value_expr(arg, context) is_valid_integer = ((expected_arg in ('int128', 'uint256') and isinstance(vsub.typ, BaseType)) and (vsub.typ.typ in ('int128', 'uint256') and vsub.typ.is_literal) and (SizeLimits.in_bounds( expected_arg, vsub.value))) if is_base_type(vsub.typ, expected_arg): return vsub elif is_valid_integer: return vsub else: vsub = vsub or Expr(arg, context).lll_node if vsub.typ == parsed_expected_type: return Expr(arg, context).lll_node if len(expected_arg_typelist) == 1: raise TypeMismatchException( f"Expecting {expected_arg} for argument {index} of {function_name}", arg) else: raise TypeMismatchException( f"Expecting one of {expected_arg_typelist} for argument {index} of {function_name}", arg)