0 | module Go.AST
  1 |
  2 | import Data.List
  3 | import Data.List.Quantifiers
  4 | import Data.List1
  5 | import Go.Token
  6 |
  7 | %default total
  8 |
  9 | export
 10 | interface Expression a where
 11 |
 12 | export
 13 | interface Statement a where
 14 |
 15 | export
 16 | interface Specification a where
 17 |
 18 | export
 19 | interface Declaration a where
 20 |
 21 | export
 22 | interface GoType a where
 23 |
 24 | public export
 25 | record Comment where
 26 |   constructor MkComment
 27 |   text : String
 28 |
 29 | public export
 30 | record CommentGroup where
 31 |   constructor MkCommentGroup
 32 |   comments : List1 Comment
 33 |
 34 | record Identifier
 35 | record BasicLiteral
 36 |
 37 | public export
 38 | record Field t {auto 0 tt : GoType t} where
 39 |   constructor MkField
 40 |   doc : Maybe CommentGroup
 41 |   names : List Identifier
 42 |   type : Maybe t
 43 |   tag : Maybe BasicLiteral
 44 |   comment : Maybe CommentGroup
 45 |
 46 | public export
 47 | data FieldList : (ts : List Type) -> Type where
 48 |   Nil : FieldList []
 49 |   (::) : {auto tt : GoType t} -> Field t -> FieldList ts -> FieldList (t::ts)
 50 |
 51 | public export
 52 | record TypeIdentifier where
 53 |   constructor MkTypeIdentifier
 54 |   package : Maybe Identifier
 55 |   name : Identifier
 56 |
 57 | export
 58 | implementation GoType TypeIdentifier where
 59 |
 60 | public export
 61 | record ArrayType l e where
 62 |   constructor MkArrayType
 63 |   length : Maybe l
 64 |   element : e
 65 |
 66 | public export
 67 | implementation Expression l => GoType e => GoType (ArrayType l e) where
 68 |
 69 | public export
 70 | record StructType ts where
 71 |   constructor MkStructType
 72 |   fields : FieldList ts
 73 |
 74 | public export
 75 | implementation GoType (StructType ts) where
 76 |
 77 | public export
 78 | record FunctionType ts ps rs where
 79 |   constructor MkFunctionType
 80 |   typeParams : FieldList ts
 81 |   params : FieldList ps
 82 |   results : FieldList rs
 83 |
 84 | public export
 85 | implementation GoType (FunctionType ts ps rs) where
 86 |
 87 | public export
 88 | record InterfaceType ts where
 89 |   constructor MkInterfaceType
 90 |   methods : FieldList ts
 91 |
 92 | public export
 93 | implementation GoType (InterfaceType ts) where
 94 |
 95 | public export
 96 | record MapType k v where
 97 |   constructor MkMapType
 98 |   key : k
 99 |   value : v
