|
20 | 20 | _NS_VAR_NAME = '*ns*'
|
21 | 21 | _NS_VAR_NS = _CORE_NS
|
22 | 22 | _PYTHON_PACKAGE_NAME = 'basilisp'
|
| 23 | +_GENERATED_PYTHON_VAR_NAME = '*generated-python*' |
23 | 24 | _PRINT_GENERATED_PY_VAR_NAME = '*print-generated-python*'
|
24 | 25 |
|
25 | 26 |
|
@@ -186,6 +187,9 @@ class Namespace:
|
186 | 187 | 'basilisp.lang.util'])
|
187 | 188 | .map(sym.symbol)))
|
188 | 189 | GATED_IMPORTS = pset(['basilisp.core'])
|
| 190 | + |
| 191 | + _IGNORED_CORE_MAPPINGS = pset([_GENERATED_PYTHON_VAR_NAME]) |
| 192 | + |
189 | 193 | _NAMESPACES = atom.Atom(pmap())
|
190 | 194 |
|
191 | 195 | __slots__ = ('_name', '_module', '_mappings', '_refers', '_aliases', '_imports')
|
@@ -293,7 +297,8 @@ def __import_core_mappings(ns_cache: PMap,
|
293 | 297 | if core_ns is None:
|
294 | 298 | raise KeyError(f"Namespace {core_ns_name} not found")
|
295 | 299 | for s, var in core_ns.mappings.items():
|
296 |
| - new_ns.intern(s, var) |
| 300 | + if s.name not in Namespace._IGNORED_CORE_MAPPINGS: |
| 301 | + new_ns.intern(s, var) |
297 | 302 |
|
298 | 303 | @staticmethod
|
299 | 304 | def __get_or_create(ns_cache: PMap,
|
@@ -577,6 +582,21 @@ def resolve_alias(s: sym.Symbol) -> sym.Symbol:
|
577 | 582 | return sym.symbol(s.name, ns=ns.name)
|
578 | 583 |
|
579 | 584 |
|
| 585 | +def add_generated_python(generated_python: str, |
| 586 | + var_name: str = _GENERATED_PYTHON_VAR_NAME, |
| 587 | + which_ns: Optional[str] = None) -> None: |
| 588 | + """Add generated Python code to a dynamic variable in which_ns.""" |
| 589 | + if which_ns is None: |
| 590 | + which_ns = get_current_ns().name |
| 591 | + ns_sym = sym.Symbol(var_name, ns=which_ns) |
| 592 | + v = Maybe(Var.find(ns_sym)) \ |
| 593 | + .or_else(lambda: Var.intern(sym.symbol(which_ns), # type: ignore |
| 594 | + sym.symbol(var_name), |
| 595 | + "", |
| 596 | + dynamic=True)) |
| 597 | + v.value = v.value + generated_python |
| 598 | + |
| 599 | + |
580 | 600 | def print_generated_python(var_name: str = _PRINT_GENERATED_PY_VAR_NAME,
|
581 | 601 | core_ns_name: str = _CORE_NS) -> bool:
|
582 | 602 | """Return the value of the `*print-generated-python*` dynamic variable."""
|
@@ -616,5 +636,10 @@ def in_ns(s: sym.Symbol):
|
616 | 636 | Var.intern(
|
617 | 637 | core_ns_sym,
|
618 | 638 | sym.symbol(_PRINT_GENERATED_PY_VAR_NAME),
|
619 |
| - True, |
| 639 | + False, |
| 640 | + dynamic=True) |
| 641 | + Var.intern( |
| 642 | + core_ns_sym, |
| 643 | + sym.symbol(_GENERATED_PYTHON_VAR_NAME), |
| 644 | + "", |
620 | 645 | dynamic=True)
|
0 commit comments