Mangle macro names

This commit is contained in:
Kodi Arfer 2017-11-15 16:17:28 -08:00
parent 0c816f2e83
commit ccb3ba6092
2 changed files with 15 additions and 2 deletions

View File

@ -6,6 +6,7 @@ from hy._compat import PY3
import hy.inspect
from hy.models import replace_hy_obj, HyExpression, HySymbol
from hy.lex.parser import hy_symbol_mangle
from hy._compat import str_type
from hy.errors import HyTypeError, HyMacroExpansionError
@ -35,6 +36,7 @@ def macro(name):
This function is called from the `defmacro` special form in the compiler.
"""
name = hy_symbol_mangle(name)
def _(fn):
fn.__name__ = '({})'.format(name)
try:
@ -95,14 +97,15 @@ def require(source_module, target_module,
seen_names = set()
if prefix:
prefix += "."
assignments = {hy_symbol_mangle(str_type(k)): v for k, v in assignments.items()}
for d in _hy_macros, _hy_tag:
for name, macro in d[source_module].items():
seen_names.add(name)
if all_macros:
d[target_module][prefix + name] = macro
d[target_module][hy_symbol_mangle(prefix + name)] = macro
elif name in assignments:
d[target_module][prefix + assignments[name]] = macro
d[target_module][hy_symbol_mangle(prefix + assignments[name])] = macro
if not all_macros:
unseen = frozenset(assignments.keys()).difference(seen_names)
@ -184,6 +187,7 @@ def macroexpand_1(tree, compiler):
opts = {}
if isinstance(fn, HySymbol):
fn = hy_symbol_mangle(str_type(fn))
m = _hy_macros[compiler.module_name].get(fn)
if m is None:
m = _hy_macros[None].get(fn)

View File

@ -106,6 +106,15 @@
(assert (= hyx_Xlatin_capital_letter_xXXskull_and_crossbonesX "treasure"))))
(defmacro m---x [form]
[form form])
(defn test-macro []
(setv x "")
(assert (= (m---x (do (+= x "a") 1)) [1 1]))
(assert (= (m___x (do (+= x "b") 2)) [2 2]))
(assert (= x "aabb")))
(deftag tm---x [form]
[form form])
(defn test-tag-macro []