100 |
101 | public export
102 | implementation GoType k => GoType v => GoType (MapType k v) where
103 |
104 | public export
105 | data ChanDirection
106 |   = Send
107 |   | Receive
108 |   | Both
109 |
110 | public export
111 | record ChanType e where
112 |   constructor MkChanType
113 |   direction : ChanDirection
114 |   value : e
115 |
116 | public export
117 | implementation GoType e => GoType (ChanType e) where
118 |
119 | public export
120 | record BadStatement where
121 |   constructor MkBadStatement
122 |
123 | public export
124 | implementation Statement BadStatement where
125 |
126 | public export
127 | record DeclarationStatement t where
128 |   constructor MkDeclarationStatement
129 |   declaration : t
130 |
131 | public export
132 | implementation Declaration t => Statement (DeclarationStatement t) where
133 |
134 | public export
135 | record EmptyStatement where
136 |   constructor MkEmptyStatement
137 |   isImplicit : Bool
138 |
139 | public export
140 | implementation Statement EmptyStatement where
141 |
142 | public export
143 | record LabeledStatement t where
144 |   constructor MkLabeledStatement
145 |   label : Identifier
146 |   statement : t
147 |
148 | public export
149 | implementation Statement t => Statement (LabeledStatement t) where
150 |
151 | public export
152 | record ExpressionStatement e where
153 |   constructor MkExpressionStatement
154 |   doc : Maybe CommentGroup
155 |   expression : e
156 |   comment : Maybe CommentGroup
157 |
158 | public export
159 | implementation Expression e => Statement (ExpressionStatement e) where
160 |
161 | public export
162 | record SendStatement c v where
163 |   constructor MkSendStatement
164 |   chan : c
165 |   value : v
166 |
167 | public export
168 | implementation Expression c => Expression v => Expression (SendStatement c v) where
169 |
170 | public export
171 | data IncOrDec : Operator -> Type where
172 |   Inc : IncOrDec MkInc
173 |   Dec : IncOrDec MkDec
174 |
175 | public export
176 | implementation Show (IncOrDec MkInc) where
177 |   show _ = show MkInc
178 |
179 | public export
180 | implementation Show (IncOrDec MkDec) where
181 |   show _ = show MkDec
182 |
183 | public export
184 | record IncDecStatement e o where
185 |   constructor MkIncDecStatement
186 |   expression : e
187 |   token : IncOrDec o
188 |
189 | public export
190 | implementation Expression e => Statement (IncDecStatement e o) where
191 |
192 | public export
193 | record AssignmentStatement ls rs where
194 |   constructor MkAssignmentStatement
195 |   doc : Maybe CommentGroup
196 |   left : HList ls
197 |   token : Operator
198 |   right : HList rs
199 |   comment : Maybe CommentGroup
200 |
201 | public export
202 | implementation All Expression ls => All Expression rs => NonEmpty ls => NonEmpty rs => Statement (AssignmentStatement ls rs) where
203 |
204 | -- early definiton of Ellipsis
205 | public export
206 | record Ellipsis t where
207 |   constructor MkEllipsis
208 |   elementType : Maybe t
209 |
210 | public export
211 | implementation Expression t => Expression (Ellipsis t) where
212 |
213 | -- early definition of CallExpression
214 | public export
215 | record CallExpression f as e where
216 |   constructor MkCallExpression
217 |   function : f
218 |   args : HList as
219 |   ellipsis : Maybe $ Ellipsis e
220 |
221 | public export
222 | implementation Expression f => All Expression as => Expression e => Expression (CallExpression f as e) where
223 |
224 | public export
225 | record GoStatement f as e where
226 |   constructor MkGoStatement
227 |   call : CallExpression f as e
228 |
229 | public export
230 | implementation Expression f => All Expression as => Expression e => Statement (GoStatement f as e) where
231 |
232 | public export
233 | record DeferStatement f as e where
234 |   constructor MkDeferStatement
235 |   call : CallExpression f as e
236 |
237 |
238 | public export
239 | implementation Expression f => All Expression as => Expression e => Statement (DeferStatement f as e) where
240 |
241 | public export
242 | record ReturnStatement rs where
243 |   constructor MkReturnStatement
244 |   doc : Maybe CommentGroup
245 |   results : HList rs
246 |
247 | public export
248 | implementation All Expression rs => Statement (ReturnStatement rs) where
249 |
250 | public export
251 | data BranchStatementToken : Keyword -> Type where
252 |   IsBreak : BranchStatementToken MkBreak
253 |   IsContinue : BranchStatementToken MkContinue
254 |   IsGoto : BranchStatementToken MkBreak
255 |   IsFallthrough : BranchStatementToken MkFallthrough
256 |
257 | public export
258 | record BranchStatement (kw : Keyword) where
259 |   constructor MkBranchStatement
260 |   token : BranchStatementToken kw
261 |   label : Maybe Identifier
262 |
263 | public export
264 | implementation Statement (BranchStatement kw) where
265 |
266 | public export
267 | record BlockStatement sts where
268 |   constructor MkBlockStatement
269 |   statements : HList sts
270 |
271 | public export
272 | implementation All Statement sts => Statement (BlockStatement sts) where
273 |
274 | public export
275 | record IfStatement i c sts e where
276 |   constructor MkIfStatement
277 |   init : Maybe i
278 |   condition : c
279 |   body : BlockStatement sts
280 |   elseBranch : Maybe e
281 |
282 | public export
283 | implementation Statement i => Expression c => All Statement sts => Statement (IfStatement i c sts e) where
284 |
285 | public export
286 | record CaseClause es sts where
287 |   constructor MkCaseClause
288 |   list : HList es
289 |   body : HList sts
290 |
291 | public export
292 | implementation All Expression es => NonEmpty sts => All Statement sts => Statement (CaseClause es sts) where
293 |
294 | public export
295 | data IsCaseClause : Type -> Type where
296 |   ItIsCaseClause : All Expression es => NonEmpty sts => All Statement sts => IsCaseClause (CaseClause es sts)
297 |
298 | public export
299 | record SwitchStatement i e sts where
300 |   constructor MkSwitchStatement
301 |   init : Maybe i
302 |   tag : Maybe e
303 |   body : BlockStatement sts
304 |
305 | public export
306 | implementation Statement i => Expression e => All Statement sts => All IsCaseClause sts => Statement (SwitchStatement i e sts) where
307 |
308 | public export
309 | record TypeSwitchStatement i a sts where
310 |   constructor MkTypeSwitchStatement
311 |   init : Maybe i
312 |   assign : a
313 |   body : BlockStatement sts
314 |
315 | public export
316 | implementation Statement i => Statement a => All Statement sts => Statement (TypeSwitchStatement i a sts) where
317 |
318 | public export
319 | data IsSendStatement : Type -> Type where
320 |   ItIsSendStatement : Expression c => Expression v => IsSendStatement (SendStatement c v)
321 |
322 | public export
323 | record CommClause s sts where
324 |   constructor MkCommClause
325 |   comm : Maybe s
326 |   body : HList sts
327 |
328 | public export
329 | implementation IsSendStatement s => All Statement sts => Statement (CommClause s sts) where
330 |
331 | public export
332 | record SelectStatement sts where
333 |   constructor MkSelectStatement
334 |   body : BlockStatement sts
335 |
336 | public export
337 | implementation All Statement sts => Statement (SelectStatement sts) where
338 |
339 | public export
340 | record ForStatement i c p sts where
341 |   constructor MkForStatement
342 |   init : Maybe i
343 |   condition : Maybe c
344 |   post : Maybe p
345 |   body : BlockStatement sts
346 |
347 | public export
348 | implementation Statement i => Expression c => Statement p => All Statement sts => Statement (ForStatement i c p sts) where
349 |
350 | public export
351 | data AssignOrDefine : Operator -> Type where
352 |   ItIsAssign : AssignOrDefine MkAssign
353 |   ItIsDefine : AssignOrDefine MkDefine
354 |
355 | public export
356 | implementation Show (AssignOrDefine MkAssign) where
357 |   show _ = show MkAssign
358 |
359 | public export
360 | implementation Show (AssignOrDefine MkDefine) where
361 |   show _ = show MkDefine
362 |
363 | public export
364 | record KeyValueRangeStatement k v a e sts where
365 |   constructor MkKeyValueRangeStatement
366 |   key : k
367 |   value : v
368 |   token : AssignOrDefine a
369 |   expression : e
370 |   body : BlockStatement sts
371 |
372 | public export
373 | implementation Expression k => Expression v => AssignOrDefine a => Expression e => All Statement sts => Statement (KeyValueRangeStatement k v a e sts) where
374 |
375 | public export
376 | record ValueRangeStatement v a e sts where
377 |   constructor MkValueRangeStatement
378 |   value : v
379 |   token : AssignOrDefine a
380 |   expression : e
381 |   body : BlockStatement sts
382 |
383 | public export
384 | implementation Expression v => AssignOrDefine a => Expression e => All Statement sts => Statement (ValueRangeStatement v a e sts) where
385 |
386 | public export
387 | record RangeStatement e sts where
388 |   constructor MkRangeStatement
389 |   expression : e
390 |   body : BlockStatement sts
391 |
392 | public export
393 | implementation Expression e => All Statement sts => Statement (RangeStatement e sts) where
394 |
395 | --- Expressions
396 |
397 | public export
398 | record BadExpression where
399 |   constructor MkBadExpression
400 |
401 | public export
402 | implementation Expression BadExpression where
403 |
404 | public export
405 | record Identifier where
406 |   constructor MkIdentifier
407 |   name : String
408 |   -- object : Maybe Object
409 |
410 | public export
411 | implementation Expression Identifier where
412 |
413 | public export
414 | record BasicLiteral where
415 |   constructor MkBasicLiteral
416 |   kind : Token.Literal
417 |   value : String
418 |
419 | public export
420 | implementation Expression BasicLiteral where
421 |
422 | public export
423 | record FunctionLiteral ts ps rs sts where
424 |   constructor MkFunctionLiteral
425 |   type : FunctionType ts ps rs
426 |   body : BlockStatement sts
427 |
428 | public export
429 | implementation All Statement sts => GoType (FunctionType ts ps rs) => Expression (FunctionLiteral ts ps rs sts) where
430 |
431 | public export
432 | record CompositLiteral t es where
433 |   constructor MkCompositLiteral
434 |   type : Maybe t
435 |   expressions : HList es
436 |
437 | public export
438 | implementation GoType t => All Expression es => Expression (CompositLiteral t es) where
439 |
440 | public export
441 | record ParenExpression e where
442 |   constructor MkParenExpression
443 |   expression : e
444 |
445 | public export
446 | implementation Expression e => Expression (ParenExpression e) where
447 |
448 | public export
449 | record SelectorExpression e where
450 |   constructor MkSelectorExpression
451 |   expression : e
452 |   selector : Identifier
453 |
454 | public export
455 | implementation Expression e => Expression (SelectorExpression e) where
456 |
457 | public export
458 | record CastExpression t e where
459 |   constructor MkCastExpression
460 |   type : t
461 |   expression : e
462 |
463 | public export
464 | implementation GoType t => Expression e => Expression (CastExpression t e) where
465 |
466 | public export
467 | record MakeExpression t es where
468 |   constructor MkMakeExpression
469 |   type : t
470 |   expressions : HList es
471 |
472 | public export
473 | implementation GoType t => All Expression es => Expression (MakeExpression t es) where
474 |
475 | public export
476 | record IndexExpression e i where
477 |   constructor MkIndexExpression
478 |   expression : e
479 |   index : i
480 |
481 | public export
482 | implementation Expression e => Expression i => Expression (IndexExpression e i) where
483 |
484 | public export
485 | record IndexListExpression e is where
486 |   constructor MkIndexListExpression
487 |   expression : e
488 |   indices : HList is
489 |
490 | public export
491 | implementation Expression e => All Expression is => Expression (IndexListExpression e is) where
492 |
493 | public export
494 | record SliceExpression e l h m where
495 |   constructor MkSliceExpression
496 |   expression : e
497 |   low : Maybe l
498 |   high : Maybe h
499 |   max : Maybe m
500 |
501 | public export
502 | implementation Expression e => Expression l => Expression h => Expression m => Expression (SliceExpression e l h m) where
503 |
504 | public export
505 | record TypeAssertExpression e t where
506 |   constructor MkTypeAssertExpression
507 |   expression : e
508 |   type : t
509 |
510 | public export
511 | implementation Expression e => GoType t => Expression (TypeAssertExpression e t) where
512 |
513 | public export
514 | record StarExpression e where
515 |   constructor MkStarExpression
516 |   expression : e
517 |
518 | public export
519 | implementation Expression e => Expression (StarExpression e) where
520 |
521 | public export
522 | record UnaryExpression e where
523 |   constructor MkUnaryExpression
524 |   operator : Operator
525 |   expression : e
526 |
527 | public export
528 | implementation Expression e => Expression (UnaryExpression e) where
529 |
530 | public export
531 | record BinaryExpression x y where
532 |   constructor MkBinaryExpression
533 |   first : x
534 |   operator : Operator
535 |   last : y
536 |
537 | public export
538 | implementation Expression x => Expression y => Expression (BinaryExpression x y) where
539 |
540 | public export
541 | record KeyValueExpression k v where
542 |   constructor MkKeyValueExpression
543 |   key : k
544 |   value : v
545 |
546 | public export
547 | implementation Expression k => Expression v => Expression (KeyValueExpression k v) where
548 |
549 | --- Specifications
550 |
551 | public export
552 | record ImportSpec where
553 |   constructor MkImportSpec
554 |   doc : Maybe CommentGroup
555 |   name : Maybe Identifier
556 |   path : BasicLiteral
557 |   comment : Maybe CommentGroup
558 |
559 | public export
560 | implementation Specification ImportSpec where
561 |
562 | public export
563 | record ValueSpec t es where
564 |   constructor MkValueSpec
565 |   doc : Maybe CommentGroup
566 |   names : List1 Identifier
567 |   type : Maybe t
568 |   values : HList es
569 |   comment : Maybe CommentGroup
570 |
571 | public export
572 | implementation GoType t => All Expression es => Specification (ValueSpec t es) where
573 |
574 | public export
575 | record TypeSpec ts t where
576 |   constructor MkTypeSpec
577 |   doc : Maybe CommentGroup
578 |   name : Identifier
579 |   typeParams : FieldList ts
580 |   type : t
581 |   comment : Maybe CommentGroup
582 |
583 | public export
584 | implementation GoType t => Specification (TypeSpec ts t) where
585 |
586 | -- declarations
587 |
588 | public export
589 | record BadDeclaration where
590 |   constructor MkBadDeclaration
591 |
592 | public export
593 | implementation Declaration BadDeclaration where
594 |
595 | public export
596 | data GenericDeclarationToken : Keyword -> Type where
597 |   Import : GenericDeclarationToken MkImport
598 |   Const : GenericDeclarationToken MkConst
599 |   Type' : GenericDeclarationToken MkType
600 |   Var : GenericDeclarationToken MkVar
601 |
602 | public export
603 | implementation Show (GenericDeclarationToken MkImport) where
604 |   show _ = show MkImport
605 |
606 | public export
607 | implementation Show (GenericDeclarationToken MkConst) where
608 |   show _ = show MkConst
609 |
610 | public export
611 | implementation Show (GenericDeclarationToken MkType) where
612 |   show _ = show MkType
613 |
614 | public export
615 | implementation Show (GenericDeclarationToken MkVar) where
616 |   show _ = show MkVar
617 |
618 | public export
619 | record GenericDeclaration (t : Keyword) xs where
620 |   constructor MkGenericDeclaration
621 |   doc : Maybe CommentGroup
622 |   token : GenericDeclarationToken t
623 |   specs : HList xs
624 |
625 | public export
626 | implementation NonEmpty xs => All Specification xs => Declaration (GenericDeclaration t xs) where
627 |
628 | public export
629 | record FuncDeclaration rcs ts ps rs sts where
630 |   constructor MkFuncDeclaration
631 |   doc : Maybe CommentGroup
632 |   reciever : FieldList rcs
633 |   name : Identifier
634 |   type : FunctionType ts ps rs
635 |   body : BlockStatement sts
636 |
637 | public export
638 | implementation All Statement sts => Declaration (FuncDeclaration rcs ts ps rs sts) where
639 |
640 | public export
641 | record File ds {auto 0 dsd : All Declaration ds} where
642 |   constructor MkFile
643 |   doc : Maybe CommentGroup
644 |   name : String
645 |   packageName : Identifier
646 |   decls : HList ds
647 |   imports : List ImportSpec
648 |   unresolved : List Identifier
649 |   comments : List CommentGroup
650 |
651 | public export
652 | data BadType = MkBadType
653 |
654 | public export
655 | implementation GoType BadType where
656 |
657 |