These changes make `HyREPL` use a single `HyASTCompiler` instance, instead of
creating one every time a valid source string is processed.
This change avoids the unnecessary re-initiation of the standard library
`import` and `require` steps that currently occur within the module tracked by a
`HyREPL` instance.
Also, one can now pass an existing compiler instance to `hy_repl` and
`hy_compiler`.
Closeshylang/hy#1698.
Previously, when importing `hy` (and any of its sub-packages/modules), Hy source
compilation for `hy.core.language` was necessarily triggered. This, in turn,
would trigger compilation of the other standard library source files.
This commit removes that chain of events and allows the `hy` package to be
imported without any Hy compilation.
Furthermore, `read` and `read_str` are now implemented in Python and the Hy
standard library files now handle their own dependencies explicitly (i.e. they
`import` and/or `require` the other standard library files upon which they
depend).
The latter changes were necessary, because the automatically triggered
compilation of `hy.core.language` (and associated standard library files) was
serving--implicitly--as a means of producing bytecode in an order that just
happened to work for any compilation occurring afterward. This chain of
events/dependencies was extremely cryptic, brittle, and difficult to debug, and
these changes should help to remedy that.
Closeshylang/hy#1697.
Functions and variables relating to compilation and parsing have been moved to
`compiler.py` and `lex/__init__.py`, respectively. Those functions are
- `hy_parse` from `hy.importer` to `hy.lex`
- `hy_eval`, `ast_compile`, and `calling_module` from `hy.importer` to
`hy.compiler`
Closeshylang/hy#1695.
Fixes:
>>> from hy._compat import isidentifier
>>> isidentifier(u" 0\n 0")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "hy/_compat.py", line 47, in isidentifier
tokens = list(T.generate_tokens(StringIO(x).readline))
File "/usr/lib/python2.7/tokenize.py", line 374, in generate_tokens
("<tokenize>", lnum, pos, line))
File "<tokenize>", line 2
0
^
IndentationError: unindent does not match any outer indentation level
This commit adds just enough namespacing to resolve a macro first in the macro's
defining module's namespace (i.e. the module assigned to the `HyASTCompiler`),
then in the namespace/module it's evaluated in. Namespacing is accomplished by
adding a `module` attribute to `HySymbol`, so that `HyExpression`s can be
checked for this definition namespace attribute and their car symbol resolved
per the above.
As well, a couple tests have been added that cover
- the loading of module-level macros
- e.g. that only macros defined in the `require`d module are added
- the AST generated for `require`
- using macros loaded from modules imported via bytecode
- the non-local macro namespace resolution described above
- a `require`d macro that uses a macro `require` exclusively in its
module-level namespace
- and that (second-degree `require`d) macros can reference variables within
their module-level namespaces.
Closeshylang/hy#1268, closeshylang/hy#1650, closeshylang/hy#1416.
Newly imported modules with compile and/or run-time errors were not being
removed from `sys.modules`. This commit modifies the Python 2.7 loader so that
it follows Python's failed-initial-import logic and removes the module from
`sys.modules`.
This change a Hy-preferring `runhy` that is used by cmdline Hy. Standard
`runpy` is still patched so that it can run `.hy` files, but the default
behaviour for unknown filetypes is preserved (i.e. assume they are Python
source).
Closeshylang/hy#1677.