Skip to content

Main

main

RESERVED_KEYWORDS = ['let', 'if', 'else', 'true', 'false'] module-attribute

IDENT = re.compile('[a-zA-Z_][a-zA-Z0-9_]*') module-attribute

INTEGER = re.compile('[0-9]+') module-attribute

LET = Punctuation(re.compile('let'), "'let'") module-attribute

EQUAL = Punctuation(re.compile('='), 'equal') module-attribute

SEMICOLON = Punctuation(re.compile(';'), 'semicolon') module-attribute

SINGLE_PERIOD = Punctuation(re.compile('\\.(?!\\.)'), 'period') module-attribute

COMMA = Punctuation(re.compile(','), 'comma') module-attribute

IF = Punctuation(re.compile('if'), "'if'") module-attribute

ELSE = Punctuation(re.compile('else'), "'else'") module-attribute

TRUE = Punctuation(re.compile('true'), "'true'") module-attribute

FALSE = Punctuation(re.compile('false'), "'false'") module-attribute

STAR = Punctuation(re.compile('\\*'), 'star') module-attribute

PLUS = Punctuation(re.compile('\\+'), 'plus') module-attribute

RANGE = Punctuation(re.compile('\\.\\.'), 'range') module-attribute

PIPE = Punctuation(re.compile('\\|'), 'pipe') module-attribute

EQUAL_CMP = Punctuation(re.compile('=='), 'equality comparator') module-attribute

LT_CMP = Punctuation(re.compile('<'), 'less than comparator') module-attribute

GT_CMP = Punctuation(re.compile('>'), 'greater than comparator') module-attribute

LTE_CMP = Punctuation(re.compile('<='), 'less than or equal comparator') module-attribute

GTE_CMP = Punctuation(re.compile('>='), 'greater than or equal comparator') module-attribute

NEQ_CMP = Punctuation(re.compile('!='), 'not equal comparator') module-attribute

BOOL_AND = Punctuation(re.compile('&&'), 'boolean and') module-attribute

BOOL_OR = Punctuation(re.compile('\\|\\|'), 'boolean or') module-attribute

BOOL_NEG = Punctuation(re.compile('!'), 'boolean negation') module-attribute

LPAREN = Punctuation(re.compile('\\('), 'left parenthesis') module-attribute

RPAREN = Punctuation(re.compile('\\)'), 'right parenthesis') module-attribute

LCURL = Punctuation(re.compile('\\{'), 'left curly bracket') module-attribute

RCURL = Punctuation(re.compile('\\}'), 'right curly bracket') module-attribute

T = TypeVar('T') module-attribute

PARSE_BINOP_PRIORITY: tuple[tuple[BinaryOp, ...], ...] = ((Multiplication(),), (Addition(),), (ListRange(), Comparator(EQUAL_CMP, 'eq'), Comparator(LTE_CMP, 'ule'), Comparator(GTE_CMP, 'uge'), Comparator(GT_CMP, 'ugt'), Comparator(LT_CMP, 'ult'), Comparator(NEQ_CMP, 'ne')), (BoolAnd(), BoolOr())) module-attribute

arg_parser = argparse.ArgumentParser(prog='list-lang', description='Parses list-lang programs and transforms them.') module-attribute

args = arg_parser.parse_args() module-attribute

code = args.filename.read() module-attribute

module = program_to_mlir_module(code) module-attribute

ctx = Context() module-attribute

Punctuation dataclass

Source code in xdsl/frontend/listlang/main.py
@dataclass
class Punctuation:
    rg: re.Pattern[str]
    name: str

rg: re.Pattern[str] instance-attribute

name: str instance-attribute

__init__(rg: re.Pattern[str], name: str) -> None

Binding dataclass

Source code in xdsl/frontend/listlang/main.py
@dataclass
class Binding:
    value: SSAValue
    typ: ListLangType

value: SSAValue instance-attribute

typ: ListLangType instance-attribute

