Commit Graph

1331 Commits

Author SHA1 Message Date
Kodi Arfer
762e5fad2d Remove Python 2 support in hy.compiler 2019-06-04 14:01:59 -04:00
Kodi Arfer
67def3359f Remove Python 2 support from hy.importer 2019-06-04 14:01:59 -04:00
Kodi Arfer
5dcb03b64d Move isidentifier to hy.lex 2019-06-04 14:01:59 -04:00
Kodi Arfer
d7da03be12 Simplify hy._compat.isidentifier 2019-06-04 14:01:59 -04:00
Kodi Arfer
e45cee575a Move rename_function to hy.macros 2019-06-04 14:01:59 -04:00
Kodi Arfer
7991c59480 Remove handling of UCS-2 2019-06-04 14:01:59 -04:00
Kodi Arfer
c255f0d03c Remove old hy._compat raising code 2019-06-04 14:01:59 -04:00
Kodi Arfer
ecf0352d37 Remove aliases: builtins, FileNotFoundError 2019-06-04 14:01:59 -04:00
Kodi Arfer
2685b01a4b Remove miscellaneous PY3 checks 2019-06-04 14:01:59 -04:00
Kodi Arfer
bba97ab2a6 Remove hy._compat's type aliases 2019-06-04 14:01:59 -04:00
Kodi Arfer
e77ce92635 Simplify gensym format 2019-04-29 08:10:51 -04:00
Kodi Arfer
6c74cf1f07 Add setx for assignment expressions 2019-04-23 15:35:12 -04:00
Kodi Arfer
8df0a41d7d Provide Module(..., type_ignores) for Python 3.8 2019-04-23 15:35:12 -04:00
Tristan Cacqueray
d793cee90a add tuple? function hy.core
`tuple?` will test if the argument is an instance of tuple.
2019-04-23 15:25:49 -04:00
Tristan Cacqueray
1c7ca7ac1f update contrib and macro to use the new list? function 2019-04-09 00:07:10 +00:00
Tristan Cacqueray
b0ed103931 add list? function to hy.core
`list?` will test if the argument is an instance of list.
2019-04-09 00:07:10 +00:00
Kodi Arfer
cbca711865
Merge pull request #1758 from brandonwillard/fix-decorator-error
Check arguments in with-decorator tag
2019-04-08 14:15:32 -04:00
Andrew R. M
da823d2cad Fix a temporary-file crash 2019-04-06 15:43:10 -04:00
Kodi Arfer
ad97042b6b Don't test Python 3.4 2019-03-17 18:36:53 -04:00
Kodi Arfer
0fe7f42efc Remove internal checks for Python 3.5 2019-03-17 18:28:39 -04:00
Brandon T. Willard
9203fcaeb5 Check arguments in with-decorator tag
Fixes #1757.
2019-03-16 23:19:49 -05:00
Kodi Arfer
5bfc140b4d Implement format strings 2019-02-26 14:04:24 -05:00
Kodi Arfer
56f51a9a20 Implement hy.lex.parse_one_thing 2019-02-26 13:57:50 -05:00
Kodi Arfer
997321d31c Fix .end_line and .end_column of single-token models 2019-02-24 10:13:17 -05:00
Brandon T. Willard
9e62903d8a Add special exception and handling for wrapper errors 2019-02-07 13:45:41 -05:00
Brandon T. Willard
4ae4baac2a Cache command line source for exceptions
Source entered interactively can now be displayed in traceback output.  Also,
the REPL object is now available in its namespace, so that, for instance,
display options--like `spy`--can be turned on and off interactively.

Closes hylang/hy#1397.
2019-02-07 13:45:41 -05:00
Brandon T. Willard
fb6feaf082 Improve correspondence with Python errors and console behavior
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang/hy#1486.
2019-02-07 13:45:41 -05:00
Brandon T. Willard
cadfa4152b Make colored output configurable
Colored exception output is now disabled by default and configurable through
`hy.errors._hy_colored_errors` and the environment variable
`HY_COLORED_ERRORS`.

Likewise, Hy model/AST color printing is now configurable and disabled by
default.  The corresponding variables are `hy.models._hy_colored_ast_objects`
and `HY_COLORED_AST_OBJECTS`.

