0 | module Go.AST.Combinators
3 | import public Data.List
4 | import public Data.List1
5 | import public Data.List.Quantifiers
7 | import public Go.Token
10 | interface Commentable a where
11 | setComments : CommentGroup -> a -> a
14 | comment : Commentable a => String -> a -> a
15 | comment str = setComments $
MkCommentGroup $
singleton $
MkComment str
18 | comments : Commentable a => (cs : List String) -> {auto 0 ok : NonEmpty cs} -> a -> a
19 | comments (x::xs) = setComments $
MkCommentGroup $
map MkComment (x:::xs)
22 | implementation Statement (AssignmentStatement ls rs) => Commentable (AssignmentStatement ls rs) where
23 | setComments cg = { comment := Just cg }
26 | implementation Statement (ExpressionStatement e) => Commentable (ExpressionStatement e) where
27 | setComments cg = { comment := Just cg }
30 | implementation Specification (ValueSpec e es) => Commentable (ValueSpec e es) where
31 | setComments cg = { comment := Just cg }
34 | interface Documentable a where
35 | setDocs : CommentGroup -> a -> a
38 | doc : Documentable a => String -> a -> a
39 | doc str = setDocs $
MkCommentGroup $
singleton $
MkComment str
42 | docs : Documentable a => (ds : List String) -> {auto 0 ok : NonEmpty ds} -> a -> a
43 | docs (d::ds) = setDocs $
MkCommentGroup $
map MkComment (d:::ds)
46 | implementation Statement (AssignmentStatement ls rs) => Documentable (AssignmentStatement ls rs) where
47 | setDocs ds = { doc := Just ds }
50 | implementation Specification (ValueSpec ls rs) => Documentable (ValueSpec ls rs) where
51 | setDocs ds = { doc := Just ds }
54 | implementation Statement (ExpressionStatement e) => Documentable (ExpressionStatement e) where
55 | setDocs ds = { doc := Just ds }
58 | implementation Statement (ReturnStatement rs) => Documentable (ReturnStatement rs) where
59 | setDocs ds = { doc := Just ds }
62 | implementation Declaration (FuncDeclaration rcs ts ps rs sts) => Documentable (FuncDeclaration rcs ts ps rs sts) where
63 | setDocs ds = { doc := Just ds }
66 | data Package = MkPackage String
69 | package : String -> Package
76 | id_ name = MkIdentifier name
80 | { ds : List Type } ->
81 | { auto 0 ok : All Declaration ds } ->
87 | file name (MkPackage pkg) imports decls = MkFile Nothing name (id_ pkg) decls imports [] []
91 | hexDigit : Int -> Char
92 | hexDigit v = if v < 10 then chr $
v + ord '0'
93 | else chr $
v - 10 + ord 'a'
95 | unicodeLiteral : Char -> String
99 | high1 = (v `shiftR` 4) .&. 0x0f
100 | low2 = (v `shiftR` 8) .&. 0x0f
101 | high2 = (v `shiftR` 12) .&. 0x0f
102 | in if high2 > 0 || low2 > 0 then pack $
[ '\\', 'u', hexDigit high2, hexDigit low2, hexDigit high1, hexDigit low1 ]
103 | else pack $
[ '\\', 'x', hexDigit high1, hexDigit low1 ]
109 | runeL c = MkBasicLiteral MkChar $
escape c
111 | escape : Char -> String
112 | escape c = if (c >= ' ') && (c /= '\\')
113 | && (c /= '\'') && (c <= '~')
126 | other => unicodeLiteral other
138 | stringL str = MkBasicLiteral MkString escaped
140 | escape : Char -> String
141 | escape c = if (c >= ' ') && (c /= '\\')
142 | && (c /= '"') && (c <= '~')
155 | other => unicodeLiteral other
158 | escaped = concatMap escape $
unpack str
164 | intL i = MkBasicLiteral MkInt $
show i
170 | floatL f = MkBasicLiteral MkFloat $
show f
177 | expL f e = MkBasicLiteral MkFloat $
concat [floored, "e", show e]
180 | floored = if f == floor f then show $
the Int $
cast $
floor f
187 | imagL i = MkBasicLiteral MkImag "\{show i}i"
193 | boolL b = MkBasicLiteral MkIdentifier $
case b of
200 | All Expression es =>
203 | CompositLiteral t es
204 | compositL t es = MkCompositLiteral (Just t) es
208 | All Expression es =>
210 | CompositLiteral BadType es
211 | compositL' es = MkCompositLiteral Nothing es
215 | All Statement sts =>
219 | FunctionLiteral [] ps rs sts
220 | funcL ps rs sts = MkFunctionLiteral (MkFunctionType [] ps rs) (MkBlockStatement sts)
226 | import_ path = MkImportSpec Nothing Nothing (stringL path) Nothing
233 | importN name path = MkImportSpec Nothing (Just $
id_ name) (stringL path) Nothing
236 | void : FieldList []
245 | fields fs t = MkField Nothing (id_ <$> fs) (Just t) Nothing Nothing
251 | fields' fs = MkField Nothing (id_ <$> fs) (Maybe BadType `the` Nothing) Nothing Nothing
259 | field f t = MkField Nothing [id_ f] (Just t) Nothing Nothing
265 | field' f = MkField Nothing [id_ f] Nothing Nothing Nothing
272 | fieldT t = MkField Nothing [] (Just t) Nothing Nothing
281 | tid p n = MkTypeIdentifier (Just $
id_ p) (id_ n)
287 | tid' n = MkTypeIdentifier Nothing (id_ n)
293 | struct = MkStructType
302 | array l t = MkArrayType (Just l) t
308 | ArrayType BadExpression t
309 | array' t = MkArrayType Nothing t
318 | map_ k v = MkMapType k v
324 | FunctionType [] ps rs
325 | func' ps rs = MkFunctionType [] ps rs
328 | bool : TypeIdentifier
332 | string : TypeIdentifier
333 | string = tid' "string"
336 | int : TypeIdentifier
340 | int8 : TypeIdentifier
344 | int16 : TypeIdentifier
345 | int16 = tid' "int16"
348 | int32 : TypeIdentifier
349 | int32 = tid' "int32"
352 | int64 : TypeIdentifier
353 | int64 = tid' "int64"
356 | uint : TypeIdentifier
360 | uint8 : TypeIdentifier
361 | uint8 = tid' "uint8"
364 | uint16 : TypeIdentifier
365 | uint16 = tid' "uint16"
368 | uint32 : TypeIdentifier
369 | uint32 = tid' "uint32"
372 | uint64 : TypeIdentifier
373 | uint64 = tid' "uint64"
376 | uintptr : TypeIdentifier
377 | uintptr = tid' "uintptr"
380 | byte : TypeIdentifier
384 | rune : TypeIdentifier
388 | float32 : TypeIdentifier
389 | float32 = tid' "float32"
392 | float64 : TypeIdentifier
393 | float64 = tid' "float64"
396 | complex64 : TypeIdentifier
397 | complex64 = tid' "complex64"
400 | complex128 : TypeIdentifier
401 | complex128 = tid' "complex128"
403 | namespace Declaration
409 | { 0 sts : List Type } ->
410 | { auto 0 ok : All Statement sts } ->
412 | FuncDeclaration [] [] ps rs sts
413 | func name ps rs sts = MkFuncDeclaration Nothing [] (id_ name) (MkFunctionType [] ps rs) (MkBlockStatement sts)
418 | All Specification es =>
420 | GenericDeclaration MkType es
421 | types es = MkGenericDeclaration Nothing Type' es
430 | type name typeParams t = MkTypeSpec Nothing (id_ name) typeParams t Nothing
435 | All Specification es =>
437 | GenericDeclaration MkConst es
438 | consts es = MkGenericDeclaration Nothing Const es
443 | All Expression es =>
444 | (is : List Identifier) ->
445 | {auto 0 ok : NonEmpty is} ->
449 | const_ (i::is) t es = MkValueSpec Nothing (i:::is) (Just t) es Nothing
453 | All Expression es =>
454 | (is : List Identifier) ->
455 | {auto 0 ok : NonEmpty is} ->
457 | ValueSpec BadType es
458 | const' (i::is) es = MkValueSpec Nothing (i:::is) Nothing es Nothing
463 | All Specification es =>
465 | GenericDeclaration MkVar es
466 | vars es = MkGenericDeclaration Nothing Var es
470 | All Expression es =>
471 | (is : List Identifier) ->
472 | {auto 0 ok : NonEmpty is} ->
474 | ValueSpec BadType es
475 | var' (i::is) es = MkValueSpec Nothing (i:::is) Nothing es Nothing
480 | All Expression es =>
481 | (is : List Identifier) ->
482 | {auto 0 ok : NonEmpty is} ->
486 | var (i::is) t es = MkValueSpec Nothing (i:::is) (Just t) es Nothing
488 | namespace Statement
492 | All Statement ts =>
495 | block = MkBlockStatement
498 | expr : Expression e => e -> ExpressionStatement e
499 | expr e = MkExpressionStatement Nothing e Nothing
502 | decl : Declaration d => d -> DeclarationStatement d
503 | decl d = MkDeclarationStatement d
511 | label str s = MkLabeledStatement (id_ str) s
515 | CallExpression f as e ->
516 | DeferStatement f as e
517 | defer c = MkDeferStatement c
521 | All Expression es =>
524 | return es = MkReturnStatement Nothing es
529 | BranchStatement MkContinue
530 | continue label = MkBranchStatement IsContinue $
Just $
id_ label
533 | continue_ : BranchStatement MkContinue
534 | continue_ = MkBranchStatement IsContinue Nothing
541 | All Statement sts =>
546 | ForStatement i c p sts
547 | for_ i c p sts = MkForStatement (Just i) (Just c) (Just p) (MkBlockStatement sts)
551 | All Statement sts =>
553 | ForStatement BadStatement BadExpression BadStatement sts
554 | forever sts = MkForStatement Nothing Nothing Nothing $
MkBlockStatement sts
559 | All Statement sts =>
562 | ForStatement BadStatement c BadStatement sts
563 | while c sts = MkForStatement Nothing (Just c) Nothing $
MkBlockStatement sts
568 | All Statement sts =>
570 | (value : String) ->
573 | KeyValueRangeStatement Identifier Identifier MkDefine r sts
574 | rangeKV k v r sts = MkKeyValueRangeStatement (id_ k) (id_ v) ItIsDefine r $
MkBlockStatement sts
579 | All Statement sts =>
580 | (value : String) ->
583 | ValueRangeStatement Identifier MkDefine r sts
584 | rangeV v r sts = MkValueRangeStatement (id_ v) ItIsDefine r $
MkBlockStatement sts
589 | All Statement sts =>
592 | RangeStatement r sts
593 | range r sts = MkRangeStatement r $
MkBlockStatement sts
598 | All Statement sts =>
601 | IfStatement BadStatement c sts BadStatement
602 | if_ c sts = MkIfStatement Nothing c (MkBlockStatement sts) Nothing
608 | All Statement sts =>
612 | IfStatement i c sts BadStatement
613 | ifS i c sts = MkIfStatement (Just i) c (MkBlockStatement sts) Nothing
618 | All Statement sts =>
623 | IfStatement BadStatement c sts e
624 | ifE c sts e = MkIfStatement Nothing c (MkBlockStatement sts) (Just e)
630 | All Statement sts =>
636 | IfStatement i c sts e
637 | ifSE i c sts e = MkIfStatement (Just i) c (MkBlockStatement sts) (Just e)
643 | All Statement sts =>
644 | All IsCaseClause sts =>
648 | SwitchStatement i e sts
649 | switchS i e sts = MkSwitchStatement (Just i) (Just e) (MkBlockStatement sts)
654 | All Statement sts =>
655 | All IsCaseClause sts =>
658 | SwitchStatement BadStatement e sts
659 | switch e sts = MkSwitchStatement Nothing (Just e) (MkBlockStatement sts)
663 | All Statement sts =>
664 | All IsCaseClause sts =>
666 | SwitchStatement BadStatement BadExpression sts
667 | switch' sts = MkSwitchStatement Nothing Nothing (MkBlockStatement sts)
671 | All Expression es =>
672 | All Statement sts =>
677 | case_ es sts = MkCaseClause es sts
681 | All Statement sts =>
685 | default_ sts = MkCaseClause [] sts
692 | paren = MkParenExpression
698 | { 0 args : List Type } ->
699 | { auto 0 argsOk : All Expression args } ->
701 | CallExpression fn args BadExpression
702 | call fn args = MkCallExpression fn args Nothing
711 | cast_ = MkCastExpression
719 | TypeAssertExpression e t
720 | typeAssert e t = MkTypeAssertExpression e t
725 | All Expression es =>
728 | MakeExpression t es
729 | make t es = MkMakeExpression t es
737 | IndexExpression e i
738 | index e i = MkIndexExpression e i
750 | SliceExpression e l h m
751 | slice e l h m = MkSliceExpression e (Just l) (Just h) (Just m)
761 | SliceExpression e l h BadExpression
762 | sliceLH e l h = MkSliceExpression e (Just l) (Just h) Nothing
770 | SliceExpression e l BadExpression BadExpression
771 | sliceL e l = MkSliceExpression e (Just l) Nothing Nothing
779 | SliceExpression e BadExpression h BadExpression
780 | sliceH e h = MkSliceExpression e Nothing (Just h) Nothing
786 | SliceExpression e BadExpression BadExpression BadExpression
787 | slice' e = MkSliceExpression e Nothing Nothing Nothing
793 | IncDecStatement e MkInc
794 | inc e = MkIncDecStatement e Inc
800 | IncDecStatement e MkDec
801 | dec e = MkIncDecStatement e Dec
808 | minus' e = MkUnaryExpression MkSub e
815 | SelectorExpression e
816 | (/./) e f = MkSelectorExpression e $
id_ f
824 | KeyValueExpression e1 e2
825 | (/:/) e1 e2 = MkKeyValueExpression e1 e2
827 | export infixl 3 /./
, /:/
835 | BinaryExpression e1 e2
836 | (/==/) e1 e2 = MkBinaryExpression e1 MkEql e2
844 | BinaryExpression e1 e2
845 | (/!=/) e1 e2 = MkBinaryExpression e1 MkNotEql e2
853 | BinaryExpression e1 e2
854 | (/</) e1 e2 = MkBinaryExpression e1 MkLess e2
862 | BinaryExpression e1 e2
863 | (/<=/) e1 e2 = MkBinaryExpression e1 MkLessThanOrEqual e2
871 | BinaryExpression e1 e2
872 | (/>/) e1 e2 = MkBinaryExpression e1 MkGreater e2
880 | BinaryExpression e1 e2
881 | (/>=/) e1 e2 = MkBinaryExpression e1 MkGreaterThanOrEqual e2
883 | export infixl 7 /==/
, /!=/
, /</
, /<=/
, />/
, />=/
891 | BinaryExpression e1 e2
892 | (/+/) e1 e2 = MkBinaryExpression e1 MkAdd e2
900 | BinaryExpression e1 e2
901 | (/-/) e1 e2 = MkBinaryExpression e1 MkSub e2
909 | BinaryExpression e1 e2
910 | (/|/) e1 e2 = MkBinaryExpression e1 MkOr e2
918 | BinaryExpression e1 e2
919 | (/^/) e1 e2 = MkBinaryExpression e1 MkXor e2
921 | export infixl 8 /+/
, /-/
, /|/
, /^/
929 | BinaryExpression e1 e2
930 | (/*/) e1 e2 = MkBinaryExpression e1 MkMul e2
938 | BinaryExpression e1 e2
939 | (///) e1 e2 = MkBinaryExpression e1 MkQuo e2
947 | BinaryExpression e1 e2
948 | (/%/) e1 e2 = MkBinaryExpression e1 MkRem e2
956 | BinaryExpression e1 e2
957 | (/<</) e1 e2 = MkBinaryExpression e1 MkShl e2
965 | BinaryExpression e1 e2
966 | (/>>/) e1 e2 = MkBinaryExpression e1 MkShr e2
974 | BinaryExpression e1 e2
975 | (/&/) e1 e2 = MkBinaryExpression e1 MkAnd e2
977 | export infixl 9 /*/
, ///
, /%/
, /<</
, />>/
, /&/
981 | All Expression ls =>
982 | All Expression rs =>
987 | AssignmentStatement ls rs
988 | (/:=/) ls rs = MkAssignmentStatement Nothing ls MkDefine rs Nothing
992 | All Expression ls =>
993 | All Expression rs =>
998 | AssignmentStatement ls rs
999 | (/=/) ls rs = MkAssignmentStatement Nothing ls MkAssign rs Nothing
1009 | AssignmentStatement ls rs
1010 | (/+=/) ls rs = MkAssignmentStatement Nothing ls MkAddAssign rs Nothing
1020 | AssignmentStatement ls rs
1021 | (/-=/) ls rs = MkAssignmentStatement Nothing ls MkSubAssign rs Nothing
1023 | export infixl 7 /:=/
, /=/
, /+=/
, /-=/
1030 | ptrOf e = MkUnaryExpression MkAnd e
1037 | star e = MkStarExpression e