__init__(value: SSAValue, typ: ListLangType) -> None

ParsingContext

Source code in xdsl/frontend/listlang/main.py
class ParsingContext:
    cursor: CodeCursor
    bindings: ScopedDict[str, Binding]

    def __init__(self, code: str):
        self.cursor = CodeCursor(code)
        self.bindings = ScopedDict()

    def error(self, msg: str) -> ParseError:
        return ParseError(self.cursor.pos, msg)

cursor: CodeCursor = CodeCursor(code) instance-attribute

bindings: ScopedDict[str, Binding] = ScopedDict() instance-attribute

__init__(code: str)

Source code in xdsl/frontend/listlang/main.py
def __init__(self, code: str):
    self.cursor = CodeCursor(code)
    self.bindings = ScopedDict()

error(msg: str) -> ParseError

Source code in xdsl/frontend/listlang/main.py
def error(self, msg: str) -> ParseError:
    return ParseError(self.cursor.pos, msg)

BinaryOp

Source code in xdsl/frontend/listlang/main.py
class BinaryOp:
    def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
        """
        Returns true if the expected glyph was found.
        """
        ...

    def build(
        self,
        builder: Builder,
        lhs: Located[TypedExpression],
        rhs: Located[TypedExpression],
    ) -> TypedExpression: ...

parse_opt_glyph(ctx: ParsingContext) -> Located[bool]

Returns true if the expected glyph was found.

Source code in xdsl/frontend/listlang/main.py
def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
    """
    Returns true if the expected glyph was found.
    """
    ...

build(builder: Builder, lhs: Located[TypedExpression], rhs: Located[TypedExpression]) -> TypedExpression

Source code in xdsl/frontend/listlang/main.py
def build(
    self,
    builder: Builder,
    lhs: Located[TypedExpression],
    rhs: Located[TypedExpression],
) -> TypedExpression: ...

Multiplication

Bases: BinaryOp

Source code in xdsl/frontend/listlang/main.py
class Multiplication(BinaryOp):
    def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
        return parse_opt_punct(ctx, STAR)

    def build(
        self,
        builder: Builder,
        lhs: Located[TypedExpression],
        rhs: Located[TypedExpression],
    ) -> TypedExpression:
        if not isinstance(lhs.value.typ, ListLangInt):
            raise ParseError(
                lhs.loc.pos,
                f"expected expression of type {ListLangInt()} "
                f"in multiplication, got {lhs.value.typ}",
            )

        if not isinstance(rhs.value.typ, ListLangInt):
            raise ParseError(
                rhs.loc.pos,
                f"expected expression of type {ListLangInt()} "
                f"in multiplication, got {rhs.value.typ}",
            )

        mul_op = builder.insert(arith.MuliOp(lhs.value.value, rhs.value.value))
        mul_op.result.name_hint = compose_name_hints(
            lhs.value.value, "times", rhs.value.value
        )
        return TypedExpression(mul_op.result, lhs.value.typ)

parse_opt_glyph(ctx: ParsingContext) -> Located[bool]

Source code in xdsl/frontend/listlang/main.py
def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
    return parse_opt_punct(ctx, STAR)

build(builder: Builder, lhs: Located[TypedExpression], rhs: Located[TypedExpression]) -> TypedExpression

Source code in xdsl/frontend/listlang/main.py
def build(
    self,
    builder: Builder,
    lhs: Located[TypedExpression],
    rhs: Located[TypedExpression],
) -> TypedExpression:
    if not isinstance(lhs.value.typ, ListLangInt):
        raise ParseError(
            lhs.loc.pos,
            f"expected expression of type {ListLangInt()} "
            f"in multiplication, got {lhs.value.typ}",
        )

    if not isinstance(rhs.value.typ, ListLangInt):
        raise ParseError(
            rhs.loc.pos,
            f"expected expression of type {ListLangInt()} "
            f"in multiplication, got {rhs.value.typ}",
        )

    mul_op = builder.insert(arith.MuliOp(lhs.value.value, rhs.value.value))
    mul_op.result.name_hint = compose_name_hints(
        lhs.value.value, "times", rhs.value.value
    )
    return TypedExpression(mul_op.result, lhs.value.typ)

