-
Notifications
You must be signed in to change notification settings - Fork 7
/
lint.scm
22113 lines (20160 loc) · 859 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-doc-strings* #f) ; old-style (CL) doc strings
(define *report-func-as-arg-arity-mismatch* #f) ; as it says...
(define *report-ridiculous-variable-names* 50) ; max length of var name
(define *report-bad-variable-names* '(l ll .. O ~ else)) ; bad names -- a list to check such as:
;;; '(l ll .. ~ else data datum new item info temp tmp temporary val vals value foo bar baz aux dummy O var res retval result count str)
;;; else is evaluated in cond
;;; => is evaluated in both cond and case, so both are better left alone (see s7test.scm for examples)
;;; at the top-level, r7rs deems it an error to change their value
;;; also '__func__, 'signature and 'documentation as local vars could cause confusion
(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 (32 is too high I think)
(define *report-clobbered-function-return-value* #f) ; function returns constant sequence, which is then stomped on -- very rare!
(define *report-boolean-functions-misbehaving* #t) ; function name ends in #\? but function returns a non-boolean value -- dubious.
(define *report-repeated-code-fragments* 200) ; #t, #f, or an int = min reported fragment size * uses * uses, #t=130.
(define *report-quasiquote-rewrites* #t) ; simple quasiquote stuff rewritten as a normal list expression
(define *fragment-max-size* 128) ; biggest seen if 512: 180 -- appears to be in a test suite, if 128 max at 125
(define *fragment-min-size* 5) ; smallest seen - 1 -- maybe 8 would be better
(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)
;; see snd-lint.scm.
;;; --------------------------------------------------------------------------------
(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)
(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-ref 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?
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-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->keyword 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 tree-memq 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-values #_apply-values #_append unquote))
;; do not include file-exists? or directory? (also not peek-char because these are checked via eval)
ht))
(built-in-functions (let ((ht (make-hash-table)))
(for-each
(lambda (op)
(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
string->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 string->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 tree-memq object->let
getenv directory? file-exists?
#_list-values #_apply-values #_append unquote))
ht))
(makers (let ((h (make-hash-table)))
(for-each
(lambda (op)
(set! (h op) #t))
'(gensym sublet inlet make-iterator let->list random-state random-state->list number->string object->let
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-values #_append gentemp)) ; gentemp for other schemes
h))
(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 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 make-rectangular
imag-part real-part abs magnitude angle max min exact->inexact
modulo remainder quotient lcm gcd
rationalize inexact->exact random
logior lognot logxor logand integer-length numerator denominator
floor round truncate ceiling ash
;; r7rs
exact inexact
;; hooboy...
fix:+ fx+ flo:+ fl+ fix:* fx* flo:* fl* fix:- fx- flo:- fl- fix:/ fx/ flo:/ fl/
))
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))
(notables (let ((h (make-hash-table)))
(for-each
(lambda (op)
(set! (h (car op)) (cadr op)))
'((< >=) (> <=) (<= >) (>= <)
(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<?)
(odd? even?) (even? odd?) (exact? inexact?) (inexact? exact?)))
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))
(definers (let ((h (make-hash-table)))
(for-each (lambda (d)
(set! (h d) #t))
'(define define* define-constant lambda lambda* curlet require load eval eval-string
define-macro define-macro* define-bacro define-bacro* define-expansion
definstrument define-animal define-envelope
define-values define-module define-method
define-syntax define-public define-inlinable define-integrable define^))
h))
(open-definers (let ((h (make-hash-table)))
(for-each (lambda (d)
(set! (h d) #t))
'(define define* define-constant require load eval eval-string
define-macro define-macro* define-bacro define-bacro* define-expansion
definstrument define-animal define-envelope defgenerator
define-values define-module define-method
define-syntax define-public define-inlinable define-integrable define^))
h))
(cxars (hash-table '(car . ()) '(caar . car) '(cdar . cdr)
'(caaar . caar) '(cdaar . cdar) '(cddar . cddr) '(cadar . cadr)
'(caaaar . caaar) '(caadar . caadr) '(cadaar . cadar) '(caddar . caddr)
'(cdaaar . cdaar) '(cdadar . cdadr) '(cddaar . cddar) '(cdddar . cdddr)))
(outport #t)
(linted-files ())
(big-constants (make-hash-table))
(other-names-counts (make-hash-table))
(*e* #f)
(other-identifiers (make-hash-table))
(quote-warnings 0)
;; these line numbers are trying to reduce redundant output
(last-simplify-boolean-line-number -1)
(last-simplify-numeric-line-number -1)
(last-simplify-cxr-line-number -1)
(last-if-line-number -1)
(last-if->case-line-number -1)
(last-let->case-line-number -1)
(last-checker-line-number -1)
(last-cons-line-number -1)
(last-rewritten-internal-define #f)
(last-lambda-let #f)
(last-lambda-let-funcs #f)
(local-function-context 40)
(line-number -1)
(lint-in-with-let #f)
(pp-left-margin 4)
(lint-left-margin 1)
(*current-file* "")
(*top-level-objects* (make-hash-table))
(*output-port* *stderr*)
(fragments (do ((v (make-vector *fragment-max-size* #f))
(i 0 (+ i 1)))
((= i *fragment-max-size*) v)
(set! (v i) (make-hash-table))))
(*max-cdr-len* 16)) ; 40 is too high, 24 questionable, if #f the let+do rewrite is turned off
(set! *e* (curlet))
(set! *lint* (curlet)) ; external access to (for example) the built-in-functions hash-table via (*lint* 'built-in-functions)
;; -------- 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-lint-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))
(str2 (object->string f2)))
(let ((len1 (length str1))
(len2 (length str2)))
(when (> len1 target-line-length)
(set! str1 (truncated-list->string f1))
(set! len1 (length str1)))
(when (> len2 target-line-length)
(let ((old-len2 len2))
(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))
(when (> len2 (* 10 old-len2)) ; this is aimed at some pathological s7test cases -- never hit otherwise I think
(set! str2 (truncated-list->string f2))
(set! len2 (length str2)))))
(if (< (+ len1 len2) target-line-length)
(format #f "~A -> ~A" str1 str2)
(format #f "~%~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))
(str2 (object->string f2)))
(let ((len1 (length str1))
(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)))
(if (< (+ len1 len2) target-line-length)
(format #f "~A -> ~A" str1 str2)
(format #f "~%~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 (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-retcons (dilambda (lambda (v) (let-ref (cdr v) 'retcons)) (lambda (v x) (let-set! (cdr v) 'retcons 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 (easily) settable
(define var-refenv
(dilambda (lambda (v)
(let-ref (cdr v) 'refenv))
(lambda (v e)
(let ((old-e (let-ref (cdr v) 'refenv)))
(if (null? old-e) ; nil if unset
(let-set! (cdr v) 'refenv e) ; so set to current
(if (not (eq? old-e e)) ; not eq, so (for now) set to #f
(let-set! (cdr v) 'refenv #f))))
e)))
(define var-side-effect
(dilambda (lambda (v)
(case (let-ref (cdr v) 'side-effect)
((()) (let-set! (cdr v) 'side-effect (get-side-effect v)))
(else)))
(lambda (v x)
(let-set! (cdr v) 'side-effect x))))
(define var-signature
(dilambda (lambda (v)
(case (let-ref (cdr v) 'signature)
((()) (let-set! (cdr v) 'signature (get-signature v)))
(else)))
(lambda (v x)
(let-set! (cdr v) 'signature x))))
(define (make-lint-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 ()
'env ()
'refenv ()
'setters ()
'set 0
'ref (if old (length old) 0)))))
;; -------- the usual list functions --------
(define (len=1? x)
(and (pair? x)
(null? (cdr x))))
(define (len=2? x)
(and (pair? x)
(pair? (cdr x))
(null? (cddr x))))
(define (len=3? x)
(and (pair? x)
(pair? (cdr x))
(pair? (cddr x))
(null? (cdddr x))))
(define (len>1? x)
(and (pair? x)
(pair? (cdr x))))
(define (len>2? x)
(and (pair? x)
(pair? (cdr x))
(pair? (cddr x))))
(define (proper-pair? x)
(and (pair? x)
(proper-list? (cdr x))))
(define (unquoted-pair? x)
(and (pair? x)
(not (eq? (car x) 'quote))))
(define (remove item sequence)
(cond ((null? sequence) ())
((equal? item (car sequence)) (cdr sequence))
(else (cons (car sequence)
(remove item (cdr sequence))))))
(define (remq-set items sequence)
(cond ((null? sequence)
())
((memq (car sequence) items)
(remq-set items (cdr sequence)))
(else
(cons (car sequence)
(remq-set items (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)
(and (sequence? sequence)
(call-with-exit
(lambda (return)
(for-each (lambda (arg) (if (not (f arg)) (return #f))) sequence)))))))
(define any?
(let ((documentation "(any? func sequence) returns #t if func approves of any member of sequence")
(signature '(boolean? procedure? sequence?)))
(lambda (f sequence)
(and (sequence? 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-nonce x tree)
(let ((count 0))
(let nonce ((t tree))
(cond ((eq? x t)
(set! count (+ count 1)))
((and (unquoted-pair? t)
(< count 2))
(nonce (car t))
(nonce (cdr t)))))
(= count 1)))
(define (tree-count x tree1)
(let counter ((count 0) (tree tree1))
(if (eq? x tree)
(+ count 1)
(if (not (unquoted-pair? tree))
count
(counter (counter count (car tree)) (cdr tree))))))
(define (tree-count2 x tree1)
(let counter ((count 0) (tree tree1))
(if (eq? x tree)
(+ count 1)
(if (or (>= count 3)
(not (unquoted-pair? tree)))
count
(counter (counter count (car tree)) (cdr tree))))))
(define (proper-tree? tree)
(or (not (pair? tree))
(and (proper-list? tree)
(every? proper-tree? (cdr tree)))))
(define (shadowed? sym tree1)
(let shadow? ((tree tree1))
(and (pair? tree)
(or (and (null? (cdr tree))
(shadow? (car tree)))
(and (pair? (cdr tree))
(null? (cddr tree))
(or (shadow? (car tree))
(shadow? (cdr tree))))
(and (pair? (cdr tree))
(pair? (cddr tree))
(case (car tree)
((let let*)
(and (len>1? (cdr tree))
(or (eq? sym (cadr tree))
(let ((vars ((if (symbol? (cadr tree)) caddr cadr) tree)))
(or (and (pair? vars)
(assq sym vars))
(shadow? ((if (symbol? (cadr tree)) cdddr cddr) tree)))))))
((letrec letrec* do)
(or (and (pair? (cadr tree))
(assq sym (cadr tree)))
(shadow? (cddr tree))))
((define lambda define-macro define-constant)
(or (eq? sym (cadr tree))
(and (pair? (cadr tree))
(memq sym (cadr tree)))
(shadow? (cddr tree))))
((lambda* define* define-macro*)
(or (eq? sym (cadr tree))
(and (pair? (cadr tree))
(member sym (cadr tree) (lambda (a b)
(or (eq? a b)
(and (pair? b)
(eq? a (car b)))))))
(shadow? (cddr tree))))
(else
(or (shadow? (car tree))
(shadow? (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-member sym tree1)
(let tm ((tree tree1))
(and (pair? tree)
(or (eq? (car tree) sym)
(tm (car tree))
(tm (cdr tree))))))
(define (tree-equal-member sym tree)
(and (pair? tree)
(or (equal? (car tree) sym)
(tree-equal-member sym (car tree))
(tree-equal-member sym (cdr tree)))))
(define (tree-unquoted-member sym tree)
(and (unquoted-pair? tree)
(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 tree1)
(let ts ((tree tree1))
(and (unquoted-pair? tree)
(or (memq (car tree) set)
(ts (car tree))
(ts (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 (tree-table-car-member set tree) ; hash-table as car
(and (pair? tree)
(or (and (hash-table-ref set (car tree))
tree)
(and (pair? (car tree))
(tree-table-car-member set (car tree)))
(and (pair? (cdr tree))
(member #f (cdr tree) (lambda (a b) (tree-table-car-member set b)))))))
(define (maker? tree)
(tree-table-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-values)
(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))))))
(define (unbegin x)
((if (and (pair? x)
(list? (cdr x))
(eq? (car x) 'begin))
cdr list)
x))
;; -------- types --------
(define (unspecified? x)
(eq? x #<unspecified>))
(define (quoted-pair? x)
(and (pair? x)
(eq? (car x) 'quote)
(pair? (cdr x))
(pair? (cadr x))))
(define (quoted-undotted-pair? x)
(and (quoted-pair? x)
(positive? (length (cadr x)))))
(define (quoted-null? x)
(and (len=2? x)
(eq? (car x) 'quote)
(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)))
;; no hits (in this context): (make-list 0 ...) (string->list "") (vector->list #()) (reverse ()) (copy ()) (append ()) (append)
(else #f)))))
(define (quoted-not? x)
(and (len=2? x)
(eq? (car x) 'quote)
(not (cadr x))))
(define (quoted-symbol? x)
(and (len=2? x)
(eq? (car x) 'quote)
(symbol? (cadr x))))
(define (just-symbols? form)
(or (null? form)
(symbol? form)
(and (pair? form)
(symbol? (car form))
(just-symbols? (cdr form)))))
(define (code-constant? x)
(and (or (not (symbol? x))
(keyword? x))
(or (not (pair? x))
(eq? (car x) 'quote))))
(define constant-expression?
(let ((constant-functions (let ((ht (make-hash-table)))
(for-each
(lambda (op)
(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
c-pointer c-pointer? c-object? car case 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-upcase char-upper-case? char-whitespace? char<=? char<?
char=? char>=? char>? char? complex complex? cons constant? continuation? cos
cosh cyclic-sequences
denominator dilambda?
eof-object? eq? equal? eqv? even? exact->inexact exact? exp expt
float? float-vector-ref float-vector? floor
gcd gensym?
hash-table-entries hash-table-ref hash-table?
imag-part inexact->exact inexact? infinite? inlet input-port?
int-vector-ref int-vector? iterator-at-end? iterator-sequence integer->char
integer-decode-float integer-length integer? iterator?
keyword->symbol keyword?
lcm let->list length let-ref let? list-ref
list-tail list? log logand logbit? logior lognot logxor
macro? magnitude make-polar
make-rectangular max member memq memv min modulo morally-equal?
nan? negative? not null? number->string number? numerator
object->string odd? openlet? or output-port?
pair? port-closed? positive?
procedure-setter procedure-signature procedure? proper-list? provided?
quote quotient
random-state? rational? rationalize real-part real? remainder reverse round
sequence? sin sinh square sqrt string->number string->symbol
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?
substring symbol symbol->keyword symbol->string symbol?
tan tanh tree-leaves tree-memq truncate
vector-dimensions vector-length vector-ref vector?
zero?))
ht)))
(lambda (val env)
(or (code-constant? val)
(and (pair? val)
(hash-table-ref constant-functions (car val))
(not (var-member (car val) env))
(every? code-constant? (cdr val)))))))
(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 "in ~A, ~A" caller
(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)
(if (pair? (cdr tree))
(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