Refactor macroexpand_1
This commit is contained in:
parent
116d7fa6ec
commit
026316ebef
63
hy/macros.py
63
hy/macros.py
@ -174,45 +174,40 @@ def macroexpand(tree, compiler):
|
|||||||
def macroexpand_1(tree, compiler):
|
def macroexpand_1(tree, compiler):
|
||||||
"""Expand the toplevel macro from `tree` once, in the context of
|
"""Expand the toplevel macro from `tree` once, in the context of
|
||||||
`module_name`."""
|
`module_name`."""
|
||||||
if isinstance(tree, HyExpression):
|
if not isinstance(tree, HyExpression) or tree == []:
|
||||||
if tree == []:
|
return tree
|
||||||
return tree
|
|
||||||
|
|
||||||
fn = tree[0]
|
fn = tree[0]
|
||||||
if fn in ("quote", "quasiquote"):
|
if fn in ("quote", "quasiquote") or not isinstance(fn, HySymbol):
|
||||||
return tree
|
return tree
|
||||||
|
|
||||||
opts = {}
|
fn = mangle(fn)
|
||||||
|
m = _hy_macros[compiler.module_name].get(fn) or _hy_macros[None].get(fn)
|
||||||
|
if not m:
|
||||||
|
return tree
|
||||||
|
|
||||||
if isinstance(fn, HySymbol):
|
opts = {}
|
||||||
fn = mangle(str_type(fn))
|
if m._hy_macro_pass_compiler:
|
||||||
m = _hy_macros[compiler.module_name].get(fn)
|
opts['compiler'] = compiler
|
||||||
if m is None:
|
|
||||||
m = _hy_macros[None].get(fn)
|
|
||||||
if m is not None:
|
|
||||||
if m._hy_macro_pass_compiler:
|
|
||||||
opts['compiler'] = compiler
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
m_copy = make_empty_fn_copy(m)
|
m_copy = make_empty_fn_copy(m)
|
||||||
m_copy(compiler.module_name, *tree[1:], **opts)
|
m_copy(compiler.module_name, *tree[1:], **opts)
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
msg = "expanding `" + str(tree[0]) + "': "
|
msg = "expanding `" + str(tree[0]) + "': "
|
||||||
msg += str(e).replace("<lambda>()", "", 1).strip()
|
msg += str(e).replace("<lambda>()", "", 1).strip()
|
||||||
raise HyMacroExpansionError(tree, msg)
|
raise HyMacroExpansionError(tree, msg)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
obj = m(compiler.module_name, *tree[1:], **opts)
|
obj = m(compiler.module_name, *tree[1:], **opts)
|
||||||
except HyTypeError as e:
|
except HyTypeError as e:
|
||||||
if e.expression is None:
|
if e.expression is None:
|
||||||
e.expression = tree
|
e.expression = tree
|
||||||
raise
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
msg = "expanding `" + str(tree[0]) + "': " + repr(e)
|
msg = "expanding `" + str(tree[0]) + "': " + repr(e)
|
||||||
raise HyMacroExpansionError(tree, msg)
|
raise HyMacroExpansionError(tree, msg)
|
||||||
replace_hy_obj(obj, tree)
|
return replace_hy_obj(obj, tree)
|
||||||
return obj
|
|
||||||
return tree
|
|
||||||
|
|
||||||
|
|
||||||
def tag_macroexpand(tag, tree, compiler):
|
def tag_macroexpand(tag, tree, compiler):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user