Addition

Bases: BinaryOp

Source code in xdsl/frontend/listlang/main.py
class Addition(BinaryOp):
    def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
        return parse_opt_punct(ctx, PLUS)

    def build(
        self,
        builder: Builder,
        lhs: Located[TypedExpression],
        rhs: Located[TypedExpression],
    ) -> TypedExpression:
        if not isinstance(lhs.value.typ, ListLangInt):
            raise ParseError(
                lhs.loc.pos,
                f"expected expression of type {ListLangInt()} "
                f"in addition, got {lhs.value.typ}",
            )

        if not isinstance(rhs.value.typ, ListLangInt):
            raise ParseError(
                rhs.loc.pos,
                f"expected expression of type {ListLangInt()} "
                f"in addition, got {rhs.value.typ}",
            )

        add_op = builder.insert(arith.AddiOp(lhs.value.value, rhs.value.value))
        add_op.result.name_hint = compose_name_hints(
            lhs.value.value, "plus", rhs.value.value
        )
        return TypedExpression(add_op.result, lhs.value.typ)

parse_opt_glyph(ctx: ParsingContext) -> Located[bool]

Source code in xdsl/frontend/listlang/main.py
def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
    return parse_opt_punct(ctx, PLUS)

build(builder: Builder, lhs: Located[TypedExpression], rhs: Located[TypedExpression]) -> TypedExpression

Source code in xdsl/frontend/listlang/main.py
def build(
    self,
    builder: Builder,
    lhs: Located[TypedExpression],
    rhs: Located[TypedExpression],
) -> TypedExpression:
    if not isinstance(lhs.value.typ, ListLangInt):
        raise ParseError(
            lhs.loc.pos,
            f"expected expression of type {ListLangInt()} "
            f"in addition, got {lhs.value.typ}",
        )

    if not isinstance(rhs.value.typ, ListLangInt):
        raise ParseError(
            rhs.loc.pos,
            f"expected expression of type {ListLangInt()} "
            f"in addition, got {rhs.value.typ}",
        )

    add_op = builder.insert(arith.AddiOp(lhs.value.value, rhs.value.value))
    add_op.result.name_hint = compose_name_hints(
        lhs.value.value, "plus", rhs.value.value
    )
    return TypedExpression(add_op.result, lhs.value.typ)

ListRange

Bases: BinaryOp

Source code in xdsl/frontend/listlang/main.py
class ListRange(BinaryOp):
    def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
        return parse_opt_punct(ctx, RANGE)

    def build(
        self,
        builder: Builder,
        lhs: Located[TypedExpression],
        rhs: Located[TypedExpression],
    ) -> TypedExpression:
        lower = lhs
        upper = rhs

        if not isinstance(lower.value.typ, ListLangInt):
            raise ParseError(
                lower.loc.pos,
                f"expected {ListLangInt()} type for range lower bound, "
                f"got {lower.value.typ}",
            )

        if not isinstance(upper.value.typ, ListLangInt):
            raise ParseError(
                upper.loc.pos,
                f"expected {ListLangInt()} type for range upper bound, "
                f"got {upper.value.typ}",
            )

        list_type = ListLangList(ListLangInt())
        list_range = builder.insert(
            list_dialect.RangeOp(
                lower.value.value,
                upper.value.value,
                list_type.xdsl(),
            )
        )
        list_range.result.name_hint = "_int_list"
        return TypedExpression(list_range.result, list_type)

parse_opt_glyph(ctx: ParsingContext) -> Located[bool]

Source code in xdsl/frontend/listlang/main.py
def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
    return parse_opt_punct(ctx, RANGE)

build(builder: Builder, lhs: Located[TypedExpression], rhs: Located[TypedExpression]) -> TypedExpression

