Commit Graph

83 Commits

Author SHA1 Message Date
Kodi Arfer 48d481b5d3 Fix a check for empty input in `macroexpand`
The equality check hasn't done the right thing since HyExpression became tuple-backed (#1804).
2020-04-03 14:24:48 -04:00
Kodi Arfer 80eb27906a Update copyright years 2020-01-09 14:05:12 -05:00
Kodi Arfer ba9b0239c7 Fix crashes on the new Python 3.8 alpha 2019-06-04 16:03:52 -04:00
Kodi Arfer e45cee575a Move `rename_function` to hy.macros 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
Brandon T. Willard 9e62903d8a Add special exception and handling for wrapper errors 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 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 62638b44a3 Update copyright years 2019-02-07 08:57:35 -05:00
Kodi Arfer 983ea2dda2 Make (require [foo [*]]) pull in macros required by `foo` 2019-01-31 12:42:40 -05: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 `require`s
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
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 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 9368e4bc4e Use model patterns for `import` and `require`
In the process, I've banned the syntax `(import [foo])` in favor of `(import foo)`.
2018-05-20 14:07:44 -07:00
Kodi Arfer c1a487cdf7 Move logic from macroexpand_1 to macroexpand
By ending macro-expansion immediately when appropriate, this change fixes a bug arising from the fact that NaN != NaN.
2018-04-21 12:25:29 -07:00
Kodi Arfer 026316ebef Refactor macroexpand_1 2018-04-21 12:25:29 -07:00
Kodi Arfer 116d7fa6ec Copy in compile_atom instead of macroexpand_1
This copying is what keeps all the mutating code in the compiler methods (e.g., `expr.pop(0)`) from breaking cases in which Hy model objects are compiled more than once or inspected after compilation.
2018-04-21 12:25:29 -07:00
Kodi Arfer 85968e70dd Add `mangle` and `unmangle` as core functions 2018-03-13 14:55:55 -04:00
Kodi Arfer ccb3ba6092 Mangle macro names 2018-03-13 14:55:55 -04:00
Kodi Arfer 52edad28e2 Overhaul mangling rules 2018-03-13 14:55:53 -04:00
Simon Gomizelj ef2f435647 Add a hy.introspect module to deal with getargspec deprecation 2018-02-14 16:05:31 -05:00
Kodi Arfer 6de7ddfee5 Update copyright years 2018-01-01 10:38:33 -05:00
gilch 3707681056 make deftag/defmacro macros, not special forms 2017-10-29 17:52:40 -06:00
gilch a54f6aa38b add hidden &name parameter to macros
The module name allows macros to preexpand their body in the proper
context.
2017-09-23 14:59:43 -06:00
Kodi Arfer b7657e8fa3 Require HySymbols instead of HyStrings more often 2017-07-12 10:57:35 -07:00
Kodi Arfer 6db3da779e Don't wrap values while expanding macros
This is the compiler's job now.
2017-07-03 17:01:12 -07:00
gilch 6cd3201421 rename sharp macros to tag macros 2017-06-22 22:48:46 -06:00
gilch 20c26a52e4 make sharp macros take arbitrary identifiers
Previously, only a single character was allowed.
2017-06-22 22:46:24 -06:00
Kodi Arfer 2eb81864df Make all files comply with license-header policy 2017-04-27 14:16:57 -07:00
Kodi Arfer 4c38e2c9dd Rename reader macros to "sharp macros" (#1282)
They're not actually reader macros, since their arguments are parsed s-expressions, like a regular macro, not pre-parsed source text.
2017-04-21 10:07:48 -05:00
Kodi Arfer ace125ee9b Consolidate hy.models.* and tests/models/* into one file apiece
They were a lot of similar, tiny files.
2017-02-16 19:43:00 -08:00
Tuukka Turto e588b4928d add defmacro! and fix macro expansion error message (#1172)
* added defmacro!

* revert #924

#924 had an error and should never have been merged in the first place. (see #903)

* put back import getargspec

Without the `formatargspec` this time.

* Give better error message on failed macro expansion

Better error messages work most of the time. In cases where there are
parameters that aren't valid in Python, error message shown is rather
ugly. But this is better than no error messages at all and such
macros with strange parameter names are rather rare.

* fix flake8 errors

* Minor English improvements
2016-12-14 18:10:46 -06:00
Kodi Arfer 14fddbe6c3 Give `require` the same features as `import` (#1142)
Give `require` the same features as `import`

You can now do (require foo), (require [foo [a b c]]), (require [foo [*]]), and (require [foo :as bar]). The first and last forms get you macros named foo.a, foo.b, etc. or bar.a, bar.b, etc., respectively. The second form only gets the macros in the list.

Implements #1118 and perhaps partly addresses #277.

N.B. The new meaning of (require foo) will cause all existing code that uses macros to break. Simply replace these forms with (require [foo [*]]) to get your code working again.

There's a bit of a hack involved in the forms (require foo) or (require [foo :as bar]). When you call (foo.a ...) or (bar.a ...), Hy doesn't actually look inside modules. Instead, these (require ...) forms give the macros names that have periods in them, which happens to work fine with the way Hy finds and interprets macro calls.

* Make `require` syntax stricter and add tests

* Update documentation for `require`

* Documentation wording improvements

* Allow :as in `require` name lists
2016-11-03 09:35:58 +02:00
Paul Tagliamonte 8d2143177e Overhaul macros to allow macros to ref the Compiler
This allows macros to take a keyword dict containing useful things by
defining a keyword argument. This allows us to pass in new objects
which might be handy to have in macros.

This changeset refactors module_name to become `compiler`, so that we
can pass the compiler itself into the macros as `opts['compiler']`.

This allows the macro to both get the macro name
(`compiler.module_name`), as well as use the compiler to build AST.

In the future, this will enable us to create "super-macros" which return
AST, not HyAST, in order to manually create insane things from userland.

For userland macros (not `defmacro`) the core.language `macroexpand`
will go ahead and make a new compiler for you.
2015-12-23 15:52:47 -05:00
Johnathon Mlady c5600d7b38 non-critical macro patch: fixed what seemed to be a typo...'emtpy' -?> 'empty' (unless emtpy has an undocumented meaning) 2015-11-20 13:29:34 -05:00
Adam Schwalm f18007955d Better error messages on invalid macro arguments 2015-08-30 17:14:22 -05:00
Zack M. Davis 016557deab reader macro #@ for with-decorator
The `with-decorator` special form is not the most ergonomic—this commit
introduces a new builtin `#@` reader macro that expands to an invocation
of `with-decorator`. To support this, `reader_macroexpand` is made to
also look in the default `None` namespace, in imitation of how
regular (non-reader) macros defined in hy.core are looked up. The
docstring of `hy.macros.reader` is also edited slightly for accuracy.

This in the matter of issue #856.
2015-07-26 15:05:38 -07:00
Zhao Shenyang dc607763e2 Fix some coding style problems 2015-01-24 09:25:04 +08:00
Zhao Shenyang 2375392962 Add 'replace_hy_obj' to safely replace Hy objects
Currently '.replace' method is used to replace hy objects. This is not
safe when we are not sure if the 'obj' in 'obj.replace(other)' is an
instance of HyObject.

In these cases, we can use function 'replace_hy_obj(obj, other)'
instead. This function will try to wrap 'obj' if it's not an instance of
HyObject.

This also means that we need a wrapping function in hy.models'. Hence I
moved the '_wrap_value' function from hy.macros into hy.models. To avoid
circular importing, the wrapper functions are provided individually by
each model type's own file.
2015-01-24 08:16:38 +08:00
Michael Maltese 6e61f04e46 HyMacroExpansionError shouldn't truncate message
For example:

```
$ hy
hy 0.10.1 using CPython(default) 2.7.8 on Darwin
=> (defmacro hi [] (raise (TypeError "This message will be truncated")))
=> (hi)
  File "<input>", line 1, column 1

  (hi)
  ^--^
HyMacroExpansionError: `hi' message will be truncated
````
2014-11-27 15:29:34 -08:00
Ian Denhardt 4d2aad1b0f Call _wrap_value recursively on HyExpressions.
Without this, cases like:

    (defmacro foo [] `(~0))
    (foo)

throw exceptions, because the embedded zero is not a HyObject.
2014-09-21 11:36:40 -04:00
Paul Tagliamonte a5ad0203d1 Flake8 cleanup 2014-03-29 14:48:30 -04:00
James King b97e0258e1 Fix for #497
One would expect the form:

    > (defmacro a (&rest b) b)
    > (a 1 2)

To return a tuple object but we have no Hy model so it returns a HyList.
Not sure if this is the right thing to do.
2014-02-19 00:09:37 -05:00
Foxboron a35ecc41bd Fix reader macros to actually be macros 2014-01-17 01:07:47 +01:00
Bob Tolbert 765dba3e56 More updates, including from Foxboron, for errors like (for) 2013-12-31 09:56:03 -07:00
Foxboron f064d3f121 Errors into errors.py, added HyMacroExpansionError, fixed macro arg fail 2013-12-31 09:56:03 -07:00