2018-01-01 16:38:33 +01:00
|
|
|
# Copyright 2018 the authors.
|
2017-04-27 23:16:57 +02:00
|
|
|
# This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
# license. See the LICENSE.
|
2013-03-08 04:52:47 +01:00
|
|
|
|
2018-02-27 20:53:23 +01:00
|
|
|
from hy._compat import PY3
|
2018-02-09 03:46:03 +01:00
|
|
|
import hy.inspect
|
2017-07-12 19:57:35 +02:00
|
|
|
from hy.models import replace_hy_obj, HyExpression, HySymbol
|
2018-02-27 20:53:23 +01:00
|
|
|
from hy.lex.parser import hy_symbol_mangle
|
2017-11-16 01:17:28 +01:00
|
|
|
from hy._compat import str_type
|
2018-02-27 20:53:23 +01:00
|
|
|
|
2013-12-26 17:36:45 +01:00
|
|
|
from hy.errors import HyTypeError, HyMacroExpansionError
|
2013-12-26 04:02:20 +01:00
|
|
|
|
2013-05-16 15:34:14 +02:00
|
|
|
from collections import defaultdict
|
2013-07-06 20:00:11 +02:00
|
|
|
|
2013-08-17 17:37:48 +02:00
|
|
|
CORE_MACROS = [
|
2013-07-06 20:00:11 +02:00
|
|
|
"hy.core.bootstrap",
|
|
|
|
]
|
|
|
|
|
2013-08-17 17:37:48 +02:00
|
|
|
EXTRA_MACROS = [
|
|
|
|
"hy.core.macros",
|
|
|
|
]
|
|
|
|
|
2013-05-16 15:34:14 +02:00
|
|
|
_hy_macros = defaultdict(dict)
|
2017-06-21 05:48:54 +02:00
|
|
|
_hy_tag = defaultdict(dict)
|
2013-03-08 04:52:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
def macro(name):
|
2013-09-29 17:55:15 +02:00
|
|
|
"""Decorator to define a macro called `name`.
|
|
|
|
|
|
|
|
This stores the macro `name` in the namespace for the module where it is
|
|
|
|
defined.
|
|
|
|
|
|
|
|
If the module where it is defined is in `hy.core`, then the macro is stored
|
|
|
|
in the default `None` namespace.
|
|
|
|
|
|
|
|
This function is called from the `defmacro` special form in the compiler.
|
|
|
|
|
|
|
|
"""
|
2017-11-16 01:17:28 +01:00
|
|
|
name = hy_symbol_mangle(name)
|
2013-03-08 04:52:47 +01:00
|
|
|
def _(fn):
|
2017-09-24 19:54:54 +02:00
|
|
|
fn.__name__ = '({})'.format(name)
|
2016-12-15 01:10:46 +01:00
|
|
|
try:
|
2018-02-09 03:46:03 +01:00
|
|
|
fn._hy_macro_pass_compiler = hy.inspect.has_kwargs(fn)
|
2016-12-15 01:10:46 +01:00
|
|
|
except Exception:
|
|
|
|
# An exception might be raised if fn has arguments with
|
|
|
|
# names that are invalid in Python.
|
|
|
|
fn._hy_macro_pass_compiler = False
|
|
|
|
|
2013-05-16 15:34:14 +02:00
|
|
|
module_name = fn.__module__
|
|
|
|
if module_name.startswith("hy.core"):
|
|
|
|
module_name = None
|
|
|
|
_hy_macros[module_name][name] = fn
|
2013-03-08 05:04:20 +01:00
|
|
|
return fn
|
2013-03-08 04:52:47 +01:00
|
|
|
return _
|
|
|
|
|
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
def tag(name):
|
|
|
|
"""Decorator to define a tag macro called `name`.
|
2013-12-15 17:47:24 +01:00
|
|
|
|
|
|
|
This stores the macro `name` in the namespace for the module where it is
|
|
|
|
defined.
|
|
|
|
|
|
|
|
If the module where it is defined is in `hy.core`, then the macro is stored
|
|
|
|
in the default `None` namespace.
|
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
This function is called from the `deftag` special form in the compiler.
|
2013-12-15 17:47:24 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
def _(fn):
|
2018-02-27 20:53:23 +01:00
|
|
|
_name = hy_symbol_mangle('#{}'.format(name))
|
|
|
|
if not PY3:
|
|
|
|
_name = _name.encode('UTF-8')
|
|
|
|
fn.__name__ = _name
|
2013-12-15 17:47:24 +01:00
|
|
|
module_name = fn.__module__
|
|
|
|
if module_name.startswith("hy.core"):
|
|
|
|
module_name = None
|
2018-02-27 20:53:23 +01:00
|
|
|
_hy_tag[module_name][hy_symbol_mangle(name)] = fn
|
2013-12-15 17:47:24 +01:00
|
|
|
|
|
|
|
return fn
|
|
|
|
return _
|
|
|
|
|
|
|
|
|
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 08:35:58 +01:00
|
|
|
def require(source_module, target_module,
|
|
|
|
all_macros=False, assignments={}, prefix=""):
|
|
|
|
"""Load macros from `source_module` in the namespace of
|
|
|
|
`target_module`. `assignments` maps old names to new names, but is
|
|
|
|
ignored if `all_macros` is true. If `prefix` is nonempty, it is
|
|
|
|
prepended to the name of each imported macro. (This means you get
|
|
|
|
macros named things like "mymacromodule.mymacro", which looks like
|
|
|
|
an attribute of a module, although it's actually just a symbol
|
|
|
|
with a period in its name.)
|
2013-09-29 17:55:15 +02:00
|
|
|
|
|
|
|
This function is called from the `require` special form in the compiler.
|
|
|
|
|
|
|
|
"""
|
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 08:35:58 +01:00
|
|
|
|
|
|
|
seen_names = set()
|
|
|
|
if prefix:
|
|
|
|
prefix += "."
|
2017-11-16 01:17:28 +01:00
|
|
|
assignments = {hy_symbol_mangle(str_type(k)): v for k, v in assignments.items()}
|
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 08:35:58 +01:00
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
for d in _hy_macros, _hy_tag:
|
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 08:35:58 +01:00
|
|
|
for name, macro in d[source_module].items():
|
|
|
|
seen_names.add(name)
|
|
|
|
if all_macros:
|
2017-11-16 01:17:28 +01:00
|
|
|
d[target_module][hy_symbol_mangle(prefix + name)] = macro
|
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 08:35:58 +01:00
|
|
|
elif name in assignments:
|
2017-11-16 01:17:28 +01:00
|
|
|
d[target_module][hy_symbol_mangle(prefix + assignments[name])] = macro
|
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 08:35:58 +01:00
|
|
|
|
|
|
|
if not all_macros:
|
|
|
|
unseen = frozenset(assignments.keys()).difference(seen_names)
|
|
|
|
if unseen:
|
|
|
|
raise ImportError("cannot require names: " + repr(list(unseen)))
|
2013-12-15 17:47:24 +01:00
|
|
|
|
2013-05-16 15:34:14 +02:00
|
|
|
|
2013-08-17 17:37:48 +02:00
|
|
|
def load_macros(module_name):
|
2013-09-29 17:55:15 +02:00
|
|
|
"""Load the hy builtin macros for module `module_name`.
|
|
|
|
|
|
|
|
Modules from `hy.core` can only use the macros from CORE_MACROS.
|
|
|
|
Other modules get the macros from CORE_MACROS and EXTRA_MACROS.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _import(module, module_name=module_name):
|
|
|
|
"__import__ a module, avoiding recursions"
|
|
|
|
if module != module_name:
|
|
|
|
__import__(module)
|
|
|
|
|
2013-08-17 17:37:48 +02:00
|
|
|
for module in CORE_MACROS:
|
2013-09-29 17:55:15 +02:00
|
|
|
_import(module)
|
2013-08-17 17:37:48 +02:00
|
|
|
|
|
|
|
if module_name.startswith("hy.core"):
|
|
|
|
return
|
|
|
|
|
|
|
|
for module in EXTRA_MACROS:
|
2013-09-29 17:55:15 +02:00
|
|
|
_import(module)
|
2013-07-06 20:00:11 +02:00
|
|
|
|
|
|
|
|
2015-11-20 19:29:34 +01:00
|
|
|
def make_empty_fn_copy(fn):
|
2016-12-15 01:10:46 +01:00
|
|
|
try:
|
|
|
|
# This might fail if fn has parameters with funny names, like o!n. In
|
|
|
|
# such a case, we return a generic function that ensures the program
|
|
|
|
# can continue running. Unfortunately, the error message that might get
|
|
|
|
# raised later on while expanding a macro might not make sense at all.
|
|
|
|
|
2018-02-09 03:46:03 +01:00
|
|
|
formatted_args = hy.inspect.format_args(fn)
|
2016-12-15 01:10:46 +01:00
|
|
|
fn_str = 'lambda {}: None'.format(
|
|
|
|
formatted_args.lstrip('(').rstrip(')'))
|
|
|
|
empty_fn = eval(fn_str)
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
def empty_fn(*args, **kwargs):
|
|
|
|
None
|
2015-08-30 20:14:16 +02:00
|
|
|
|
|
|
|
return empty_fn
|
|
|
|
|
|
|
|
|
2015-12-23 21:13:18 +01:00
|
|
|
def macroexpand(tree, compiler):
|
2013-09-29 17:55:15 +02:00
|
|
|
"""Expand the toplevel macros for the `tree`.
|
|
|
|
|
|
|
|
Load the macros from the given `module_name`, then expand the (top-level)
|
|
|
|
macros in `tree` until it stops changing.
|
|
|
|
|
|
|
|
"""
|
2015-12-23 21:13:18 +01:00
|
|
|
load_macros(compiler.module_name)
|
2013-09-29 17:55:15 +02:00
|
|
|
old = None
|
|
|
|
while old != tree:
|
|
|
|
old = tree
|
2015-12-23 21:13:18 +01:00
|
|
|
tree = macroexpand_1(tree, compiler)
|
2013-09-29 17:55:15 +02:00
|
|
|
return tree
|
|
|
|
|
|
|
|
|
2015-12-23 21:13:18 +01:00
|
|
|
def macroexpand_1(tree, compiler):
|
2013-09-29 17:55:15 +02:00
|
|
|
"""Expand the toplevel macro from `tree` once, in the context of
|
|
|
|
`module_name`."""
|
2013-03-09 00:46:51 +01:00
|
|
|
if isinstance(tree, HyExpression):
|
2013-06-26 01:58:28 +02:00
|
|
|
if tree == []:
|
2013-06-26 02:20:15 +02:00
|
|
|
return tree
|
2013-06-26 08:44:09 +02:00
|
|
|
|
2013-06-26 01:58:28 +02:00
|
|
|
fn = tree[0]
|
2013-05-11 00:29:42 +02:00
|
|
|
if fn in ("quote", "quasiquote"):
|
|
|
|
return tree
|
2013-07-06 20:00:11 +02:00
|
|
|
ntree = HyExpression(tree[:])
|
2013-03-14 01:41:53 +01:00
|
|
|
ntree.replace(tree)
|
2013-03-09 00:46:51 +01:00
|
|
|
|
2015-12-23 21:13:18 +01:00
|
|
|
opts = {}
|
|
|
|
|
2017-07-12 19:57:35 +02:00
|
|
|
if isinstance(fn, HySymbol):
|
2017-11-16 01:17:28 +01:00
|
|
|
fn = hy_symbol_mangle(str_type(fn))
|
2015-12-23 21:13:18 +01:00
|
|
|
m = _hy_macros[compiler.module_name].get(fn)
|
2013-05-16 15:34:14 +02:00
|
|
|
if m is None:
|
|
|
|
m = _hy_macros[None].get(fn)
|
|
|
|
if m is not None:
|
2015-12-23 21:13:18 +01:00
|
|
|
if m._hy_macro_pass_compiler:
|
|
|
|
opts['compiler'] = compiler
|
|
|
|
|
2013-12-26 04:02:20 +01:00
|
|
|
try:
|
2015-11-20 19:29:34 +01:00
|
|
|
m_copy = make_empty_fn_copy(m)
|
2017-09-23 20:55:44 +02:00
|
|
|
m_copy(compiler.module_name, *ntree[1:], **opts)
|
2015-08-30 20:14:16 +02:00
|
|
|
except TypeError as e:
|
|
|
|
msg = "expanding `" + str(tree[0]) + "': "
|
|
|
|
msg += str(e).replace("<lambda>()", "", 1).strip()
|
|
|
|
raise HyMacroExpansionError(tree, msg)
|
2016-12-15 01:10:46 +01:00
|
|
|
|
2015-08-30 20:14:16 +02:00
|
|
|
try:
|
2017-09-23 20:55:44 +02:00
|
|
|
obj = m(compiler.module_name, *ntree[1:], **opts)
|
2013-12-26 17:36:45 +01:00
|
|
|
except HyTypeError as e:
|
|
|
|
if e.expression is None:
|
|
|
|
e.expression = tree
|
|
|
|
raise
|
2013-12-26 04:02:20 +01:00
|
|
|
except Exception as e:
|
2014-11-28 00:29:34 +01:00
|
|
|
msg = "expanding `" + str(tree[0]) + "': " + repr(e)
|
2013-12-26 04:02:20 +01:00
|
|
|
raise HyMacroExpansionError(tree, msg)
|
2015-01-24 01:16:38 +01:00
|
|
|
replace_hy_obj(obj, tree)
|
2013-03-09 22:42:07 +01:00
|
|
|
return obj
|
2013-03-09 00:46:51 +01:00
|
|
|
return ntree
|
2013-03-08 04:52:47 +01:00
|
|
|
return tree
|
2014-01-14 02:38:16 +01:00
|
|
|
|
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
def tag_macroexpand(tag, tree, compiler):
|
|
|
|
"""Expand the tag macro "tag" with argument `tree`."""
|
2015-12-23 21:13:18 +01:00
|
|
|
load_macros(compiler.module_name)
|
2014-01-14 02:38:16 +01:00
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
tag_macro = _hy_tag[compiler.module_name].get(tag)
|
|
|
|
if tag_macro is None:
|
2015-07-26 23:19:10 +02:00
|
|
|
try:
|
2017-06-21 05:48:54 +02:00
|
|
|
tag_macro = _hy_tag[None][tag]
|
2015-07-26 23:19:10 +02:00
|
|
|
except KeyError:
|
|
|
|
raise HyTypeError(
|
2017-05-10 03:54:32 +02:00
|
|
|
tag,
|
2017-06-21 05:48:54 +02:00
|
|
|
"`{0}' is not a defined tag macro.".format(tag)
|
2015-07-26 23:19:10 +02:00
|
|
|
)
|
2014-01-14 02:38:16 +01:00
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
expr = tag_macro(tree)
|
2017-06-23 01:07:50 +02:00
|
|
|
return replace_hy_obj(expr, tree)
|