import ast

from . import _compile, _Expr


class _AutoExpr(_Expr):
    def __init__(self, value):
        super().__init__(value)
        self._compiled = None

    def __call__(self, *args, **kwargs):
        compiled = object.__getattribute__(self, "_compiled")
        if compiled is None:
            compiled = _compile(self)
            self._compiled = compiled
        return compiled(*args, **kwargs)


_ = _AutoExpr(ast.Name(id="$", ctx=ast.Load()))
