-
Notifications
You must be signed in to change notification settings - Fork 0
/
TParse.hs
957 lines (795 loc) · 26.3 KB
/
TParse.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
{-# OPTIONS_GHC -fno-warn-overlapping-patterns #-}
module TParse
( tParse
, parseFile
, pretty
, prex
, echo
, echoFile
) where
import Syntax
import Data.Char (ord,isSpace,isDigit,isLower,isUpper,isAlphaNum)
import Data.List (intersperse)
-- parser produced by Happy Version 1.18.5
data HappyAbsSyn
= HappyTerminal (Token)
| HappyErrorToken Int
| HappyAbsSyn4 ([Stmt])
| HappyAbsSyn6 (Stmt)
| HappyAbsSyn7 (Exp)
{- to allow type-synonyms as our monads (likely
- with explicitly-specified bind and return)
- in Haskell98, it seems that with
- /type M a = .../, then /(HappyReduction M)/
- is not allowed. But Happy is a
- code-generator that can just substitute it.
type HappyReduction m =
Int
-> (Token)
-> HappyState (Token) (HappyStk HappyAbsSyn -> [(Token)] -> m HappyAbsSyn)
-> [HappyState (Token) (HappyStk HappyAbsSyn -> [(Token)] -> m HappyAbsSyn)]
-> HappyStk HappyAbsSyn
-> [(Token)] -> m HappyAbsSyn
-}
action_0,
action_1,
action_2,
action_3,
action_4,
action_5,
action_6,
action_7,
action_8,
action_9,
action_10,
action_11,
action_12,
action_13,
action_14,
action_15,
action_16,
action_17,
action_18,
action_19,
action_20,
action_21,
action_22,
action_23,
action_24,
action_25,
action_26,
action_27,
action_28,
action_29,
action_30,
action_31,
action_32,
action_33,
action_34,
action_35,
action_36,
action_37,
action_38,
action_39,
action_40,
action_41,
action_42,
action_43,
action_44,
action_45,
action_46,
action_47,
action_48,
action_49,
action_50 :: () => Int -> ({-HappyReduction (HappyIdentity) = -}
Int
-> (Token)
-> HappyState (Token) (HappyStk HappyAbsSyn -> [(Token)] -> (HappyIdentity) HappyAbsSyn)
-> [HappyState (Token) (HappyStk HappyAbsSyn -> [(Token)] -> (HappyIdentity) HappyAbsSyn)]
-> HappyStk HappyAbsSyn
-> [(Token)] -> (HappyIdentity) HappyAbsSyn)
happyReduce_1,
happyReduce_2,
happyReduce_3,
happyReduce_4,
happyReduce_5,
happyReduce_6,
happyReduce_7,
happyReduce_8,
happyReduce_9,
happyReduce_10,
happyReduce_11,
happyReduce_12,
happyReduce_13,
happyReduce_14,
happyReduce_15,
happyReduce_16,
happyReduce_17,
happyReduce_18,
happyReduce_19 :: () => ({-HappyReduction (HappyIdentity) = -}
Int
-> (Token)
-> HappyState (Token) (HappyStk HappyAbsSyn -> [(Token)] -> (HappyIdentity) HappyAbsSyn)
-> [HappyState (Token) (HappyStk HappyAbsSyn -> [(Token)] -> (HappyIdentity) HappyAbsSyn)]
-> HappyStk HappyAbsSyn
-> [(Token)] -> (HappyIdentity) HappyAbsSyn)
action_0 (21) = happyShift action_4
action_0 (23) = happyShift action_5
action_0 (25) = happyShift action_6
action_0 (27) = happyShift action_7
action_0 (4) = happyGoto action_8
action_0 (5) = happyGoto action_2
action_0 (6) = happyGoto action_3
action_0 _ = happyFail
action_1 (21) = happyShift action_4
action_1 (23) = happyShift action_5
action_1 (25) = happyShift action_6
action_1 (27) = happyShift action_7
action_1 (5) = happyGoto action_2
action_1 (6) = happyGoto action_3
action_1 _ = happyFail
action_2 (21) = happyShift action_4
action_2 (23) = happyShift action_5
action_2 (25) = happyShift action_6
action_2 (27) = happyShift action_7
action_2 (6) = happyGoto action_17
action_2 _ = happyReduce_1
action_3 _ = happyReduce_3
action_4 (16) = happyShift action_16
action_4 _ = happyFail
action_5 (16) = happyShift action_15
action_5 _ = happyFail
action_6 (16) = happyShift action_11
action_6 (24) = happyShift action_12
action_6 (26) = happyShift action_13
action_6 (27) = happyShift action_14
action_6 (7) = happyGoto action_10
action_6 _ = happyFail
action_7 (15) = happyShift action_9
action_7 _ = happyFail
action_8 (28) = happyAccept
action_8 _ = happyFail
action_9 (16) = happyShift action_11
action_9 (24) = happyShift action_12
action_9 (26) = happyShift action_13
action_9 (27) = happyShift action_14
action_9 (7) = happyGoto action_29
action_9 _ = happyFail
action_10 (8) = happyShift action_21
action_10 (9) = happyShift action_22
action_10 (10) = happyShift action_23
action_10 (11) = happyShift action_24
action_10 (12) = happyShift action_25
action_10 (13) = happyShift action_26
action_10 (14) = happyShift action_27
action_10 (20) = happyShift action_28
action_10 _ = happyFail
action_11 (16) = happyShift action_11
action_11 (24) = happyShift action_12
action_11 (26) = happyShift action_13
action_11 (27) = happyShift action_14
action_11 (7) = happyGoto action_20
action_11 _ = happyFail
action_12 _ = happyReduce_19
action_13 _ = happyReduce_9
action_14 _ = happyReduce_10
action_15 (16) = happyShift action_11
action_15 (24) = happyShift action_12
action_15 (26) = happyShift action_13
action_15 (27) = happyShift action_14
action_15 (7) = happyGoto action_19
action_15 _ = happyFail
action_16 (16) = happyShift action_11
action_16 (24) = happyShift action_12
action_16 (26) = happyShift action_13
action_16 (27) = happyShift action_14
action_16 (7) = happyGoto action_18
action_16 _ = happyFail
action_17 _ = happyReduce_2
action_18 (8) = happyShift action_21
action_18 (9) = happyShift action_22
action_18 (10) = happyShift action_23
action_18 (11) = happyShift action_24
action_18 (12) = happyShift action_25
action_18 (13) = happyShift action_26
action_18 (14) = happyShift action_27
action_18 (17) = happyShift action_40
action_18 _ = happyFail
action_19 (8) = happyShift action_21
action_19 (9) = happyShift action_22
action_19 (10) = happyShift action_23
action_19 (11) = happyShift action_24
action_19 (12) = happyShift action_25
action_19 (13) = happyShift action_26
action_19 (14) = happyShift action_27
action_19 (17) = happyShift action_39
action_19 _ = happyFail
action_20 (8) = happyShift action_21
action_20 (9) = happyShift action_22
action_20 (10) = happyShift action_23
action_20 (11) = happyShift action_24
action_20 (12) = happyShift action_25
action_20 (13) = happyShift action_26
action_20 (14) = happyShift action_27
action_20 (17) = happyShift action_38
action_20 _ = happyFail
action_21 (16) = happyShift action_11
action_21 (24) = happyShift action_12
action_21 (26) = happyShift action_13
action_21 (27) = happyShift action_14
action_21 (7) = happyGoto action_37
action_21 _ = happyFail
action_22 (16) = happyShift action_11
action_22 (24) = happyShift action_12
action_22 (26) = happyShift action_13
action_22 (27) = happyShift action_14
action_22 (7) = happyGoto action_36
action_22 _ = happyFail
action_23 (16) = happyShift action_11
action_23 (24) = happyShift action_12
action_23 (26) = happyShift action_13
action_23 (27) = happyShift action_14
action_23 (7) = happyGoto action_35
action_23 _ = happyFail
action_24 (16) = happyShift action_11
action_24 (24) = happyShift action_12
action_24 (26) = happyShift action_13
action_24 (27) = happyShift action_14
action_24 (7) = happyGoto action_34
action_24 _ = happyFail
action_25 (16) = happyShift action_11
action_25 (24) = happyShift action_12
action_25 (26) = happyShift action_13
action_25 (27) = happyShift action_14
action_25 (7) = happyGoto action_33
action_25 _ = happyFail
action_26 (16) = happyShift action_11
action_26 (24) = happyShift action_12
action_26 (26) = happyShift action_13
action_26 (27) = happyShift action_14
action_26 (7) = happyGoto action_32
action_26 _ = happyFail
action_27 (16) = happyShift action_11
action_27 (24) = happyShift action_12
action_27 (26) = happyShift action_13
action_27 (27) = happyShift action_14
action_27 (7) = happyGoto action_31
action_27 _ = happyFail
action_28 _ = happyReduce_8
action_29 (8) = happyShift action_21
action_29 (9) = happyShift action_22
action_29 (10) = happyShift action_23
action_29 (11) = happyShift action_24
action_29 (12) = happyShift action_25
action_29 (13) = happyShift action_26
action_29 (14) = happyShift action_27
action_29 (20) = happyShift action_30
action_29 _ = happyFail
action_30 _ = happyReduce_4
action_31 (8) = happyShift action_21
action_31 (9) = happyShift action_22
action_31 (10) = happyShift action_23
action_31 (11) = happyShift action_24
action_31 _ = happyReduce_17
action_32 (8) = happyShift action_21
action_32 (9) = happyShift action_22
action_32 (10) = happyShift action_23
action_32 (11) = happyShift action_24
action_32 (14) = happyShift action_27
action_32 _ = happyReduce_16
action_33 (8) = happyShift action_21
action_33 (9) = happyShift action_22
action_33 (10) = happyShift action_23
action_33 (11) = happyShift action_24
action_33 (14) = happyShift action_27
action_33 _ = happyReduce_15
action_34 _ = happyReduce_14
action_35 _ = happyReduce_13
action_36 (10) = happyShift action_23
action_36 (11) = happyShift action_24
action_36 _ = happyReduce_12
action_37 (10) = happyShift action_23
action_37 (11) = happyShift action_24
action_37 _ = happyReduce_11
action_38 _ = happyReduce_18
action_39 (18) = happyShift action_42
action_39 _ = happyFail
action_40 (18) = happyShift action_41
action_40 _ = happyFail
action_41 (21) = happyShift action_4
action_41 (23) = happyShift action_5
action_41 (25) = happyShift action_6
action_41 (27) = happyShift action_7
action_41 (5) = happyGoto action_44
action_41 (6) = happyGoto action_3
action_41 _ = happyFail
action_42 (21) = happyShift action_4
action_42 (23) = happyShift action_5
action_42 (25) = happyShift action_6
action_42 (27) = happyShift action_7
action_42 (5) = happyGoto action_43
action_42 (6) = happyGoto action_3
action_42 _ = happyFail
action_43 (19) = happyShift action_46
action_43 (21) = happyShift action_4
action_43 (23) = happyShift action_5
action_43 (25) = happyShift action_6
action_43 (27) = happyShift action_7
action_43 (6) = happyGoto action_17
action_43 _ = happyFail
action_44 (19) = happyShift action_45
action_44 (21) = happyShift action_4
action_44 (23) = happyShift action_5
action_44 (25) = happyShift action_6
action_44 (27) = happyShift action_7
action_44 (6) = happyGoto action_17
action_44 _ = happyFail
action_45 (22) = happyShift action_47
action_45 _ = happyReduce_5
action_46 _ = happyReduce_7
action_47 (18) = happyShift action_48
action_47 _ = happyFail
action_48 (21) = happyShift action_4
action_48 (23) = happyShift action_5
action_48 (25) = happyShift action_6
action_48 (27) = happyShift action_7
action_48 (5) = happyGoto action_49
action_48 (6) = happyGoto action_3
action_48 _ = happyFail
action_49 (19) = happyShift action_50
action_49 (21) = happyShift action_4
action_49 (23) = happyShift action_5
action_49 (25) = happyShift action_6
action_49 (27) = happyShift action_7
action_49 (6) = happyGoto action_17
action_49 _ = happyFail
action_50 _ = happyReduce_6
happyReduce_1 = happySpecReduce_1 4 happyReduction_1
happyReduction_1 (HappyAbsSyn4 happy_var_1)
= HappyAbsSyn4
(reverse happy_var_1
)
happyReduction_1 _ = notHappyAtAll
happyReduce_2 = happySpecReduce_2 5 happyReduction_2
happyReduction_2 (HappyAbsSyn6 happy_var_2)
(HappyAbsSyn4 happy_var_1)
= HappyAbsSyn4
(happy_var_2 : happy_var_1
)
happyReduction_2 _ _ = notHappyAtAll
happyReduce_3 = happySpecReduce_1 5 happyReduction_3
happyReduction_3 (HappyAbsSyn6 happy_var_1)
= HappyAbsSyn4
([happy_var_1]
)
happyReduction_3 _ = notHappyAtAll
happyReduce_4 = happyReduce 4 6 happyReduction_4
happyReduction_4 (_ `HappyStk`
(HappyAbsSyn7 happy_var_3) `HappyStk`
_ `HappyStk`
(HappyTerminal (TVAR happy_var_1)) `HappyStk`
happyRest)
= HappyAbsSyn6
(Asg happy_var_1 happy_var_3
) `HappyStk` happyRest
happyReduce_5 = happyReduce 7 6 happyReduction_5
happyReduction_5 (_ `HappyStk`
(HappyAbsSyn4 happy_var_6) `HappyStk`
_ `HappyStk`
_ `HappyStk`
(HappyAbsSyn7 happy_var_3) `HappyStk`
_ `HappyStk`
_ `HappyStk`
happyRest)
= HappyAbsSyn6
(If happy_var_3 (reverse happy_var_6)
) `HappyStk` happyRest
happyReduce_6 = happyReduce 11 6 happyReduction_6
happyReduction_6 (_ `HappyStk`
(HappyAbsSyn4 happy_var_10) `HappyStk`
_ `HappyStk`
_ `HappyStk`
_ `HappyStk`
(HappyAbsSyn4 happy_var_6) `HappyStk`
_ `HappyStk`
_ `HappyStk`
(HappyAbsSyn7 happy_var_3) `HappyStk`
_ `HappyStk`
_ `HappyStk`
happyRest)
= HappyAbsSyn6
(Ite happy_var_3 (reverse happy_var_6) (reverse happy_var_10)
) `HappyStk` happyRest
happyReduce_7 = happyReduce 7 6 happyReduction_7
happyReduction_7 (_ `HappyStk`
(HappyAbsSyn4 happy_var_6) `HappyStk`
_ `HappyStk`
_ `HappyStk`
(HappyAbsSyn7 happy_var_3) `HappyStk`
_ `HappyStk`
_ `HappyStk`
happyRest)
= HappyAbsSyn6
(While happy_var_3 (reverse happy_var_6)
) `HappyStk` happyRest
happyReduce_8 = happySpecReduce_3 6 happyReduction_8
happyReduction_8 _
(HappyAbsSyn7 happy_var_2)
_
= HappyAbsSyn6
(Output happy_var_2
)
happyReduction_8 _ _ _ = notHappyAtAll
happyReduce_9 = happySpecReduce_1 7 happyReduction_9
happyReduction_9 (HappyTerminal (TCON happy_var_1))
= HappyAbsSyn7
(Con (read happy_var_1 :: Int)
)
happyReduction_9 _ = notHappyAtAll
happyReduce_10 = happySpecReduce_1 7 happyReduction_10
happyReduction_10 (HappyTerminal (TVAR happy_var_1))
= HappyAbsSyn7
(Var happy_var_1
)
happyReduction_10 _ = notHappyAtAll
happyReduce_11 = happySpecReduce_3 7 happyReduction_11
happyReduction_11 (HappyAbsSyn7 happy_var_3)
_
(HappyAbsSyn7 happy_var_1)
= HappyAbsSyn7
(Op Plus happy_var_1 happy_var_3
)
happyReduction_11 _ _ _ = notHappyAtAll
happyReduce_12 = happySpecReduce_3 7 happyReduction_12
happyReduction_12 (HappyAbsSyn7 happy_var_3)
_
(HappyAbsSyn7 happy_var_1)
= HappyAbsSyn7
(Op Minus happy_var_1 happy_var_3
)
happyReduction_12 _ _ _ = notHappyAtAll
happyReduce_13 = happySpecReduce_3 7 happyReduction_13
happyReduction_13 (HappyAbsSyn7 happy_var_3)
_
(HappyAbsSyn7 happy_var_1)
= HappyAbsSyn7
(Op Mult happy_var_1 happy_var_3
)
happyReduction_13 _ _ _ = notHappyAtAll
happyReduce_14 = happySpecReduce_3 7 happyReduction_14
happyReduction_14 (HappyAbsSyn7 happy_var_3)
_
(HappyAbsSyn7 happy_var_1)
= HappyAbsSyn7
(Op Div happy_var_1 happy_var_3
)
happyReduction_14 _ _ _ = notHappyAtAll
happyReduce_15 = happySpecReduce_3 7 happyReduction_15
happyReduction_15 (HappyAbsSyn7 happy_var_3)
_
(HappyAbsSyn7 happy_var_1)
= HappyAbsSyn7
(Op More happy_var_1 happy_var_3
)
happyReduction_15 _ _ _ = notHappyAtAll
happyReduce_16 = happySpecReduce_3 7 happyReduction_16
happyReduction_16 (HappyAbsSyn7 happy_var_3)
_
(HappyAbsSyn7 happy_var_1)
= HappyAbsSyn7
(Op Equal happy_var_1 happy_var_3
)
happyReduction_16 _ _ _ = notHappyAtAll
happyReduce_17 = happySpecReduce_3 7 happyReduction_17
happyReduction_17 (HappyAbsSyn7 happy_var_3)
_
(HappyAbsSyn7 happy_var_1)
= HappyAbsSyn7
(Op Nequal happy_var_1 happy_var_3
)
happyReduction_17 _ _ _ = notHappyAtAll
happyReduce_18 = happySpecReduce_3 7 happyReduction_18
happyReduction_18 _
(HappyAbsSyn7 happy_var_2)
_
= HappyAbsSyn7
(happy_var_2
)
happyReduction_18 _ _ _ = notHappyAtAll
happyReduce_19 = happySpecReduce_1 7 happyReduction_19
happyReduction_19 _
= HappyAbsSyn7
(Input
)
happyNewToken action sts stk [] =
action 28 28 notHappyAtAll (HappyState action) sts stk []
happyNewToken action sts stk (tk:tks) =
let cont i = action i i tk (HappyState action) sts stk tks in
case tk of {
TPLUS -> cont 8;
TMINUS -> cont 9;
TMULT -> cont 10;
TDIV -> cont 11;
TMORE -> cont 12;
TEQUAL -> cont 13;
TNEQUAL -> cont 14;
TASG -> cont 15;
TOPENPAR -> cont 16;
TCLOSEPAR -> cont 17;
TOPENBRACE -> cont 18;
TCLOSEBRACE -> cont 19;
TSEMICOLON -> cont 20;
TIF -> cont 21;
TELSE -> cont 22;
TWHILE -> cont 23;
TINPUT -> cont 24;
TOUTPUT -> cont 25;
TCON happy_dollar_dollar -> cont 26;
TVAR happy_dollar_dollar -> cont 27;
_ -> happyError' (tk:tks)
}
happyError_ tk tks = happyError' (tk:tks)
newtype HappyIdentity a = HappyIdentity a
happyIdentity = HappyIdentity
happyRunIdentity (HappyIdentity a) = a
instance Monad HappyIdentity where
return = HappyIdentity
(HappyIdentity p) >>= q = q p
happyThen :: () => HappyIdentity a -> (a -> HappyIdentity b) -> HappyIdentity b
happyThen = (>>=)
happyReturn :: () => a -> HappyIdentity a
happyReturn = (return)
happyThen1 m k tks = (>>=) m (\a -> k a tks)
happyReturn1 :: () => a -> b -> HappyIdentity a
happyReturn1 = \a tks -> (return) a
happyError' :: () => [(Token)] -> HappyIdentity a
happyError' = HappyIdentity . parseError
generatedParser tks = happyRunIdentity happySomeParser where
happySomeParser = happyThen (happyParse action_0 tks) (\x -> case x of {HappyAbsSyn4 z -> happyReturn z; _other -> notHappyAtAll })
happySeq = happyDontSeq
parseError :: [Token] -> a
parseError tks
= error $ "Parse error at token " ++
spaced_list (map show (take 12 tks)) ++ " ..."
data Token
= TCON String | TVAR String
| TPLUS | TMINUS | TMULT | TDIV | TMORE | TEQUAL | TNEQUAL
| TOPENPAR | TCLOSEPAR | TOPENBRACE | TCLOSEBRACE
| TINPUT | TOUTPUT | TSEMICOLON
| TASG | TIF | TELSE | TWHILE
deriving Show
lexer :: String -> [Token]
lexer (x:xs) | isSpace x = lexer xs
lexer ('/':'/':xs)
= lexer (dropWhile (/= '\n') xs)
lexer [] = []
lexer ('+':xs) = TPLUS : lexer xs
lexer ('-':xs) = TMINUS : lexer xs
lexer ('*':xs) = TMULT : lexer xs
lexer ('/':xs) = TDIV : lexer xs
lexer ('>':xs) = TMORE : lexer xs
lexer ('=':'=':xs) = TEQUAL : lexer xs
lexer ('!':'=':xs) = TNEQUAL : lexer xs
lexer ('=':xs) = TASG : lexer xs
lexer ('(':xs) = TOPENPAR : lexer xs
lexer (')':xs) = TCLOSEPAR : lexer xs
lexer ('{':xs) = TOPENBRACE : lexer xs
lexer ('}':xs) = TCLOSEBRACE : lexer xs
lexer (';':xs) = TSEMICOLON : lexer xs
lexer ('i':'f':xs) = TIF : lexer xs
lexer ('e':'l':'s':'e':xs) = TELSE : lexer xs
lexer ('w':'h':'i':'l':'e':xs) = TWHILE : lexer xs
lexer ('i':'n':'p':'u':'t':xs) = TINPUT : lexer xs
lexer ('o':'u':'t':'p':'u':'t':xs) = TOUTPUT : lexer xs
lexer (x:xs) | isDigit x
= let (digits,rest) = span isDigit xs
in TCON (x:digits) : lexer rest
lexer (x:xs) | isLower x
= let (chars,rest) = span isAlphaNum xs
in TVAR (x:chars) : lexer rest
lexer xs
= error $ "lexical error: "
++ spaced_list (map show (take 12 xs)) ++ " ..."
tParse :: String -> [Stmt]
tParse
= generatedParser . lexer
parseFile :: FilePath -> IO [Stmt]
parseFile fp
= do tipProgram <- readFile fp
return (tParse tipProgram)
echo :: String -> String
echo = pretty . tParse
echoFile :: FilePath -> IO ()
echoFile fp
= do tipProgram <- readFile fp
putStr (pretty $ tParse tipProgram)
return ()
pretty :: [Stmt] -> String
pretty stmts
= pr stmts 0
pr (Asg v e : stmts) n
= replicate n ' ' ++ v ++ " = " ++ prex e ++ ";\n"
++ pr stmts n
pr (Output e : stmts) n
= replicate n ' ' ++ "output " ++ prex e ++ ";\n"
++ pr stmts n
pr (If e ss : stmts) n
= replicate n ' ' ++ "if (" ++ prex e ++ ") {\n"
++ pr ss (n+2) ++ replicate n ' ' ++ "}\n"
++ pr stmts n
pr (Ite e ss1 ss2 : stmts) n
= replicate n ' ' ++ "if (" ++ prex e ++ ") {\n"
++ pr ss1 (n+2) ++ replicate n ' ' ++ "} else {\n"
++ pr ss2 (n+2) ++ replicate n ' ' ++ "}\n"
++ pr stmts n
pr (While e ss : stmts) n
= replicate n ' ' ++ "while (" ++ prex e ++ ") {\n"
++ pr ss (n+2) ++ replicate n ' ' ++ "}\n"
++ pr stmts n
pr [] _
= ""
prex :: Exp -> String
prex (Con k)
= show k
prex (Var v)
= v
prex Input
= "input"
prex (Op kind e1 e2)
= prex' e1 ++ prop kind ++ prex' e2
where
prex' (Op kind e1 e2)
= '(' : prex' e1 ++ prop kind ++ prex' e2 ++ ")"
prex' e
= prex e
prop :: Opkind -> String
prop op
= case op of
Plus -> " + "
Minus -> " - "
Mult -> " * "
Div -> " / "
More -> " > "
Equal -> " == "
Nequal -> " != "
spaced_list :: [String] -> String
spaced_list = concat . intersperse " "
{-# LINE 1 "templates/GenericTemplate.hs" #-}
{-# LINE 1 "templates/GenericTemplate.hs" #-}
{-# LINE 1 "<built-in>" #-}
{-# LINE 1 "<command-line>" #-}
{-# LINE 1 "templates/GenericTemplate.hs" #-}
-- Id: GenericTemplate.hs,v 1.26 2005/01/14 14:47:22 simonmar Exp
{-# LINE 30 "templates/GenericTemplate.hs" #-}
{-# LINE 51 "templates/GenericTemplate.hs" #-}
{-# LINE 61 "templates/GenericTemplate.hs" #-}
{-# LINE 70 "templates/GenericTemplate.hs" #-}
infixr 9 `HappyStk`
data HappyStk a = HappyStk a (HappyStk a)
-----------------------------------------------------------------------------
-- starting the parse
happyParse start_state = happyNewToken start_state notHappyAtAll notHappyAtAll
-----------------------------------------------------------------------------
-- Accepting the parse
-- If the current token is (1), it means we've just accepted a partial
-- parse (a %partial parser). We must ignore the saved token on the top of
-- the stack in this case.
happyAccept (1) tk st sts (_ `HappyStk` ans `HappyStk` _) =
happyReturn1 ans
happyAccept j tk st sts (HappyStk ans _) =
(happyReturn1 ans)
-----------------------------------------------------------------------------
-- Arrays only: do the next action
{-# LINE 148 "templates/GenericTemplate.hs" #-}
-----------------------------------------------------------------------------
-- HappyState data type (not arrays)
newtype HappyState b c = HappyState
(Int -> -- token number
Int -> -- token number (yes, again)
b -> -- token semantic value
HappyState b c -> -- current state
[HappyState b c] -> -- state stack
c)
-----------------------------------------------------------------------------
-- Shifting a token
happyShift new_state (1) tk st sts stk@(x `HappyStk` _) =
let (i) = (case x of { HappyErrorToken (i) -> i }) in
-- trace "shifting the error token" $
new_state i i tk (HappyState (new_state)) ((st):(sts)) (stk)
happyShift new_state i tk st sts stk =
happyNewToken new_state ((st):(sts)) ((HappyTerminal (tk))`HappyStk`stk)
-- happyReduce is specialised for the common cases.
happySpecReduce_0 i fn (1) tk st sts stk
= happyFail (1) tk st sts stk
happySpecReduce_0 nt fn j tk st@((HappyState (action))) sts stk
= action nt j tk st ((st):(sts)) (fn `HappyStk` stk)
happySpecReduce_1 i fn (1) tk st sts stk
= happyFail (1) tk st sts stk
happySpecReduce_1 nt fn j tk _ sts@(((st@(HappyState (action))):(_))) (v1`HappyStk`stk')
= let r = fn v1 in
happySeq r (action nt j tk st sts (r `HappyStk` stk'))
happySpecReduce_2 i fn (1) tk st sts stk
= happyFail (1) tk st sts stk
happySpecReduce_2 nt fn j tk _ ((_):(sts@(((st@(HappyState (action))):(_))))) (v1`HappyStk`v2`HappyStk`stk')
= let r = fn v1 v2 in
happySeq r (action nt j tk st sts (r `HappyStk` stk'))
happySpecReduce_3 i fn (1) tk st sts stk
= happyFail (1) tk st sts stk
happySpecReduce_3 nt fn j tk _ ((_):(((_):(sts@(((st@(HappyState (action))):(_))))))) (v1`HappyStk`v2`HappyStk`v3`HappyStk`stk')
= let r = fn v1 v2 v3 in
happySeq r (action nt j tk st sts (r `HappyStk` stk'))
happyReduce k i fn (1) tk st sts stk
= happyFail (1) tk st sts stk
happyReduce k nt fn j tk st sts stk
= case happyDrop (k - ((1) :: Int)) sts of
sts1@(((st1@(HappyState (action))):(_))) ->
let r = fn stk in -- it doesn't hurt to always seq here...
happyDoSeq r (action nt j tk st1 sts1 r)
happyMonadReduce k nt fn (1) tk st sts stk
= happyFail (1) tk st sts stk
happyMonadReduce k nt fn j tk st sts stk =
happyThen1 (fn stk tk) (\r -> action nt j tk st1 sts1 (r `HappyStk` drop_stk))
where (sts1@(((st1@(HappyState (action))):(_)))) = happyDrop k ((st):(sts))
drop_stk = happyDropStk k stk
happyMonad2Reduce k nt fn (1) tk st sts stk
= happyFail (1) tk st sts stk
happyMonad2Reduce k nt fn j tk st sts stk =
happyThen1 (fn stk tk) (\r -> happyNewToken new_state sts1 (r `HappyStk` drop_stk))
where (sts1@(((st1@(HappyState (action))):(_)))) = happyDrop k ((st):(sts))
drop_stk = happyDropStk k stk
new_state = action
happyDrop (0) l = l
happyDrop n ((_):(t)) = happyDrop (n - ((1) :: Int)) t
happyDropStk (0) l = l
happyDropStk n (x `HappyStk` xs) = happyDropStk (n - ((1)::Int)) xs
-----------------------------------------------------------------------------
-- Moving to a new state after a reduction
{-# LINE 246 "templates/GenericTemplate.hs" #-}
happyGoto action j tk st = action j j tk (HappyState action)
-----------------------------------------------------------------------------
-- Error recovery ((1) is the error token)
-- parse error if we are in recovery and we fail again
happyFail (1) tk old_st _ stk =
-- trace "failing" $
happyError_ tk
{- We don't need state discarding for our restricted implementation of
"error". In fact, it can cause some bogus parses, so I've disabled it
for now --SDM
-- discard a state
happyFail (1) tk old_st (((HappyState (action))):(sts))
(saved_tok `HappyStk` _ `HappyStk` stk) =
-- trace ("discarding state, depth " ++ show (length stk)) $
action (1) (1) tk (HappyState (action)) sts ((saved_tok`HappyStk`stk))
-}
-- Enter error recovery: generate an error token,
-- save the old token and carry on.
happyFail i tk (HappyState (action)) sts stk =
-- trace "entering error recovery" $
action (1) (1) tk (HappyState (action)) sts ( (HappyErrorToken (i)) `HappyStk` stk)
-- Internal happy errors:
notHappyAtAll = error "Internal Happy error\n"
-----------------------------------------------------------------------------
-- Hack to get the typechecker to accept our action functions
-----------------------------------------------------------------------------
-- Seq-ing. If the --strict flag is given, then Happy emits
-- happySeq = happyDoSeq
-- otherwise it emits
-- happySeq = happyDontSeq
happyDoSeq, happyDontSeq :: a -> b -> b
happyDoSeq a b = a `seq` b
happyDontSeq a b = b
-----------------------------------------------------------------------------
-- Don't inline any functions from the template. GHC has a nasty habit
-- of deciding to inline happyGoto everywhere, which increases the size of
-- the generated parser quite a bit.
{-# LINE 310 "templates/GenericTemplate.hs" #-}
{-# NOINLINE happyShift #-}
{-# NOINLINE happySpecReduce_0 #-}
{-# NOINLINE happySpecReduce_1 #-}
{-# NOINLINE happySpecReduce_2 #-}
{-# NOINLINE happySpecReduce_3 #-}
{-# NOINLINE happyReduce #-}
{-# NOINLINE happyMonadReduce #-}
{-# NOINLINE happyGoto #-}
{-# NOINLINE happyFail #-}
-- end of Happy Template.