>>> from Redy.Opt.ConstExpr import constexpr, const, optimize, macro
    >>> import dis
    >>> a = 1; b = ""; c = object()
    >>> x = 1
    >>> @optimize
    >>> def f(y):
    >>>     val1: const[int] = a
    >>>     val2: const = b
    >>>     if constexpr[x is c]:
    >>>         return val1, y
    >>>     elif constexpr[x is 1]:
    >>>         return None, y
    >>>     else:
    >>>         return val2, y
    >>> assert f(1) == (None, 1)
    >>> dis.dis(f)
    >>> @optimize
    >>> def f(x):
    >>>     d: const = 1
    >>>     return x + d + constexpr[2]
    #
    #
    >>> dis.dis(f)
    >>> print('result:', f(1))
    #
    #
    >>> @optimize
    >>> def f(z):
    >>>     @macro
    >>>     def g(a):
    >>>         x = a + 1
    #
    >>>     g(z)
    >>>     return x
    #
    #
    >>> dis.dis(f)
    >>> print('result:', f(1))
    #
    >>> c = 10
    #
    #
    >>> @optimize
    >>> def f(x):
    >>>     if constexpr[1 + c < 10]:
    >>>         return x + 1
    >>>     else:
    >>>         return x - 1
    #
    #
    >>> print(dis.dis(f))
    >>> print(f(5))
    #
    >>> @optimize
    >>> def f(x):
    >>>     return (x + constexpr[c * 20]) if constexpr[c > 10] else  constexpr[c - 2]
    #
    >>> dis.dis(f)
    >>> print(f(20))

    >>> def g(lst: list):
    >>>    k = 1

    >>>    @optimize
    >>>    def _():
    >>>        nonlocal k
    >>>        f: const = lst.append
    >>>        for i in range(1000):
    >>>            f(i)
    >>>        k += 1
    >>>        f(k)

    >>>    _()

    >>>    return lst


    >>> # dis.dis(g)
    >>> print(g([]))

def _constexpr_transform(fn):
    """
    >>> from Redy.Opt.ConstExpr import constexpr, const, optimize, macro
    >>> import dis
    >>> a = 1; b = ""; c = object()
    >>> x = 1
    >>> @optimize
    >>> def f(y):
    >>>     val1: const[int] = a
    >>>     val2: const = b
    >>>     if constexpr[x is c]:
    >>>         return val1, y
    >>>     elif constexpr[x is 1]:
    >>>         return None, y
    >>>     else:
    >>>         return val2, y
    >>> assert f(1) == (None, 1)
    >>> dis.dis(f)
    >>> @optimize
    >>> def f(x):
    >>>     d: const = 1
    >>>     return x + d + constexpr[2]
    #
    #
    >>> dis.dis(f)
    >>> print('result:', f(1))
    #
    #
    >>> @optimize
    >>> def f(z):
    >>>     @macro
    >>>     def g(a):
    >>>         x = a + 1
    #
    >>>     g(z)
    >>>     return x
    #
    #
    >>> dis.dis(f)
    >>> print('result:', f(1))
    #
    >>> c = 10
    #
    #
    >>> @optimize
    >>> def f(x):
    >>>     if constexpr[1 + c < 10]:
    >>>         return x + 1
    >>>     else:
    >>>         return x - 1
    #
    #
    >>> print(dis.dis(f))
    >>> print(f(5))
    #
    >>> @optimize
    >>> def f(x):
    >>>     return (x + constexpr[c * 20]) if constexpr[c > 10] else  constexpr[c - 2]
    #
    >>> dis.dis(f)
    >>> print(f(20))

    >>> def g(lst: list):
    >>>    k = 1

    >>>    @optimize
    >>>    def _():
    >>>        nonlocal k
    >>>        f: const = lst.append
    >>>        for i in range(1000):
    >>>            f(i)
    >>>        k += 1
    >>>        f(k)

    >>>    _()

    >>>    return lst


    >>> # dis.dis(g)
    >>> print(g([]))

    """
    code_string = inspect.getsource(fn)
    while _s.match(code_string):
        code_string = textwrap.dedent(code_string)

    module = ast.parse(code_string)
    fn_ast = module.body[0]
    fn: types.FunctionType
    fn_name = fn.__name__

    closure = fn.__closure__
    closure_dict = {v: c.cell_contents for v, c in zip(fn.__code__.co_freevars, closure if closure else ())}
    ctx = CompilingTimeMapping(fn.__globals__, closure_dict)
    ce = ConstExpr(ctx, [], OrderedDict(), {}, [], fn.__code__.co_filename)
    body = fn_ast.body

    macro_def = new_transformer(ce, MazcroDef)
    macro_invoke = new_transformer(ce, MacroInvoke)
    const_def = new_transformer(ce, ConstExprConstDef)
    const_if = new_transformer(ce, ConstExprIf)
    name_fold = new_transformer(ce, ConstExprNameFold)

    body = _visit_suite(macro_def.visit, body)
    body = _visit_suite(macro_invoke.visit, body)
    body = _visit_suite(const_def.visit, body)
    body = _visit_suite(const_if.visit, body)
    body = _visit_suite(name_fold.visit, body)
    fn_ast.body = body
    module.body = [fn_ast]
    code = compile(module, "<const-optimize>", "exec")
    fn_code: types.CodeType = next(
            each for each in code.co_consts if isinstance(each, types.CodeType) and each.co_name == fn_name)

    fn_code = const_link(fn_code, ce.constant_symbols, ce.additional_consts, fn.__code__, ce.nonlocal_names)

    new_fn = types.FunctionType(fn_code, fn.__globals__, fn.__name__, fn.__defaults__, fn.__closure__)

    new_fn.__annotations__ = fn.__annotations__
    new_fn.__doc__ = fn.__doc__
    new_fn.__kwdefaults__ = fn.__kwdefaults__
    new_fn.__module__ = fn.__module__
    return new_fn