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


Python expression.HyExpression類代碼示例

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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