Source code in xdsl/frontend/listlang/main.py
def build(
    self,
    builder: Builder,
    lhs: Located[TypedExpression],
    rhs: Located[TypedExpression],
) -> TypedExpression:
    lower = lhs
    upper = rhs

    if not isinstance(lower.value.typ, ListLangInt):
        raise ParseError(
            lower.loc.pos,
            f"expected {ListLangInt()} type for range lower bound, "
            f"got {lower.value.typ}",
        )

    if not isinstance(upper.value.typ, ListLangInt):
        raise ParseError(
            upper.loc.pos,
            f"expected {ListLangInt()} type for range upper bound, "
            f"got {upper.value.typ}",
        )

    list_type = ListLangList(ListLangInt())
    list_range = builder.insert(
        list_dialect.RangeOp(
            lower.value.value,
            upper.value.value,
            list_type.xdsl(),
        )
    )
    list_range.result.name_hint = "_int_list"
    return TypedExpression(list_range.result, list_type)

Comparator dataclass

Bases: BinaryOp

Source code in xdsl/frontend/listlang/main.py
@dataclass
class Comparator(BinaryOp):
    glyph: Punctuation
    arith_code: str

    def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
        return parse_opt_punct(ctx, self.glyph)

    def build(
        self,
        builder: Builder,
        lhs: Located[TypedExpression],
        rhs: Located[TypedExpression],
    ) -> TypedExpression:
        if not isinstance(lhs.value.typ, ListLangInt):
            raise ParseError(
                lhs.loc.pos,
                f"expected expression of type {ListLangInt()} "
                f"in comparison, got {lhs.value.typ}",
            )

        if not isinstance(rhs.value.typ, ListLangInt):
            raise ParseError(
                rhs.loc.pos,
                f"expected expression of type {ListLangInt()} "
                f"in comparison, got {rhs.value.typ}",
            )

        cmpi_op = builder.insert(
            arith.CmpiOp(lhs.value.value, rhs.value.value, self.arith_code)
        )
        cmpi_op.result.name_hint = compose_name_hints(
            lhs.value.value, self.arith_code, rhs.value.value
        )
        return TypedExpression(cmpi_op.result, ListLangBool())

glyph: Punctuation instance-attribute

arith_code: str instance-attribute

__init__(glyph: Punctuation, arith_code: str) -> None

parse_opt_glyph(ctx: ParsingContext) -> Located[bool]

Source code in xdsl/frontend/listlang/main.py
def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
    return parse_opt_punct(ctx, self.glyph)

build(builder: Builder, lhs: Located[TypedExpression], rhs: Located[TypedExpression]) -> TypedExpression

Source code in xdsl/frontend/listlang/main.py
def build(
    self,
    builder: Builder,
    lhs: Located[TypedExpression],
    rhs: Located[TypedExpression],
) -> TypedExpression:
    if not isinstance(lhs.value.typ, ListLangInt):
        raise ParseError(
            lhs.loc.pos,
            f"expected expression of type {ListLangInt()} "
            f"in comparison, got {lhs.value.typ}",
        )

    if not isinstance(rhs.value.typ, ListLangInt):
        raise ParseError(
            rhs.loc.pos,
            f"expected expression of type {ListLangInt()} "
            f"in comparison, got {rhs.value.typ}",
        )

    cmpi_op = builder.insert(
        arith.CmpiOp(lhs.value.value, rhs.value.value, self.arith_code)
    )
    cmpi_op.result.name_hint = compose_name_hints(
        lhs.value.value, self.arith_code, rhs.value.value
    )
    return TypedExpression(cmpi_op.result, ListLangBool())

BoolAnd

Bases: BinaryOp

