Repository navigation
Expand file tree
/
Copy pathparse.84
More file actions
926 lines (880 loc) · 30.6 KB
/
Copy pathparse.84
File metadata and controls
926 lines (880 loc) · 30.6 KB
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
{
: file
}
Where
Define (file text)
(parse text expand (rule 'file_block))
Where
Define (parse text expand parser)
Let {i token} (SCAN.token text (SCAN.whitespace text 0))
In
Iterate {parser cont peek i} From {parser 'halt 'pure.token i}
Match parser {
| 'rule.term (Continue (expand term) cont peek i)
| 'fail.message 'fail.{message i}
| 'peek (Continue peek cont peek i)
| 'pop
Let {i token} (SCAN.token text (SCAN.whitespace text i))
In
(Continue peek cont 'pure.token i)
| 'pure.x
Match cont {
| 'halt 'succeed.x
| 'bind1.{f cont} (Continue (f x) cont peek i)
}
| 'bind1.{p1 f} (Continue p1 'bind1.{f cont} peek i)
}
Define (expand term)
Match term {
| 'file_block file_block
| 'expr expr
| 'binder_group binder_group
| 'binder binder
| 'block_body block_body
| 'stmt stmt
| 'begin_body begin_body
| 'sequence2.{parser termination_check} (sequence2 parser termination_check)
| 'sequence.{parser maybe_terminator} (sequence parser maybe_terminator)
| 'chain chain
| 'define_pattern define_pattern
}
Where
Let file_block
(lift2 (rule 'expr)
(sequence (ignore1 (match "Where") (rule 'binder_group)) 'nothing)
Func {expr binder_groups}
(LIST.reduce binder_groups expr
Func {expr binders}
'block.{(LIST.reverse binders) expr}))
Let expr
Let okay (pure {})
In
Define (check token)
Match token {
| 'eof (fail "Unexpected end of file.")
| 'op.name
If (Or (STRING.equal name "-") (STRING.equal name "!"))
okay
(fail "Unexpected unary operator.")
| 'sym.text
Cond {
| (STRING.equal text "(") okay
| (STRING.equal text "{") okay
| (STRING.equal text "[") okay
| (STRING.equal text "'") okay
| (STRING.equal text "True") okay
| (STRING.equal text "False") okay
| (STRING.equal text "If") okay
| (STRING.equal text "Cond") okay
| (STRING.equal text "Match") okay
| (STRING.equal text "Func") okay
| (STRING.equal text "Package") okay
| (STRING.equal text "Block") okay
| (STRING.equal text "Iterate") okay
| (STRING.equal text "Unfold") okay
| (STRING.equal text "Begin") okay
| (STRING.equal text "When") okay
| True (fail "Unexpected token while parsing expression.")
}
| _ okay
}
Define (go token)
Match token {
| 'num.n (pure 'num.n)
| 'str.s (pure 'str.s)
| 'op.name
Cond {
| (STRING.equal name "-")
(lift1 (rule 'expr)
Func expr 'app.{'prim."negate" [expr & 'nil]})
| (STRING.equal name "!")
(lift1 (rule 'expr)
Func expr 'if.{expr 'false 'true})
}
| 'id.name
(if_match "."
(lift1 chain
Func chain 'chain.{'var.name chain})
(pure 'var.name))
| 'sym.text
Cond {
| (STRING.equal text "(")
(bind1 peek
Func token
Let maybe_parser
Match token {
| 'sym.s (special_app_parser 'expr s)
| _ 'nothing
}
In
Match maybe_parser {
| 'just.parser
(ignore1 pop parser)
| 'nothing
(lift2 (rule 'expr) (sequence (rule 'expr) 'just.")")
Func {func args} 'app.{func args})
})
| (STRING.equal text "{")
Let record_init
(bind2
(ignore1 (match ":") id)
(lift1 peek
Func token
Match token {
| 'sym.s
(Or (STRING.equal s ":") (STRING.equal s "}"))
| _ False
})
Func {name is_expr_omitted}
If is_expr_omitted
(pure {name 'var.name})
(lift1 (rule 'expr)
Func expr {name expr}))
In
(if_can_match ":"
(lift1 (sequence record_init 'just."}")
Func labels_and_inits
'record.(LIST.unzip labels_and_inits))
(lift1 (sequence (rule 'expr) 'just."}")
Func exprs
Match (extract_singleton exprs) {
| 'nothing 'tuple.exprs
| 'just.expr expr
}))
| (STRING.equal text "[")
(bind2 (rule 'expr)
(sequence
(lift2 op (rule 'expr)
Func {op right} {op right})
'just."]")
Func {left pairs}
Match pairs {
| 'nil (pure left)
| 'cons.{pair pairs}
Let {op right} pair
In
Let ops_match
(LIST.reduce pairs True
Func {flag {later_op _}}
(And flag (STRING.equal later_op op)))
Let rights
(LIST.map pairs
Func {_ right} right)
In
If ops_match
(infix_expr op left [right & rights])
(fail "Ambiguous infix expression.")
})
| (STRING.equal text "'")
(lift2 id (if_match "." (rule 'expr) (pure empty_tuple))
Func {label expr} 'labeled.{label expr})
| (STRING.equal text "True")
(pure 'true)
| (STRING.equal text "False")
(pure 'false)
| (STRING.equal text "If")
(lift3 (rule 'expr) (rule 'expr) (rule 'expr)
Func {test then else} 'if.{test then else})
| (STRING.equal text "Cond")
Let cond_clause
(ignore1 (match "|")
(lift2 (rule 'expr) (rule 'block_body)
Func {test body} {test body}))
In
(ignore1 (match "{")
(lift1 (sequence cond_clause 'just."}")
Func clauses 'cond.clauses))
| (STRING.equal text "Match")
Let match_clause
(ignore1 (match "|")
(lift2 match_pattern (rule 'block_body)
Func {pat body} {pat body}))
In
(lift2 (rule 'expr)
(ignore1 (match "{") (sequence match_clause 'just."}"))
Func {expr clauses} 'match.{expr clauses})
| (STRING.equal text "Func")
(lift2
(if_match "{"
(sequence pattern 'just."}")
(if_match "_"
(pure ['ignore & 'nil])
(lift1 id
Func name ['var.name & 'nil])))
(rule 'block_body)
Func {pats body} 'func.{pats body})
| (STRING.equal text "Package")
(bind1 peek
Func token
Match token {
| 'str.s
Define (strip_quotes s)
(STRING.clip s 1 [(STRING.length s) - 1])
In
(ignore1 pop (pure 'package.(strip_quotes s)))
| _ (fail "Malformed Package expression.")
})
| (STRING.equal text "Block")
(rule 'block_body)
| (STRING.equal text "Iterate")
(bind2
(if_match "{"
(sequence simple_pattern 'just."}")
(lift1 id
Func name [name & 'nil]))
(if_match "From"
(lift1
(if_match "{"
(sequence (rule 'expr) 'just."}")
(lift1 (rule 'expr) [Func expr [expr & 'nil]]))
Func inits 'just.inits)
(pure 'nothing))
Func {vars maybe_inits}
Let inits
Match maybe_inits {
| 'just.inits inits
| 'nothing (LIST.map vars [Func name 'var.name])
}
In
If [(LIST.length vars) != (LIST.length inits)]
(fail "Wrong number of initializers.")
(lift1 (rule 'block_body)
Func expr 'iterate.{vars inits expr}))
| (STRING.equal text "Unfold")
(bind2
(if_match "{"
(sequence simple_pattern 'just."}")
(lift1 id
Func name [name & 'nil]))
(if_match "From"
(lift1
(if_match "{"
(sequence (rule 'expr) 'just."}")
(lift1 (rule 'expr) [Func expr [expr & 'nil]]))
Func inits 'just.inits)
(pure 'nothing))
Func {vars maybe_inits}
Let inits
Match maybe_inits {
| 'just.inits inits
| 'nothing (LIST.map vars [Func name 'var.name])
}
In
If [(LIST.length vars) != (LIST.length inits)]
(fail "Wrong number of initializers.")
(lift1 (rule 'block_body)
Func expr 'unfold.{vars inits expr}))
| (STRING.equal text "Begin")
(rule 'begin_body)
| (STRING.equal text "When")
(lift2 (rule 'expr) (rule 'begin_body)
Func {test then} 'if.{test then empty_tuple})
}
}
In
(ignore1 (bind1 peek check) (bind1 pop go))
Let binder_group
(bind1 peek
Func token
Let has_binder
Match token {
| 'sym.s (Or (STRING.equal s "Let") (STRING.equal s "Define"))
| _ False
}
In
If has_binder
(lift2 (rule 'binder) (rule 'binder_group) LIST.cons)
(pure 'nil))
Let binder
Let let_binder
(lift2
(if_match "{"
(lift1 (sequence simple_pattern 'just."}")
Func vars
Match (extract_singleton vars) {
| 'nothing 'tuple.vars
| 'just.name 'var.name
})
(lift1 id
Func name 'var.name))
(rule 'block_body)
Func {pat expr} 'let.{pat expr})
Let define_binder
(lift2 (rule 'define_pattern) (rule 'block_body)
Func {{name pats_chain} expr}
Let expr
(LIST.reduce pats_chain expr
Func {expr pats} 'func.{pats expr})
In
'let.{'var.name expr})
In
(bind1 peek
Func token
Let maybe_binder
Match token {
| 'sym.s
Cond {
| (STRING.equal s "Let") 'just.let_binder
| (STRING.equal s "Define") 'just.define_binder
| True 'nothing
}
| _ 'nothing
}
In
Match maybe_binder {
| 'nothing (fail "Expected binder.")
| 'just.binder (ignore1 pop binder)
})
Let block_body
(bind1 peek
Func token
Let has_binder
Match token {
| 'sym.s (Or (STRING.equal s "Let") (STRING.equal s "Define"))
| _ False
}
In
If has_binder
(lift2 (rule 'binder_group) (ignore1 (match "In") (rule 'block_body))
Func {binders expr} 'block.{(LIST.reverse binders) expr})
(rule 'expr))
Let stmt
Let okay (pure {})
In
Define (check token)
Match token {
| 'eof (fail "Unexpected end of file.")
| 'sym.text
Cond {
| (STRING.equal text "(") okay
| (STRING.equal text "[") okay
| (STRING.equal text "If") okay
| (STRING.equal text "Cond") okay
| (STRING.equal text "Match") okay
| (STRING.equal text "Block") okay
| (STRING.equal text "Iterate") okay
| (STRING.equal text "Unfold") okay
| (STRING.equal text "Begin") okay
| (STRING.equal text "When") okay
| True (fail "Unexpected token while parsing statement.")
}
| _ (fail "Unexpected token while parsing statement.")
}
Define (go token)
Match token {
| 'sym.text
Cond {
| (STRING.equal text "(")
(bind1 (ignore1 pop peek)
Func token
Let maybe_parser
Match token {
| 'sym.s (special_app_parser 'stmt s)
| _ 'nothing
}
In
Match maybe_parser {
| 'just.parser
(ignore1 pop parser)
| 'nothing
(lift2 (rule 'expr) (sequence (rule 'expr) 'just.")")
Func {func args} 'app.{func args})
})
| True (rule 'expr)
}
}
In
(bind1 peek
Func token
Let has_binder
Match token {
| 'sym.s (Or (STRING.equal s "Let") (STRING.equal s "Define"))
| _ False
}
In
If has_binder
(rule 'binder)
(ignore1 (bind1 peek check) (bind1 peek go)))
Let begin_body
Let statements
Define (termination_check token)
Match token {
| 'sym.s (Or (STRING.equal s "|") (STRING.equal s "}"))
| _ False
}
Define (combine_statements stmts)
Match (LIST.reverse stmts) {
| 'nil empty_tuple
| 'cons.{final_term definite_stmts}
Let {stmts expr}
Match final_term {
| 'return.exprs
{
definite_stmts
Match (extract_singleton exprs) {
| 'nothing 'tuple.exprs
| 'just.expr expr
}
}
| 'continue._
{
definite_stmts
final_term
}
| 'let.{pat expr}
{
definite_stmts
expr
}
| _
{
'cons.{final_term definite_stmts}
empty_tuple
}
}
In
Iterate {expr stmts binders} From {expr stmts 'nil}
Match stmts {
| 'nil
Match binders {
| 'nil expr
| 'cons._ 'block.{(LIST.reverse binders) expr}
}
| 'cons.{stmt stmts}
Match stmt {
| 'let._
Let expr
Match binders {
| 'nil expr
| 'cons._
'block.{(LIST.reverse binders) expr}
}
In
Let expr 'block.{[stmt & 'nil] expr}
In
(Continue expr stmts 'nil)
| _
Let binder 'let.{empty_tuple stmt}
In
(Continue expr stmts [binder & binders])
}
}
}
In
(lift1 (sequence2 (rule 'stmt) termination_check)
combine_statements)
In
Let cond_clause
(ignore1 (match "|")
(lift2 (rule 'expr) statements
Func {test body} {test body}))
Let match_clause
(ignore1 (match "|")
(lift2 match_pattern statements
Func {pat body} {pat body}))
Define (ensure_completeness mode clauses)
Let clauses (LIST.reverse clauses)
In
Match mode {
| 'cond
Match clauses {
| 'nil [{'true empty_tuple} & 'nil]
| 'cons.{clause _}
Let {test _} clause
In
(LIST.reverse
Match test {
| 'true clauses
| _ [{'true empty_tuple} & clauses]
})
}
| 'match
Match clauses {
| 'nil [{'default empty_tuple} & 'nil]
| 'cons.{clause _}
Let {pat _} clause
In
(LIST.reverse
Match pat {
| 'default clauses
| _ [{'default empty_tuple} & clauses]
})
}
}
In
(if_match "Cond"
(ignore1 (match "{")
(lift1 (sequence cond_clause 'just."}")
Func clauses 'cond.(ensure_completeness 'cond clauses)))
(if_match "Match"
(bind1 (rule 'expr)
Func expr
(ignore1 (match "{")
(lift1 (sequence match_clause 'just."}")
Func clauses
Let clauses (ensure_completeness 'match clauses)
In
'match.{expr clauses})))
(ignore1 (match "{")
(lift2 statements (match "}")
Func {expr _} expr))))
Where
Define (special_app_parser mode s)
Let maybe_parser
Match mode {
| 'stmt
Cond {
| (STRING.equal s "Return")
Let parser
(lift1 (sequence (rule 'expr) 'just.")")
Func exprs 'return.exprs)
In
'just.parser
| True 'nothing
}
| 'expr 'nothing
}
In
Match maybe_parser {
| 'just._ maybe_parser
| 'nothing
Cond {
| (STRING.equal s "Prim")
Let parser
(lift2 id (sequence (rule 'expr) 'just.")")
Func {name args} 'app.{'prim.name args})
In
'just.parser
| (STRING.equal s "Continue")
Let parser
(lift1 (sequence (rule 'expr) 'just.")")
Func exprs 'continue.exprs)
In
'just.parser
| (STRING.equal s "Fold")
Let parser
(lift1 (sequence (rule 'expr) 'just.")")
Func exprs 'fold.exprs)
In
'just.parser
| (STRING.equal s "Reduce")
Let parser
(bind2 op (sequence (rule 'expr) 'just.")")
Func {op exprs}
Match exprs {
| 'nil (fail "Missing arguments.")
| 'cons.{left rights}
Match rights {
| 'nil (pure left)
| 'cons._ (infix_expr op left rights)
}
})
In
'just.parser
| (STRING.equal s "And")
Let parser
(lift1 (sequence (rule 'expr) 'just.")")
Func exprs
Match exprs {
| 'nil 'true
| 'cons.{expr exprs}
(LIST.reduce exprs expr
Func {conj expr} 'and.{conj expr})
})
In
'just.parser
| (STRING.equal s "Or")
Let parser
(lift1 (sequence (rule 'expr) 'just.")")
Func exprs
Match exprs {
| 'nil 'false
| 'cons.{expr exprs}
(LIST.reduce exprs expr
Func {disj expr} 'or.{disj expr})
})
In
'just.parser
| True
'nothing
}
}
Let match_pattern
Let vars
(if_match "{"
(lift1 (sequence simple_pattern 'just."}")
Func vars
Match (extract_singleton vars) {
| 'nothing 'tuple.vars
| 'just.name 'var.name
})
(if_match "_" (pure 'ignore)
(lift1 id
Func name 'var.name)))
In
(bind1 peek
Func token
Match token {
| 'sym.s
Cond {
| (STRING.equal s "_") (ignore1 pop (pure 'default))
| (STRING.equal s "'")
(ignore1 pop
(lift2 id (if_match "." vars (pure empty_tuple))
Func {label vars} 'labeled.{label vars}))
| True (fail "Malformed Match pattern.")
}
| 'eof (fail "Unexpected end of file.")
| _ (fail "Malformed Match pattern.")
})
Let define_pattern
(ignore1 (match "(")
(if_can_match "("
(lift2 (rule 'define_pattern) (sequence pattern 'just.")")
Func {{name more_pats} pats}
{name [pats & more_pats]})
(lift2 id (sequence pattern 'just.")")
Func {name pats}
{name [pats & 'nil]})))
Where
Define (infix_expr op left rights)
Define (app left op right)
Cond {
| (STRING.equal op "compose_left")
'app.{'prim."compose" [left & right & 'nil]}
| (STRING.equal op "compose_right")
'app.{'prim."compose" [right & left & 'nil]}
| (STRING.equal op "apply_left")
'app.{left [right & 'nil]}
| (STRING.equal op "apply_right")
'app.{right [left & 'nil]}
| True
'app.{'prim.op [left & right & 'nil]}
}
In
Let assoc
Cond {
| (STRING.equal op "add") 'left
| (STRING.equal op "subtract") 'not
| (STRING.equal op "multiply") 'left
| (STRING.equal op "quotient") 'not
| (STRING.equal op "remainder") 'not
| (STRING.equal op "less") 'not
| (STRING.equal op "greater") 'not
| (STRING.equal op "equal") 'not
| (STRING.equal op "not_equal") 'not
| (STRING.equal op "less_or_equal") 'not
| (STRING.equal op "greater_or_equal") 'not
| (STRING.equal op "cons") 'right
| (STRING.equal op "compose_left") 'right
| (STRING.equal op "compose_right") 'left
| (STRING.equal op "apply_left") 'right
| (STRING.equal op "apply_right") 'left
}
In
Define (reduce_left op left rights)
(pure
(LIST.reduce rights left
Func {left right} (app left op right)))
Define (reduce_right op left rights)
(pure
Match (LIST.reverse [left & rights]) {
| 'cons.{right lefts}
(LIST.reduce lefts right
Func {right left} (app left op right))
})
Define (reduce_not op left rights)
Match rights {
| 'cons.{right rights}
Match rights {
| 'nil (pure (app left op right))
| 'cons._ (fail "No associativity rule for operator.")
}
}
In
Let reduce
Match assoc {
| 'left reduce_left
| 'right reduce_right
| 'not reduce_not
}
In
(reduce op left rights)
Let pattern
(bind1 peek
Func token
Match token {
| 'id.name (ignore1 pop (pure 'var.name))
| 'sym.s
Cond {
| (STRING.equal s "{")
(ignore1 pop
(lift1 (sequence simple_pattern 'just."}")
Func pats 'tuple.pats))
| (STRING.equal s "_") (ignore1 pop (pure 'ignore))
| True (fail "Invalid pattern.")
}
| 'eof (fail "Unexpected end of file.")
| _ (fail "Unexpected token while parsing pattern.")
})
Where
Let simple_pattern
(bind1 peek
Func token
Match token {
| 'id.name (ignore1 pop (pure name))
| 'sym.s
Cond {
| (STRING.equal s "_") (ignore1 pop (pure "_"))
| True (fail "Invalid pattern.")
}
| 'eof (fail "Unexpected end of file.")
| _ (fail "Unexpected token.")
})
Define (sequence2 parser termination_check)
(bind1 (lift1 peek termination_check)
Func is_terminated
If is_terminated
(pure 'nil)
(lift2 parser (rule 'sequence2.{parser termination_check})
LIST.cons))
Define (sequence parser maybe_terminator)
Let check_for_termination
(lift1 peek
Func token
Match token {
| 'eof True
| 'sym.s
Match maybe_terminator {
| 'nothing False
| 'just.terminator (STRING.equal s terminator)
}
| _ False
})
In
(bind1 check_for_termination
Func is_terminated
If is_terminated
(ignore1 pop (pure 'nil))
(lift2 parser (rule 'sequence.{parser maybe_terminator}) LIST.cons))
Let chain
(bind1 peek
Func token
Let access
Match token {
| 'id.name (ignore1 pop (pure 'id.name))
| 'num.n (ignore1 pop (pure 'num.n))
| 'eof (fail "Unexpected end of file.")
| _ (fail "Unexpected token.")
}
In
(lift2 access (if_match "." (rule 'chain) (pure 'nil))
Func {access chain} [access & chain]))
Where
Let id
(bind1 peek
Func token
Match token {
| 'id.name (ignore1 pop (pure name))
| 'eof (fail "Unexpected end of file.")
| _ (fail "Unexpected token.")
})
Let op
Define (long_name s)
Cond {
| (STRING.equal s "+") "add"
| (STRING.equal s "-") "subtract"
| (STRING.equal s "*") "multiply"
| (STRING.equal s "/") "quotient"
| (STRING.equal s "%") "remainder"
| (STRING.equal s "<") "less"
| (STRING.equal s ">") "greater"
| (STRING.equal s "=") "equal"
| (STRING.equal s "!=") "not_equal"
| (STRING.equal s "<=") "less_or_equal"
| (STRING.equal s ">=") "greater_or_equal"
| (STRING.equal s "&") "cons"
| (STRING.equal s "<<") "compose_left"
| (STRING.equal s ">>") "compose_right"
| (STRING.equal s "<-") "apply_left"
| (STRING.equal s "->") "apply_right"
}
In
(bind1 peek
Func token
Match token {
| 'op.s (ignore1 pop (pure (long_name s)))
| _ (fail "Unexpected token.")
})
Where
Define (match s)
(bind1 peek
Func token
If (is_symbol_with_text token s)
pop
(fail (STRING.concat ["Expected \"" & s & "\"." & 'nil])))
Define (if_match s then else)
(bind1 peek
Func token
If (is_symbol_with_text token s)
(ignore1 pop then)
else)
Define (if_can_match s then else)
(bind1 peek
Func token
If (is_symbol_with_text token s)
then
else)
Where
Define (is_symbol_with_text token s)
Match token {
| 'sym.t (STRING.equal s t)
| _ False
}
Define (extract_singleton items)
Match items {
| 'nil 'nothing
| 'cons.{item more_items}
Match more_items {
| 'nil 'just.item
| 'cons._ 'nothing
}
}
Let empty_tuple 'tuple.'nil
Where
Define (lift3 p1 p2 p3 f)
(bind1 p1
Func x1
(bind1 p2
Func x2
(bind1 p3
Func x3 (pure (f x1 x2 x3)))))
Define (lift2 p1 p2 f)
(bind1 p1
Func x1
(bind1 p2
Func x2 (pure (f x1 x2))))
Define (lift1 p1 f)
(bind1 p1
Func x1 (pure (f x1)))
Define (bind2 p1 p2 f)
(bind1 p1
Func x1
(bind1 p2
Func x2 (f x1 x2)))
Define (ignore1 p1 p2)
(bind1 p1
Func _ p2)
Where
Let pop 'pop
Let peek 'peek
Define (rule term) 'rule.term
Define (fail message) 'fail.message
Define (pure x) 'pure.x
Define (bind1 p1 f) 'bind1.{p1 f}
Where
Let LIST Package "list"
Let OS Package "os"
Let SCAN Package "scan"
Let STDIO Package "stdio"
Let STRING Package "string"