Closes hylang/hy#1429, closes hylang/hy#1510.
2019-02-07 13:43:58 -05:00
Brandon T. Willard
e468d5f081 Refactor REPL error handling and filter Hy internal trace output
These changes make the Hy REPL more closely follow `code.InteractiveConsole`'s
class interface and provide minimally intrusive traceback print-out filtering
via a context manager that temporarily alters `sys.excepthook`.  In other words,
exception messages from the REPL will no longer show Hy internal
code (e.g. importer, compiler and parsing functions).

The boolean variable `hy.errors._hy_filter_internal_errors` dynamically
enables/disables trace filtering, and the env variable
`HY_FILTER_INTERNAL_ERRORS` can be used as the initial value.
2019-02-07 13:43:58 -05:00
Brandon T. Willard
51c7efe6e8 Retain compiled source and file information for exceptions
This commit refactors the exception/error classes and their handling.
It also retains Hy source strings and their originating file information, when
available, all throughout the core parser and compiler functions.

As well, with these changes, calling code is no longer responsible for providing
source and file details to exceptions,

Closes hylang/hy#657.
2019-02-07 13:43:58 -05:00
Kodi Arfer
902926c543 Use ._syntax_error in place of HyTypeError.
And standardize the indentation of these calls.
2019-02-07 13:43:57 -05:00
Kodi Arfer
62638b44a3 Update copyright years 2019-02-07 08:57:35 -05:00
Kodi Arfer
f1e693c96b Fix a Python 2 crash 2019-02-03 14:13:48 -05:00
Kodi Arfer
983ea2dda2 Make (require [foo [*]]) pull in macros required by foo 2019-01-31 12:42:40 -05:00
Kodi Arfer
b777972a0e Fix mangling of characters below 0xFF 2018-12-14 15:54:23 -05:00
Brandon T. Willard
b8b02c9df9 Handle empty defmain args
Closes hylang/hy#1707.
2018-11-29 14:37:52 -06:00
Brandon T. Willard
15c68455ec Use a fixed compiler in HyREPL
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`.

Closes hylang/hy#1698.
2018-11-28 16:35:42 -06:00
Brandon T. Willard
8b6646d5c9 Remove hy.core compilation requirement from hy package
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.

Closes hylang/hy#1697.
2018-11-28 14:12:33 -05:00
Brandon T. Willard
86fda31ab1 Move compilation and parsing functions out of importer.py
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`

Closes hylang/hy#1695.
2018-11-28 14:12:33 -05:00
Jakub Wilk
28504ba85d Catch IndentationError in isidentifier()
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
2018-11-27 17:15:21 -05:00
Brandon T. Willard
aa9182d76c Make the stdlib dictionary a class instance variable 2018-11-08 22:57:17 -06:00
Brandon T. Willard
010986e8ca Implement minimal macro namespacing and add tests
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.

