Skip to content

Commit

Permalink
Harmonize readerless env resolution
Browse files Browse the repository at this point in the history
Now uses _resolve_env() to walk up the stack like macroexpand and friends, and like evaluate/execute.

It no longer makes an empty environment, you'd have to pass it explicitly.
  • Loading branch information
gilch committed Oct 22, 2024
1 parent 73b04bb commit 1ea7776
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/hissp/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,23 +653,24 @@ def _pairs(it: Iterable[T]) -> Iterable[tuple[T, T]]:
raise CompileError("incomplete pair") from None


def readerless(form: object, env: Env | None = None) -> str:
"""Compile a Hissp form to Python without evaluating it.
Uses the current `ENV` for context, unless an alternative is provided.
(Creates a temporary environment if neither is available.)
Returns the Python in a string.
"""
if env is None and (env := ENV.get()) is None:
env = {"__name__": "__main__"}
return Compiler(env=env, evaluate=False).compile([form])


def _resolve_env(env: Env | None = None) -> Env:
if env is not None or (env := ENV.get()) is not None:
return env
return inspect.currentframe().f_back.f_back.f_globals


def readerless(form: object, env: Env | None = None) -> str:
"""Compile a Hissp form to Python without evaluating it.
Returns the compiled Python in a string.
Unless an alternative ``env`` is specified, uses the current `ENV`
(available in a `macro_context`) when available, otherwise uses the
calling frame's globals.
"""
return Compiler(env=_resolve_env(env), evaluate=False).compile([form])


def evaluate(form: object, env: Env | None = None):
"""Convenience function to evaluate a Hissp form.
Expand Down

0 comments on commit 1ea7776

Please sign in to comment.