本文整理汇总了Python中hy.models.expression.HyExpression类的典型用法代码示例。如果您正苦于以下问题:Python HyExpression类的具体用法?Python HyExpression怎么用?Python HyExpression使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HyExpression类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: macroexpand_1
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
示例2: process
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
示例3: process
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: _make_expression
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
示例5: visit
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
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: for_macro
def for_macro(tree):
tree.pop(0)
ret = None
# for [x iter y iter] ...
# ->
# foreach x iter
# foreach y iter
# ...
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
示例8: threading_tail_macro
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
示例9: let_macro
def let_macro(variables, *body):
expr = HyExpression([HySymbol("fn"), HyList([])])
for var in variables:
if isinstance(var, list):
expr.append(HyExpression([HySymbol("setf"),
var[0], var[1]]))
else:
expr.append(HyExpression([HySymbol("setf"),
var, HySymbol("None")]))
return HyExpression([expr + list(body)])
示例10: let_macro
def let_macro(tree):
tree.pop(0) # "let"
ret = tree.pop(0) # vars
# tree is now the body
expr = HyExpression([HySymbol("fn"), HyList([])])
for var in ret:
expr.append(HyExpression([HySymbol("setf"), var[0], var[1]]))
for stmt in tree:
expr.append(stmt)
return HyExpression([expr])
示例11: router
def router(tree, rkwargs=None):
tree = HyExpression(tree)
name = tree.pop(0)
path = tree.pop(0)
tree.insert(0, HySymbol("fn"))
tree = HyExpression([HySymbol("def"), name, tree])
route = HyExpression([HySymbol(".route"), HySymbol("app"), path])
if rkwargs:
route = HyExpression([HySymbol("kwapply"), route,
HyDict({HyString("methods"): rkwargs})])
return HyExpression([HySymbol("with_decorator"), route, tree])
示例12: visit
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
示例13: macroexpand_1
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
示例14: compile_expression
def compile_expression(self, expression):
fn = expression[0]
if isinstance(fn, HyString):
if fn in _compile_table:
return _compile_table[fn](self, expression)
if expression[0].startswith("."):
return self.compile_dotted_expression(expression)
if isinstance(fn, HyKeyword):
new_expr = HyExpression(["get", expression[1], fn])
new_expr.start_line = expression.start_line
new_expr.start_column = expression.start_column
return self.compile_index_expression(new_expr)
return ast.Call(func=self.compile(fn),
args=[self.compile(x) for x in expression[1:]],
keywords=[],
starargs=None,
kwargs=None,
lineno=expression.start_line,
col_offset=expression.start_column)
示例15: macroexpand
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