Skip to content

Commit

Permalink
add type decl parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
superlopuh committed Dec 7, 2024
1 parent 4476110 commit aded9a7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
27 changes: 27 additions & 0 deletions asl_xdsl/frontend/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,30 @@ def parse_ast_tail(parser: BaseParser) -> T_Exception:
def parse_ast(parser: BaseParser) -> T_Exception:
parser.parse_characters(T_Exception.__name__)
return T_Exception.parse_ast_tail(parser)


class D_TypeDecl(NamedTuple):
id: str
ty: Ty
fields: tuple[str, tuple[Field, ...]] | None

@staticmethod
def parse_optional_field(
parser: BaseParser,
) -> tuple[str, tuple[Field, ...]] | None:
if parser.parse_characters("None"):
return None
else:
raise NotImplementedError()

@staticmethod
def parse_ast(parser: BaseParser) -> D_TypeDecl:
parser.parse_characters(D_TypeDecl.__name__)
parser.parse_punctuation("(")
id = parser.parse_str_literal()
parser.parse_punctuation(",")
ty = Ty.parse_ast(parser)
parser.parse_punctuation(",")
fields = D_TypeDecl.parse_optional_field(parser)
parser.parse_punctuation(")")
return D_TypeDecl(id, ty, fields)
9 changes: 8 additions & 1 deletion tests/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from xdsl.parser import BaseParser, ParserState
from xdsl.utils.lexer import Input, Lexer

from asl_xdsl.frontend.ast import T_Exception, Ty
from asl_xdsl.frontend.ast import D_TypeDecl, T_Exception, Ty


def p(input: str) -> BaseParser:
Expand Down Expand Up @@ -30,3 +30,10 @@ def test_parse_exception():
def test_parse_type():
parser = p("annot (T_Exception [])")
assert Ty.parse_ast(parser) == Ty(T_Exception(()))


def test_type_decl():
parser = p('D_TypeDecl ("except", annot (T_Exception []), None)')
assert D_TypeDecl.parse_ast(parser) == D_TypeDecl(
"except", Ty(T_Exception(())), None
)

0 comments on commit aded9a7

Please sign in to comment.