@@ -260,6 +260,101 @@ def __str__(self) -> str:
260260 return f'| { patterns } => { self .rhs } '
261261
262262
263+ @final
264+ @dataclass (frozen = True )
265+ class Structure (Declaration ):
266+ ident : DeclId
267+ signature : Signature | None
268+ extends : tuple [Term , ...]
269+ ctor : StructCtor | None
270+ deriving : tuple [str , ...]
271+ modifiers : Modifiers | None
272+
273+ def __init__ (
274+ self ,
275+ ident : str | DeclId ,
276+ signature : Signature | None = None ,
277+ extends : Iterable [Term ] | None = None ,
278+ ctor : StructCtor | None = None ,
279+ deriving : Iterable [str ] | None = None ,
280+ modifiers : Modifiers | None = None ,
281+ ):
282+ ident = DeclId (ident ) if isinstance (ident , str ) else ident
283+ extends = tuple (extends ) if extends is not None else ()
284+ deriving = tuple (deriving ) if deriving is not None else ()
285+ object .__setattr__ (self , 'ident' , ident )
286+ object .__setattr__ (self , 'signature' , signature )
287+ object .__setattr__ (self , 'extends' , extends )
288+ object .__setattr__ (self , 'ctor' , ctor )
289+ object .__setattr__ (self , 'deriving' , deriving )
290+ object .__setattr__ (self , 'modifiers' , modifiers )
291+
292+ def __str__ (self ) -> str :
293+ lines = []
294+
295+ modifiers = f'{ self .modifiers } ' if self .modifiers else ''
296+ binders = (
297+ ' ' .join (str (binder ) for binder in self .signature .binders )
298+ if self .signature and self .signature .binders
299+ else ''
300+ )
301+ binders = f' { binders } ' if binders else ''
302+ extends = ', ' .join (str (extend ) for extend in self .extends )
303+ extends = f' extends { extends } ' if extends else ''
304+ ty = f' : { self .signature .ty } ' if self .signature and self .signature .ty else ''
305+ where = ' where' if self .ctor else ''
306+ lines .append (f'{ modifiers } structure { self .ident } { binders } { extends } { ty } { where } ' )
307+
308+ if self .deriving :
309+ lines .append (f' deriving { self .deriving } ' )
310+
311+ if self .ctor :
312+ lines .extend (f' { line } ' for line in str (self .ctor ).splitlines ())
313+
314+ return '\n ' .join (lines )
315+
316+
317+ @final
318+ @dataclass (frozen = True )
319+ class StructCtor :
320+ fields : tuple [Binder , ...] # TODO implement StructField
321+ ident : StructIdent | None
322+
323+ def __init__ (
324+ self ,
325+ fields : Iterable [Binder ],
326+ ident : str | StructIdent | None = None ,
327+ ):
328+ fields = tuple (fields )
329+ ident = StructIdent (ident ) if isinstance (ident , str ) else ident
330+ object .__setattr__ (self , 'fields' , fields )
331+ object .__setattr__ (self , 'ident' , ident )
332+
333+ def __str__ (self ) -> str :
334+ lines = []
335+ if self .ident :
336+ lines .append (f'{ self .ident } ::' )
337+ for field in self .fields :
338+ if isinstance (field , ExplBinder ) and len (field .idents ) == 1 :
339+ (ident ,) = field .idents
340+ ty = '' if field .ty is None else f' : { field .ty } '
341+ lines .append (f'{ ident } { ty } ' )
342+ else :
343+ lines .append (str (field ))
344+ return '\n ' .join (lines )
345+
346+
347+ @final
348+ @dataclass (frozen = True )
349+ class StructIdent :
350+ ident : str
351+ modifiers : Modifiers | None = None
352+
353+ def __str__ (self ) -> str :
354+ modifiers = f'{ self .modifiers } ' if self .modifiers else ''
355+ return f'{ modifiers } { self .ident } '
356+
357+
263358@final
264359@dataclass (frozen = True )
265360class DeclId :
0 commit comments