Source code in xdsl/frontend/listlang/main.py
class BoolAnd(BinaryOp):
    def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
        return parse_opt_punct(ctx, BOOL_AND)

    def build(
        self,
        builder: Builder,
        lhs: Located[TypedExpression],
        rhs: Located[TypedExpression],
    ) -> TypedExpression:
        if not isinstance(lhs.value.typ, ListLangBool):
            raise ParseError(
                lhs.loc.pos,
                f"expected expression of type {ListLangBool()} "
                f"in boolean and, got {lhs.value.typ}",
            )

        if not isinstance(rhs.value.typ, ListLangBool):
            raise ParseError(
                rhs.loc.pos,
                f"expected expression of type {ListLangBool()} "
                f"in boolean and, got {rhs.value.typ}",
            )

        and_op = builder.insert(arith.AndIOp(lhs.value.value, rhs.value.value))
        and_op.result.name_hint = compose_name_hints(
            lhs.value.value, "and", rhs.value.value
        )
        return TypedExpression(and_op.result, lhs.value.typ)

parse_opt_glyph(ctx: ParsingContext) -> Located[bool]

Source code in xdsl/frontend/listlang/main.py
def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
    return parse_opt_punct(ctx, BOOL_AND)

build(builder: Builder, lhs: Located[TypedExpression], rhs: Located[TypedExpression]) -> TypedExpression

Source code in xdsl/frontend/listlang/main.py
def build(
    self,
    builder: Builder,
    lhs: Located[TypedExpression],
    rhs: Located[TypedExpression],
) -> TypedExpression:
    if not isinstance(lhs.value.typ, ListLangBool):
        raise ParseError(
            lhs.loc.pos,
            f"expected expression of type {ListLangBool()} "
            f"in boolean and, got {lhs.value.typ}",
        )

    if not isinstance(rhs.value.typ, ListLangBool):
        raise ParseError(
            rhs.loc.pos,
            f"expected expression of type {ListLangBool()} "
            f"in boolean and, got {rhs.value.typ}",
        )

    and_op = builder.insert(arith.AndIOp(lhs.value.value, rhs.value.value))
    and_op.result.name_hint = compose_name_hints(
        lhs.value.value, "and", rhs.value.value
    )
    return TypedExpression(and_op.result, lhs.value.typ)

BoolOr

Bases: BinaryOp

Source code in xdsl/frontend/listlang/main.py
class BoolOr(BinaryOp):
    def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
        return parse_opt_punct(ctx, BOOL_OR)

    def build(
        self,
        builder: Builder,
        lhs: Located[TypedExpression],
        rhs: Located[TypedExpression],
    ) -> TypedExpression:
        if not isinstance(lhs.value.typ, ListLangBool):
            raise ParseError(
                lhs.loc.pos,
                f"expected expression of type {ListLangBool()} "
                f"in boolean or, got {lhs.value.typ}",
            )

        if not isinstance(rhs.value.typ, ListLangBool):
            raise ParseError(
                rhs.loc.pos,
                f"expected expression of type {ListLangBool()} "
                f"in boolean or, got {rhs.value.typ}",
            )

        or_op = builder.insert(arith.OrIOp(lhs.value.value, rhs.value.value))
        or_op.result.name_hint = compose_name_hints(
            lhs.value.value, "or", rhs.value.value
        )
        return TypedExpression(or_op.result, lhs.value.typ)

parse_opt_glyph(ctx: ParsingContext) -> Located[bool]

Source code in xdsl/frontend/listlang/main.py
def parse_opt_glyph(self, ctx: ParsingContext) -> Located[bool]:
    return parse_opt_punct(ctx, BOOL_OR)

build(builder: Builder, lhs: Located[TypedExpression], rhs: Located[TypedExpression]) -> TypedExpression

