Commit Graph

72 Commits

Author SHA1 Message Date
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
Foxboron c9fdd40c9f Hy reader macros #377
Added first iteration of reader macros
Refactored defmacro and defreader
Added test inn hy/tests/lex/test_lex.py
Added new test in hy/tests/native/tests
Added new test in hy/tests/macros.

changed the error given in the dispatch macro and added some handling for missing symbol and invalid characters
2013-12-23 14:33:51 +01:00
Abhishek L 948e6d34c7 Merge branch master onto pr/284
Conflicts:
	hy/core/language.hy
2013-12-06 20:07:39 +05:30
Abhishek L f1c68bd51a Allow macros to return None, fixes #357
Allows Hy macros to return None, to test this

  (defmacro foo [])
  (foo)

Should work now
2013-12-06 00:27:45 +05:30
Guillermo Vaya f61d702788 Int conversion to long in py2.x
Updated to current master
    Droped HyInt/HyLong commit
2013-10-11 01:49:42 +02:00
Nicolas Dandrimont d5bf328aa7 Cleanup the hy.macros module
Add comments to the functions, reorder, make the file clearer
2013-09-29 18:13:28 +02:00
Berker Peksag f21ddeeded Add hy._compat module.
There was a couple of duplicate imports and type checkings in the
codebase. So I added a new module to unify all Python 2 and 3
compatibility codes.

Also, this is a somewhat common pattern in Python. See Jinja2 for
example:

https://github.com/mitsuhiko/jinja2/blob/master/jinja2/_compat.py
2013-09-29 12:10:08 +03:00
Paul Tagliamonte 1c12b2870e Add yield from via macro magic.
This will let us use (basic) yield from behavior from Python 2. This
 isn't complete, and is low-hanging fruit for others willing to hack
 on hy.

 I've also changed the macrosystem to allow for proper bootstrapping.
 This is similar to how it's done elsewhere in the codebase (stdlib
 stuff).
2013-08-17 11:37:48 -04:00
Paul Tagliamonte b78be9a594 Jank @olasd's hack, clean up core.
The core shall from now on be only for the core language bits. Macro
 bits shall live in hy.macros and in hy.compiler. This cleans up
 garbage.
2013-07-06 14:00:11 -04:00
Nicolas Dandrimont b65c2a4596 whitespace fix 2013-06-26 08:50:37 +02:00
Guillermo Vaya 482282178c changed macro to ignore empty trees and made compiler do the substitution work 2013-06-26 08:50:37 +02:00
Guillermo Vaya 4c7b4f70e3 changed try to testing for an empty list for legibility (as suggested by paultag), also added to authors 2013-06-26 08:50:37 +02:00