當前位置: 首頁>>代碼示例>>Python>>正文


Python HyExpression.replace方法代碼示例

本文整理匯總了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
開發者ID:cbbrowne,項目名稱:hy,代碼行數:33,代碼來源:macros.py

示例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
開發者ID:laarmen,項目名稱:hy,代碼行數:31,代碼來源:bootstrap.py

示例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
開發者ID:laarmen,項目名稱:hy,代碼行數:37,代碼來源:macros.py

示例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
開發者ID:copyninja,項目名稱:hy,代碼行數:34,代碼來源:macros.py

示例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
開發者ID:eal,項目名稱:hy,代碼行數:11,代碼來源:mangles.py

示例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
開發者ID:laarmen,項目名稱:hy,代碼行數:12,代碼來源:bootstrap.py

示例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
開發者ID:eigenhombre,項目名稱:hy,代碼行數:13,代碼來源:bootstrap.py

示例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)
開發者ID:Eleonore9,項目名稱:hy,代碼行數:9,代碼來源:test_compiler.py

示例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
開發者ID:koo5,項目名稱:hy,代碼行數:18,代碼來源:mangles.py

示例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
開發者ID:rwtolbert,項目名稱:hy,代碼行數:46,代碼來源:macros.py

示例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
開發者ID:sbp,項目名稱:hy,代碼行數:24,代碼來源:macros.py

示例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
開發者ID:qyqx,項目名稱:hy,代碼行數:26,代碼來源:macros.py

示例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
開發者ID:jonathanmarvens,項目名稱:hython,代碼行數:26,代碼來源:macros.py


注:本文中的hy.models.expression.HyExpression.replace方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。