示例#1
0
def eta_expand_tuple(
    path: qlast.Path,
    stype: s_types.Tuple,
    *,
    ctx: context.ContextLevel,
) -> qlast.Expr:
    """η-expansion of tuples

    η-expansion of tuple types is straightforward and traditional:
        EXPAND(tuple<t, s>, p) = (EXPAND(t, p.0), EXPAND(s, p.1))
    is the case for pairs. n-ary and named cases are generalized in the
    obvious way.
    The one exception is that the expansion of the empty tuple type is
    `p` and not `()`, to ensure that the path appears in the output.
    """
    if not stype.get_subtypes(ctx.env.schema):
        return path

    els = [
        qlast.TupleElement(
            name=qlast.ObjectRef(name=name),
            val=eta_expand(astutils.extend_path(path, name), subtype, ctx=ctx),
        ) for name, subtype in stype.iter_subtypes(ctx.env.schema)
    ]

    if stype.is_named(ctx.env.schema):
        return qlast.NamedTuple(elements=els)
    else:
        return qlast.Tuple(elements=[el.val for el in els])
示例#2
0
    def visit_Tuple(self, node):
        if node.named:
            result = qlast.NamedTuple(elements=[
                qlast.TupleElement(name=el.name, val=self.visit(el.val))
                for el in node.elements
            ])
        else:
            result = qlast.Tuple(
                elements=[self.visit(e.val) for e in node.elements])

        return result
示例#3
0
 def reduce_LPAREN_NamedTupleElementList_COMMA_RPAREN(self, *kids):
     self.val = qlast.NamedTuple(elements=kids[1].val)