|
1 | 1 | import ast
|
2 | 2 | import itertools
|
| 3 | +import os |
3 | 4 | import types
|
4 |
| -from typing import Optional, Callable, Any, Iterable, List, Dict |
| 5 | +from typing import Any, Callable, Dict, Iterable, List, Optional |
5 | 6 |
|
6 | 7 | from astor import code_gen as codegen
|
7 | 8 |
|
8 | 9 | import basilisp.lang.runtime as runtime
|
9 | 10 | from basilisp.lang.compiler.exception import CompilerException, CompilerPhase # noqa
|
10 | 11 | from basilisp.lang.compiler.generator import ( # noqa
|
11 |
| - GeneratorContext, |
12 | 12 | GeneratedPyAST,
|
| 13 | + GeneratorContext, |
| 14 | + USE_VAR_INDIRECTION, |
| 15 | + WARN_ON_VAR_INDIRECTION, |
13 | 16 | expressionize as _expressionize,
|
14 | 17 | gen_py_ast,
|
15 | 18 | py_module_preamble,
|
16 | 19 | statementize as _statementize,
|
17 |
| - USE_VAR_INDIRECTION, |
18 |
| - WARN_ON_VAR_INDIRECTION, |
19 | 20 | )
|
20 | 21 | from basilisp.lang.compiler.optimizer import PythonASTOptimizer
|
21 | 22 | from basilisp.lang.compiler.parser import ( # noqa
|
22 | 23 | ParserContext,
|
23 |
| - parse_ast, |
24 | 24 | WARN_ON_SHADOWED_NAME,
|
25 | 25 | WARN_ON_SHADOWED_VAR,
|
26 | 26 | WARN_ON_UNUSED_NAMES,
|
| 27 | + parse_ast, |
27 | 28 | )
|
28 | 29 | from basilisp.lang.typing import ReaderForm
|
29 | 30 | from basilisp.lang.util import genname
|
@@ -66,6 +67,23 @@ def py_ast_optimizer(self) -> PythonASTOptimizer:
|
66 | 67 | return self._optimizer
|
67 | 68 |
|
68 | 69 |
|
| 70 | +def _emit_ast_string(module: ast.AST) -> None: # pragma: no cover |
| 71 | + """Emit the generated Python AST string either to standard out or to the |
| 72 | + *generated-python* dynamic Var for the current namespace. If the |
| 73 | + BASILISP_EMIT_GENERATED_PYTHON env var is not set True, this method is a |
| 74 | + no-op.""" |
| 75 | + # TODO: eventually, this default should become "false" but during this |
| 76 | + # period of heavy development, having it set to "true" by default |
| 77 | + # is tremendously useful |
| 78 | + if os.getenv("BASILISP_EMIT_GENERATED_PYTHON", "true") != "true": |
| 79 | + return |
| 80 | + |
| 81 | + if runtime.print_generated_python(): |
| 82 | + print(to_py_str(module)) |
| 83 | + else: |
| 84 | + runtime.add_generated_python(to_py_str(module)) |
| 85 | + |
| 86 | + |
69 | 87 | def compile_and_exec_form( # pylint: disable= too-many-arguments
|
70 | 88 | form: ReaderForm,
|
71 | 89 | ctx: CompilerContext,
|
@@ -102,10 +120,7 @@ def compile_and_exec_form( # pylint: disable= too-many-arguments
|
102 | 120 | ast_module = ctx.py_ast_optimizer.visit(ast_module)
|
103 | 121 | ast.fix_missing_locations(ast_module)
|
104 | 122 |
|
105 |
| - if runtime.print_generated_python(): |
106 |
| - print(to_py_str(ast_module)) # pragma: no cover |
107 |
| - else: |
108 |
| - runtime.add_generated_python(to_py_str(ast_module)) |
| 123 | + _emit_ast_string(ast_module) |
109 | 124 |
|
110 | 125 | bytecode = compile(ast_module, ctx.filename, "exec")
|
111 | 126 | if collect_bytecode:
|
@@ -135,10 +150,7 @@ def _incremental_compile_module(
|
135 | 150 | module = optimizer.visit(module)
|
136 | 151 | ast.fix_missing_locations(module)
|
137 | 152 |
|
138 |
| - if runtime.print_generated_python(): |
139 |
| - print(to_py_str(module)) # pragma: no cover |
140 |
| - else: |
141 |
| - runtime.add_generated_python(to_py_str(module)) |
| 153 | + _emit_ast_string(module) |
142 | 154 |
|
143 | 155 | bytecode = compile(module, source_filename, "exec")
|
144 | 156 | if collect_bytecode:
|
|
0 commit comments