| 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 |
(define (rest-values rest . default) |
| 26 |
(let* ((caller (if (or (null? default) |
| 27 |
(boolean? (car default)) |
| 28 |
(integer? (car default)) |
| 29 |
(memq (car default) (list + -))) |
| 30 |
'() |
| 31 |
(if (string? rest) rest (list rest)))) |
| 32 |
(rest-list (if (null? caller) rest (car default))) |
| 33 |
(rest-length (if (list? rest-list) |
| 34 |
(length rest-list) |
| 35 |
(if (string? caller) |
| 36 |
(error caller rest-list 'rest-list |
| 37 |
'(list? rest-list)) |
| 38 |
(apply error "bad rest list" rest-list 'rest-list |
| 39 |
'(list? rest-list) caller)))) |
| 40 |
(default (if (null? caller) default (cdr default))) |
| 41 |
(default-list (if (null? default) default (cdr default))) |
| 42 |
(default-length (length default-list)) |
| 43 |
(number |
| 44 |
(and (not (null? default)) |
| 45 |
(let ((option (car default))) |
| 46 |
(or (and (integer? option) |
| 47 |
(or (and (> rest-length (abs option)) |
| 48 |
(if (string? caller) |
| 49 |
(error caller rest-list 'rest-list |
| 50 |
`(<= (length rest-list) |
| 51 |
,(abs option))) |
| 52 |
(apply error "too many arguments" |
| 53 |
rest-list 'rest-list |
| 54 |
`(<= (length rest-list) |
| 55 |
,(abs option)) |
| 56 |
caller))) |
| 57 |
(and (> default-length (abs option)) |
| 58 |
(if (string? caller) |
| 59 |
(error caller default-list |
| 60 |
'default-list |
| 61 |
`(<= (length default-list) |
| 62 |
,(abs option))) |
| 63 |
(apply error "too many defaults" |
| 64 |
default-list 'default-list |
| 65 |
`(<= (length default-list) |
| 66 |
,(abs option)) |
| 67 |
caller))) |
| 68 |
option)) |
| 69 |
(eq? option #t) |
| 70 |
(and (not option) 'false) |
| 71 |
(and (eq? option +) +) |
| 72 |
(and (eq? option -) -) |
| 73 |
(if (string? caller) |
| 74 |
(error caller option 'option |
| 75 |
'(or (boolean? option) |
| 76 |
(integer? option) |
| 77 |
(memq option (list + -)))) |
| 78 |
(apply error "bad optional argument" option 'option |
| 79 |
'(or (boolean? option) |
| 80 |
(integer? option) |
| 81 |
(memq option (list + -))) |
| 82 |
caller))))))) |
| 83 |
(cond |
| 84 |
((or (eq? #t number) (eq? 'false number)) |
| 85 |
(and (not (every pair? default-list)) |
| 86 |
(if (string? caller) |
| 87 |
(error caller default-list 'default-list |
| 88 |
'(every pair? default-list)) |
| 89 |
(apply error "bad default list" default-list 'default-list |
| 90 |
'(every pair? default-list) caller))) |
| 91 |
(let loop ((rest-list rest-list) |
| 92 |
(default-list default-list) |
| 93 |
(result '())) |
| 94 |
(if (null? default-list) |
| 95 |
(if (null? rest-list) |
| 96 |
(apply values (reverse result)) |
| 97 |
(if (eq? #t number) |
| 98 |
(if (string? caller) |
| 99 |
(error caller rest-list 'rest-list '(null? rest-list)) |
| 100 |
(apply error "bad argument" rest-list 'rest-list |
| 101 |
'(null? rest-list) caller)) |
| 102 |
(apply values (append-reverse result rest-list)))) |
| 103 |
(if (null? rest-list) |
| 104 |
(apply values (append-reverse result (map car default-list))) |
| 105 |
(let ((default (car default-list))) |
| 106 |
(let lp ((rest rest-list) |
| 107 |
(head '())) |
| 108 |
(if (null? rest) |
| 109 |
(loop (reverse head) |
| 110 |
(cdr default-list) |
| 111 |
(cons (car default) result)) |
| 112 |
(if (list? default) |
| 113 |
(if (member (car rest) default) |
| 114 |
(loop (append-reverse head (cdr rest)) |
| 115 |
(cdr default-list) |
| 116 |
(cons (car rest) result)) |
| 117 |
(lp (cdr rest) (cons (car rest) head))) |
| 118 |
(if ((cdr default) (car rest)) |
| 119 |
(loop (append-reverse head (cdr rest)) |
| 120 |
(cdr default-list) |
| 121 |
(cons (car rest) result)) |
| 122 |
(lp (cdr rest) (cons (car rest) head))))))))))) |
| 123 |
((or (and (integer? number) (> number 0)) |
| 124 |
(eq? number +)) |
| 125 |
(and (not (every pair? default-list)) |
| 126 |
(if (string? caller) |
| 127 |
(error caller default-list 'default-list |
| 128 |
'(every pair? default-list)) |
| 129 |
(apply error "bad default list" default-list 'default-list |
| 130 |
'(every pair? default-list) caller))) |
| 131 |
(let loop ((rest rest-list) |
| 132 |
(default default-list)) |
| 133 |
(if (or (null? rest) (null? default)) |
| 134 |
(apply values |
| 135 |
(if (> default-length rest-length) |
| 136 |
(append rest-list |
| 137 |
(map car (list-tail default-list rest-length))) |
| 138 |
rest-list)) |
| 139 |
(let ((arg (car rest)) |
| 140 |
(par (car default))) |
| 141 |
(if (list? par) |
| 142 |
(if (member arg par) |
| 143 |
(loop (cdr rest) (cdr default)) |
| 144 |
(if (string? caller) |
| 145 |
(error caller arg 'arg `(member arg ,par)) |
| 146 |
(apply error "unmatched argument" |
| 147 |
arg 'arg `(member arg ,par) caller))) |
| 148 |
(if ((cdr par) arg) |
| 149 |
(loop (cdr rest) (cdr default)) |
| 150 |
(if (string? caller) |
| 151 |
(error caller arg 'arg `(,(cdr par) arg)) |
| 152 |
(apply error "incorrect argument" |
| 153 |
arg 'arg `(,(cdr par) arg) caller)))))))) |
| 154 |
(else |
| 155 |
(apply values (if (> default-length rest-length) |
| 156 |
(append rest-list (list-tail default-list rest-length)) |
| 157 |
rest-list)))))) |
| 158 |
|
| 159 |
(define-syntax arg-and |
| 160 |
(syntax-rules() |
| 161 |
((arg-and arg (a1 a2 ...) ...) |
| 162 |
(and (or (symbol? 'arg) |
| 163 |
(error "bad syntax" 'arg '(symbol? 'arg) |
| 164 |
'(arg-and arg (a1 a2 ...) ...))) |
| 165 |
(or (a1 a2 ...) |
| 166 |
(error "incorrect argument" arg 'arg '(a1 a2 ...))) |
| 167 |
...)) |
| 168 |
((arg-and caller arg (a1 a2 ...) ...) |
| 169 |
(and (or (symbol? 'arg) |
| 170 |
(error "bad syntax" 'arg '(symbol? 'arg) |
| 171 |
'(arg-and caller arg (a1 a2 ...) ...))) |
| 172 |
(or (a1 a2 ...) |
| 173 |
(if (string? caller) |
| 174 |
(error caller arg 'arg '(a1 a2 ...)) |
| 175 |
(error "incorrect argument" arg 'arg '(a1 a2 ...) caller))) |
| 176 |
...)))) |
| 177 |
|
| 178 |
|
| 179 |
(define-syntax caller-arg-and |
| 180 |
(syntax-rules() |
| 181 |
((caller-arg-and caller arg (a1 a2 ...) ...) |
| 182 |
(and (or (symbol? 'arg) |
| 183 |
(error "bad syntax" 'arg '(symbol? 'arg) |
| 184 |
'(caller-arg-and caller arg (a1 a2 ...) ...))) |
| 185 |
(or (a1 a2 ...) |
| 186 |
(if (string? caller) |
| 187 |
(error caller arg 'arg '(a1 a2 ...)) |
| 188 |
(error "incorrect argument" arg 'arg '(a1 a2 ...) caller))) |
| 189 |
...)) |
| 190 |
((caller-arg-and null caller arg (a1 a2 ...) ...) |
| 191 |
(and (or (symbol? 'arg) |
| 192 |
(error "bad syntax" 'arg '(symbol? 'arg) |
| 193 |
'(caller-arg-and caller arg (a1 a2 ...) ...))) |
| 194 |
(or (a1 a2 ...) |
| 195 |
(if (string? caller) |
| 196 |
(error caller arg 'arg '(a1 a2 ...)) |
| 197 |
(error "incorrect argument" arg 'arg '(a1 a2 ...) caller))) |
| 198 |
...)))) |
| 199 |
|
| 200 |
(define-syntax arg-ands |
| 201 |
(syntax-rules (common) |
| 202 |
((arg-ands (a1 a2 ...) ...) |
| 203 |
(and (arg-and a1 a2 ...) ...)) |
| 204 |
((arg-ands common caller (a1 a2 ...) ...) |
| 205 |
(and (caller-arg-and caller a1 a2 ...) ...)))) |
| 206 |
|
| 207 |
(define-syntax arg-or |
| 208 |
(syntax-rules() |
| 209 |
((arg-or arg (a1 a2 ...) ...) |
| 210 |
(or (and (not (symbol? 'arg)) |
| 211 |
(error "bad syntax" 'arg '(symbol? 'arg) |
| 212 |
'(arg-or arg (a1 a2 ...) ...))) |
| 213 |
(and (a1 a2 ...) |
| 214 |
(error "incorrect argument" arg 'arg '(a1 a2 ...))) |
| 215 |
...)) |
| 216 |
((arg-or caller arg (a1 a2 ...) ...) |
| 217 |
(or (and (not (symbol? 'arg)) |
| 218 |
(error "bad syntax" 'arg '(symbol? 'arg) |
| 219 |
'(arg-or caller arg (a1 a2 ...) ...))) |
| 220 |
(and (a1 a2 ...) |
| 221 |
(if (string? caller) |
| 222 |
(error caller arg 'arg '(a1 a2 ...)) |
| 223 |
(error "incorrect argument" arg 'arg '(a1 a2 ...) caller))) |
| 224 |
...)))) |
| 225 |
|
| 226 |
|
| 227 |
(define-syntax caller-arg-or |
| 228 |
(syntax-rules() |
| 229 |
((caller-arg-or caller arg (a1 a2 ...) ...) |
| 230 |
(or (and (not (symbol? 'arg)) |
| 231 |
(error "bad syntax" 'arg '(symbol? 'arg) |
| 232 |
'(caller-arg-or caller arg (a1 a2 ...) ...))) |
| 233 |
(and (a1 a2 ...) |
| 234 |
(if (string? caller) |
| 235 |
(error caller arg 'arg '(a1 a2 ...)) |
| 236 |
(error "incorrect argument" arg 'arg '(a1 a2 ...) caller))) |
| 237 |
...)) |
| 238 |
((caller-arg-or null caller arg (a1 a2 ...) ...) |
| 239 |
(or (and (not (symbol? 'arg)) |
| 240 |
(error "bad syntax" 'arg '(symbol? 'arg) |
| 241 |
'(caller-arg-or caller arg (a1 a2 ...) ...))) |
| 242 |
(and (a1 a2 ...) |
| 243 |
(if (string? caller) |
| 244 |
(error caller arg 'arg '(a1 a2 ...)) |
| 245 |
(error "incorrect argument" arg 'arg '(a1 a2 ...) caller))) |
| 246 |
...)))) |
| 247 |
|
| 248 |
(define-syntax arg-ors |
| 249 |
(syntax-rules (common) |
| 250 |
((arg-ors (a1 a2 ...) ...) |
| 251 |
(or (arg-or a1 a2 ...) ...)) |
| 252 |
((arg-ors common caller (a1 a2 ...) ...) |
| 253 |
(or (caller-arg-or caller a1 a2 ...) ...)))) |
| 254 |
|
| 255 |
(define-syntax err-and |
| 256 |
(syntax-rules () |
| 257 |
((err-and err expression ...) |
| 258 |
(and (or expression |
| 259 |
(if (string? err) |
| 260 |
(error err 'expression) |
| 261 |
(error "false expression" 'expression err))) |
| 262 |
...)))) |
| 263 |
|
| 264 |
(define-syntax err-ands |
| 265 |
(syntax-rules () |
| 266 |
((err-ands (err expression ...) ...) |
| 267 |
(and (err-and err expression ...) |
| 268 |
...)))) |
| 269 |
|
| 270 |
(define-syntax err-or |
| 271 |
(syntax-rules () |
| 272 |
((err-or err expression ...) |
| 273 |
(or (and expression |
| 274 |
(if (string? err) |
| 275 |
(error err 'expression) |
| 276 |
(error "true expression" 'expression err))) |
| 277 |
...)))) |
| 278 |
|
| 279 |
(define-syntax err-ors |
| 280 |
(syntax-rules () |
| 281 |
((err-ors (err expression ...) ...) |
| 282 |
(or (err-or err expression ...) |
| 283 |
...)))) |
| 284 |
|