-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.scm
16015 lines (14589 loc) · 606 KB
/
lint.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
;;; lint for s7 scheme
;;;
;;; (lint "file.scm") checks file.scm for infelicities
;;; to control the kinds of checks, set the variables below.
;;; for tests and examples, see lint-test in s7test.scm
(provide 'lint.scm)
(define *report-unused-parameters* #f) ; many of these are reported anyway if they are passed some non-#f value
(define *report-unused-top-level-functions* #f) ; very common in Scheme, but #t makes the ghastly leakage of names obvious
(define *report-shadowed-variables* #f) ; shadowed parameters, etc
(define *report-undefined-identifiers* #f) ; names we can't account for
(define *report-multiply-defined-top-level-functions* #f) ; top-level funcs defined in more than one file
(define *report-nested-if* 4) ; 3 is lowest, this sets the nesting level that triggers an if->cond suggestion
(define *report-short-branch* 12) ; controls when a lop-sided if triggers a reordering suggestion
(define *report-one-armed-if* 90) ; if -> when/unless, can be #f/#t; if an integer, sets tree length which triggers revision (80 is too small)
(define *report-loaded-files* #f) ; if load is encountered, include that file in the lint process
(define *report-any-!-as-setter* #t) ; unknown funcs/macros ending in ! are treated as setters
(define *report-function-stuff* #t) ; checks for missed function uses etc
(define *report-doc-strings* #f) ; old-style (CL) doc strings
(define *report-func-as-arg-arity-mismatch* #f) ; as it says... (slow, and this error almost never happens)
(define *report-constant-expressions-in-do* #f) ; kinda dumb
(define *report-bad-variable-names* '(l ll O)) ; bad names -- a list to check such as:
;;; '(l ll data datum new item info temp tmp val vals value foo bar baz aux dummy O var res retval result count str)
(define *report-built-in-functions-used-as-variables* #f) ; string and length are the most common cases
(define *report-forward-functions* #f) ; functions used before being defined
(define *report-sloppy-assoc* #t) ; i.e. (cdr (assoc x y)) and the like
(define *report-bloated-arg* 24) ; min arg expr tree size that can trigger a rewrite-as-let suggestion
(define *lint* #f) ; the lint let
;; this gives other programs a way to extend or edit lint's tables: for example, the
;; table of functions that are simple (no side effects) is (*lint* 'no-side-effect-functions)
;;; --------------------------------------------------------------------------------
(when (provided? 'pure-s7)
(define (make-polar mag ang) (complex (* mag (cos ang)) (* mag (sin ang))))
(define (char-ci=? . chars) (apply char=? (map char-upcase chars)))
(define (char-ci<=? . chars) (apply char<=? (map char-upcase chars)))
(define (char-ci>=? . chars) (apply char>=? (map char-upcase chars)))
(define (char-ci<? . chars) (apply char<? (map char-upcase chars)))
(define (char-ci>? . chars) (apply char>? (map char-upcase chars)))
(define (string-ci=? . strs) (apply string=? (map string-upcase strs)))
(define (string-ci<=? . strs) (apply string<=? (map string-upcase strs)))
(define (string-ci>=? . strs) (apply string>=? (map string-upcase strs)))
(define (string-ci<? . strs) (apply string<? (map string-upcase strs)))
(define (string-ci>? . strs) (apply string>? (map string-upcase strs)))
(define (let->list e)
(if (let? e)
(reverse! (map values e))
(error 'wrong-type-arg "let->list argument should be an environment: ~A" str))))
(format *stderr* "loading lint.scm~%")
(set! reader-cond #f)
(define-macro (reader-cond . clauses) `(values)) ; clobber reader-cond to avoid (incorrect) unbound-variable errors
#|
;; debugging version
(define-expansion (lint-format str caller . args)
`(begin
(format outport "lint.scm line ~A~%" ,(port-line-number))
(lint-format-1 ,str ,caller ,@args)))
|#
;;; --------------------------------------------------------------------------------
(define lint
(let ((no-side-effect-functions
(let ((ht (make-hash-table)))
(for-each
(lambda (op)
(hash-table-set! ht op #t))
'(* + - / < <= = > >=
abs acos acosh and angle append aritable? arity ash asin asinh assoc assq assv atan atanh
begin boolean? byte-vector byte-vector?
caaaar caaadr caaar caadar caaddr caadr caar cadaar cadadr cadar caddar cadddr caddr cadr
call-with-input-string call-with-input-file
c-pointer c-pointer? c-object? call-with-exit car case catch cdaaar cdaadr cdaar cdadar cdaddr cdadr cdar cddaar cddadr
cddar cdddar cddddr cdddr cddr cdr ceiling char->integer char-alphabetic? char-ci<=?
char-ci<? char-ci=? char-ci>=? char-ci>? char-downcase char-lower-case? char-numeric?
char-position char-ready? char-upcase char-upper-case? char-whitespace? char<=? char<?
char=? char>=? char>? char? complex complex? cond cons constant? continuation? cos
cosh curlet current-error-port current-input-port current-output-port cyclic-sequences
defined? denominator dilambda? do dynamic-wind
eof-object? eq? equal? eqv? even? exact->inexact exact? exp expt
float? float-vector float-vector-ref float-vector? floor for-each funclet
gcd gensym gensym? ; why was gensym omitted earlier?
hash-table hash-table* hash-table-entries hash-table-ref hash-table? help hook-functions
if imag-part inexact->exact inexact? infinite? inlet input-port?
int-vector int-vector-ref int-vector? iterator-at-end? iterator-sequence integer->char
integer-decode-float integer-length integer? iterator?
keyword->symbol keyword?
lambda lambda* lcm let->list length let let* let-ref let? letrec letrec* list list->string list->vector list-ref
list-tail list? log logand logbit? logior lognot logxor
macro? magnitude make-byte-vector make-float-vector make-int-vector make-hash-table make-hook make-iterator make-keyword make-list make-polar
make-rectangular make-shared-vector make-string make-vector map max member memq memv min modulo morally-equal?
nan? negative? not null? number->string number? numerator
object->string odd? openlet? or outlet output-port? owlet
pair-line-number pair-filename pair? port-closed? port-filename port-line-number positive? procedure-documentation
procedure-setter procedure-signature procedure-source procedure? proper-list? provided?
quasiquote quote quotient
random-state random-state->list random-state? rational? rationalize real-part real? remainder reverse rootlet round
s7-version sequence? sin sinh square sqrt stacktrace string string->list string->number string->symbol string-append
string-ci<=? string-ci<? string-ci=? string-ci>=? string-ci>? string-downcase string-length
string-position string-ref string-upcase string<=? string<? string=? string>=? string>? string?
sublet substring symbol symbol->dynamic-value symbol->keyword symbol->string symbol->value symbol?
tan tanh tree-leaves truncate
unless
values vector vector-append vector->list vector-dimensions vector-length vector-ref vector?
when with-baffle with-let with-input-from-file with-input-from-string with-output-to-string
zero?
#_{list} #_{apply_values} #_{append} unquote))
;; do not include file-exists? or directory?
;; should this include peek-char or unlet ?
ht))
(built-in-functions (let ((ht (make-hash-table)))
(for-each
(lambda (op)
(hash-table-set! ht op #t))
'(symbol? gensym? keyword? let? openlet? iterator? constant? macro? c-pointer? c-object?
input-port? output-port? eof-object? integer? number? real? complex? rational? random-state?
char? string? list? pair? vector? float-vector? int-vector? byte-vector? hash-table?
continuation? procedure? dilambda? boolean? float? proper-list? sequence? null? gensym
symbol->string string->symbol symbol symbol->value symbol->dynamic-value symbol-access
make-keyword symbol->keyword keyword->symbol outlet rootlet curlet unlet sublet varlet
cutlet inlet owlet coverlet openlet let-ref let-set! make-iterator iterate iterator-sequence
iterator-at-end? provided? provide defined? c-pointer port-line-number port-filename
pair-line-number pair-filename port-closed? current-input-port current-output-port
current-error-port let->list char-ready? close-input-port close-output-port flush-output-port
open-input-file open-output-file open-input-string open-output-string get-output-string
newline write display read-char peek-char write-char write-string read-byte write-byte
read-line read-string read call-with-input-string call-with-input-file with-input-from-string
with-input-from-file call-with-output-string call-with-output-file with-output-to-string
with-output-to-file real-part imag-part numerator denominator even? odd? zero? positive?
negative? infinite? nan? complex magnitude angle rationalize abs exp log sin cos tan asin
acos atan sinh cosh tanh asinh acosh atanh sqrt expt floor ceiling truncate round lcm gcd
+ - * / max min quotient remainder modulo = < > <= >= logior logxor logand lognot ash
random-state random inexact->exact exact->inexact integer-length make-polar make-rectangular
logbit? integer-decode-float exact? inexact? random-state->list number->string string->number
char-upcase char-downcase char->integer integer->char char-upper-case? char-lower-case?
char-alphabetic? char-numeric? char-whitespace? char=? char<? char>? char<=? char>=?
char-position string-position make-string string-ref string-set! string=? string<? string>?
string<=? string>=? char-ci=? char-ci<? char-ci>? char-ci<=? char-ci>=? string-ci=? string-ci<?
string-ci>? string-ci<=? string-ci>=? string-copy string-fill! list->string string-length
string->list string-downcase string-upcase string-append substring string object->string
format cons car cdr set-car! set-cdr! caar cadr cdar cddr caaar caadr cadar cdaar caddr
cdddr cdadr cddar caaaar caaadr caadar cadaar caaddr cadddr cadadr caddar cdaaar cdaadr
cdadar cddaar cdaddr cddddr cddadr cdddar assoc member list list-ref list-set! list-tail
make-list length copy fill! reverse reverse! sort! append assq assv memq memv vector-append
list->vector vector-fill! vector-length vector->list vector-ref vector-set! vector-dimensions
make-vector make-shared-vector vector float-vector make-float-vector float-vector-set!
float-vector-ref int-vector make-int-vector int-vector-set! int-vector-ref ->byte-vector
byte-vector make-byte-vector hash-table hash-table* make-hash-table hash-table-ref
hash-table-set! hash-table-entries cyclic-sequences call/cc call-with-current-continuation
call-with-exit load autoload eval eval-string apply for-each map dynamic-wind values
catch throw error procedure-documentation procedure-signature help procedure-source funclet
procedure-setter arity aritable? not eq? eqv? equal? morally-equal? gc s7-version emergency-exit
exit dilambda make-hook hook-functions stacktrace tree-leaves
#_{list} #_{apply_values} #_{append} unquote))
ht))
(makers '(gensym sublet inlet make-iterator let->list random-state random-state->list number->string
make-string string string-copy copy list->string string->list string-append substring object->string
format cons list make-list reverse append vector-append list->vector vector->list make-vector
make-shared-vector vector make-float-vector float-vector make-int-vector int-vector byte-vector
hash-table hash-table* make-hash-table make-hook #_{list} #_{append} gentemp)) ; gentemp for other schemes
(non-negative-ops (let ((h (make-hash-table)))
(for-each
(lambda (op)
(set! (h op) #t))
'(string-length vector-length abs magnitude denominator gcd lcm tree-leaves
char->integer byte-vector-ref byte-vector-set! hash-table-entries write-byte
char-position string-position pair-line-number port-line-number))
h))
(numeric-ops (let ((h (make-hash-table)))
(for-each
(lambda (op)
(set! (h op) #t))
'(+ * - /
sin cos tan asin acos atan sinh cosh tanh asinh acosh atanh
log exp expt sqrt make-polar complex
imag-part real-part abs magnitude angle max min exact->inexact
modulo remainder quotient lcm gcd
rationalize inexact->exact random
logior lognot logxor logand numerator denominator
floor round truncate ceiling ash))
h))
(bools (let ((h (make-hash-table)))
(for-each
(lambda (op)
(set! (h op) #t))
'(symbol? integer? rational? real? number? complex? float? keyword? gensym? byte-vector? string? list? sequence?
char? boolean? float-vector? int-vector? vector? let? hash-table? input-port? null? pair? proper-list?
output-port? iterator? continuation? dilambda? procedure? macro? random-state? eof-object? c-pointer?
unspecified? c-object? constant?))
h))
(booleans (let ((h (make-hash-table)))
(for-each
(lambda (op)
(set! (h op) #t))
'(symbol? integer? rational? real? number? complex? float? keyword? gensym? byte-vector? string? list? sequence?
char? boolean? float-vector? int-vector? vector? let? hash-table? input-port? null? pair? proper-list?
output-port? iterator? continuation? dilambda? procedure? macro? random-state? eof-object? c-pointer? c-object?
unspecified? exact? inexact? defined? provided? even? odd? char-whitespace? char-numeric? char-alphabetic?
negative? positive? zero? constant? infinite? nan? char-upper-case? char-lower-case? directory? file-exists?))
h))
(reversibles (let ((h (make-hash-table)))
(for-each
(lambda (op)
(set! (h (car op)) (cadr op)))
'((< >) (> <) (<= >=) (>= <=)
(* *) (+ +) (= =) (char=? char=?) (string=? string=?)
(eq? eq?) (eqv? eqv?) (equal? equal?) (morally-equal? morally-equal?)
(logand logand) (logxor logxor) (logior logior)
(max max) (min min) (lcm lcm) (gcd gcd)
(char<? char>?) (char>? char<?) (char<=? char>=?) (char>=? char<=?)
(string<? string>?) (string>? string<?) (string<=? string>=?) (string>=? string<=?)
(char-ci<? char-ci>?) (char-ci>? char-ci<?) (char-ci<=? char-ci>=?) (char-ci>=? char-ci<=?)
(string-ci<? string-ci>?) (string-ci>? string-ci<?) (string-ci<=? string-ci>=?) (string-ci>=? string-ci<=?)))
h))
(syntaces (let ((h (make-hash-table)))
(for-each
(lambda (op)
(set! (h op) #t))
'(quote if begin let let* letrec letrec* cond case or and do set! unless when
with-let with-baffle
lambda lambda* define define*
define-macro define-macro* define-bacro define-bacro*
define-constant define-expansion))
h))
(outport #t)
(linted-files ())
(big-constants ())
(equable-closures ())
(other-names-counts (make-hash-table))
(*e* #f)
(other-identifiers #f)
(quote-warnings 0)
(last-simplify-boolean-line-number -1)
(last-simplify-numeric-line-number -1)
(last-simplify-cxr-line-number -1)
(last-if-line-number -1)
(last-checker-line-number -1)
(last-cons-line-number -1)
(last-rewritten-internal-define #f)
(line-number -1)
(pp-left-margin 4)
(lint-left-margin 1)
(*current-file* "")
(*top-level-objects* (make-hash-table))
(*output-port* *stderr*))
(set! *e* (curlet))
(set! *lint* *e*) ; external access to (for example) the built-in-functions hash-table via (*lint* 'built-in-functions)
(define-macro (let*-temporarily vars . body)
`(with-let (#_inlet :orig (#_curlet)
:saved (#_list ,@(map car vars)))
(dynamic-wind
(lambda () #f)
(lambda ()
(with-let orig
,@(map (lambda (v)
`(set! ,(car v) ,(cadr v)))
vars)
,@body))
(lambda ()
,@(map (let ((ctr -1))
(lambda (v)
(if (symbol? (car v))
`(set! (orig ',(car v)) (list-ref saved ,(set! ctr (+ ctr 1))))
`(set! (with-let orig ,(car v)) (list-ref saved ,(set! ctr (+ ctr 1)))))))
vars)))))
(define-macro (let-temporarily vars . body)
`(with-let (#_inlet :orig (#_curlet)
:saved (#_list ,@(map car vars))
:new (#_list ,@(map cadr vars)))
(dynamic-wind
(lambda () #f)
(lambda () ; this could be (with-let orig (let ,vars ,@body)) but I want to handle stuff like individual vector locations
,@(map (let ((ctr -1))
(lambda (v)
(if (symbol? (car v))
`(set! (orig ',(car v)) (list-ref new ,(set! ctr (+ ctr 1))))
`(set! (with-let orig ,(car v)) (list-ref new ,(set! ctr (+ ctr 1)))))))
vars)
(with-let orig ,@body))
(lambda ()
,@(map (let ((ctr -1))
(lambda (v)
(if (symbol? (car v))
`(set! (orig ',(car v)) (list-ref saved ,(set! ctr (+ ctr 1))))
`(set! (with-let orig ,(car v)) (list-ref saved ,(set! ctr (+ ctr 1)))))))
vars)))))
;; -------- lint-format --------
(define target-line-length 80)
(define (truncated-list->string form)
;; return form -> string with limits on its length
(let* ((str (object->string form))
(len (length str)))
(if (< len target-line-length)
str
(do ((i (- target-line-length 6) (- i 1)))
((or (= i 40)
(char-whitespace? (str i)))
(string-append (substring str 0 (if (<= i 40) (- target-line-length 6) i)) "..."))))))
(define lint-pp #f) ; avoid crosstalk with other schemes' definitions of pp and pretty-print (make-var also collides)
(define lint-pretty-print #f)
(let ()
(require write.scm)
(set! lint-pp pp);
(set! lint-pretty-print pretty-print))
(define (lists->string f1 f2)
;; same but 2 strings that may need to be lined up vertically
(let* ((str1 (object->string f1))
(len1 (length str1))
(str2 (object->string f2))
(len2 (length str2)))
(when (> len1 target-line-length)
(set! str1 (truncated-list->string f1))
(set! len1 (length str1)))
(when (> len2 target-line-length)
(set! ((funclet lint-pretty-print) '*pretty-print-left-margin*) pp-left-margin)
(set! ((funclet lint-pretty-print) '*pretty-print-length*) (- 114 pp-left-margin))
(set! str2 (lint-pp f2))
(set! len2 (length str2)))
(format #f (if (< (+ len1 len2) target-line-length)
(values "~A -> ~A" str1 str2)
(values "~%~NC~A ->~%~NC~A" pp-left-margin #\space str1 pp-left-margin #\space str2)))))
(define (truncated-lists->string f1 f2)
;; same but 2 strings that may need to be lined up vertically and both are truncated
(let* ((str1 (object->string f1))
(len1 (length str1))
(str2 (object->string f2))
(len2 (length str2)))
(when (> len1 target-line-length)
(set! str1 (truncated-list->string f1))
(set! len1 (length str1)))
(when (> len2 target-line-length)
(set! str2 (truncated-list->string f2))
(set! len2 (length str2)))
(format #f (if (< (+ len1 len2) target-line-length)
(values "~A -> ~A" str1 str2)
(values "~%~NC~A ->~%~NC~A" pp-left-margin #\space str1 pp-left-margin #\space str2)))))
(define made-suggestion 0)
(define (lint-format str caller . args)
(let ((outstr (apply format #f
(string-append (if (< 0 line-number 100000)
"~NC~A (line ~D): "
"~NC~A: ")
str "~%")
lint-left-margin #\space
(truncated-list->string caller)
(if (< 0 line-number 100000)
(values line-number args)
args))))
(set! made-suggestion (+ made-suggestion 1))
(display outstr outport)
(if (> (length outstr) (+ target-line-length 40))
(newline outport))))
(define (lint-format* caller . strs)
(let* ((outstr (format #f
(if (< 0 line-number 100000)
"~NC~A (line ~D): "
"~NC~A:~A")
lint-left-margin #\space
(truncated-list->string caller)
(if (< 0 line-number 100000)
line-number
" ")))
(current-end (length outstr)))
;; (set! made-suggestion (+ made-suggestion 1))
(display outstr outport)
(for-each (lambda (s)
(let ((len (length s)))
(if (> (+ len current-end) target-line-length)
(begin
(format outport "~%~NC~A" (+ lint-left-margin 4) #\space s)
(set! current-end len))
(begin
(display s outport)
(set! current-end (+ current-end len))))))
strs)
(newline outport)))
(define (local-line-number tree)
(let ((tree-line (if (pair? tree) (pair-line-number tree) 0)))
(if (and (< 0 tree-line 100000)
(not (= tree-line line-number)))
(format #f " (line ~D)" tree-line)
"")))
;; -------- vars --------
(define var-name car)
(define (var? v) (and (pair? v) (let? (cdr v))))
(define var-member assq)
(define var-ref (dilambda (lambda (v) (let-ref (cdr v) 'ref)) (lambda (v x) (let-set! (cdr v) 'ref x))))
(define var-set (dilambda (lambda (v) (let-ref (cdr v) 'set)) (lambda (v x) (let-set! (cdr v) 'set x))))
(define var-history (dilambda (lambda (v) (let-ref (cdr v) 'history)) (lambda (v x) (let-set! (cdr v) 'history x))))
(define var-ftype (dilambda (lambda (v) (let-ref (cdr v) 'ftype)) (lambda (v x) (let-set! (cdr v) 'ftype x))))
(define var-arglist (dilambda (lambda (v) (let-ref (cdr v) 'arglist)) (lambda (v x) (let-set! (cdr v) 'arglist x))))
(define var-definer (dilambda (lambda (v) (let-ref (cdr v) 'definer)) (lambda (v x) (let-set! (cdr v) 'definer x))))
(define var-leaves (dilambda (lambda (v) (let-ref (cdr v) 'leaves)) (lambda (v x) (let-set! (cdr v) 'leaves x))))
(define var-scope (dilambda (lambda (v) (let-ref (cdr v) 'scope)) (lambda (v x) (let-set! (cdr v) 'scope x))))
(define var-setters (dilambda (lambda (v) (let-ref (cdr v) 'setters)) (lambda (v x) (let-set! (cdr v) 'setters x))))
(define var-env (dilambda (lambda (v) (let-ref (cdr v) 'env)) (lambda (v x) (let-set! (cdr v) 'env x))))
(define var-decl (dilambda (lambda (v) (let-ref (cdr v) 'decl)) (lambda (v x) (let-set! (cdr v) 'decl x))))
(define var-match-list (dilambda (lambda (v) (let-ref (cdr v) 'match-list)) (lambda (v x) (let-set! (cdr v) 'match-list x))))
(define var-initial-value (lambda (v) (let-ref (cdr v) 'initial-value))) ; not settable
(define var-side-effect (dilambda (lambda (v)
(if (null? (let-ref (cdr v) 'side-effect))
(let-set! (cdr v) 'side-effect (get-side-effect v))
(let-ref (cdr v) 'side-effect)))
(lambda (v x)
(let-set! (cdr v) 'side-effect x))))
(define var-signature (dilambda (lambda (v)
(if (null? (let-ref (cdr v) 'signature))
(let-set! (cdr v) 'signature (get-signature v))
(let-ref (cdr v) 'signature)))
(lambda (v x)
(let-set! (cdr v) 'signature x))))
(define* (make-var name initial-value definer)
(let ((old (hash-table-ref other-identifiers name)))
(cons name (inlet 'initial-value initial-value
'definer definer
'history (if old
(begin
(hash-table-set! other-identifiers name #f)
(if initial-value (cons initial-value old) old))
(if initial-value (list initial-value) ()))
'scope ()
'setters ()
'set 0
'ref (if old (length old) 0)))))
;; -------- the usual list functions --------
(define (remove item sequence)
(cond ((null? sequence) ())
((equal? item (car sequence)) (cdr sequence))
(else (cons (car sequence) (remove item (cdr sequence))))))
(define (remove-all item sequence)
(map (lambda (x)
(if (equal? x item)
(values)
x))
sequence))
(define (remove-if p lst)
(cond ((null? lst) ())
((p (car lst)) (remove-if p (cdr lst)))
(else (cons (car lst)
(remove-if p (cdr lst))))))
(define (lint-remove-duplicates lst env)
(reverse (let rem-dup ((lst lst)
(nlst ()))
(cond ((null? lst) nlst)
((and (member (car lst) nlst)
(not (and (pair? (car lst))
(side-effect? (car lst) env))))
(rem-dup (cdr lst) nlst))
(else (rem-dup (cdr lst) (cons (car lst) nlst)))))))
(define applicable? arity)
(define every?
(let ((documentation "(every? func sequence) returns #t if func approves of every member of sequence")
(signature '(boolean? procedure? sequence?)))
(lambda (f sequence)
(call-with-exit
(lambda (return)
(for-each (lambda (arg) (if (not (f arg)) (return #f))) sequence)
#t)))))
(define any?
(let ((documentation "(any? func sequence) returns #t if func approves of any member of sequence")
(signature '(boolean? procedure? sequence?)))
(lambda (f sequence)
(call-with-exit
(lambda (return)
(for-each (lambda (arg) (if (f arg) (return #t))) sequence)
#f)))))
(define collect-if
(let ((documentation "(collect-if type func sequence) gathers the elements of sequence that satisfy func, and returns them via type:\n\
(collect-if list integer? #(1.4 2/3 1 1+i 2)) -> '(1 2)"))
(lambda (type f sequence)
(apply type (map (lambda (arg) (if (f arg) arg (values))) sequence)))))
(define find-if
(let ((documentation "(find-if func sequence) applies func to each member of sequence.\n\
If func approves of one, find-if returns that member of the sequence"))
(lambda (f sequence)
(call-with-exit
(lambda (return)
(for-each (lambda (arg)
(if (f arg)
(return arg)))
sequence)
#f)))))
;; -------- trees --------
(define copy-tree
(let ((documentation "(copy-tree lst) returns a full copy of lst"))
(lambda (lis)
(if (pair? lis)
(cons (copy-tree (car lis))
(copy-tree (cdr lis)))
lis))))
(define (tree-count1 x tree count)
(if (eq? x tree)
(+ count 1)
(if (or (>= count 2)
(not (pair? tree))
(eq? (car tree) 'quote))
count
(tree-count1 x (car tree) (tree-count1 x (cdr tree) count)))))
(define (tree-count2 x tree count)
(if (eq? x tree)
(+ count 1)
(if (or (>= count 3)
(not (pair? tree))
(eq? (car tree) 'quote))
count
(tree-count2 x (car tree) (tree-count2 x (cdr tree) count)))))
(define (proper-tree? tree)
(or (not (pair? tree))
(and (proper-list? tree)
(every? proper-tree? (cdr tree)))))
(define (gather-symbols tree)
(let ((syms ()))
(let walk ((p tree))
(if (pair? p)
(if (symbol? (car p))
(if (not (eq? (car p) 'quote))
(for-each (lambda (a)
(if (symbol? a)
(if (not (memq a syms))
(set! syms (cons a syms)))
(if (pair? a) (walk a))))
(cdr p)))
(if (pair? (car p))
(begin
(walk (car p))
(walk (cdr p)))))
(if (and (symbol? tree)
(not (memq tree syms)))
(set! syms (cons tree syms)))))
syms))
(define (tree-arg-member sym tree)
(and (proper-list? tree)
(or (and (memq sym (cdr tree))
tree)
(and (pair? (car tree))
(tree-arg-member sym (car tree)))
(and (pair? (cdr tree))
(call-with-exit
(lambda (return)
(for-each
(lambda (b)
(cond ((and (pair? b)
(tree-arg-member sym b))
=> return)))
(cdr tree))
#f))))))
(define (tree-memq sym tree)
(or (eq? sym tree)
(and (pair? tree)
(not (eq? (car tree) 'quote))
(or (eq? (car tree) sym)
(tree-memq sym (car tree))
(tree-memq sym (cdr tree))))))
(define (tree-member sym tree)
(and (pair? tree)
(or (eq? (car tree) sym)
(tree-member sym (car tree))
(tree-member sym (cdr tree)))))
(define (tree-unquoted-member sym tree)
(and (pair? tree)
(not (eq? (car tree) 'quote))
(or (eq? (car tree) sym)
(tree-unquoted-member sym (car tree))
(tree-unquoted-member sym (cdr tree)))))
(define (tree-car-member sym tree)
(and (pair? tree)
(or (eq? (car tree) sym)
(and (pair? (car tree))
(tree-car-member sym (car tree)))
(and (pair? (cdr tree))
(member sym (cdr tree) tree-car-member)))))
(define (tree-sym-set-member sym set tree) ; sym as arg, set as car
(and (pair? tree)
(or (memq (car tree) set)
(and (pair? (car tree))
(tree-sym-set-member sym set (car tree)))
(and (pair? (cdr tree))
(or (member sym (cdr tree))
(member #f (cdr tree) (lambda (a b) (tree-sym-set-member sym set b))))))))
(define (tree-set-member set tree)
(and (pair? tree)
(not (eq? (car tree) 'quote))
(or (memq (car tree) set)
(tree-set-member set (car tree))
(tree-set-member set (cdr tree)))))
(define (tree-table-member table tree)
(and (pair? tree)
(or (hash-table-ref table (car tree))
(tree-table-member table (car tree))
(tree-table-member table (cdr tree)))))
(define (tree-set-car-member set tree) ; set as car
(and (pair? tree)
(or (and (memq (car tree) set)
tree)
(and (pair? (car tree))
(tree-set-car-member set (car tree)))
(and (pair? (cdr tree))
(member #f (cdr tree) (lambda (a b) (tree-set-car-member set b)))))))
(define (maker? tree)
(tree-set-car-member makers tree))
(define (tree-symbol-walk tree syms)
(if (pair? tree)
(if (eq? (car tree) 'quote)
(if (and (pair? (cdr tree))
(symbol? (cadr tree))
(not (memq (cadr tree) (car syms))))
(tree-symbol-walk (cddr tree) (begin (set-car! syms (cons (cadr tree) (car syms))) syms)))
(if (eq? (car tree) {list})
(if (and (pair? (cdr tree))
(pair? (cadr tree))
(eq? (caadr tree) 'quote)
(symbol? (cadadr tree))
(not (memq (cadadr tree) (cadr syms))))
(tree-symbol-walk (cddr tree) (begin (list-set! syms 1 (cons (cadadr tree) (cadr syms))) syms)))
(begin
(tree-symbol-walk (car tree) syms)
(tree-symbol-walk (cdr tree) syms))))))
;; -------- types --------
(define (any-real? lst) ; ignore 0.0 and 1.0 in this since they normally work
(and (pair? lst)
(or (and (number? (car lst))
(not (rational? (car lst)))
(not (= (car lst) 0.0))
(not (= (car lst) 1.0)))
(any-real? (cdr lst)))))
(define (quoted-undotted-pair? x)
(and (pair? x)
(eq? (car x) 'quote)
(pair? (cdr x))
(pair? (cadr x))
(positive? (length (cadr x)))))
(define (quoted-null? x)
(and (pair? x)
(eq? (car x) 'quote)
(pair? (cdr x))
(null? (cadr x))))
(define (any-null? x)
(or (null? x)
(and (pair? x)
(case (car x)
((quote)
(and (pair? (cdr x))
(null? (cadr x))))
((list)
(null? (cdr x)))
(else #f)))))
(define (quoted-not? x)
(and (pair? x)
(eq? (car x) 'quote)
(pair? (cdr x))
(not (cadr x))))
(define (quoted-symbol? x)
(and (pair? x)
(eq? (car x) 'quote)
(pair? (cdr x))
(symbol? (cadr x))))
(define (code-constant? x)
(and (or (not (symbol? x))
(keyword? x))
(or (not (pair? x))
(eq? (car x) 'quote))))
(define (just-symbols? form)
(or (null? form)
(symbol? form)
(and (pair? form)
(symbol? (car form))
(just-symbols? (cdr form)))))
(define (list-any? f lst)
(if (pair? lst)
(or (f (car lst))
(list-any? f (cdr lst)))
(f lst)))
(define syntax?
(let ((syns (let ((h (make-hash-table)))
(for-each (lambda (x)
(hash-table-set! h x #t))
(list quote if when unless begin set! let let* letrec letrec* cond and or case do
lambda lambda* define define* define-macro define-macro* define-bacro define-bacro*
define-constant with-baffle macroexpand with-let))
h)))
(lambda (obj) ; a value, not a symbol
(hash-table-ref syns obj))))
;; -------- func info --------
(define (arg-signature fnc env)
(and (symbol? fnc)
(let ((fd (var-member fnc env)))
(if (var? fd)
(and (symbol? (var-ftype fd))
(var-signature fd))
(or (and (eq? *e* *lint*)
(procedure-signature fnc))
(let ((f (symbol->value fnc *e*)))
(and (procedure? f)
(procedure-signature f))))))))
(define (arg-arity fnc env)
(and (symbol? fnc)
(let ((fd (var-member fnc env)))
(if (var? fd)
(and (not (eq? (var-decl fd) 'error))
(arity (var-decl fd)))
(let ((f (symbol->value fnc *e*)))
(and (procedure? f)
(arity f)))))))
(define (dummy-func caller form f)
(catch #t
(lambda ()
(eval f))
(lambda args
(lint-format* caller
(string-append "in " (truncated-list->string form) ", ")
(apply format #f (cadr args))))))
(define (count-values body)
(let ((mn #f)
(mx #f))
(if (pair? body)
(let counter ((ignored #f) ; 'ignored is for member's benefit
(tree (list-ref body (- (length body) 1))))
(if (pair? tree)
(if (eq? (car tree) 'values)
(let ((args (- (length tree) 1)))
(for-each (lambda (p)
(if (and (pair? p) (eq? (car p) 'values))
(set! args (- (+ (args (length p)) 2)))))
(cdr tree))
(set! mn (min (or mn args) args))
(set! mx (max (or mx args) args)))
(begin
(if (pair? (car tree))
(counter 'values (car tree)))
(if (pair? (cdr tree))
(member #f (cdr tree) counter)))))
#f)) ; return #f so member doesn't quit early
(and mn (list mn mx))))
(define (get-signature v)
(define (signer endb env)
(and (not (side-effect? endb env))
(cond ((not (pair? endb))
(and (not (symbol? endb))
(list (->lint-type endb))))
((arg-signature (car endb) env)
=> (lambda (a)
(and (pair? a)
(list (car a)))))
((and (eq? (car endb) 'if)
(pair? (cddr endb)))
(let ((a1 (signer (caddr endb) env))
(a2 (and (pair? (cdddr endb))
(signer (cadddr endb) env))))
(if (not a2)
a1
(and (equal? a1 a2) a1))))
(else #f))))
(let ((ftype (var-ftype v))
(initial-value (var-initial-value v))
(arglist (var-arglist v))
(env (var-env v)))
(let ((body (and (memq ftype '(define define* lambda lambda* let))
(cddr initial-value))))
(and (pair? body)
(let ((sig (signer (list-ref body (- (length body) 1)) env)))
(if (not (pair? sig))
(set! sig (list #t)))
(when (and (proper-list? arglist)
(not (any? keyword? arglist)))
(for-each
(lambda (arg) ; new function's parameter
(set! sig (cons #t sig))
;; (if (pair? arg) (set! arg (car arg)))
;; causes trouble when tree-count1 sees keyword args in s7test.scm
(if (= (tree-count1 arg body 0) 1)
(let ((p (tree-arg-member arg body)))
(when (pair? p)
(let ((f (car p))
(m (memq arg (cdr p))))
(if (pair? m)
(let ((fsig (arg-signature f env)))
(if (pair? fsig)
(let ((chk (catch #t (lambda () (fsig (- (length p) (length m)))) (lambda args #f))))
(if (and (symbol? chk) ; it defaults to #t
(not (memq chk '(integer:any? integer:real?))))
(set-car! sig chk)))))))))))
arglist))
(and (any? (lambda (a) (not (eq? a #t))) sig)
(reverse sig)))))))
(define (args->proper-list args)
(cond ((symbol? args) (list args))
((not (pair? args)) args)
((pair? (car args)) (cons (caar args) (args->proper-list (cdr args))))
(else (cons (car args) (args->proper-list (cdr args))))))
(define (out-vars func-name arglist body) ; t367 has tests
(let ((ref ())
(set ()))
(let var-walk ((tree body)
(e (cons func-name arglist)))
(define (var-walk-body tree e)
(when (pair? tree)
(for-each (lambda (p) (set! e (var-walk p e))) tree)))
(define (shadowed v)
(if (and (or (memq v e) (memq v ref))
(not (memq v set)))
(set! set (cons v set)))
v)
(if (symbol? tree)
(if (not (or (memq tree e) (memq tree ref) (defined? tree (rootlet))))
(set! ref (cons tree ref)))
(when (pair? tree)
(if (not (pair? (cdr tree)))
(var-walk (car tree) e)
(case (car tree)
((set! vector-set! list-set! hash-table-set! float-vector-set! int-vector-set!
string-set! let-set! fill! string-fill! list-fill! vector-fill!
reverse! sort! set-car! set-cdr!)
(let ((sym (if (symbol? (cadr tree))
(cadr tree)
(if (pair? (cadr tree)) (caadr tree)))))
(if (not (or (memq sym e) (memq sym set)))
(set! set (cons sym set)))
(var-walk (cddr tree) e)))
((let letrec)
(if (and (pair? (cdr tree))
(pair? (cddr tree)))
(let* ((named (symbol? (cadr tree)))
(vars (if named
(list (shadowed (cadr tree)))
())))
(for-each (lambda (v)
(when (and (pair? v)
(pair? (cdr v)))
(var-walk (cadr v) e)
(set! vars (cons (shadowed (car v)) vars))))
((if named caddr cadr) tree))
(var-walk-body ((if named cdddr cddr) tree) (append vars e)))))
((case)
(when (and (pair? (cdr tree))
(pair? (cddr tree)))
(for-each (lambda (c)
(when (pair? c)
(var-walk (cdr c) e)))
(cddr tree))))
((quote) #f)
((let* letrec*)
(let* ((named (symbol? (cadr tree)))
(vars (if named (list (cadr tree)) ())))
(for-each (lambda (v)
(when (and (pair? v)
(pair? (cdr v)))
(var-walk (cadr v) (append vars e))
(set! vars (cons (shadowed (car v)) vars))))
((if named caddr cadr) tree))
(var-walk-body ((if named cdddr cddr) tree) (append vars e))))
((do)
(let ((vars ()))
(when (pair? (cadr tree))
(for-each (lambda (v)
(when (and (pair? v)
(pair? (cdr v)))
(var-walk (cadr v) e)
(set! vars (cons (shadowed (car v)) vars))))
(cadr tree))
(for-each (lambda (v)
(if (and (pair? v)
(pair? (cdr v))
(pair? (cddr v)))
(var-walk (caddr v) (append vars e))))
(cadr tree)))
(when (pair? (cddr tree))
(var-walk (caddr tree) (append vars e))
(var-walk-body (cdddr tree) (append vars e)))))
((lambda lambda*)
(var-walk-body (cddr tree) (append (args->proper-list (cadr tree)) e)))
((define* define-macro define-macro* define-bacro define-bacro*)
(if (and (pair? (cdr tree))
(pair? (cddr tree)))
(begin
(set! e (cons (caadr tree) e))
(var-walk-body (cddr tree) (append (args->proper-list (cdadr tree)) e)))))
((define define-constant)
(if (and (pair? (cdr tree))
(pair? (cddr tree)))
(if (symbol? (cadr tree))
(begin
(var-walk (caddr tree) e)
(set! e (cons (cadr tree) e)))
(begin
(set! e (cons (caadr tree) e))
(var-walk-body (cddr tree) (append (args->proper-list (cdadr tree)) e))))))
(else
(var-walk (car tree) e)
(var-walk (cdr tree) e))))))
e)
(list ref set)))
(define (get-side-effect v)
(let ((ftype (var-ftype v)))
(or (not (memq ftype '(define define* lambda lambda*)))
(let ((body (cddr (var-initial-value v)))
(env (var-env v))
(args (cons (var-name v) (args->proper-list (var-arglist v)))))
(let ((outvars (append (cadr (out-vars (var-name v) args body)) args)))
(any? (lambda (f)