Example #1
0
def serialize_sequence(sequence, context):
    ir_set = IR.set([serialize(value, context) for value in sequence])

    if all(isinstance(item, _BASIC_SET_TYPES) for item in sequence):
        # {1, 2, 3} / {<ast::op>'Add', <ast::op>'Sub', ...}
        return ir_set
    else:
        # Inserting a sequence of AST objects would require special
        # attention to calculate the index property.
        target = IR.name("item")
        scope = IR.namespace({"items": ir_set})
        loop = IR.loop(
            target,
            IR.call("enumerate", [IR.name("items")]),
            IR.select(
                IR.attribute(target, 1),
                selections=[
                    IR.assign(IR.property("index"), IR.attribute(target, 0))
                ],
            ),
        )
        return IR.add_namespace(scope, loop)
Example #2
0
    filters = None
    for key, value in node.filters.items():
        if value is grammar.Ignore:
            continue

        if right_filter := state.compile(key, value):
            filters = IR.combine_filters(filters, right_filter)

    if state.is_root:
        state.scope.exit()
        if state_filters := IR.unpack_filters(state.filters):
            filters = IR.combine_filters(filters, state_filters)
        if state.variables:
            namespace = IR.namespace(state.variables)
            filters = IR.add_namespace(namespace, IR.select(filters))
        return IR.select(state.match, filters=filters)

    if filters is None:
        filters = IR.filter(state.parents[-1].compute_path(),
                            IR.wrap(state.match), "IS")

    return filters


@codegen.register(grammar.MatchEnum)
def convert_match_enum(node, state):
    expr = IR.enum_member(node.base, node.name)
    return IR.filter(state.compute_path(), expr, "=")