From 008aa6e6b7e55f08d05a47780eed965d3b65525a Mon Sep 17 00:00:00 2001 From: Andrew Segavac Date: Sun, 13 Jun 2021 10:46:41 -0600 Subject: [PATCH] fixed issue with struct literal names --- boring/parse.py | 7 +++---- boring/type_checking.py | 8 +++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/boring/parse.py b/boring/parse.py index d8df141..5f62e4b 100644 --- a/boring/parse.py +++ b/boring/parse.py @@ -69,7 +69,6 @@ class LiteralFloat: @dataclass class LiteralStruct: - name: str fields: Dict[str, "Expression"] type: TypeUsage @@ -205,7 +204,7 @@ boring_grammar = r""" literal_int : SIGNED_INT literal_struct_field : identifier ":" expression - literal_struct : identifier "{" (literal_struct_field ",")* "}" + literal_struct : data_type "{" (literal_struct_field ",")* "}" function_call : expression "(" [expression ("," expression)*] ")" @@ -324,9 +323,9 @@ class TreeToBoring(Transformer): return name, expression def literal_struct(self, literal_struct) -> LiteralStruct: - name = literal_struct[0] + data_type = literal_struct[0] fields = {key: value for (key, value) in literal_struct[1:]} - return LiteralStruct(name=name, fields=fields, type=DataTypeUsage(name=name)) + return LiteralStruct(fields=fields, type=data_type) def identifier(self, i) -> str: (i,) = i diff --git a/boring/type_checking.py b/boring/type_checking.py index 28ac63c..ed4c047 100644 --- a/boring/type_checking.py +++ b/boring/type_checking.py @@ -90,7 +90,8 @@ def assert_exists(ctx: Context, type: parse.TypeUsage): class TypeChecker: def with_module(self, ctx: Context, module: parse.Module) -> bool: for type_declaration in module.types: - ctx.environment[type_declaration.name] = type_declaration + if isinstance(type_declaration, parse.StructTypeDeclaration): + ctx.environment[type_declaration.name] = type_declaration for type_declaration in module.types: if isinstance(type_declaration, parse.StructTypeDeclaration): for name, field in type_declaration.fields.items(): @@ -361,8 +362,9 @@ class TypeChecker: def with_literal_struct( self, ctx: Context, literal_struct: parse.LiteralStruct ) -> bool: - assert literal_struct.name in ctx.environment, literal_struct.name - struct_declaration = ctx.environment[literal_struct.name] + assert isinstance(literal_struct.type, parse.DataTypeUsage) + assert literal_struct.type.name in ctx.environment, literal_struct.type.name + struct_declaration = ctx.environment[literal_struct.type.name] assert isinstance(struct_declaration, parse.StructTypeDeclaration) changed = False for name, field_type in struct_declaration.fields.items():