-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop.scm
1091 lines (1023 loc) · 37.9 KB
/
loop.scm
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
;;; **********************************************************************
;;;
;;; Copyright (C) 2002 Heinrich Taube ([email protected])
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License
;;; as published by the Free Software Foundation; either version 2
;;; of the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; **********************************************************************
;;; $Name: $
;;; $Revision: 1.6 $
;;; $Date: 2005/11/17 13:29:37 $
;;;
;;; Implementation of the CLTL2 loop macro. The following
;;; non Rev 5 definitions need to be in effect before the file
;;; is loaded:
;;;
;;; (define-macro (name . args) ...)
;;; (error string)
;;; (gensym string)
;;;
(define-macro (loop . args)
(scheme-loop args))
(define-macro (push val sym)
`(begin (set! ,sym (cons ,val ,sym)) ,sym))
(define-macro (pop sym)
(let ((v (gensym "v")))
`(let ((,v (car ,sym)))
(set! ,sym (cdr ,sym))
,v)))
;;; this next one is a no-op but i need it as a marker for my cltl2
;;; translator.
(define-macro (function sym) sym)
;;; getters and setters for the loop-clause "struct"
(define (loop-operator c) (vector-ref c 0))
(define (loop-operator-set! c x) (vector-set! c 0 x))
(define (loop-bindings c) (vector-ref c 1))
(define (loop-bindings-set! c x) (vector-set! c 1 x))
(define (loop-collectors c) (vector-ref c 2))
(define (loop-collectors-set! c x) (vector-set! c 2 x))
(define (loop-initially c) (vector-ref c 3))
(define (loop-initially-set! c x) (vector-set! c 3 x))
(define (loop-end-tests c) (vector-ref c 4))
(define (loop-end-tests-set! c x) (vector-set! c 4 x))
(define (loop-looping c) (vector-ref c 5))
(define (loop-looping-set! c x) (vector-set! c 5 x))
(define (loop-stepping c) (vector-ref c 6))
(define (loop-stepping-set! c x) (vector-set! c 6 x))
(define (loop-finally c) (vector-ref c 7))
(define (loop-finally-set! c x) (vector-set! c 7 x))
(define (loop-returning c) (vector-ref c 8))
(define (loop-returning-set! c x) (vector-set! c 8 x))
(define (make-loop-clause . args)
(let ((v (vector #f '() '() '() '() '() '() '() '())))
(if (null? args) v
(do ((a args (cddr a)))
((null? a) v)
(case (car a)
((operator) (loop-operator-set! v (cadr a)))
((bindings) (loop-bindings-set! v (cadr a)))
((collectors) (loop-collectors-set! v (cadr a)))
((initially) (loop-initially-set! v (cadr a)))
((end-tests) (loop-end-tests-set! v (cadr a)))
((looping) (loop-looping-set! v (cadr a)))
((stepping) (loop-stepping-set! v (cadr a)))
((finally) (loop-finally-set! v (cadr a)))
((returning) (loop-returning-set! v (cadr a))))))))
(define (gather-clauses caller clauses)
;; nconc all clausal expressions into one structure
(let ((gather-clause
(lambda (clauses accessor)
;; append data from clauses
(do ((l '()))
((null? clauses) l)
(set! l (append l (accessor (car clauses))))
(set! clauses (cdr clauses))))))
(make-loop-clause 'operator caller
'bindings
(gather-clause clauses
(function loop-bindings))
'collectors
(gather-clause clauses
(function loop-collectors))
'initially
(gather-clause clauses
(function loop-initially))
'end-tests
(gather-clause clauses
(function loop-end-tests))
'looping
(gather-clause clauses
(function loop-looping))
'stepping
(gather-clause clauses
(function loop-stepping))
'finally
(gather-clause clauses
(function loop-finally))
'returning
(gather-clause clauses
(function loop-returning)))))
(define (loop-op? x ops)
(assoc x ops))
(define (loop-variable? x)
(symbol? x))
(define (make-binding var val)
(list var val))
(define (loop-error ops forms . args)
;; all error messages include error context.
(let ((loop-context
(lambda (lst ops)
;; return tail of expr up to next op in cdr of tail
(do ((h lst)
(l '()))
((or (null? lst)
;; ignore op if in front.
(and (not (eq? h lst))
(loop-op? (car lst) ops)))
(reverse l))
(set! l (cons (car lst) l))
(set! lst (cdr lst))))))
(let ((forms (loop-context forms ops)))
(newline)
(display "LOOP ERROR: ")
(do ((tail args (cdr tail)))
((null? tail) #f)
(display (car tail)))
(newline)
(display "clause context: ")
(if (null? forms)
(display "()")
(do ((tail forms (cdr tail)))
((null? tail) #f)
(if (eq? tail forms) (display "'"))
(display (car tail))
(display (if (null? (cdr tail)) "'" " "))))
(newline)
(error "illegal loop syntax"))))
(define (parse-for forms clauses ops)
;; forms is (FOR ...)
(let ((op (loop-op? (car forms) ops)))
(if (null? (cdr forms))
(loop-error ops forms "Variable expected but source code ran out." )
(let ((var (cadr forms)))
(if (loop-variable? var)
(if (null? (cddr forms))
(loop-error ops forms
"'for' clause expected but source code ran out.")
;; find the iteration path in the op
(let ((path (assoc (caddr forms) (cdddr op))))
;; path is (<pathop> <parser>)
(if (not path)
(loop-error ops forms "'" (caddr forms) "'"
" is not valid with 'for'.")
( (cadr path) forms clauses ops))))
(loop-error ops forms "Found '" (cadr forms)
"' where a variable expected."))))))
(define (parse-numerical-for forms clauses ops)
;; forms is (FOR <var> <OP> ...)
;; where <OP> is guaranteed to be one of: FROM TO BELOW ABOVE DOWNTO
clauses
(let ((var (cadr forms))
(tail (cddr forms))
(bind '())
(from #f)
(head #f)
(last #f)
(stop #f)
(step #f)
(test #f)
(incr #f))
(do ((next #f))
((or (null? tail) (loop-op? (car tail) ops)))
(set! next (pop tail))
(if (null? tail)
(loop-error ops forms
"Expected expression but source code ran out."))
(case next
((from downfrom)
(if head (loop-error ops forms "Found '" next "' when '"
head "' in effect."))
(set! head next)
(set! from (pop tail)))
((below)
(if last (loop-error ops forms "Found '" next "' when '"
last "' in effect."))
(set! stop (pop tail))
(set! last next))
((to)
(if last (loop-error ops forms "Found '" next "' when '"
last "' in effect."))
(set! stop (pop tail) )
(set! last next))
((above )
(if last (loop-error ops forms "Found '" next "' when '"
last "' in effect."))
(set! stop (pop tail))
(set! last next))
((downto )
(if last (loop-error ops forms "Found '" next "' when '"
last "' in effect."))
(set! stop (pop tail))
(set! last next))
((by)
(if step (loop-error ops forms "Found duplicate 'by'."))
(set! step (pop tail)))
(else
(loop-error ops forms
"'" next "' is not valid with 'for'."))))
(if (not head)
(set! head 'from))
(if (or (eq? head 'downfrom)
(eq? last 'downto)
(eq? last 'above))
(begin
(set! incr '-)
(if (eq? last 'above)
(set! test '<=)
(set! test '<))) ; allow to for downto
(begin
(set! incr '+)
(if (eq? last 'below)
(set! test '>=)
(set! test '>))))
;; add binding for initial value
(push (make-binding var (or from 0)) bind)
;; add binding for non-constant stepping values.
(if (not step)
(set! step 1)
(if (not (number? step))
(let ((var (gensym "v")))
(push (make-binding var step) bind)
(set! step var))))
(set! step `(set! ,var (,incr ,var ,step)))
(if stop
(let ((end (gensym "v")))
(push (make-binding end stop) bind)
(set! stop (list test var end))))
(values (make-loop-clause 'operator 'for
'bindings (reverse bind)
'stepping (list step)
'end-tests (if (not stop)
'() (list stop)))
tail)))
(define (parse-repeat forms clauses ops)
;; forms is (REPEAT <FORM> ...)
(if (null? (cdr forms))
(loop-error ops forms
"'repeat' clause expected but source code ran out." )
(call-with-values (lambda ()
(parse-numerical-for
(list 'for (gensym "v") 'below (cadr forms))
clauses ops))
(lambda (clause ignore)
ignore
(values clause (cddr forms))))))
(define (parse-sequence-iteration forms clauses ops)
;; tail is (FOR <var> <OP> ...)
;; <OP> is guaranteed to be one of: IN ON ACROSS
clauses
(let ((head forms)
(var (cadr forms))
(seq (gensym "v"))
(tail (cddr forms))
(bind '())
(data #f)
(init '())
(loop '())
(incr #f)
(stop '())
(step '())
(type #f))
(do ((next #f))
((or (null? tail) (loop-op? (car tail) ops)))
(set! next (pop tail))
(when (null? tail)
(loop-error ops head
"Expression expected but source code ran out." ))
(case next
((in on across)
(if type (loop-error ops head
"Extraneous '" next "' when '"
type "' in effect."))
(set! type next)
(set! data (pop tail)))
((by )
(if incr
(loop-error ops head "Duplicate 'by'." )
(if (eq? type 'across)
(loop-error ops head "'by' is invalid with 'across'." )
(set! incr (pop tail)))))
(else
(loop-error ops head "'" next "' is not valid with 'for'."))))
; add bindings for stepping var and source
(push (make-binding var #f) bind)
(push (make-binding seq data) bind)
(if (eq? type 'across)
(let ((pos (gensym "v"))
(max (gensym "v")))
(push (make-binding pos 0) bind)
(push (make-binding max #f) bind)
(push `(set! ,max (vector-length ,seq)) init)
(push `(set! ,pos (+ 1 ,pos)) step)
(push `(set! ,var (vector-ref ,seq ,pos)) loop)
(push `(>= ,pos ,max) stop))
(begin
(if incr
(if (and (list? incr) (eq? (car incr) 'quote))
(push `(set! ,seq (,(cadr incr) ,seq)) step)
(push `(set! ,seq (,incr ,seq)) step))
(push `(set! ,seq (cdr ,seq)) step))
(push (if (eq? type 'in)
`(set! ,var (car ,seq))
`(set! ,var ,seq))
loop)
(push `(null? ,seq) stop)))
(values (make-loop-clause 'operator 'for
'bindings (reverse bind)
'end-tests stop
'initially init
'looping loop
'stepping step)
tail)))
(define (parse-general-iteration forms clauses ops)
;; forms is (FOR <var> = ...)
clauses
(let ((head forms)
(var (cadr forms))
(tail (cddr forms))
(init #f)
(type #f)
(loop #f)
(step #f))
(do ((next #f))
((or (null? tail) (loop-op? (car tail) ops)))
(set! next (pop tail))
(if (null? tail)
(loop-error ops head
"Expression expected but source code ran out."))
(case next
((= )
(if type (loop-error ops head "Duplicate '='."))
(set! loop `(set! ,var ,(pop tail)))
(set! type next))
((then )
(if init (loop-error ops head "Duplicate 'then'."))
(set! init loop)
(set! loop #f)
(set! step `(set! ,var ,(pop tail)))
(set! type next))
(else
(loop-error ops head "'" next "' is not valid with 'for'."))))
(values (make-loop-clause 'operator 'for
'bindings (list (make-binding var #f))
'initially (if init (list init) '())
'looping (if loop (list loop) '())
'stepping (if step (list step) '()))
tail)))
(define (parse-with forms clauses ops)
;; forms is (WITH <var> = ...)
clauses
(let ((head forms)
(tail (cdr forms))
(var #f)
(expr #f)
(and? #f)
(bind '())
(init '()))
(do ((need #t)
(next #f))
((or (null? tail) (loop-op? (car tail) ops)))
(set! next (pop tail))
(cond ((and (loop-variable? next) need)
(if var
(loop-error ops head
"Found '" next "' where 'and' expected."))
(if expr
(loop-error ops head
"Found '" next "' where 'and' expected."))
(set! var next)
(set! expr #f)
(set! and? #f)
(set! need #f))
((eq? next 'and)
(if and?
(loop-error ops head "Duplicate 'and'.")
(if var
(if expr
(begin
(push (make-binding var #f) bind)
(push `(set! ,var ,expr) init))
(push (make-binding var #f) bind))
(loop-error ops head "Extraneous 'and'.")))
(set! var #f)
(set! expr #f)
(set! and? #t)
(set! need #t))
((eq? next '=)
(if expr
(loop-error ops head
"Found '=' where 'and' expected.")
(set! expr (pop tail))))
(else
(if need
(loop-error ops head
"Found '" next "' where variable expected.")
(loop-error ops head "Found '" next
"' where '=' or 'and' expected.")))))
(if and?
(loop-error ops head "Extraneous 'and'.")
(if var
(if expr
(begin (push (make-binding var #f) bind)
(push `(set! ,var ,expr) init))
(push (make-binding var #f) bind))))
(values (make-loop-clause 'operator 'with
'bindings (reverse bind)
'initially (reverse init))
tail)))
(define (parse-do forms clauses ops)
clauses
(let ((head forms)
(oper (pop forms))
(body '()))
(do ()
((or (null? forms)
(loop-op? (car forms) ops))
(if (null? body)
(loop-error ops head "Missing '" oper "' expression.")
(set! body (reverse body))))
(push (car forms) body)
(set! forms (cdr forms)))
(values
(make-loop-clause 'operator oper 'looping body)
forms)))
(define (parse-finally forms clauses ops)
clauses
(let ((oper (pop forms))
(expr #f))
(if (null? forms)
(loop-error ops forms "Missing '" oper "' expression."))
(set! expr (pop forms))
(values (make-loop-clause 'operator oper 'finally (list expr))
forms)))
(define (parse-initially forms clauses ops)
clauses
(let ((oper (pop forms))
(expr #f))
(if (null? forms)
(loop-error ops forms "Missing '" oper "' expression."))
(set! expr (pop forms))
(values (make-loop-clause 'operator oper 'initially (list expr))
forms)))
(define (lookup-collector var clauses)
;; collector is list: (<var> <type> <acc> <head>)
;; returns the clause where the collect variable VAR is
;; actually bound or nil if var hasn't already been bound
;; if var is nil only the single system allocated collecter
;; is possibly returned.
(let ((checkthem (lambda (var lis)
(do ((a #f))
((or (null? lis) a) a)
(if (eq? var (car (car lis))) ;collector-var
(set! a (car lis)))
(set! lis (cdr lis))))))
(do ((c #f))
((or (null? clauses) c) c)
(set! c (checkthem var (loop-collectors (car clauses))))
(set! clauses (cdr clauses)))))
(define (compatible-accumulation? typ1 typ2)
(let ((l1 '(collect append nconc))
(l2 '(never always))
(l3 '(minimize maximize)))
(or (eq? typ1 typ2)
(and (member typ1 l1) (member typ2 l1))
(and (member typ1 l2) (member typ2 l2))
(and (member typ1 l3) (member typ2 l3)))))
(define (parse-accumulation forms clauses ops)
;; forms is (<op> form ...)
;; where <op> is collect append nconc
(let ((save forms)
(oper (pop forms))
(make-collector (lambda (var type acc head)
(list var type acc head)))
;; removed because noop
;;(collector-var (lambda (col) (car col)))
(collector-type (lambda (col) (cadr col)))
(collector-acc (lambda (col) (caddr col)))
(collector-head (lambda (col) (cadddr col)))
(expr #f)
(coll #f)
(new? #f)
(into #f)
(loop '())
(bind '())
(init '())
(tests '())
(return '()))
(if (null? forms)
(loop-error ops forms "Missing '" oper "' expression."))
(set! expr (pop forms))
(if (not (null? forms))
(if (eq? (car forms) 'into)
(begin
(if (null? (cdr forms))
(loop-error ops save "Missing 'into' variable."))
(if (loop-variable? (cadr forms))
(begin (set! into (cadr forms))
(set! forms (cddr forms)))
(loop-error ops save "Found '" (car forms)
"' where 'into' variable expected.")))))
;; search for a clause that already binds either the user specified
;; accumulator (into) or a system allocated one if no into.
;; system collectors
;; o only one allowed, all accumuations must be compatible
;; o returns value
;; value collector: (nil <op> <#:acc>)
;; list collector: (nil <op> <#:tail> <#:head>)
;; into collectors
;; o any number allowed
;; o returns nothing.
;; value collector: (<into> <op> <into> )
;; list collector: (<into> <op> <#:tail> <#:head>)
(set! coll (lookup-collector into clauses))
(if (not coll)
(set! new? #t)
;; accumulator already established by earlier clause
;; check to make sure clauses are compatible.
(if (not (compatible-accumulation? oper (collector-type coll)))
(loop-error ops save "'" (collector-type coll)
"' and '" oper "' are incompatible accumulators.")))
(case oper
((sum count)
(let ((acc #f))
(if new?
(begin
(set! acc (or into (gensym "v")))
(push (make-binding acc 0) bind)
;; coll= (nil <op> <#:acc>) or (<into> <op> <into>)
(set! coll (make-collector into oper acc #f))
;; only add a return value if new collector isnt into
(if (not into) (push acc return)))
(set! acc (collector-acc coll)))
(if (eq? oper 'sum)
(push `(set! ,acc (+ ,acc ,expr)) loop)
(push `(if ,expr (set! ,acc (+ ,acc 1))) loop))))
((minimize maximize)
(let ((var (gensym "v"))
(opr (if (eq? oper 'minimize) '< '>))
(acc #f))
(if new?
(begin
(set! acc (or into (gensym "v")))
(push (make-binding acc #f) bind)
;; coll= (nil <op> <#:acc>) or (<into> <op> <into>)
(set! coll (make-collector into oper acc #f))
;; only add a return value if new collector isnt into
(if (not into) (push `(or ,acc 0) return)))
(set! acc (collector-acc coll)))
(push (make-binding var #f) bind)
(push `(begin (set! ,var ,expr)
(if (or (not ,acc)
(,opr ,var ,acc))
(set! ,acc ,var)))
loop)))
((append collect nconc)
;; for list accumulation a pointer to the tail of the list
;; is updated and the head of the list is returned. any
;; into variable is set to the head inside the loop.
(let ((head #f)
(tail #f))
(if (not new?)
(begin (set! tail (collector-acc coll))
(set! head (collector-head coll)))
(begin
(if into (push (make-binding into '(list)) bind))
(set! tail (gensym "v"))
;; allocate a pointer to the head of list
(set! head (gensym "v"))
(push (make-binding head '(list #f)) bind)
(push (make-binding tail #f) bind)
;; initialize tail to head
(push `(set! ,tail ,head) init)
(set! coll (make-collector into oper tail head))
;; only add a return value if new collector isnt into
(if (not into)
(push `(cdr ,head) return))))
;; add loop accumulation forms
(if (eq? oper 'append)
(begin
(push `(set-cdr! ,tail (append ,expr (list))) loop)
(push `(set! ,tail (last-pair ,tail)) loop))
(if (eq? oper 'collect)
(begin
(push `(set-cdr! ,tail (list ,expr)) loop)
(push `(set! ,tail (cdr ,tail)) loop))
(begin
(push `(set-cdr! ,tail ,expr) loop)
(push `(set! ,tail (last-pair ,tail)) loop))))
;; update user into variable inside the main loop
;; regardless of whether its a new collector or not
(if into
(push `(set! ,into (cdr ,head)) loop)))))
(values (make-loop-clause 'operator oper
'bindings (reverse bind)
'initially (reverse init)
'looping (reverse loop)
'returning (reverse return)
'collectors (if new? (list coll) '())
'end-tests (reverse tests))
forms)))
;(define (loop-stop expr)
; `(%done% ,expr))
(define (loop-return expr)
`(return ,expr))
(define (parse-while-until forms clauses ops)
clauses
(let ((head forms)
(oper (pop forms))
(test #f)
(stop '(go #t))) ; :done
(if (null? forms)
(loop-error ops head "Missing '" oper "' expression."))
(case oper
((until ) (set! test (pop forms)))
((while ) (set! test `(not ,(pop forms)))))
;; calls the DONE continuation.
(values (make-loop-clause 'operator oper
'looping (list `(if ,test ,stop)))
forms)))
(define (parse-thereis forms clauses ops)
clauses
(let ((oper (car forms))
(expr #f)
(bool #f)
(func #f))
(if (null? (cdr forms))
(loop-error ops forms "Missing '" (car forms) "' expression." ))
(set! expr (cadr forms))
;; fourth element of operator definition must be
;; a function that returns the stop expression.
(set! func (cadddr (loop-op? oper ops) ))
(case oper
((thereis )
;; return true as soon as expr is true or false at end
(set! bool #f))
((always )
;; return false as soon as expr is false, or true at end
(set! expr `(not ,expr))
(set! bool #t))
((never )
;; return false as soon as expr is true, or true at end
(set! bool #t)))
(set! forms (cddr forms))
;; this calls the RETURN continuation
(values (make-loop-clause 'operator 'thereis
'looping
(list `(if ,expr ,(func (not bool))))
'returning
(list bool))
forms)))
(define (parse-return forms clauses ops)
clauses
(let ((oper (car forms))
(expr #f)
(func #f))
(if (null? (cdr forms))
(loop-error ops forms "Missing '" (car forms) "' expression."))
(set! expr (cadr forms))
(set! forms (cddr forms))
;; fourth element of operator definition must be
;; a function that returns the stop expression.
(set! func (cadddr (loop-op? oper ops) ))
;; this calls the RETURN continuation
(values (make-loop-clause 'operator 'return
'looping `(,(func expr)))
forms)))
(define (legal-in-conditional? x ops)
;; FIXED (member (loop-operator...))
(let ((op (loop-op? x ops)))
(if (and op
(not (null? (cddr op)))
(eq? (caddr op) 'task)
(not (member (car op) '(thereis never always))))
op #f)))
(define (parse-then-else-dependents forms clauses ops)
(let ((previous forms)
(stop? #f)
(parsed '()))
(do ((op #f)
(clause #f)
(remains #f))
((or (null? forms) stop?))
(set! op (legal-in-conditional? (car forms) ops))
(if (not op)
(loop-error ops previous "'" (car forms)
"' is not conditional operator."))
;(multiple-value-setq
; (clause remains)
; ( (cadr op) forms (append clauses parsed) ops))
(call-with-values
(lambda () ( (cadr op) forms (append clauses parsed) ops))
(lambda (a b) (set! clause a) (set! remains b)))
;(format #t "~%after call clause=~s forms=~S" clause forms)
(set! parsed (append parsed (list clause)))
(set! previous forms)
(set! forms remains)
(if (not (null? forms))
(if (eq? (car forms) 'and)
(begin
(set! forms (cdr forms))
(if (null? forms)
(loop-error ops previous "Missing 'and' clause.")))
(if (eq? (car forms) 'else)
(set! stop? #t)
(if (loop-op? (car forms) ops)
(set! stop? #t))))))
(values parsed forms)))
(define (parse-conditional forms clauses ops)
(let ((ops (cons '(else ) ops))
(save forms)
(oper (car forms))
(loop (list)) ; avoid '() because of acl bug
(expr (list))
(then (list))
(else (list)))
(if (null? (cdr forms))
(loop-error ops save "Missing '" oper "' expression."))
(set! forms (cdr forms))
(set! expr (pop forms))
(if (null? forms)
(loop-error ops forms "Missing conditional clause."))
(if (eq? oper 'unless)
(set! expr (list 'not expr)))
(call-with-values
(lambda () (parse-then-else-dependents forms clauses ops))
(lambda (a b)
(set! then a)
(set! forms b)))
;; combine dependant clauses if more than one
(if (not (null? (cdr then)))
(set! then (gather-clauses (list) then))
(set! then (car then)))
(loop-operator-set! then 'if)
;; this (if ...) is hacked so that it is a newly
;; allocated list. otherwise acl and clisp have a
;; nasty structure sharing problem.
(set! loop (list 'if expr
(append `(begin ,@(loop-looping then)) (list))
#f))
(if (and (not (null? forms))
(eq? (car forms) 'else))
(begin
(set! forms (cdr forms))
(when (null? forms)
(loop-error ops save "Missing 'else' clause."))
(call-with-values
(lambda ()
(parse-then-else-dependents
forms (append clauses (list then))
ops))
(lambda (a b) (set! else a) (set! forms b)))
(if (not (null? (cdr else)))
(set! else (gather-clauses '() else))
(set! else (car else)))
(set-car! (cdddr loop) `(begin ,@(loop-looping else)))
;; flush loop forms so we dont gather actions.
(loop-looping-set! then '())
(loop-looping-set! else '())
(set! then (gather-clauses 'if (list then else)))))
(loop-looping-set! then (list loop))
(values then forms)))
(define (parse-clauses forms cond? ops)
(if (or (null? forms)
(not (symbol? (car forms))))
(list (make-loop-clause 'operator 'do 'looping forms))
(let ((op-type? (lambda (op type)
(and (not (null? (cddr op)))
(eq? (caddr op) type)))))
(let ((previous forms)
(clauses '()))
(do ((op #f)
(clause #f)
(remains '())
(body '()) )
((null? forms))
(if (and cond? (eq? (car forms) 'and))
(pop forms))
(set! op (loop-op? (car forms) ops))
(if (not op)
(loop-error ops previous "Found '" (car forms)
"' where operator expected."))
;(multiple-value-setq (clause remains)
; ((cadr op) forms clauses ops))
(call-with-values
(lambda () ( (cadr op) forms clauses ops))
(lambda (a b)
(set! clause a)
(set! remains b)))
(if (op-type? op 'task)
(set! body op)
(if (op-type? op 'iter)
(if (not (null? body))
(loop-error ops previous "'" (car op)
"' clause cannot follow '"
(car body) "'."))))
(set! previous forms)
(set! forms remains)
(set! clauses (append clauses (list clause))))
clauses))))
(define (parse-iteration caller forms ops)
(gather-clauses caller (parse-clauses forms '() ops)))
;;;
;;; loop implementation
;;;
(define *loop-operators*
;; each clause is (<op> <parser> <tag> . <whatever>)
(list (list 'with (function parse-with) #f)
(list 'initially (function parse-initially) #f)
(list 'repeat (function parse-repeat) 'iter)
(list 'for (function parse-for) 'iter
(list 'from (function parse-numerical-for))
(list 'downfrom (function parse-numerical-for))
(list 'below (function parse-numerical-for))
(list 'to (function parse-numerical-for))
(list 'above (function parse-numerical-for))
(list 'downto (function parse-numerical-for))
(list 'in (function parse-sequence-iteration))
(list 'on (function parse-sequence-iteration))
(list 'across (function parse-sequence-iteration))
(list '= (function parse-general-iteration)))
(list 'as (function parse-for) 'iter)
(list 'do (function parse-do) 'task)
(list 'collect (function parse-accumulation) 'task)
(list 'append (function parse-accumulation) 'task)
(list 'nconc (function parse-accumulation) 'task)
(list 'sum (function parse-accumulation) 'task)
(list 'count (function parse-accumulation) 'task)
(list 'minimize (function parse-accumulation) 'task)
(list 'maximize (function parse-accumulation) 'task)
(list 'thereis (function parse-thereis) 'task
(function loop-return))
(list 'always (function parse-thereis) 'task
(function loop-return))
(list 'never (function parse-thereis) 'task
(function loop-return))
(list 'return (function parse-return) 'task
(function loop-return))
(list 'while (function parse-while-until) #f )
(list 'until (function parse-while-until) #f )
(list 'when (function parse-conditional) 'task)
(list 'unless (function parse-conditional) 'task)
(list 'if (function parse-conditional) 'task)
(list 'finally (function parse-finally) #f)))
;;;
;;; loop expansions for scheme and cltl2
;;;
(define (scheme-loop forms)
(let ((name (gensym "v"))
(parsed (parse-iteration 'loop forms *loop-operators*))
(end-test '())
(done '(go #t)) ; :done
(return #f))
;(write (list :parsed-> parsed))
;; cltl2's loop needs a way to stop iteration from with the run
;; block (the done form) and/or immediately return a value
;; (the return form). scheme doesnt have a block return or a
;; go/tagbody mechanism these conditions are implemented using
;; continuations. The forms that done and return expand to are
;; not hardwired into the code because this utility is also used
;; by CM's 'process' macro. Instead, the done and return forms
;; are returned by functions assocated with the relevant operator
;; data. For example, the function that returns the return form
;; is stored as the fourth element in the return operator data.
;; and the done function is stored in the while and until op data.
;; the cadddr of the RETURN operator is a function that
;; provides the form for immediately returning a value
;; from the iteration.
(let ((returnfn (cadddr (assoc 'return *loop-operators*))))
(set! return (returnfn
(if (null? (loop-returning parsed))
#f
(car (loop-returning parsed))))))
;; combine any end-tests into a single IF expression
;; that calls the (done) continuation if true. multiple
;; tests are OR'ed togther
(set! end-test
(let ((ends (loop-end-tests parsed)))
(if (null? ends)
'()
(list
`(if ,(if (null? (cdr ends))
(car ends)
(cons 'or ends))
;; calls the done continuation
,done
#f)))))
`(let (,@ (loop-bindings parsed))
,@(loop-initially parsed)
(call-with-current-continuation
(lambda (return) ; <- (return) returns from this lambda
(call-with-current-continuation
(lambda (go) ; <- (go #t) returns from this lambda
;; a named let provides the actual looping mechanism.
;; the various tests and actions may exit via the
;; (done) or (return) continuations.
(let ,name ()
,@end-test
,@(loop-looping parsed)
,@(loop-stepping parsed)
(,name))))
;; this is the lexical point for (go #t) continuation.
,@(loop-finally parsed)
;; invoke the RETURN continuation with loop value or #f
,return)))))
;;;
;;; these two function are never called in scheme but they are
;;; translated to the cltl sources.
;;;
(define-macro (iter . args)
(cltl2-loop args))
(define (cltl2-loop forms)
(let* ((iter (parse-iteration 'iter forms *loop-operators*))
;; the cadddr of RETURN operator is a function that
;; returns the form for returning from iteration.
(retn (apply (cadddr (assoc 'return *loop-operators*))
(list (car (loop-returning iter))))))
`(, 'let ,(loop-bindings iter)
,@(loop-initially iter)