-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.hs
2158 lines (1936 loc) · 58.1 KB
/
parse.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Control.Monad.State
( State
, filterM
, get
, guard
, put
, runState
)
import Data.Functor.Identity (Identity)
import Data.List as L
import Data.List.Split (splitOn)
import Data.Maybe (isJust)
import Data.Monoid ((<>))
import qualified Data.Text as T
import System.Directory (doesFileExist, getDirectoryContents)
import System.Environment (getEnv)
import System.FilePath ((</>))
import System.IO (IOMode (ReadMode), openFile, hGetContents)
import Text.ParserCombinators.Parsec
( GenParser
, Parser
, ParseError
, SourcePos
, (<|>)
, (<?>)
, anyChar
, chainl1
, char
, eof
, getPosition
, lookAhead
, many
, manyTill
, noneOf
, oneOf
, optionMaybe
, parse
, sepEndBy
, setPosition
, string
, token
, try
)
import qualified Text.ParserCombinators.Parsec.Token as P
import Text.ParserCombinators.Parsec.Language (javaStyle)
main :: IO ()
main = do
cartDir <- getEnv "CARTLIFE"
files <- L.sort <$> filter isSourceFile <$> (getFiles cartDir)
mapM_ parseFile files
--let file = cartDir </> "AskOnly.asc"
--parseFile file
isSourceFile :: String -> Bool
isSourceFile s = L.isSuffixOf ".asc" s || L.isSuffixOf ".ash" s
parseFile :: String -> IO ()
parseFile filePath = do
print filePath
content <- fileContent filePath
let toks = cartScan content
let eitherAst = parse cartParse "(parser input)" =<< toks
let eitherAst2 = rewriteWith blockify <$> eitherAst
--dumpToks $ take 200 <$> toks
case eitherAst2 of
Left err -> putStrLn $ show err
Right ast2 -> writeFile (filePath <> "_b") $ renderToString $ render $ ast2
getFiles :: FilePath -> IO [FilePath]
getFiles p = filterM doesFileExist =<< getRelDirectoryContents p
getRelDirectoryContents :: FilePath -> IO [FilePath]
getRelDirectoryContents p = map (p </>) <$> getDirectoryContents p
fileContent :: FilePath -> IO String
fileContent p = do
inFile <- openFile p ReadMode
hGetContents inFile
data Decoration = DLineComment !String
| DDirective !String
| DBlankLine
| DBlockComment !String
| DEndComment !String
| DInlineComment !String
deriving (Show, Eq)
type PreToken = (SourcePos, PreTok)
data PreTok = PTDecoration !Decoration
| PTContent !String
deriving (Show)
type DecoratedString = (SourcePos, [Decoration], [Decoration], String)
type Token = (SourcePos, [Decoration], Tok)
data Tok =
TString !String
| TInteger !Integer
| TFloat !Double
| TTrue
| TFalse
| TMember
| TSemicolon
| TAssign
| TComma
| TDot
| TEq
| TNeq
| TLt
| TGt
| TLtEq
| TGtEq
| TPlus
| TMinus
| TDiv
| TMult
| TNot
| TBinAnd
| TBinOr
| TLogAnd
| TLogOr
| TPlusAssign
| TMinusAssign
| TMultAssign
| TDivAssign
| TInc
| TDec
| TLBrace
| TRBrace
| TLParen
| TRParen
| TLBracket
| TRBracket
| TIdentifier !String
| TElse
| TEnum
| TExport
| TFunction
| TIf
| TImport
| TNew
| TReturn
| TStatic
| TStruct
| TThis
| TWhile
| TEof
deriving (Eq)
instance Show Tok where
show (TString s) = "string \"" ++ show s ++ "\""
show (TInteger i) = "integer \"" ++ show i ++ "\""
show (TFloat d) = "double \"" ++ show d ++ "\""
show TTrue = "true"
show TFalse = "false"
show TMember = "::"
show TSemicolon = ";"
show TAssign = "="
show TComma = ","
show TDot = "."
show TEq = "=="
show TNeq = "!="
show TLt = "<"
show TGt = ">"
show TLtEq = "<="
show TGtEq = ">="
show TPlus = "+"
show TMinus = "-"
show TDiv = "/"
show TMult = "*"
show TNot = "!"
show TBinAnd = "&"
show TBinOr = "|"
show TLogAnd = "&&"
show TLogOr = "||"
show TPlusAssign = "+="
show TMinusAssign = "-="
show TMultAssign = "*="
show TDivAssign = "/="
show TInc = "++"
show TDec = "--"
show TLBrace = "{"
show TRBrace = "}"
show TLParen = "("
show TRParen = ")"
show TLBracket = "["
show TRBracket = "]"
show (TIdentifier s) = "identifier \"" ++ show s ++ "\""
show TElse = "else"
show TEnum = "enum"
show TExport = "export"
show TFunction = "function"
show TIf = "if"
show TImport = "import"
show TNew = "new"
show TReturn = "return"
show TStatic = "static"
show TStruct = "struct"
show TThis = "this"
show TWhile = "while"
show TEof = "EOF"
data Ast = Ast ![ATopLevel]
deriving (Show)
data ATopLevel =
AEof ![Decoration]
| AFunctionDec {
afdSignature :: !AFunctionSignature
, afdBlock :: !ABlock
}
| ATopLevelVarDec !AVarDec
| ATopLevelImportDec !AImportDec
| AEnumDec {
aedName :: !String
, aedValues :: ![String]
, aedEnumDecs :: ![Decoration]
, aedNameDecs :: ![Decoration]
, aedLBraceDecs :: ![Decoration]
, aedValueDecs :: ![[Decoration]]
, aedCommaDecs :: ![[Decoration]]
, aedRBraceDecs :: ![Decoration]
, aedSemicolonDecs :: ![Decoration]
}
| AStructDec {
asdName :: !String
, asdMembers :: ![AStructMember]
, asdStructDecs :: ![Decoration]
, asdNameDecs :: ![Decoration]
, asdLBraceDecs :: ![Decoration]
, asdRBraceDecs :: ![Decoration]
, asdSemicolonDecs :: ![Decoration]
}
| AExportDec {
axdName :: !String
, axdExportDecs :: ![Decoration]
, axdNameDecs :: ![Decoration]
, axdSemicolonDecs :: ![Decoration]
}
deriving (Show)
data AStructMember =
AStructMemberImport !AImportDec
| AStructMemberVar !AVarDec
deriving (Show)
data AImportDec =
AImportFunctionSig {
aifsFunctionSig :: !AFunctionSignature
, aifsImportDecs :: ![Decoration]
, aifsSemicolonDecs :: ![Decoration]
}
| AImportVarDec {
aivdVarDec :: !AVarDec
, aivdImportDecs :: ![Decoration]
}
deriving (Show)
data AFunctionSignature =
AFunctionSignature {
afsIsStatic :: !Bool
, afsReturnType :: !String
, afsName :: !String
, afsMember :: !(Maybe String)
, afsParams :: ![AFunctionDecParam]
, afsStaticDecs :: ![Decoration]
, afsReturnTypeDecs :: ![Decoration]
, afsNameDecs :: ![Decoration]
, afsMemberDecs :: ![Decoration]
, afsMemberNameDecs :: ![Decoration]
, afsLParenDecs :: ![Decoration]
, afsCommaDecs :: ![[Decoration]]
, afsRParenDecs :: ![Decoration]
}
deriving (Show)
data AFunctionDecParam =
AFunctionDecExtenderParam {
afdepName :: !String
, afdepThisDecs :: ![Decoration]
, afdepNameDecs :: ![Decoration]
, afdepMultDecs :: ![Decoration]
}
| AFunctionDecRegularParam {
afdrpTypeName :: !ATypeName
, afdrpVarInit :: !AVarInit
}
deriving (Show)
data ABlock =
ABlock {
abCommands :: ![ACommand]
, abLBraceDecs :: ![Decoration]
, abRBraceDecs :: ![Decoration]
}
deriving (Show)
data ACommand =
AIfCommand {
aicTestExpr :: !AExpr
, aicTrueCommand :: !ACommand
, aicFalseCommand :: !(Maybe ACommand)
, aicIfDecs :: ![Decoration]
, aicLParenDecs :: ![Decoration]
, aicRParenDecs :: ![Decoration]
}
| AElseCommand {
aecCommand :: !ACommand
, aecElseDecs :: ![Decoration]
}
| AWhileCommand {
awcTestExpr :: !AExpr
, awcCommand :: !ACommand
, awcWhileDecs :: ![Decoration]
, awcLParenDecs :: ![Decoration]
, awcRParenDecs :: ![Decoration]
}
| AReturnCommand {
arcExpr :: !(Maybe AExpr)
, arcReturnDecs :: ![Decoration]
, arcSemicolonDecs :: ![Decoration]
}
| AVarDecCommand !AVarDec
| AExprCommand {
aecExpr :: !AExpr
, aecSemicolonDecs :: ![Decoration]
}
| ABlockCommand !ABlock
deriving (Show)
data AVarDec =
AVarDec {
avdTypeName :: !ATypeName
, avdVars :: ![AVarInit]
, avdCommaDecs :: ![[Decoration]]
, avdSemicolonDecs :: ![Decoration]
}
deriving (Show)
data AVarInit =
AVarInit {
aviId :: !String
, aviSubscripts :: ![AVarSubscript]
, aviInit :: !(Maybe AExpr)
, aviIdDecs :: ![Decoration]
, aviLBracketDecs :: ![[Decoration]]
, aviRBracketDecs :: ![[Decoration]]
, aviAssignDecs :: ![Decoration]
}
deriving (Show)
data AVarSubscript =
AVarSubscriptEmpty
| AVarSubscriptId {
avsidId :: !String
, avsidIdDecs :: ![Decoration]
}
| AVarSubscriptInt {
avsiInt :: !Integer
, avsiIntDecs :: ![Decoration]
}
deriving (Show)
data AExpr =
AFalseExpr {
afeFalseDecs :: ![Decoration]
}
| ATrueExpr {
ateTrueDecs :: ![Decoration]
}
| AThisExpr {
ateThisDecs :: ![Decoration]
}
| ANewExpr {
aneTypeName :: !ATypeName
, aneSizeExpr :: !AExpr
, aneNewDecs :: ![Decoration]
, aneLBracketDecs :: ![Decoration]
, aneRBracketDecs :: ![Decoration]
}
| AFloatExpr {
afeFloat :: !Double
, afeFloatDecs :: ![Decoration]
}
| AIntExpr {
aieInt :: !Integer
, aieIntDecs :: ![Decoration]
}
| AStringExpr {
aseString :: !String
, aseStringDecs :: ![Decoration]
}
| AIdentifierExpr {
aieId :: !String
, aieIdDecs :: ![Decoration]
}
| AIndexExpr {
aieExpr :: !AExpr
, aieIndexExpr :: !AExpr
, aieLBracketDecs :: ![Decoration]
, aieRBracketDecs :: ![Decoration]
}
| ACallExpr {
aceFunctionExpr :: !AExpr
, aceParams :: ![AExpr]
, aceLParenDecs :: ![Decoration]
, aceRParenDecs :: ![Decoration]
, aceCommaDecs :: ![[Decoration]]
}
| AMemberExpr {
ameExpr :: !AExpr
, ameMember :: !String
, ameDotDecs :: ![Decoration]
, ameMemberDecs :: ![Decoration]
}
| AUnaryOpExpr {
auoeExpr :: !AExpr
, auoeOp :: !String
, auoeOpDecs :: ![Decoration]
}
| APostOpExpr {
apoeExpr :: !AExpr
, apoeOp :: !String
, apoeOpDecs :: ![Decoration]
}
| ABinOpExpr {
aboeExpr1 :: !AExpr
, aboeExpr2 :: !AExpr
, aboeOp :: !String
, aboeOpDecs :: ![Decoration]
}
| AParenExpr {
apeExpr :: !AExpr
, apeLParenDecs :: ![Decoration]
, apeRParenDecs :: ![Decoration]
}
| ACastExpr {
aceTypeName :: !ATypeName
, aceCastExpr :: !AExpr
, aceLParenDecs :: ![Decoration]
, aceRParenDecs :: ![Decoration]
}
deriving (Show)
data ATypeName =
ATypeName {
atnName :: !String
, atnIsPointer :: !Bool
, atnNameDecs :: ![Decoration]
, atnMultDecs :: !(Maybe [Decoration])
}
deriving (Show)
type TokenParser a = GenParser Token () a
type TokenDecParser a = TokenParser ([Decoration], a)
type PreTokenParser a = GenParser PreToken () a
data Rewriter = Rewriter {
rewriteAst :: Rewriter -> Ast -> Ast
, rewriteTopLevel :: Rewriter -> ATopLevel -> ATopLevel
, rewriteStructMember :: Rewriter -> AStructMember -> AStructMember
, rewriteImportDec :: Rewriter -> AImportDec -> AImportDec
, rewriteFunctionSignature :: Rewriter -> AFunctionSignature -> AFunctionSignature
, rewriteFunctionDecParam :: Rewriter -> AFunctionDecParam -> AFunctionDecParam
, rewriteBlock :: Rewriter -> ABlock -> ABlock
, rewriteCommand :: Rewriter -> ACommand -> ACommand
, rewriteVarDec :: Rewriter -> AVarDec -> AVarDec
, rewriteVarInit :: Rewriter -> AVarInit -> AVarInit
, rewriteVarSubscript :: Rewriter -> AVarSubscript -> AVarSubscript
, rewriteExpr :: Rewriter -> AExpr -> AExpr
, rewriteTypeName :: Rewriter -> ATypeName -> ATypeName
}
visitingRewriter :: Rewriter
visitingRewriter = Rewriter {
rewriteAst =
\r (Ast ts) -> Ast $ rewriteTopLevel r r <$> ts
, rewriteTopLevel = \r a -> case a of
AFunctionDec {
afdSignature=signature
, afdBlock=block
} -> a {
afdSignature=rewriteFunctionSignature r r signature
, afdBlock=rewriteBlock r r block
}
ATopLevelVarDec b -> ATopLevelVarDec $ rewriteVarDec r r b
ATopLevelImportDec b -> ATopLevelImportDec $ rewriteImportDec r r b
AStructDec {
asdMembers=members
} -> a {
asdMembers=rewriteStructMember r r <$> members
}
_ -> a
, rewriteStructMember = \r a -> case a of
AStructMemberImport b -> AStructMemberImport $ rewriteImportDec r r b
AStructMemberVar b -> AStructMemberVar $ rewriteVarDec r r b
, rewriteImportDec = \r a -> case a of
AImportFunctionSig {
aifsFunctionSig=functionSig
} -> a {
aifsFunctionSig=rewriteFunctionSignature r r functionSig
}
AImportVarDec {
aivdVarDec=varDec
} -> a {
aivdVarDec=rewriteVarDec r r varDec
}
, rewriteFunctionSignature = \r a -> case a of
AFunctionSignature {
afsParams=params
} -> a {
afsParams=rewriteFunctionDecParam r r <$> params
}
, rewriteFunctionDecParam = \r a -> case a of
AFunctionDecRegularParam {
afdrpTypeName=typeName
, afdrpVarInit=varInit
} -> a {
afdrpTypeName=rewriteTypeName r r typeName
, afdrpVarInit=rewriteVarInit r r varInit
}
_ -> a
, rewriteBlock = \r a -> case a of
ABlock {
abCommands=commands
} -> a {
abCommands=rewriteCommand r r <$> commands
}
, rewriteCommand = \r a -> case a of
AIfCommand {
aicTestExpr=testExpr
, aicTrueCommand=trueCommand
, aicFalseCommand=falseCommand
} -> a {
aicTestExpr=rewriteExpr r r testExpr
, aicTrueCommand=rewriteCommand r r trueCommand
, aicFalseCommand=rewriteCommand r r <$> falseCommand
}
AElseCommand {
aecCommand=command
} -> a {
aecCommand=rewriteCommand r r command
}
AWhileCommand {
awcTestExpr=testExpr
, awcCommand=command
} -> a {
awcTestExpr=rewriteExpr r r testExpr
, awcCommand=rewriteCommand r r command
}
AReturnCommand {
arcExpr=expr
} -> a {
arcExpr=rewriteExpr r r <$> expr
}
AVarDecCommand b -> AVarDecCommand $ rewriteVarDec r r b
AExprCommand {
aecExpr=expr
} -> a {
aecExpr=rewriteExpr r r expr
}
ABlockCommand b -> ABlockCommand $ rewriteBlock r r b
, rewriteVarDec = \r a -> case a of
AVarDec {
avdTypeName=typeName
, avdVars=vars
} -> a {
avdTypeName=rewriteTypeName r r typeName
, avdVars=rewriteVarInit r r <$> vars
}
, rewriteVarInit = \r a -> case a of
AVarInit {
aviSubscripts=subscripts
, aviInit=varInit
} -> a {
aviSubscripts=rewriteVarSubscript r r <$> subscripts
, aviInit=rewriteExpr r r <$> varInit
}
, rewriteVarSubscript = \_r -> id
, rewriteExpr = \r a -> case a of
ANewExpr {
aneTypeName=typeName
, aneSizeExpr=sizeExpr
} -> a {
aneTypeName=rewriteTypeName r r typeName
, aneSizeExpr=rewriteExpr r r sizeExpr
}
AIndexExpr {
aieExpr=expr
, aieIndexExpr=indexExpr
} -> a {
aieExpr=rewriteExpr r r expr
, aieIndexExpr=rewriteExpr r r indexExpr
}
ACallExpr {
aceFunctionExpr=functionExpr
, aceParams=params
} -> a {
aceFunctionExpr=rewriteExpr r r functionExpr
, aceParams=rewriteExpr r r <$> params
}
AMemberExpr {
ameExpr=expr
} -> a {
ameExpr=rewriteExpr r r expr
}
AUnaryOpExpr {
auoeExpr=expr
} -> a {
auoeExpr=rewriteExpr r r expr
}
APostOpExpr {
apoeExpr=expr
} -> a {
apoeExpr=rewriteExpr r r expr
}
ABinOpExpr {
aboeExpr1=expr1
, aboeExpr2=expr2
} -> a {
aboeExpr1=rewriteExpr r r expr1
, aboeExpr2=rewriteExpr r r expr2
}
AParenExpr {
apeExpr=expr
} -> a {
apeExpr=rewriteExpr r r expr
}
ACastExpr {
aceTypeName=typeName
, aceCastExpr=castExpr
} -> a {
aceTypeName=rewriteTypeName r r typeName
, aceCastExpr=rewriteExpr r r castExpr
}
_ -> a
, rewriteTypeName = \_r -> id
}
blockify :: Rewriter
blockify = visitingRewriter {
rewriteCommand = \r a ->
let
ensureBlocked :: ACommand -> ACommand
ensureBlocked c@(ABlockCommand _) = c
ensureBlocked c = ABlockCommand $ ABlock {
abCommands=[c]
, abLBraceDecs=[]
, abRBraceDecs=[]
}
in case a of
AIfCommand {
aicTestExpr=testExpr
, aicTrueCommand=trueCommand
, aicFalseCommand=falseCommand
} -> a {
aicTestExpr=rewriteExpr r r testExpr
, aicTrueCommand=ensureBlocked $ rewriteCommand r r trueCommand
, aicFalseCommand=rewriteCommand r r <$> falseCommand
}
AElseCommand {
aecCommand=command@(AIfCommand {})
} -> a {
aecCommand=rewriteCommand r r command
}
AElseCommand {
aecCommand=command
} -> a {
aecCommand=ensureBlocked $ rewriteCommand r r command
}
AWhileCommand {
awcTestExpr=testExpr
, awcCommand=command
} -> a {
awcTestExpr=rewriteExpr r r testExpr
, awcCommand=ensureBlocked $ rewriteCommand r r command
}
_ -> rewriteCommand visitingRewriter r a
}
rewriteWith :: Rewriter -> Ast -> Ast
rewriteWith r = (rewriteAst r) r
-- Data.ByteString might be more performant?
data Doc =
Text !([Decoration], String)
| Line
| Space
deriving (Show, Eq)
class Renderable a where
render :: a -> [Doc]
instance Renderable Ast where
render (Ast topLevels) = concat (render <$> topLevels)
instance Renderable ATopLevel where
render (AEof decs) = [Text (decs, "")]
render AFunctionDec {
afdSignature=signature
, afdBlock=block
} = render signature ++ [Space] ++ render block ++ [Line]
render (ATopLevelVarDec varDec) = render varDec ++ [Line]
render (ATopLevelImportDec importDec) = render importDec ++ [Line]
render AEnumDec {
aedName=name
, aedValues=values
, aedEnumDecs=enumDecs
, aedNameDecs=nameDecs
, aedLBraceDecs=lBraceDecs
, aedValueDecs=valueDecs
, aedCommaDecs=commaDecs
, aedRBraceDecs=rBraceDecs
, aedSemicolonDecs=semicolonDecs
} = [
Text (enumDecs, "enum")
, Space
, Text (nameDecs, name)
, Space
, Text (lBraceDecs, "{")
]
++ renderEnumValues values valueDecs commaDecs
++ [
Text (rBraceDecs, "}")
, Text (semicolonDecs, ";")
, Line
]
render AStructDec {
asdName=name
, asdMembers=members
, asdStructDecs=structDecs
, asdNameDecs=nameDecs
, asdLBraceDecs=lBraceDecs
, asdRBraceDecs=rBraceDecs
, asdSemicolonDecs=semicolonDecs
} = [
Text (structDecs, "struct")
, Space
, Text (nameDecs, name)
, Space
]
++ renderBetween (lBraceDecs, "{") (rBraceDecs, "}") members
++ [Text (semicolonDecs, ";"), Line]
render AExportDec {
axdName=name
, axdExportDecs=exportDecs
, axdNameDecs=nameDecs
, axdSemicolonDecs=semicolonDecs
} = [
Text (exportDecs, "export")
, Space
, Text (nameDecs, name)
, Text (semicolonDecs, ";")
, Line
]
renderEnumValues :: [String] -> [[Decoration]] -> [[Decoration]] -> [Doc]
renderEnumValues [] [] [] = []
renderEnumValues values valueDecs commaDecs =
[Line] ++ renderInterleaved valueDocs commaDocs ++ [Line]
where
valueDocs = (\p -> [Text p]) <$> zip valueDecs values
commaDocs = renderCommaLine <$> commaDecs
renderCommaLine dec = [Text (dec, ","), Line]
instance Renderable ABlock where
render ABlock {
abCommands=[]
, abLBraceDecs=lBraceDecs
, abRBraceDecs=rBraceDecs
} = [Text (lBraceDecs, "{") , Text (rBraceDecs, "}")]
render ABlock {
abCommands=commands
, abLBraceDecs=lBraceDecs
, abRBraceDecs=rBraceDecs
} = [Text (lBraceDecs, "{"), Line]
++ L.intercalate [Line] (render <$> commands)
++ [Line, Text (rBraceDecs, "}")]
instance Renderable ACommand where
render AIfCommand {
aicTestExpr=testExpr
, aicTrueCommand=trueCommand
, aicFalseCommand=falseCommand
, aicIfDecs=ifDecs
, aicLParenDecs=lParenDecs
, aicRParenDecs=rParenDecs
} = [
Text (ifDecs, "if")
, Space
, Text (lParenDecs, "(")
]
++ render testExpr
++ [Text (rParenDecs, ")"), Space]
++ render trueCommand
++ maybe [] render falseCommand
render AElseCommand {
aecCommand=command
, aecElseDecs=elseDecs
} = [
Space
, Text (elseDecs, "else")
, Space
]
++ render command
render AWhileCommand {
awcTestExpr=testExpr
, awcCommand=command
, awcWhileDecs=whileDecs
, awcLParenDecs=lParenDecs
, awcRParenDecs=rParenDecs
} = [
Text (whileDecs, "while")
, Space
, Text (lParenDecs, "(")
]
++ render testExpr
++ [Text (rParenDecs, ")"), Space]
++ render command
render AReturnCommand {
arcExpr=Nothing
, arcReturnDecs=returnDecs
, arcSemicolonDecs=semicolonDecs
} = [
Text (returnDecs, "return")
, Text (semicolonDecs, ";")
]
render AReturnCommand {
arcExpr=Just expr
, arcReturnDecs=returnDecs
, arcSemicolonDecs=semicolonDecs
} = [Text (returnDecs, "return")]
++ render expr
++ [Text (semicolonDecs, ";")]
render (AVarDecCommand varDec) = render varDec
render AExprCommand {
aecExpr=expr
, aecSemicolonDecs=semicolonDecs
} = render expr ++ [Text (semicolonDecs, ";")]
render (ABlockCommand block) = render block
instance Renderable AStructMember where
render (AStructMemberImport imp) = render imp
render (AStructMemberVar var) = render var
instance Renderable AImportDec where
render AImportFunctionSig {
aifsFunctionSig=functionSig
, aifsImportDecs=importDecs
, aifsSemicolonDecs=semicolonDecs
} = [Text (importDecs, "import"), Space]
++ render functionSig
++ [Text (semicolonDecs, ";")]
render AImportVarDec {
aivdVarDec=varDec
, aivdImportDecs=importDecs
} = [Text (importDecs, "import"), Space] ++ render varDec
instance Renderable AFunctionSignature where
render AFunctionSignature {
afsIsStatic=isStatic
, afsReturnType=returnType
, afsName=name
, afsMember=member
, afsParams=params
, afsStaticDecs=staticDecs
, afsReturnTypeDecs=returnTypeDecs
, afsMemberDecs=memberDecs
, afsNameDecs=nameDecs
, afsMemberNameDecs=memberNameDecs
, afsLParenDecs=lParenDecs
, afsCommaDecs=commaDecs
, afsRParenDecs=rParenDecs
} = (if isStatic then [Text (staticDecs, "static"), Space] else [])
++ [Text (returnTypeDecs, if returnType == "void" then "function" else "returnType"), Space]
++ [Text (nameDecs, name)]
++ maybe [] (\s -> [Text (memberDecs, "::"), Text (memberNameDecs, s)]) member
++ [Text (lParenDecs, "(")]
++ renderInterleaved paramDocs commaDocs
++ [Text (rParenDecs, ")")]
where
paramDocs = render <$> params
commaDocs = renderComma <$> commaDecs
instance Renderable AFunctionDecParam where
render AFunctionDecExtenderParam {
afdepName=name
, afdepThisDecs=thisDecs
, afdepNameDecs=nameDecs
, afdepMultDecs=multDecs
} = [
Text (thisDecs, "this")
, Space
, Text (nameDecs, name)
, Text (multDecs, "*")
]
render AFunctionDecRegularParam {
afdrpTypeName=typeName
, afdrpVarInit=varInit
} = render typeName ++ [Space] ++ render varInit
instance Renderable AVarDec where
render AVarDec {
avdTypeName=name
, avdVars=vars
, avdCommaDecs=commaDecs
, avdSemicolonDecs=semicolonDecs
} = render name
++ [Space]
++ renderInterleaved varDocs sepDocs
++ [Text (semicolonDecs, ";")]
where
varDocs = render <$> vars
sepDocs = renderComma <$> commaDecs
instance Renderable ATypeName where
render ATypeName {
atnName=name
, atnIsPointer=isPointer
, atnNameDecs=nameDecs
, atnMultDecs=multDecs
} = [Text (nameDecs, name)]
++ (if isPointer then
[Space, Text (maybe [] id multDecs, "*")]
else [])
instance Renderable AVarInit where
render AVarInit {
aviId=varId
, aviSubscripts=subscripts
, aviInit=initializer
, aviIdDecs=idDecs
, aviLBracketDecs=lBracketDecs
, aviRBracketDecs=rBracketDecs
, aviAssignDecs=assignDecs
} = [Text (idDecs, varId)]
++ (concat $ zipWith3 renderSubscript lBracketDecs subscripts rBracketDecs)
++ maybe [] (\e -> [Space, Text (assignDecs, "="), Space] ++ render e) initializer
where
renderSubscript lbDecs subscript rbDecs =
[Text (lbDecs, "[")]
++ render subscript
++ [Text (rbDecs, "]")]
instance Renderable AVarSubscript where
render AVarSubscriptEmpty = []
render AVarSubscriptId {
avsidId=ident
, avsidIdDecs=idDecs
} = [Text (idDecs, ident)]
render AVarSubscriptInt {
avsiInt=int_
, avsiIntDecs=intDecs
} = [Text (intDecs, show int_)]
instance Renderable AExpr where
render AFalseExpr {
afeFalseDecs=falseDecs
} = [Text (falseDecs, "false")]
render ATrueExpr {
ateTrueDecs=trueDecs
} = [Text (trueDecs, "true")]
render AThisExpr {
ateThisDecs=thisDecs
} = [Text (thisDecs, "this")]
render ANewExpr {
aneTypeName=name
, aneSizeExpr=expr
, aneNewDecs=newDecs
, aneLBracketDecs=lBracketDecs
, aneRBracketDecs=rBracketDecs
} = [Text (newDecs, "new"), Space]
++ render name
++ [Text (lBracketDecs, "[")]
++ render expr
++ [Text (rBracketDecs, "]")]
render AFloatExpr {
afeFloat=d
, afeFloatDecs=floatDecs
} = [Text $ (floatDecs, show d)]
render AIntExpr {
aieInt=int_
, aieIntDecs=intDecs
} = [Text (intDecs, show int_)]
render AStringExpr {
aseString=s
, aseStringDecs=stringDecs
} = [Text (stringDecs, formatStringLiteral s)]
render AIdentifierExpr {
aieId=ident
, aieIdDecs=idDecs
} = [Text (idDecs, ident)]
render AIndexExpr {
aieExpr=expr
, aieIndexExpr=indexExpr
, aieLBracketDecs=lBracketDecs
, aieRBracketDecs=rBracketDecs
} = render expr
++ [Text (lBracketDecs, "[")]