本文整理汇总了Python中hy.models.expression.HyExpression.replace方法的典型用法代码示例。如果您正苦于以下问题:Python HyExpression.replace方法的具体用法?Python HyExpression.replace怎么用?Python HyExpression.replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hy.models.expression.HyExpression
的用法示例。
在下文中一共展示了HyExpression.replace方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process
# 需要导入模块: from hy.models.expression import HyExpression [as 别名]
# 或者: from hy.models.expression.HyExpression import replace [as 别名]
def process(tree):
if isinstance(tree, HyExpression):
fn = tree[0]
ntree = HyExpression([fn] + [process(x) for x in tree[1:]])
ntree.replace(tree)
if isinstance(fn, HyString):
if fn in _hy_macros:
m = _hy_macros[fn]
obj = m(ntree)
obj.replace(tree)
return obj
ntree.replace(tree)
return ntree
if isinstance(tree, HyDict):
obj = HyDict(dict((process(x), process(tree[x])) for x in tree))
obj.replace(tree)
return obj
if isinstance(tree, HyList):
obj = HyList([process(x) for x in tree]) # NOQA
# flake8 thinks we're redefining from 52.
obj.replace(tree)
return obj
if isinstance(tree, list):
return [process(x) for x in tree]
return tree
示例2: for_macro
# 需要导入模块: from hy.models.expression import HyExpression [as 别名]
# 或者: from hy.models.expression.HyExpression import replace [as 别名]
def for_macro(*tree):
ret = None
# for [x iter y iter] ...
# ->
# foreach x iter
# foreach y iter
# ...
tree = HyExpression(tree).replace(tree[0])
it = iter(tree.pop(0))
blocks = list(zip(it, it)) # List for Python 3.x degenerating.
key, val = blocks.pop(0)
ret = HyExpression([HySymbol("foreach"),
HyList([key, val])])
root = ret
ret.replace(tree)
for key, val in blocks:
# x, [1, 2, 3, 4]
nret = HyExpression([HySymbol("foreach"),
HyList([key, val])])
nret.replace(key)
ret.append(nret)
ret = nret
[ret.append(x) for x in tree] # we really need [email protected]
return root
示例3: process
# 需要导入模块: from hy.models.expression import HyExpression [as 别名]
# 或者: from hy.models.expression.HyExpression import replace [as 别名]
def process(tree, module_name):
if isinstance(tree, HyExpression):
if tree == []:
return tree
fn = tree[0]
if fn in ("quote", "quasiquote"):
return tree
ntree = HyExpression(
[fn] + [process(x, module_name) for x in tree[1:]]
)
ntree.replace(tree)
if isinstance(fn, HyString):
m = _hy_macros[module_name].get(fn)
if m is None:
m = _hy_macros[None].get(fn)
if m is not None:
obj = _wrap_value(m(*ntree[1:]))
obj.replace(tree)
return obj
ntree.replace(tree)
return ntree
if isinstance(tree, HyList):
obj = tree.__class__([process(x, module_name) for x in tree]) # NOQA
# flake8 thinks we're redefining from 52.
obj.replace(tree)
return obj
if isinstance(tree, list):
return [process(x, module_name) for x in tree]
return tree
示例4: macroexpand_1
# 需要导入模块: from hy.models.expression import HyExpression [as 别名]
# 或者: from hy.models.expression.HyExpression import replace [as 别名]
def macroexpand_1(tree, module_name):
"""Expand the toplevel macro from `tree` once, in the context of
`module_name`."""
if isinstance(tree, HyExpression):
if tree == []:
return tree
fn = tree[0]
if fn in ("quote", "quasiquote"):
return tree
ntree = HyExpression(tree[:])
ntree.replace(tree)
if isinstance(fn, HyString):
m = _hy_macros[module_name].get(fn)
if m is None:
m = _hy_macros[None].get(fn)
if m is not None:
try:
obj = _wrap_value(m(*ntree[1:]))
except HyTypeError as e:
if e.expression is None:
e.expression = tree
raise
except Exception as e:
msg = "`" + str(tree[0]) + "' " + " ".join(str(e).split()[1:])
raise HyMacroExpansionError(tree, msg)
obj.replace(tree)
return obj
return ntree
return tree
示例5: visit
# 需要导入模块: from hy.models.expression import HyExpression [as 别名]
# 或者: from hy.models.expression.HyExpression import replace [as 别名]
def visit(self, tree):
if isinstance(tree, HyExpression) and tree != []:
call = tree[0]
if call == "if" and self.should_hoist():
fn = HyExpression([HyExpression([HySymbol("fn"),
HyList([]),
tree])])
fn.replace(tree)
return fn
示例6: threading_tail_macro
# 需要导入模块: from hy.models.expression import HyExpression [as 别名]
# 或者: from hy.models.expression.HyExpression import replace [as 别名]
def threading_tail_macro(head, *rest):
ret = head
for node in rest:
if not isinstance(node, HyExpression):
nnode = HyExpression([node])
nnode.replace(node)
node = nnode
node.append(ret)
ret = node
return ret
示例7: threading_tail_macro
# 需要导入模块: from hy.models.expression import HyExpression [as 别名]
# 或者: from hy.models.expression.HyExpression import replace [as 别名]
def threading_tail_macro(tree):
tree.pop(0)
ret = tree.pop(0)
for node in tree:
if not isinstance(node, HyExpression):
nnode = HyExpression([node])
nnode.replace(node)
node = nnode
node.append(ret)
ret = node
return ret
示例8: _make_expression
# 需要导入模块: from hy.models.expression import HyExpression [as 别名]
# 或者: from hy.models.expression.HyExpression import replace [as 别名]
def _make_expression(*args):
h = HyExpression(args)
h.start_line = 1
h.end_line = 1
h.start_column = 1
h.end_column = 1
return h.replace(h)
示例9: visit
# 需要导入模块: from hy.models.expression import HyExpression [as 别名]
# 或者: from hy.models.expression.HyExpression import replace [as 别名]
def visit(self, tree):
"""
Visit all the nodes in the Hy code tree.
"""
if isinstance(tree, HyExpression) and tree != []:
call = tree[0]
if call == "fn" and self.should_hoist():
# if we have a Function and we should hoist it --
new_name = HySymbol(self.unique_name())
new_name.replace(tree)
fn_def = HyExpression([HySymbol("def"),
new_name,
tree])
fn_def.replace(tree)
self.hoist(fn_def)
return new_name
示例10: macroexpand_1
# 需要导入模块: from hy.models.expression import HyExpression [as 别名]
# 或者: from hy.models.expression.HyExpression import replace [as 别名]
def macroexpand_1(tree, compiler):
"""Expand the toplevel macro from `tree` once, in the context of
`module_name`."""
if isinstance(tree, HyExpression):
if tree == []:
return tree
fn = tree[0]
if fn in ("quote", "quasiquote"):
return tree
ntree = HyExpression(tree[:])
ntree.replace(tree)
opts = {}
if isinstance(fn, HyString):
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:
opts['compiler'] = compiler
try:
m_copy = make_empty_fn_copy(m)
m_copy(*ntree[1:], **opts)
except TypeError as e:
msg = "expanding `" + str(tree[0]) + "': "
msg += str(e).replace("<lambda>()", "", 1).strip()
raise HyMacroExpansionError(tree, msg)
try:
obj = wrap_value(m(*ntree[1:], **opts))
except HyTypeError as e:
if e.expression is None:
e.expression = tree
raise
except Exception as e:
msg = "expanding `" + str(tree[0]) + "': " + repr(e)
raise HyMacroExpansionError(tree, msg)
replace_hy_obj(obj, tree)
return obj
return ntree
return tree
示例11: macroexpand
# 需要导入模块: from hy.models.expression import HyExpression [as 别名]
# 或者: from hy.models.expression.HyExpression import replace [as 别名]
def macroexpand(tree, module_name):
if isinstance(tree, HyExpression):
if tree == []:
return tree
fn = tree[0]
if fn in ("quote", "quasiquote"):
return tree
ntree = HyExpression(tree[:])
ntree.replace(tree)
if isinstance(fn, HyString):
m = _hy_macros[module_name].get(fn)
if m is None:
m = _hy_macros[None].get(fn)
if m is not None:
obj = _wrap_value(m(*ntree[1:]))
obj.replace(tree)
return obj
return ntree
return tree
示例12: macroexpand_1
# 需要导入模块: from hy.models.expression import HyExpression [as 别名]
# 或者: from hy.models.expression.HyExpression import replace [as 别名]
def macroexpand_1(tree, module_name):
"""Expand the toplevel macro from `tree` once, in the context of
`module_name`."""
if isinstance(tree, HyExpression):
if tree == []:
return tree
fn = tree[0]
if fn in ("quote", "quasiquote"):
return tree
ntree = HyExpression(tree[:])
ntree.replace(tree)
if isinstance(fn, HyString):
m = _hy_macros[module_name].get(fn)
if m is None:
m = _hy_macros[None].get(fn)
if m is not None:
obj = _wrap_value(m(*ntree[1:]))
obj.replace(tree)
return obj
return ntree
return tree
示例13: process
# 需要导入模块: from hy.models.expression import HyExpression [as 别名]
# 或者: from hy.models.expression.HyExpression import replace [as 别名]
def process(tree):
if isinstance(tree, HyExpression):
fn = tree[0]
ntree = HyExpression([fn] + [process(x) for x in tree[1:]])
if isinstance(fn, HyString):
if fn in _hy_macros:
m = _hy_macros[fn]
obj = m(ntree)
obj.replace(tree)
return obj
ntree.replace(tree)
return ntree
if isinstance(tree, HyList):
obj = HyList([process(x) for x in tree])
obj.replace(tree)
return obj
if isinstance(tree, list):
return [process(x) for x in tree]
return tree