Refactor macroexpand_1
This commit is contained in:
parent
116d7fa6ec
commit
026316ebef
21
hy/macros.py
21
hy/macros.py
@ -174,22 +174,19 @@ 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
|
||||||
|
|
||||||
|
fn = mangle(fn)
|
||||||
|
m = _hy_macros[compiler.module_name].get(fn) or _hy_macros[None].get(fn)
|
||||||
|
if not m:
|
||||||
return tree
|
return tree
|
||||||
|
|
||||||
opts = {}
|
opts = {}
|
||||||
|
|
||||||
if isinstance(fn, HySymbol):
|
|
||||||
fn = mangle(str_type(fn))
|
|
||||||
m = _hy_macros[compiler.module_name].get(fn)
|
|
||||||
if m is None:
|
|
||||||
m = _hy_macros[None].get(fn)
|
|
||||||
if m is not None:
|
|
||||||
if m._hy_macro_pass_compiler:
|
if m._hy_macro_pass_compiler:
|
||||||
opts['compiler'] = compiler
|
opts['compiler'] = compiler
|
||||||
|
|
||||||
@ -210,9 +207,7 @@ def macroexpand_1(tree, compiler):
|
|||||||
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