Commit | Line | Data |
---|---|---|
9ddacf86 KN |
1 | ;;; "paramlst.scm" passing parameters by name. |
2 | ; Copyright 1995, 1996, 1997, 2001 Aubrey Jaffer | |
3 | ; | |
4 | ;Permission to copy this software, to redistribute it, and to use it | |
5 | ;for any purpose is granted, subject to the following restrictions and | |
6 | ;understandings. | |
7 | ; | |
8 | ;1. Any copy made of this software must include this copyright notice | |
9 | ;in full. | |
10 | ; | |
11 | ;2. I have made no warrantee or representation that the operation of | |
12 | ;this software will be error-free, and I am under no obligation to | |
13 | ;provide any services, by way of maintenance, update, or otherwise. | |
14 | ; | |
15 | ;3. In conjunction with products arising from the use of this | |
16 | ;material, there shall be no use of my name in any advertising, | |
17 | ;promotional, or sales literature without prior written consent in | |
18 | ;each case. | |
19 | ||
20 | ;;; Format of arity-spec: (name predicate conversion) | |
21 | ||
22 | (require 'common-list-functions) | |
23 | ||
24 | (define arity->arity-spec | |
25 | (let ((table | |
26 | `((nary | |
27 | ,(lambda (a) #t) | |
28 | ,identity) | |
29 | (nary1 | |
30 | ,(lambda (a) (not (null? a))) | |
31 | ,identity) | |
32 | (single | |
33 | ,(lambda (a) (and (pair? a) (null? (cdr a)))) | |
34 | ,car) | |
35 | (optional | |
36 | ,(lambda (a) (or (null? a) (and (pair? a) (null? (cdr a))))) | |
37 | ,identity) | |
38 | (boolean | |
39 | ,(lambda (a) | |
40 | (or (null? a) | |
41 | (and (pair? a) (null? (cdr a)) (boolean? (car a))))) | |
42 | ,(lambda (a) (if (null? a) #f (car a))))))) | |
43 | (lambda (arity) | |
44 | (assq arity table)))) | |
45 | ||
46 | (define (fill-empty-parameters defaulters parameter-list) | |
47 | (map (lambda (defaulter parameter) | |
48 | (cond ((null? (cdr parameter)) | |
49 | (cons (car parameter) | |
50 | (if defaulter (defaulter parameter-list) '()))) | |
51 | (else parameter))) | |
52 | defaulters parameter-list)) | |
53 | ||
54 | (define (check-parameters checks parameter-list) | |
55 | (and (every (lambda (check parameter) | |
56 | (every | |
57 | (lambda (p) | |
58 | (let ((good? (not (and check (not (check p)))))) | |
59 | (if (not good?) (slib:warn (car parameter) 'parameter? p)) | |
60 | good?)) | |
61 | (cdr parameter))) | |
62 | checks parameter-list) | |
63 | parameter-list)) | |
64 | ||
65 | (define (check-arities arity-specs parameter-list) | |
66 | (every (lambda (arity-spec param) | |
67 | (cond ((not arity-spec) (slib:warn 'missing 'arity arity-specs) #f) | |
68 | (((cadr arity-spec) (cdr param)) #t) | |
69 | ((null? (cdr param)) (slib:warn param 'missing) #f) | |
70 | (else (slib:warn param 'not (car arity-spec)) #f))) | |
71 | arity-specs parameter-list)) | |
72 | ||
73 | (define (parameter-list->arglist positions arities parameter-list) | |
74 | (and (= (length arities) (length positions) (length parameter-list)) | |
75 | (let ((arity-specs (map arity->arity-spec arities)) | |
76 | (ans (make-vector (length positions) #f))) | |
77 | (and (check-arities arity-specs parameter-list) | |
78 | (for-each | |
79 | (lambda (pos arity-spec param) | |
80 | (vector-set! ans (+ -1 pos) | |
81 | ((caddr arity-spec) (cdr param)))) | |
82 | positions arity-specs parameter-list) | |
83 | (vector->list ans))))) | |
84 | ||
85 | (define (make-parameter-list parameter-names) | |
86 | (map list parameter-names)) | |
87 | ||
88 | (define (parameter-list-ref parameter-list i) | |
89 | (let ((ans (assoc i parameter-list))) | |
90 | (and ans (cdr ans)))) | |
91 | ||
92 | (define (parameter-list-expand expanders parms) | |
93 | (do ((lens (map length parms) (map length parms)) | |
94 | (olens '() lens)) | |
95 | ((equal? lens olens)) | |
96 | (for-each (lambda (expander parm) | |
97 | (cond | |
98 | (expander | |
99 | (for-each | |
100 | (lambda (news) | |
101 | (cond ((adjoin-parameters! parms news)) | |
102 | (else (slib:error | |
103 | "expanded feature unknown: " news)))) | |
104 | (apply append | |
105 | (map (lambda (p) | |
106 | (cond ((expander p)) | |
107 | ((not '()) '()) | |
108 | (else (slib:error | |
109 | "couldn't expand feature: " p)))) | |
110 | (cdr parm))))))) | |
111 | expanders | |
112 | parms))) | |
113 | ||
114 | (define (adjoin-parameters! parameter-list . parameters) | |
115 | (let ((apairs (map (lambda (param) | |
116 | (cond ((pair? param) | |
117 | (assoc (car param) parameter-list)) | |
118 | (else (assoc param parameter-list)))) | |
119 | parameters))) | |
120 | (and (every identity apairs) ;same as APPLY AND? | |
121 | (for-each | |
122 | (lambda (apair param) | |
123 | (cond ((pair? param) | |
124 | (for-each (lambda (o) | |
125 | (if (not (member o (cdr apair))) | |
126 | (set-cdr! apair (cons o (cdr apair))))) | |
127 | (cdr param))) | |
128 | (else (if (not (memv #t (cdr apair))) | |
129 | (set-cdr! apair (cons #t (cdr apair))))))) | |
130 | apairs parameters) | |
131 | parameter-list))) | |
132 | ||
133 | (define (remove-parameter pname parameter-list) | |
134 | (define found? #f) | |
135 | (remove-if (lambda (elt) | |
136 | (cond ((not (and (pair? elt) (eqv? pname (car elt)))) #f) | |
137 | (found? | |
138 | (slib:error | |
139 | 'remove-parameter 'multiple pname 'in parameter-list)) | |
140 | (else (set! found? #t) #t))) | |
141 | parameter-list)) |