Source code in xdsl/frontend/listlang/main.py
def build(
    self,
    builder: Builder,
    lhs: Located[TypedExpression],
    rhs: Located[TypedExpression],
) -> TypedExpression:
    if not isinstance(lhs.value.typ, ListLangBool):
        raise ParseError(
            lhs.loc.pos,
            f"expected expression of type {ListLangBool()} "
            f"in boolean or, got {lhs.value.typ}",
        )

    if not isinstance(rhs.value.typ, ListLangBool):
        raise ParseError(
            rhs.loc.pos,
            f"expected expression of type {ListLangBool()} "
            f"in boolean or, got {rhs.value.typ}",
        )

    or_op = builder.insert(arith.OrIOp(lhs.value.value, rhs.value.value))
    or_op.result.name_hint = compose_name_hints(
        lhs.value.value, "or", rhs.value.value
    )
    return TypedExpression(or_op.result, lhs.value.typ)

parse_opt_punct(ctx: ParsingContext, punct: Punctuation) -> Located[bool]

Returns True if the punctuation was successfully parsed.

Source code in xdsl/frontend/listlang/main.py
def parse_opt_punct(ctx: ParsingContext, punct: Punctuation) -> Located[bool]:
    """
    Returns True if the punctuation was successfully parsed.
    """
    matched = ctx.cursor.next_regex(punct.rg)
    return Located(matched.loc, matched.value is not None)

parse_punct(ctx: ParsingContext, punct: Punctuation) -> Location

Source code in xdsl/frontend/listlang/main.py
def parse_punct(ctx: ParsingContext, punct: Punctuation) -> Location:
    if not (located := parse_opt_punct(ctx, punct)):
        raise ctx.error(f"expected {punct.name}")
    return located.loc

parse_opt_identifier(ctx: ParsingContext) -> Located[str | None]

Source code in xdsl/frontend/listlang/main.py
def parse_opt_identifier(ctx: ParsingContext) -> Located[str | None]:
    matched = ctx.cursor.next_regex(IDENT)
    return Located(
        matched.loc,
        matched.value.group() if matched.value is not None else None,
    )

parse_identifier(ctx: ParsingContext) -> Located[str]

Source code in xdsl/frontend/listlang/main.py
def parse_identifier(ctx: ParsingContext) -> Located[str]:
    if (ident := parse_opt_identifier(ctx)).value is None:
        raise ctx.error("expected variable identifier")
    return Located(ident.loc, ident.value)

parse_opt_integer(ctx: ParsingContext) -> Located[int | None]

Source code in xdsl/frontend/listlang/main.py
def parse_opt_integer(ctx: ParsingContext) -> Located[int | None]:
    matched = ctx.cursor.next_regex(INTEGER)
    return Located(
        matched.loc,
        int(matched.value.group()) if matched.value is not None else None,
    )

parse_integer(ctx: ParsingContext) -> Located[int]

Source code in xdsl/frontend/listlang/main.py
def parse_integer(ctx: ParsingContext) -> Located[int]:
    if (lit := parse_opt_integer(ctx)).value is None:
        raise ctx.error("expected integer constant")
    return Located(lit.loc, lit.value)

name_hint_without_prefix(value: SSAValue) -> str

Get the name hint of a value, removing a possible underscore prefix if needed.

Source code in xdsl/frontend/listlang/main.py
def name_hint_without_prefix(value: SSAValue) -> str:
    """
    Get the name hint of a value, removing a possible underscore
    prefix if needed.
    """
    name = value.name_hint
    assert name is not None
    if name.startswith("_"):
        return name[1:]
    return name

compose_name_hints(*names: SSAValue | str) -> str

Create a new name hint by composing multiple strings and name hints. The new name hint starts with an underscore, as it is a temporary.

Source code in xdsl/frontend/listlang/main.py
def compose_name_hints(*names: SSAValue | str) -> str:
    """
    Create a new name hint by composing multiple strings and name hints.
    The new name hint starts with an underscore, as it is a temporary.
    """
    return "_" + "_".join(
        name if isinstance(name, str) else name_hint_without_prefix(name)
        for name in names
    )

parse_comma_separated(ctx: ParsingContext, p: Callable[[], Located[T | None]]) -> Sequence[Located[T]]

