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
33
34
35
36
@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
73
74
75
76
@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
79
80
81
82
83
84
85
86
87
88
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
83
84
85
def __init__(self, code: str):
    self.cursor = CodeCursor(code)
    self.bindings = ScopedDict()

error(msg: str) -> ParseError

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

BinaryOp

Source code in xdsl/frontend/listlang/main.py
377
378
379
380
381
382
383
384
385
386
387
388
389
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
378
379
380
381
382
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
384
385
386
387
388
389
def build(
    self,
    builder: Builder,
    lhs: Located[TypedExpression],
    rhs: Located[TypedExpression],
) -> TypedExpression: ...

Multiplication

Bases: BinaryOp

Source code in xdsl/frontend/listlang/main.py
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
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_op(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
393
394
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
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_op(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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
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_op(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
424
425
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
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_op(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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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
455
456
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
@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_op(
            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
498
499
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
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_op(
        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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
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_op(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
531
532
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
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_op(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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
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_op(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
562
563
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
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_op(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
94
95
96
97
98
99
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
102
103
104
105
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
108
109
110
111
112
113
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
116
117
118
119
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
122
123
124
125
126
127
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
130
131
132
133
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
136
137
138
139
140
141
142
143
144
145
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
148
149
150
151
152
153
154
155
156
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
164
165
166
167
168
169
170
171
172
def parse_comma_separated(  # noqa: UP047
    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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
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
646
647
648
649
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
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
684
685
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
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
755
756
757
758
759
760
761
762
763
764
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
767
768
769
770
771
772
773
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
776
777
778
779
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
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