Commit Graph

52 Commits

Author SHA1 Message Date
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
Guillermo Vaya 7b7b953410 set the value of empty hy expression to [] 2013-06-26 08:50:37 +02:00
Paul Tagliamonte d15aa31a32 style fixes 2013-06-08 20:10:27 -04:00
Konrad Hinsen 12abef5ed5 Forgot boolean constants.. 2013-06-07 16:35:28 +02:00
Konrad Hinsen e47bac1f96 Permit macros to return constants 2013-06-05 12:19:06 +02:00
Paul Tagliamonte 9c28e0292d Merge branch 'master' into pr/185
Conflicts:
	hy/macros.py
	tests/compilers/test_compiler.py

Hoodoggy!
2013-05-17 11:17:51 -04:00
Konrad Hinsen 269da19d76 Make macros module-specific.
A macro is available in the module where it was defined and
in any module that does a require of the defining module.
Only macros defined in hy.core are globally available.
Fixes #181
2013-05-16 15:34:14 +02:00
Nicolas Dandrimont 9e03e0e6ec Modify HyDict semantics to allow nesting expressions
HyDicts are now HyLists, that get compiled down to dicts
only by the compiler.
2013-05-14 11:43:17 +02:00
Nicolas Dandrimont 0986f9001e Change macros to be called with separate arguments
This provides free argument-length checking for macros.
2013-05-11 09:09:34 +02:00
Nicolas Dandrimont 2860ef6ff7 Don't macro-expand inside quotes 2013-05-11 00:29:42 +02:00
Paul R. Tagliamonte e0ed7cac40 Revert "Make HySymbol bytes free!"
This reverts commit 8b144a4f3d.
2013-04-06 20:02:08 -04:00
Julien Danjou 8b144a4f3d Make HySymbol bytes free!
We can know use any amount and type of bytes to build a HyString, meaning we
can use Unicode and UTF-8 for our function and variables.

Eat that, snake!

Signed-off-by: Julien Danjou <julien@danjou.info>
2013-04-06 23:37:34 +02:00
Paul R. Tagliamonte 4cb4e7384e holy shit it's 2013 2013-03-18 10:27:14 -04:00
Paul R. Tagliamonte 0bc2dd8d00 Moving `for' to a "macro" 2013-03-13 20:41:53 -04:00
Thomas Mashek 8a88b2a0f0 Making dict comprehension work with py2.6 2013-03-12 12:39:23 -07:00
Paul R. Tagliamonte 57bf7b5cba syntax fixes 2013-03-10 10:35:08 -04:00
Paul R. Tagliamonte 0a86226da0 fixing up dicts + macros 2013-03-10 10:30:03 -04:00
Paul R. Tagliamonte ccfcefe207 adjust calling bits to allow ((foo)) 2013-03-09 16:42:07 -05:00
Paul Tagliamonte abf63fca12 fixing up the tests 2013-03-08 18:46:51 -05:00
Paul Tagliamonte 67b803b99a Breaking things to start again. 2013-03-08 18:18:43 -05:00
Paul R. Tagliamonte dbd9f0bbf8 Q'plah! 2013-03-07 23:04:20 -05:00