Source code in xdsl/frontend/listlang/main.py
def parse_comma_separated(
    ctx: ParsingContext, p: Callable[[], Located[T | None]]
) -> Sequence[Located[T]]:
    result: list[Located[T]] = []
    while (res := p()).value is not None:
        result.append(Located(res.loc, res.value))
        if not parse_opt_punct(ctx, COMMA).value:
            break
    return result

parse_opt_expr(ctx: ParsingContext, builder: Builder) -> Located[TypedExpression | None]

Source code in xdsl/frontend/listlang/main.py
def parse_opt_expr(
    ctx: ParsingContext, builder: Builder
) -> Located[TypedExpression | None]:
    def priority_level_parser(level: int) -> Located[TypedExpression | None]:
        if level == 0:
            return _parse_opt_expr_atom_with_methods(ctx, builder)

        if (lhs := priority_level_parser(level - 1)).value is None:
            return lhs

        lhs = Located(lhs.loc, lhs.value)

        def parse_next_operator_glyph() -> BinaryOp | None:
            operators = PARSE_BINOP_PRIORITY[level - 1]
            return next((op for op in operators if op.parse_opt_glyph(ctx)), None)

        while (selected_op := parse_next_operator_glyph()) is not None:
            if (rhs := priority_level_parser(level - 1)).value is None:
                raise ParseError(rhs.loc.pos, "expected expression")

            expr = selected_op.build(
                builder,
                Located(lhs.loc, lhs.value),
                Located(rhs.loc, rhs.value),
            )
            lhs = Located(lhs.loc, expr)

        return cast(Located[TypedExpression | None], lhs)

    return priority_level_parser(len(PARSE_BINOP_PRIORITY))

parse_expr(ctx: ParsingContext, builder: Builder) -> Located[TypedExpression]

Source code in xdsl/frontend/listlang/main.py
def parse_expr(ctx: ParsingContext, builder: Builder) -> Located[TypedExpression]:
    if (expr := parse_opt_expr(ctx, builder)).value is None:
        raise ParseError(expr.loc.pos, "expected expression")
    return Located(expr.loc, expr.value)

parse_opt_let_statement(ctx: ParsingContext, builder: Builder) -> Located[bool]

Parses a let statement and adds its binding to the provided context if it is there. Returns True if a binding was found, False otherwise.

Source code in xdsl/frontend/listlang/main.py
def parse_opt_let_statement(ctx: ParsingContext, builder: Builder) -> Located[bool]:
    """
    Parses a let statement and adds its binding to the provided context if it
    is there. Returns True if a binding was found, False otherwise.
    """

    if not (let := parse_opt_punct(ctx, LET)):
        return let

    binding_name = parse_identifier(ctx)

    if binding_name.value in RESERVED_KEYWORDS:
        raise ParseError.from_loc(
            binding_name.loc, f"'{binding_name.value}' is a reserved keyword"
        )

    parse_punct(ctx, EQUAL)

    expr = parse_expr(ctx, builder)

    parse_punct(ctx, SEMICOLON)

    expr.value.value.name_hint = binding_name.value

    ctx.bindings[binding_name.value] = Binding(expr.value.value, expr.value.typ)

    return let

parse_opt_statement(ctx: ParsingContext, builder: Builder) -> Located[bool]

Source code in xdsl/frontend/listlang/main.py
def parse_opt_statement(ctx: ParsingContext, builder: Builder) -> Located[bool]:
    return parse_opt_let_statement(ctx, builder)

parse_block_content(ctx: ParsingContext, builder: Builder) -> Located[Located[TypedExpression | None]]

Parses the content of a block and returns its trailing expression, if there is one. The first location is the start of the block content, while the second location is the start of where the trailing expression is or would be.

Source code in xdsl/frontend/listlang/main.py
def parse_block_content(
    ctx: ParsingContext, builder: Builder
) -> Located[Located[TypedExpression | None]]:
    """
    Parses the content of a block and returns its trailing expression, if there
    is one. The first location is the start of the block content, while the
    second location is the start of where the trailing expression is or would
    be.
    """

    ctx.cursor.skip_whitespaces()
    start_loc = Location(ctx.cursor.pos)

    while parse_opt_statement(ctx, builder).value:
        pass

    return Located(start_loc, parse_opt_expr(ctx, builder))

