Skip to content

Commit 7713c0c

Browse files
authored
Use Python module aliases for Basilisp lang references (#366)
* Use Python module aliases for Basilisp lang references * Comment * Changelog
1 parent fb6f16e commit 7713c0c

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Changed
9+
* Compile qualified `basilisp.lang.*` module references down to aliased references (#366)
810

911
## [v0.1.dev9] - 2019-03-29
1012
### Added

src/basilisp/lang/compiler/generator.py

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,44 @@ def _is_redefable(v: Var) -> bool:
419419
#######################
420420

421421

422+
_ATOM_ALIAS = genname("atom")
423+
_ASSOC_ALIAS = genname("assoc")
424+
_COMPILER_ALIAS = genname("compiler")
425+
_DELAY_ALIAS = genname("delay")
426+
_EXC_ALIAS = genname("exc")
422427
_KW_ALIAS = genname("kw")
423428
_LIST_ALIAS = genname("llist")
424429
_MAP_ALIAS = genname("lmap")
430+
_MULTIFN_ALIAS = genname("multifn")
431+
_READER_ALIAS = genname("reader")
425432
_RUNTIME_ALIAS = genname("runtime")
433+
_SEQ_ALIAS = genname("seq")
426434
_SET_ALIAS = genname("lset")
427435
_SYM_ALIAS = genname("sym")
428436
_VEC_ALIAS = genname("vec")
429437
_VAR_ALIAS = genname("Var")
430438
_UTIL_ALIAS = genname("langutil")
439+
440+
_MODULE_ALIASES = {
441+
"builtins": None,
442+
"basilisp.lang.atom": _ATOM_ALIAS,
443+
"basilisp.lang.associative": _ASSOC_ALIAS,
444+
"basilisp.lang.compiler": _COMPILER_ALIAS,
445+
"basilisp.lang.delay": _DELAY_ALIAS,
446+
"basilisp.lang.exception": _EXC_ALIAS,
447+
"basilisp.lang.keyword": _KW_ALIAS,
448+
"basilisp.lang.list": _LIST_ALIAS,
449+
"basilisp.lang.map": _MAP_ALIAS,
450+
"basilisp.lang.multifn": _MULTIFN_ALIAS,
451+
"basilisp.lang.reader": _READER_ALIAS,
452+
"basilisp.lang.runtime": _RUNTIME_ALIAS,
453+
"basilisp.lang.seq": _SEQ_ALIAS,
454+
"basilisp.lang.set": _SET_ALIAS,
455+
"basilisp.lang.symbol": _SYM_ALIAS,
456+
"basilisp.lang.vector": _VEC_ALIAS,
457+
"basilisp.lang.util": _UTIL_ALIAS,
458+
}
459+
431460
_NS_VAR_VALUE = f"{_NS_VAR}.value"
432461

433462
_NS_VAR_NAME = _load_attr(f"{_NS_VAR_VALUE}.name")
@@ -1902,7 +1931,12 @@ def _maybe_class_to_py_ast(_: GeneratorContext, node: MaybeClass) -> GeneratedPy
19021931
"""Generate a Python AST node for accessing a potential Python module
19031932
variable name."""
19041933
assert node.op == NodeOp.MAYBE_CLASS
1905-
return GeneratedPyAST(node=ast.Name(id=node.class_, ctx=ast.Load()))
1934+
return GeneratedPyAST(
1935+
node=ast.Name(
1936+
id=Maybe(_MODULE_ALIASES.get(node.class_)).or_else_get(node.class_),
1937+
ctx=ast.Load(),
1938+
)
1939+
)
19061940

19071941

19081942
@_with_ast_loc
@@ -1912,7 +1946,11 @@ def _maybe_host_form_to_py_ast(
19121946
"""Generate a Python AST node for accessing a potential Python module
19131947
variable name with a namespace."""
19141948
assert node.op == NodeOp.MAYBE_HOST_FORM
1915-
return GeneratedPyAST(node=_load_attr(f"{node.class_}.{node.field}"))
1949+
return GeneratedPyAST(
1950+
node=_load_attr(
1951+
f"{Maybe(_MODULE_ALIASES.get(node.class_)).or_else_get(node.class_)}.{node.field}"
1952+
)
1953+
)
19161954

19171955

19181956
#########################
@@ -2426,22 +2464,12 @@ def gen_py_ast(ctx: GeneratorContext, lisp_ast: Node) -> GeneratedPyAST:
24262464
#############################
24272465

24282466

2429-
_MODULE_ALIASES = {
2430-
"builtins": None,
2431-
"basilisp.lang.keyword": _KW_ALIAS,
2432-
"basilisp.lang.list": _LIST_ALIAS,
2433-
"basilisp.lang.map": _MAP_ALIAS,
2434-
"basilisp.lang.runtime": _RUNTIME_ALIAS,
2435-
"basilisp.lang.set": _SET_ALIAS,
2436-
"basilisp.lang.symbol": _SYM_ALIAS,
2437-
"basilisp.lang.vector": _VEC_ALIAS,
2438-
"basilisp.lang.util": _UTIL_ALIAS,
2439-
}
2440-
2441-
24422467
def _module_imports(ctx: GeneratorContext) -> Iterable[ast.Import]:
24432468
"""Generate the Python Import AST node for importing all required
24442469
language support modules."""
2470+
# Yield `import basilisp` so code attempting to call fully qualified
2471+
# `basilisp.lang...` modules don't result in compiler errors
2472+
yield ast.Import(names=[ast.alias(name="basilisp", asname=None)])
24452473
for imp in ctx.imports:
24462474
name = imp.key.name
24472475
alias = _MODULE_ALIASES.get(name, None)

src/basilisp/lang/runtime.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ class Namespace:
301301
"operator",
302302
"sys",
303303
"basilisp.lang.atom",
304+
"basilisp.lang.associative",
304305
"basilisp.lang.compiler",
305306
"basilisp.lang.delay",
306307
"basilisp.lang.exception",

0 commit comments

Comments
 (0)