Closes hylang/hy#1268, closes hylang/hy#1650, closes hylang/hy#1416.
2018-11-08 22:57:17 -06:00
Brandon T. Willard
144a7fa240 Produce Python AST for require statements and skip self requires
Closes hylang/hy#1211.
2018-11-08 22:57:17 -06:00
Kodi Arfer
58003389c5 Integrate hy.inspect into hy.macros
It's compatibility code, and there's not a lot of it, and having a module with the same name as a standard module can be a bit troublesome.
2018-11-08 22:57:17 -06:00
Kodi Arfer
701db83ba9 Remove get_arity
This function wasn't being used anywhere.
2018-11-08 22:56:42 -06:00
Brandon T. Willard
a9763b34cf Fix sys.modules for failed imports in Python 2.7
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`.
2018-09-29 20:57:28 -05:00
Brandon T. Willard
96f99c29d1 Fix missing import in doc macro expansion 2018-09-24 16:39:13 -04:00
Brandon T. Willard
c0c5c9c699 Make cmdline Hy process unknown filetypes as Hy source
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).

Closes hylang/hy#1677.
2018-09-24 16:27:50 -04:00
Brandon T. Willard
a9fca8001e Fix AST handling of docstrings and __future__ ordering
This closes hylang/hy#1367 and closes hylang/hy#1540
2018-09-11 16:26:21 -04:00
Brandon T. Willard
5d325a5156 Add a test for module docstrings 2018-08-27 01:02:29 -05:00
Brandon T. Willard
2ea1e8e017 Make Hy a Python-source module type 2018-08-26 13:20:49 -05:00
Brandon T. Willard
cbaba4a10a Use Python cmdline file-relative sys.path
Closes hylang/hy#1457.
2018-08-26 00:27:21 -05:00
Brandon T. Willard
c022abc831 Add Python cmdline bytecode option and set sys.executable
Closes hylang/hy#459.
2018-08-26 00:17:12 -05:00
Brandon T. Willard
87a5b117a1 Implement new importer using PEP-302 semantics
Python 3.x is patched in a way that integrates `.hy` source files into
Pythons default `importlib` machinery.  In Python 2.7, a PEP-302 "importer"
and "loader" is implemented according to the standard `import` logic (via
`pkgutil` and later pure-Python `imp` package code).

In both cases, the entry-point for the loaders is through `sys.path_hooks` only.
As well, the import semantics have been updated all throughout to utilize
`importlib` and follow aspects of PEP-420.  This, along with some light
patches, should allow for basic use of `runpy`, `py_compile` and `reload`.

In all cases, if a `.hy` file is shadowed by a `.py`, Hy will silently use
`.hy`.
2018-08-25 22:50:38 -05:00
Kodi Arfer
734cdcd2fd Delay importing the lexer and parser
This speeds up runs of Hy that never need to parse or compile Hy code (e.g., running a Hy program that's already byte-compiled).
2018-08-18 18:05:40 -04:00
Kodi Arfer
7ba2105a2b Fix date and time hy-reprs on Windows 2018-08-08 09:26:20 -04:00
gilch
109c0b0f5b
Merge pull request #1664 from brandonwillard/require-in-macroexpand-all
Expand `require`d macros in `macroexpand-all`
2018-08-02 23:57:32 -06:00
Brandon T. Willard
33f2b4a91a Compile requires in the body of a macro
This change enables further macro expansion for cases in which a macro
`require`s other macros within its body.
2018-07-25 17:20:19 -05:00
Kodi Arfer
081a710b0f Fix handling of unpacking in method calls and attribute lookups 2018-07-24 09:45:00 -07:00
Kodi Arfer
0f85331c81 Rename variables in @builds_model(HyExpression) 2018-07-24 09:41:57 -07:00
Kodi Arfer
1d2c73165d Make HyKeyword callable
Co-authored-by: Simon Gomizelj <simon@vodik.xyz>
2018-07-24 09:19:37 -07:00
Kodi Arfer
03aafad657 Make empty expressions illegal at the top level 2018-07-24 08:59:52 -07:00
gilch
0de8557fb5
Merge pull request #1647 from gilch/let-rebind
Fix let rebind bug.
2018-07-08 21:52:32 -06:00
Kodi Arfer
af8907b151 Minor cleanup in compile_unary_operator 2018-06-30 11:04:57 -07:00
Kodi Arfer
88f33453dc Minor cleanup for raise and try 2018-06-30 11:04:11 -07:00
Kodi Arfer
bd675a5db6 Unmangle in compile_expression before build_method
This ensures that e.g. the symbols "~" and "hyx_XtildeX" in the root position will both appear as "~" to the build method.
2018-06-30 11:04:11 -07:00
Kodi Arfer
fca2eb93b0 Remove dead code from HyASTCompiler.compile 2018-06-30 11:04:11 -07:00
Kodi Arfer
00150c088c Remove an unused helper method in the compiler 2018-06-30 11:04:11 -07:00
Kodi Arfer
e2b98effda Replace an unused variable with _ 2018-06-30 11:04:11 -07:00
Kodi Arfer
217fc2a487 Clean up _render_quoted_form 2018-06-30 11:04:11 -07:00
Kodi Arfer
8a70d5c90f Fold _branch into the compiler 2018-06-30 11:04:11 -07:00
Kodi Arfer
45ec57ab56 Simplify Result.force_expr 2018-06-30 11:04:11 -07:00
Kodi Arfer
21f7ef0713 Fold load_stdlib into the compiler 2018-06-30 11:04:11 -07:00
Kodi Arfer
d501b073d8 Fold compile_time_ns into the compiler 2018-06-30 11:04:11 -07:00
Kodi Arfer
3d3d1fe6ae Remove unused compiler subroutines 2018-06-30 11:04:11 -07:00
Kodi Arfer
9c6714c176 Remove unused imports 2018-06-30 11:04:11 -07:00
gilch
8c79015b40 Fix let rebind bug. 2018-06-27 23:41:49 -06:00
gilch
4b0e318997 Remove outdated comment in walk. 2018-06-27 23:39:44 -06:00
Kodi Arfer
7abd8ffc2a Make importing a dotted name a syntax error, per Python 2018-06-27 10:24:22 -07:00
Oskar Kvist
edbe8e3b7f Make defmacro! work with optional args 2018-06-25 11:45:23 -07:00
Kodi Arfer
76b80bad81 Remove support for the old comprehension forms 2018-06-13 17:31:08 -07:00
Kodi Arfer
cf0dafef9b Update uses of the old comprehension forms 2018-06-13 17:31:08 -07:00
Kodi Arfer
4754b152a9 Allow comprehensions with no looping parts 2018-06-13 17:31:08 -07:00
Kodi Arfer
e1972c535f Remove for/a, for*, and for/a* 2018-06-13 17:31:08 -07:00
Kodi Arfer
3256932b13 Add a version of for parallel to lfor etc. 2018-06-13 17:31:08 -07:00
Kodi Arfer
ba1dc55e96 Implement lfor, sfor, gfor, dfor 2018-06-13 17:31:08 -07:00
Kodi Arfer
7a40561db8 Add tagged model patterns 2018-06-13 17:31:08 -07:00
Kodi Arfer
5ffbb4b0eb Add Result.lineno and Result.col_offset 2018-06-13 17:31:08 -07:00
Kodi Arfer
844256b99b Make Asty use static rather than instance methods
This ensures `asty.Pass is asty.Pass`.
2018-06-13 17:31:08 -07:00
Kodi Arfer
c3d4c7aa82 Clean up else compilation in while 2018-06-11 21:05:29 -07:00
Kodi Arfer
ea899471af Remove an unused compiler function 2018-06-11 21:05:29 -07:00
Kodi Arfer
16ec46a473 Update docstring handling for Python 3.7
See https://github.com/python/cpython/pull/7121 .
2018-06-05 17:35:48 -07:00
Kodi Arfer
3204a9e8a3 Streamline auto-promotion and position spoofing
Auto-promotion now occurs in only two cases: when we start the compiler and when we expand a macro. It's fully recursive so even a non-model nested in a model will be promoted.

This change fixes some regressions induced by the stricter type checks of the pattern-matching compiler.
2018-06-05 09:23:21 -07:00
Kodi Arfer
a38c948ed2 Fix a regression with mangling module names 2018-06-03 15:38:49 -07:00
Ryan Gonzalez
e66743c976
Merge pull request #1615 from Kodiologist/no-parser-cache
Disable the parser cache
2018-05-27 16:28:00 -05:00
Kodi Arfer
e344ac1fd6 Fix the management of _stdlib
_stdlib is a global variable, so core modules could use it, contrary to our intention, so long as they happened to be compiled after a non-core module. I've added a compiler attribute to track whether we can use _stdlib.

This fix exposed some cases where hy.core.shadow tried to use a core function, so I fixed those.

I've also added an `if not _stdlib` to `load_stdlib` so that we only bother to actually load _stdlib once.
2018-05-24 17:51:21 -07:00
Kodi Arfer
1732ddec4d Disable the parser cache
I wasn't able to observe a meaningful performance improvement from it, and it requires some file I/O. Here are the times, in seconds, I observed on my ThinkPad T450s with Python 3.6 and Ubuntu 18.04:

                test 1  test 2
    master       8.96    2.59
    this change  8.88    2.62

Test 1 is the total time from:

$ time dash -c 'git clean -dfx && pip install -e . && pytest --ignore tests/test_bin.py'

Test 2 is pytest's reported total time from a subsequent:

$ pytest --ignore tests/test_bin.py
2018-05-21 09:22:49 -07:00
Kodi Arfer
0a5bc21fcf Simplify compile_numeric_literal for new astor 2018-05-20 14:11:35 -07:00
Kodi Arfer
38fdcc2114 Don't let HySymbol inherit from HyString 2018-05-20 14:11:35 -07:00
Kodi Arfer
210086c7ca Clean up the decorators used in the compiler 2018-05-20 14:11:35 -07:00