parse_opt_block(ctx: ParsingContext, builder: Builder) -> Located[Located[TypedExpression | None] | None]

Parses a block and returns its trailing expression, if there is one. The first location is the start of the block, while the second location is the start of where the trailing expression is or would be.

The scope of bindings within the block is contained, meaning a new scope level is added to the binding dictionary when parsing the block.

Source code in xdsl/frontend/listlang/main.py
def parse_opt_block(
    ctx: ParsingContext,
    builder: Builder,
) -> Located[Located[TypedExpression | None] | None]:
    """
    Parses a block and returns its trailing expression, if there is one. The
    first location is the start of the block, while the second location
    is the start of where the trailing expression is or would be.

    The scope of bindings within the block is contained, meaning a new scope
    level is added to the binding dictionary when parsing the block.
    """

    if not (lcurl := parse_opt_punct(ctx, LCURL)).value:
        return Located(lcurl.loc, None)
    ctx.bindings = ScopedDict(ctx.bindings)
    res = parse_block_content(ctx, builder)
    parse_punct(ctx, RCURL)
    assert ctx.bindings.parent is not None
    ctx.bindings = ctx.bindings.parent

    return Located(lcurl.loc, res.value)

parse_block(ctx: ParsingContext, builder: Builder) -> Located[Located[TypedExpression | None]]

Parses a block and returns its trailing expression, if there is one. The first location is the start of the block, while the second location is the start of where the trailing expression is or would be.

The scope of bindings within the block is contained, meaning a new scope level is added to the binding dictionary when parsing the block.

Source code in xdsl/frontend/listlang/main.py
def parse_block(
    ctx: ParsingContext,
    builder: Builder,
) -> Located[Located[TypedExpression | None]]:
    """
    Parses a block and returns its trailing expression, if there is one. The
    first location is the start of the block, while the second location
    is the start of where the trailing expression is or would be.

    The scope of bindings within the block is contained, meaning a new scope
    level is added to the binding dictionary when parsing the block.
    """

    if (block := parse_opt_block(ctx, builder)).value is None:
        raise ParseError(block.loc.pos, "expected block")
    return Located(block.loc, block.value)

parse_program(code: str, builder: Builder)

Parses a program. Builds the operations associated with the program, and prints the final expression.

Source code in xdsl/frontend/listlang/main.py
def parse_program(code: str, builder: Builder):
    """
    Parses a program.
    Builds the operations associated with the program, and prints the
    final expression.
    """
    expr = parse_block_content(ParsingContext(code), builder).value.value
    if expr is None:
        return
    expr.typ.print(builder, expr.value)

program_to_mlir_module(code: str) -> builtin.ModuleOp

Source code in xdsl/frontend/listlang/main.py
def program_to_mlir_module(code: str) -> builtin.ModuleOp:
    module = builtin.ModuleOp([])
    builder = Builder(InsertPoint.at_start(module.body.block))

    parse_program(code, builder)

    return module

program_to_mlir_string(code: str)

Source code in xdsl/frontend/listlang/main.py
def program_to_mlir_string(code: str):
    output = io.StringIO()
    Printer(stream=output).print_op(program_to_mlir_module(code))
    return output.getvalue()

lower_down_to(target: str | None)

Source code in xdsl/frontend/listlang/main.py
def lower_down_to(target: str | None):
    if target is None:
        return
    lowerings.LowerListToTensor().apply(ctx, module)
    module.verify()
    if target == "tensor":
        return
    lowerings.WrapModuleInFunc().apply(ctx, module)
    module.verify()
    if target == "interp":
        return
    printf_to_llvm.PrintfToLLVM().apply(ctx, module)
    module.verify()
    if target == "mlir":
        return