1 |
; Initialization file for TinySCHEME 1.41 |
2 |
|
3 |
;;;; Utility to ease macro creation |
4 |
(define (macro-expand form) |
5 |
((eval (get-closure-code (eval (car form)))) form)) |
6 |
|
7 |
(define (macro-expand-all form) |
8 |
(if (macro? form) |
9 |
(macro-expand-all (macro-expand form)) |
10 |
form)) |
11 |
|
12 |
(define *compile-hook* macro-expand-all) |
13 |
|
14 |
|
15 |
(macro (unless form) |
16 |
`(if (not ,(cadr form)) (begin ,@(cddr form)))) |
17 |
|
18 |
(macro (when form) |
19 |
`(if ,(cadr form) (begin ,@(cddr form)))) |
20 |
|
21 |
; DEFINE-MACRO Contributed by Andy Gaynor |
22 |
(macro (define-macro dform) |
23 |
(if (symbol? (cadr dform)) |
24 |
`(macro ,@(cdr dform)) |
25 |
(let ((form (gensym))) |
26 |
`(macro (,(caadr dform) ,form) |
27 |
(apply (lambda ,(cdadr dform) ,@(cddr dform)) (cdr ,form)))))) |
28 |
|
29 |
; Utilities for math. Notice that inexact->exact is primitive, |
30 |
; but exact->inexact is not. |
31 |
(define exact? integer?) |
32 |
(define (inexact? x) (and (real? x) (not (integer? x)))) |
33 |
(define (even? n) (= (remainder n 2) 0)) |
34 |
(define (odd? n) (not (= (remainder n 2) 0))) |
35 |
(define (zero? n) (= n 0)) |
36 |
(define (positive? n) (> n 0)) |
37 |
(define (negative? n) (< n 0)) |
38 |
(define complex? number?) |
39 |
(define rational? real?) |
40 |
(define (abs n) (if (>= n 0) n (- n))) |
41 |
(define (exact->inexact n) (* n 1.0)) |
42 |
(define (<> n1 n2) (not (= n1 n2))) |
43 |
|
44 |
; min and max must return inexact if any arg is inexact; use (+ n 0.0) |
45 |
(define (max . lst) |
46 |
(foldr (lambda (a b) |
47 |
(if (> a b) |
48 |
(if (exact? b) a (+ a 0.0)) |
49 |
(if (exact? a) b (+ b 0.0)))) |
50 |
(car lst) (cdr lst))) |
51 |
(define (min . lst) |
52 |
(foldr (lambda (a b) |
53 |
(if (< a b) |
54 |
(if (exact? b) a (+ a 0.0)) |
55 |
(if (exact? a) b (+ b 0.0)))) |
56 |
(car lst) (cdr lst))) |
57 |
|
58 |
(define (succ x) (+ x 1)) |
59 |
(define (pred x) (- x 1)) |
60 |
(define gcd |
61 |
(lambda a |
62 |
(if (null? a) |
63 |
0 |
64 |
(let ((aa (abs (car a))) |
65 |
(bb (abs (cadr a)))) |
66 |
(if (= bb 0) |
67 |
aa |
68 |
(gcd bb (remainder aa bb))))))) |
69 |
(define lcm |
70 |
(lambda a |
71 |
(if (null? a) |
72 |
1 |
73 |
(let ((aa (abs (car a))) |
74 |
(bb (abs (cadr a)))) |
75 |
(if (or (= aa 0) (= bb 0)) |
76 |
0 |
77 |
(abs (* (quotient aa (gcd aa bb)) bb))))))) |
78 |
|
79 |
|
80 |
(define (string . charlist) |
81 |
(list->string charlist)) |
82 |
|
83 |
(define (list->string charlist) |
84 |
(let* ((len (length charlist)) |
85 |
(newstr (make-string len)) |
86 |
(fill-string! |
87 |
(lambda (str i len charlist) |
88 |
(if (= i len) |
89 |
str |
90 |
(begin (string-set! str i (car charlist)) |
91 |
(fill-string! str (+ i 1) len (cdr charlist))))))) |
92 |
(fill-string! newstr 0 len charlist))) |
93 |
|
94 |
(define (string-fill! s e) |
95 |
(let ((n (string-length s))) |
96 |
(let loop ((i 0)) |
97 |
(if (= i n) |
98 |
s |
99 |
(begin (string-set! s i e) (loop (succ i))))))) |
100 |
|
101 |
(define (string->list s) |
102 |
(let loop ((n (pred (string-length s))) (l '())) |
103 |
(if (= n -1) |
104 |
l |
105 |
(loop (pred n) (cons (string-ref s n) l))))) |
106 |
|
107 |
(define (string-copy str) |
108 |
(string-append str)) |
109 |
|
110 |
(define (string->anyatom str pred) |
111 |
(let* ((a (string->atom str))) |
112 |
(if (pred a) a |
113 |
(error "string->xxx: not a xxx" a)))) |
114 |
|
115 |
(define (string->number str . base) |
116 |
(let ((n (string->atom str (if (null? base) 10 (car base))))) |
117 |
(if (number? n) n #f))) |
118 |
|
119 |
(define (anyatom->string n pred) |
120 |
(if (pred n) |
121 |
(atom->string n) |
122 |
(error "xxx->string: not a xxx" n))) |
123 |
|
124 |
(define (number->string n . base) |
125 |
(atom->string n (if (null? base) 10 (car base)))) |
126 |
|
127 |
|
128 |
(define (char-cmp? cmp a b) |
129 |
(cmp (char->integer a) (char->integer b))) |
130 |
(define (char-ci-cmp? cmp a b) |
131 |
(cmp (char->integer (char-downcase a)) (char->integer (char-downcase b)))) |
132 |
|
133 |
(define (char=? a b) (char-cmp? = a b)) |
134 |
(define (char<? a b) (char-cmp? < a b)) |
135 |
(define (char>? a b) (char-cmp? > a b)) |
136 |
(define (char<=? a b) (char-cmp? <= a b)) |
137 |
(define (char>=? a b) (char-cmp? >= a b)) |
138 |
|
139 |
(define (char-ci=? a b) (char-ci-cmp? = a b)) |
140 |
(define (char-ci<? a b) (char-ci-cmp? < a b)) |
141 |
(define (char-ci>? a b) (char-ci-cmp? > a b)) |
142 |
(define (char-ci<=? a b) (char-ci-cmp? <= a b)) |
143 |
(define (char-ci>=? a b) (char-ci-cmp? >= a b)) |
144 |
|
145 |
; Note the trick of returning (cmp x y) |
146 |
(define (string-cmp? chcmp cmp a b) |
147 |
(let ((na (string-length a)) (nb (string-length b))) |
148 |
(let loop ((i 0)) |
149 |
(cond |
150 |
((= i na) |
151 |
(if (= i nb) (cmp 0 0) (cmp 0 1))) |
152 |
((= i nb) |
153 |
(cmp 1 0)) |
154 |
((chcmp = (string-ref a i) (string-ref b i)) |
155 |
(loop (succ i))) |
156 |
(else |
157 |
(chcmp cmp (string-ref a i) (string-ref b i))))))) |
158 |
|
159 |
|
160 |
(define (string=? a b) (string-cmp? char-cmp? = a b)) |
161 |
(define (string<? a b) (string-cmp? char-cmp? < a b)) |
162 |
(define (string>? a b) (string-cmp? char-cmp? > a b)) |
163 |
(define (string<=? a b) (string-cmp? char-cmp? <= a b)) |
164 |
(define (string>=? a b) (string-cmp? char-cmp? >= a b)) |
165 |
|
166 |
(define (string-ci=? a b) (string-cmp? char-ci-cmp? = a b)) |
167 |
(define (string-ci<? a b) (string-cmp? char-ci-cmp? < a b)) |
168 |
(define (string-ci>? a b) (string-cmp? char-ci-cmp? > a b)) |
169 |
(define (string-ci<=? a b) (string-cmp? char-ci-cmp? <= a b)) |
170 |
(define (string-ci>=? a b) (string-cmp? char-ci-cmp? >= a b)) |
171 |
|
172 |
(define (list . x) x) |
173 |
|
174 |
(define (foldr f x lst) |
175 |
(if (null? lst) |
176 |
x |
177 |
(foldr f (f x (car lst)) (cdr lst)))) |
178 |
|
179 |
(define (unzip1-with-cdr . lists) |
180 |
(unzip1-with-cdr-iterative lists '() '())) |
181 |
|
182 |
(define (unzip1-with-cdr-iterative lists cars cdrs) |
183 |
(if (null? lists) |
184 |
(cons cars cdrs) |
185 |
(let ((car1 (caar lists)) |
186 |
(cdr1 (cdar lists))) |
187 |
(unzip1-with-cdr-iterative |
188 |
(cdr lists) |
189 |
(append cars (list car1)) |
190 |
(append cdrs (list cdr1)))))) |
191 |
|
192 |
(define (map proc . lists) |
193 |
(if (null? lists) |
194 |
(apply proc) |
195 |
(if (null? (car lists)) |
196 |
'() |
197 |
(let* ((unz (apply unzip1-with-cdr lists)) |
198 |
(cars (car unz)) |
199 |
(cdrs (cdr unz))) |
200 |
(cons (apply proc cars) (apply map (cons proc cdrs))))))) |
201 |
|
202 |
(define (for-each proc . lists) |
203 |
(if (null? lists) |
204 |
(apply proc) |
205 |
(if (null? (car lists)) |
206 |
#t |
207 |
(let* ((unz (apply unzip1-with-cdr lists)) |
208 |
(cars (car unz)) |
209 |
(cdrs (cdr unz))) |
210 |
(apply proc cars) (apply map (cons proc cdrs)))))) |
211 |
|
212 |
(define (list-tail x k) |
213 |
(if (zero? k) |
214 |
x |
215 |
(list-tail (cdr x) (- k 1)))) |
216 |
|
217 |
(define (list-ref x k) |
218 |
(car (list-tail x k))) |
219 |
|
220 |
(define (last-pair x) |
221 |
(if (pair? (cdr x)) |
222 |
(last-pair (cdr x)) |
223 |
x)) |
224 |
|
225 |
(define (head stream) (car stream)) |
226 |
|
227 |
(define (tail stream) (force (cdr stream))) |
228 |
|
229 |
(define (vector-equal? x y) |
230 |
(and (vector? x) (vector? y) (= (vector-length x) (vector-length y)) |
231 |
(let ((n (vector-length x))) |
232 |
(let loop ((i 0)) |
233 |
(if (= i n) |
234 |
#t |
235 |
(and (equal? (vector-ref x i) (vector-ref y i)) |
236 |
(loop (succ i)))))))) |
237 |
|
238 |
(define (list->vector x) |
239 |
(apply vector x)) |
240 |
|
241 |
(define (vector-fill! v e) |
242 |
(let ((n (vector-length v))) |
243 |
(let loop ((i 0)) |
244 |
(if (= i n) |
245 |
v |
246 |
(begin (vector-set! v i e) (loop (succ i))))))) |
247 |
|
248 |
(define (vector->list v) |
249 |
(let loop ((n (pred (vector-length v))) (l '())) |
250 |
(if (= n -1) |
251 |
l |
252 |
(loop (pred n) (cons (vector-ref v n) l))))) |
253 |
|
254 |
;; The following quasiquote macro is due to Eric S. Tiedemann. |
255 |
;; Copyright 1988 by Eric S. Tiedemann; all rights reserved. |
256 |
;; |
257 |
;; Subsequently modified to handle vectors: D. Souflis |
258 |
|
259 |
(macro |
260 |
quasiquote |
261 |
(lambda (l) |
262 |
(define (mcons f l r) |
263 |
(if (and (pair? r) |
264 |
(eq? (car r) 'quote) |
265 |
(eq? (car (cdr r)) (cdr f)) |
266 |
(pair? l) |
267 |
(eq? (car l) 'quote) |
268 |
(eq? (car (cdr l)) (car f))) |
269 |
(if (or (procedure? f) (number? f) (string? f)) |
270 |
f |
271 |
(list 'quote f)) |
272 |
(if (eqv? l vector) |
273 |
(apply l (eval r)) |
274 |
(list 'cons l r) |
275 |
))) |
276 |
(define (mappend f l r) |
277 |
(if (or (null? (cdr f)) |
278 |
(and (pair? r) |
279 |
(eq? (car r) 'quote) |
280 |
(eq? (car (cdr r)) '()))) |
281 |
l |
282 |
(list 'append l r))) |
283 |
(define (foo level form) |
284 |
(cond ((not (pair? form)) |
285 |
(if (or (procedure? form) (number? form) (string? form)) |
286 |
form |
287 |
(list 'quote form)) |
288 |
) |
289 |
((eq? 'quasiquote (car form)) |
290 |
(mcons form ''quasiquote (foo (+ level 1) (cdr form)))) |
291 |
(#t (if (zero? level) |
292 |
(cond ((eq? (car form) 'unquote) (car (cdr form))) |
293 |
((eq? (car form) 'unquote-splicing) |
294 |
(error "Unquote-splicing wasn't in a list:" |
295 |
form)) |
296 |
((and (pair? (car form)) |
297 |
(eq? (car (car form)) 'unquote-splicing)) |
298 |
(mappend form (car (cdr (car form))) |
299 |
(foo level (cdr form)))) |
300 |
(#t (mcons form (foo level (car form)) |
301 |
(foo level (cdr form))))) |
302 |
(cond ((eq? (car form) 'unquote) |
303 |
(mcons form ''unquote (foo (- level 1) |
304 |
(cdr form)))) |
305 |
((eq? (car form) 'unquote-splicing) |
306 |
(mcons form ''unquote-splicing |
307 |
(foo (- level 1) (cdr form)))) |
308 |
(#t (mcons form (foo level (car form)) |
309 |
(foo level (cdr form))))))))) |
310 |
(foo 0 (car (cdr l))))) |
311 |
|
312 |
;;;;;Helper for the dynamic-wind definition. By Tom Breton (Tehom) |
313 |
(define (shared-tail x y) |
314 |
(let ((len-x (length x)) |
315 |
(len-y (length y))) |
316 |
(define (shared-tail-helper x y) |
317 |
(if |
318 |
(eq? x y) |
319 |
x |
320 |
(shared-tail-helper (cdr x) (cdr y)))) |
321 |
|
322 |
(cond |
323 |
((> len-x len-y) |
324 |
(shared-tail-helper |
325 |
(list-tail x (- len-x len-y)) |
326 |
y)) |
327 |
((< len-x len-y) |
328 |
(shared-tail-helper |
329 |
x |
330 |
(list-tail y (- len-y len-x)))) |
331 |
(#t (shared-tail-helper x y))))) |
332 |
|
333 |
;;;;;Dynamic-wind by Tom Breton (Tehom) |
334 |
|
335 |
;;Guarded because we must only eval this once, because doing so |
336 |
;;redefines call/cc in terms of old call/cc |
337 |
(unless (defined? 'dynamic-wind) |
338 |
(let |
339 |
;;These functions are defined in the context of a private list of |
340 |
;;pairs of before/after procs. |
341 |
( (*active-windings* '()) |
342 |
;;We'll define some functions into the larger environment, so |
343 |
;;we need to know it. |
344 |
(outer-env (current-environment))) |
345 |
|
346 |
;;Poor-man's structure operations |
347 |
(define before-func car) |
348 |
(define after-func cdr) |
349 |
(define make-winding cons) |
350 |
|
351 |
;;Manage active windings |
352 |
(define (activate-winding! new) |
353 |
((before-func new)) |
354 |
(set! *active-windings* (cons new *active-windings*))) |
355 |
(define (deactivate-top-winding!) |
356 |
(let ((old-top (car *active-windings*))) |
357 |
;;Remove it from the list first so it's not active during its |
358 |
;;own exit. |
359 |
(set! *active-windings* (cdr *active-windings*)) |
360 |
((after-func old-top)))) |
361 |
|
362 |
(define (set-active-windings! new-ws) |
363 |
(unless (eq? new-ws *active-windings*) |
364 |
(let ((shared (shared-tail new-ws *active-windings*))) |
365 |
|
366 |
;;Define the looping functions. |
367 |
;;Exit the old list. Do deeper ones last. Don't do |
368 |
;;any shared ones. |
369 |
(define (pop-many) |
370 |
(unless (eq? *active-windings* shared) |
371 |
(deactivate-top-winding!) |
372 |
(pop-many))) |
373 |
;;Enter the new list. Do deeper ones first so that the |
374 |
;;deeper windings will already be active. Don't do any |
375 |
;;shared ones. |
376 |
(define (push-many new-ws) |
377 |
(unless (eq? new-ws shared) |
378 |
(push-many (cdr new-ws)) |
379 |
(activate-winding! (car new-ws)))) |
380 |
|
381 |
;;Do it. |
382 |
(pop-many) |
383 |
(push-many new-ws)))) |
384 |
|
385 |
;;The definitions themselves. |
386 |
(eval |
387 |
`(define call-with-current-continuation |
388 |
;;It internally uses the built-in call/cc, so capture it. |
389 |
,(let ((old-c/cc call-with-current-continuation)) |
390 |
(lambda (func) |
391 |
;;Use old call/cc to get the continuation. |
392 |
(old-c/cc |
393 |
(lambda (continuation) |
394 |
;;Call func with not the continuation itself |
395 |
;;but a procedure that adjusts the active |
396 |
;;windings to what they were when we made |
397 |
;;this, and only then calls the |
398 |
;;continuation. |
399 |
(func |
400 |
(let ((current-ws *active-windings*)) |
401 |
(lambda (x) |
402 |
(set-active-windings! current-ws) |
403 |
(continuation x))))))))) |
404 |
outer-env) |
405 |
;;We can't just say "define (dynamic-wind before thunk after)" |
406 |
;;because the lambda it's defined to lives in this environment, |
407 |
;;not in the global environment. |
408 |
(eval |
409 |
`(define dynamic-wind |
410 |
,(lambda (before thunk after) |
411 |
;;Make a new winding |
412 |
(activate-winding! (make-winding before after)) |
413 |
(let ((result (thunk))) |
414 |
;;Get rid of the new winding. |
415 |
(deactivate-top-winding!) |
416 |
;;The return value is that of thunk. |
417 |
result))) |
418 |
outer-env))) |
419 |
|
420 |
(define call/cc call-with-current-continuation) |
421 |
|
422 |
|
423 |
;;;;; atom? and equal? written by a.k |
424 |
|
425 |
;;;; atom? |
426 |
(define (atom? x) |
427 |
(not (pair? x))) |
428 |
|
429 |
;;;; equal? |
430 |
(define (equal? x y) |
431 |
(cond |
432 |
((pair? x) |
433 |
(and (pair? y) |
434 |
(equal? (car x) (car y)) |
435 |
(equal? (cdr x) (cdr y)))) |
436 |
((vector? x) |
437 |
(and (vector? y) (vector-equal? x y))) |
438 |
((string? x) |
439 |
(and (string? y) (string=? x y))) |
440 |
(else (eqv? x y)))) |
441 |
|
442 |
;;;; (do ((var init inc) ...) (endtest result ...) body ...) |
443 |
;; |
444 |
(macro do |
445 |
(lambda (do-macro) |
446 |
(apply (lambda (do vars endtest . body) |
447 |
(let ((do-loop (gensym))) |
448 |
`(letrec ((,do-loop |
449 |
(lambda ,(map (lambda (x) |
450 |
(if (pair? x) (car x) x)) |
451 |
`,vars) |
452 |
(if ,(car endtest) |
453 |
(begin ,@(cdr endtest)) |
454 |
(begin |
455 |
,@body |
456 |
(,do-loop |
457 |
,@(map (lambda (x) |
458 |
(cond |
459 |
((not (pair? x)) x) |
460 |
((< (length x) 3) (car x)) |
461 |
(else (car (cdr (cdr x)))))) |
462 |
`,vars))))))) |
463 |
(,do-loop |
464 |
,@(map (lambda (x) |
465 |
(if (and (pair? x) (cdr x)) |
466 |
(car (cdr x)) |
467 |
'())) |
468 |
`,vars))))) |
469 |
do-macro))) |
470 |
|
471 |
;;;; generic-member |
472 |
(define (generic-member cmp obj lst) |
473 |
(cond |
474 |
((null? lst) #f) |
475 |
((cmp obj (car lst)) lst) |
476 |
(else (generic-member cmp obj (cdr lst))))) |
477 |
|
478 |
(define (memq obj lst) |
479 |
(generic-member eq? obj lst)) |
480 |
(define (memv obj lst) |
481 |
(generic-member eqv? obj lst)) |
482 |
(define (member obj lst) |
483 |
(generic-member equal? obj lst)) |
484 |
|
485 |
;;;; generic-assoc |
486 |
(define (generic-assoc cmp obj alst) |
487 |
(cond |
488 |
((null? alst) #f) |
489 |
((cmp obj (caar alst)) (car alst)) |
490 |
(else (generic-assoc cmp obj (cdr alst))))) |
491 |
|
492 |
(define (assq obj alst) |
493 |
(generic-assoc eq? obj alst)) |
494 |
(define (assv obj alst) |
495 |
(generic-assoc eqv? obj alst)) |
496 |
(define (assoc obj alst) |
497 |
(generic-assoc equal? obj alst)) |
498 |
|
499 |
(define (acons x y z) (cons (cons x y) z)) |
500 |
|
501 |
;;;; Handy for imperative programs |
502 |
;;;; Used as: (define-with-return (foo x y) .... (return z) ...) |
503 |
(macro (define-with-return form) |
504 |
`(define ,(cadr form) |
505 |
(call/cc (lambda (return) ,@(cddr form))))) |
506 |
|
507 |
;;;; Simple exception handling |
508 |
; |
509 |
; Exceptions are caught as follows: |
510 |
; |
511 |
; (catch (do-something to-recover and-return meaningful-value) |
512 |
; (if-something goes-wrong) |
513 |
; (with-these calls)) |
514 |
; |
515 |
; "Catch" establishes a scope spanning multiple call-frames |
516 |
; until another "catch" is encountered. |
517 |
; |
518 |
; Exceptions are thrown with: |
519 |
; |
520 |
; (throw "message") |
521 |
; |
522 |
; If used outside a (catch ...), reverts to (error "message) |
523 |
|
524 |
(define *handlers* (list)) |
525 |
|
526 |
(define (push-handler proc) |
527 |
(set! *handlers* (cons proc *handlers*))) |
528 |
|
529 |
(define (pop-handler) |
530 |
(let ((h (car *handlers*))) |
531 |
(set! *handlers* (cdr *handlers*)) |
532 |
h)) |
533 |
|
534 |
(define (more-handlers?) |
535 |
(pair? *handlers*)) |
536 |
|
537 |
(define (throw . x) |
538 |
(if (more-handlers?) |
539 |
(apply (pop-handler)) |
540 |
(apply error x))) |
541 |
|
542 |
(macro (catch form) |
543 |
(let ((label (gensym))) |
544 |
`(call/cc (lambda (exit) |
545 |
(push-handler (lambda () (exit ,(cadr form)))) |
546 |
(let ((,label (begin ,@(cddr form)))) |
547 |
(pop-handler) |
548 |
,label))))) |
549 |
|
550 |
(define *error-hook* throw) |
551 |
|
552 |
|
553 |
;;;;; Definition of MAKE-ENVIRONMENT, to be used with two-argument EVAL |
554 |
|
555 |
(macro (make-environment form) |
556 |
`(apply (lambda () |
557 |
,@(cdr form) |
558 |
(current-environment)))) |
559 |
|
560 |
(define-macro (eval-polymorphic x . envl) |
561 |
(display envl) |
562 |
(let* ((env (if (null? envl) (current-environment) (eval (car envl)))) |
563 |
(xval (eval x env))) |
564 |
(if (closure? xval) |
565 |
(make-closure (get-closure-code xval) env) |
566 |
xval))) |
567 |
|
568 |
; Redefine this if you install another package infrastructure |
569 |
; Also redefine 'package' |
570 |
(define *colon-hook* eval) |
571 |
|
572 |
;;;;; I/O |
573 |
|
574 |
(define (input-output-port? p) |
575 |
(and (input-port? p) (output-port? p))) |
576 |
|
577 |
(define (close-port p) |
578 |
(cond |
579 |
((input-output-port? p) (close-input-port (close-output-port p))) |
580 |
((input-port? p) (close-input-port p)) |
581 |
((output-port? p) (close-output-port p)) |
582 |
(else (throw "Not a port" p)))) |
583 |
|
584 |
(define (call-with-input-file s p) |
585 |
(let ((inport (open-input-file s))) |
586 |
(if (eq? inport #f) |
587 |
#f |
588 |
(let ((res (p inport))) |
589 |
(close-input-port inport) |
590 |
res)))) |
591 |
|
592 |
(define (call-with-output-file s p) |
593 |
(let ((outport (open-output-file s))) |
594 |
(if (eq? outport #f) |
595 |
#f |
596 |
(let ((res (p outport))) |
597 |
(close-output-port outport) |
598 |
res)))) |
599 |
|
600 |
(define (with-input-from-file s p) |
601 |
(let ((inport (open-input-file s))) |
602 |
(if (eq? inport #f) |
603 |
#f |
604 |
(let ((prev-inport (current-input-port))) |
605 |
(set-input-port inport) |
606 |
(let ((res (p))) |
607 |
(close-input-port inport) |
608 |
(set-input-port prev-inport) |
609 |
res))))) |
610 |
|
611 |
(define (with-output-to-file s p) |
612 |
(let ((outport (open-output-file s))) |
613 |
(if (eq? outport #f) |
614 |
#f |
615 |
(let ((prev-outport (current-output-port))) |
616 |
(set-output-port outport) |
617 |
(let ((res (p))) |
618 |
(close-output-port outport) |
619 |
(set-output-port prev-outport) |
620 |
res))))) |
621 |
|
622 |
(define (with-input-output-from-to-files si so p) |
623 |
(let ((inport (open-input-file si)) |
624 |
(outport (open-input-file so))) |
625 |
(if (not (and inport outport)) |
626 |
(begin |
627 |
(close-input-port inport) |
628 |
(close-output-port outport) |
629 |
#f) |
630 |
(let ((prev-inport (current-input-port)) |
631 |
(prev-outport (current-output-port))) |
632 |
(set-input-port inport) |
633 |
(set-output-port outport) |
634 |
(let ((res (p))) |
635 |
(close-input-port inport) |
636 |
(close-output-port outport) |
637 |
(set-input-port prev-inport) |
638 |
(set-output-port prev-outport) |
639 |
res))))) |
640 |
|
641 |
; Random number generator (maximum cycle) |
642 |
(define *seed* 1) |
643 |
(define (random-next) |
644 |
(let* ((a 16807) (m 2147483647) (q (quotient m a)) (r (modulo m a))) |
645 |
(set! *seed* |
646 |
(- (* a (- *seed* |
647 |
(* (quotient *seed* q) q))) |
648 |
(* (quotient *seed* q) r))) |
649 |
(if (< *seed* 0) (set! *seed* (+ *seed* m))) |
650 |
*seed*)) |
651 |
|
652 |
;; SRFI-0 |
653 |
;; COND-EXPAND |
654 |
;; Implemented as a macro |
655 |
(define *features* '(srfi-0)) |
656 |
|
657 |
(define-macro (cond-expand . cond-action-list) |
658 |
(cond-expand-runtime cond-action-list)) |
659 |
|
660 |
(define (cond-expand-runtime cond-action-list) |
661 |
(if (null? cond-action-list) |
662 |
#t |
663 |
(if (cond-eval (caar cond-action-list)) |
664 |
`(begin ,@(cdar cond-action-list)) |
665 |
(cond-expand-runtime (cdr cond-action-list))))) |
666 |
|
667 |
(define (cond-eval-and cond-list) |
668 |
(foldr (lambda (x y) (and (cond-eval x) (cond-eval y))) #t cond-list)) |
669 |
|
670 |
(define (cond-eval-or cond-list) |
671 |
(foldr (lambda (x y) (or (cond-eval x) (cond-eval y))) #f cond-list)) |
672 |
|
673 |
(define (cond-eval condition) |
674 |
(cond |
675 |
((symbol? condition) |
676 |
(if (member condition *features*) #t #f)) |
677 |
((eq? condition #t) #t) |
678 |
((eq? condition #f) #f) |
679 |
(else (case (car condition) |
680 |
((and) (cond-eval-and (cdr condition))) |
681 |
((or) (cond-eval-or (cdr condition))) |
682 |
((not) (if (not (null? (cddr condition))) |
683 |
(error "cond-expand : 'not' takes 1 argument") |
684 |
(not (cond-eval (cadr condition))))) |
685 |
(else (error "cond-expand : unknown operator" (car condition))))))) |
686 |
|
687 |
(gc-verbose #t) |
688 |
|
689 |
; compatibility functions added by schmorp@schmorp.de |
690 |
(macro (defmacro dform) |
691 |
(let ((name (cadr dform)) (formals (caddr dform)) (body (cdddr dform))) |
692 |
`(define-macro (,name . ,formals) ,@body))) |
693 |
|
694 |
;; simple syntax-rules |
695 |
|
696 |
;; values/call-with-values |
697 |
(load "simple-syntax-rules/values.scm") |
698 |
;; (hash-)table |
699 |
;(load "simple-syntax-rules/table.scm") |
700 |
;; "the real stuff" |
701 |
;(load "simple-syntax-rules/usual.scm") |
702 |
;(load "simple-syntax-rules/rules.scm") |
703 |
;(load "simple-syntax-rules/memo.scm") |
704 |
;(load "simple-syntax-rules/syntax.scm") |
705 |
;(load "simple-syntax-rules/ev.scm") |
706 |
;(load "simple-syntax-rules/ex.scm") |
707 |
;(macro (define-syntax form) (expand form top-level-env)) |
708 |
|
709 |
;(load "macros/expand.scm") |
710 |
;(load "macros/misc.scm") |
711 |
;(load "macros/prefs.scm") |
712 |
;(load "macros/syntaxenv.scm") |
713 |
;(load "macros/syntaxrules.scm") |
714 |
;(load "macros/usual.scm") |
715 |
|
716 |
;; r7rs |
717 |
; char library |
718 |
; string-upcase |
719 |
; string-downcase |
720 |
; string-foldcase |
721 |
; sring-map, vector-map, string-for-each, vector-for-each |
722 |
; bytevectors |
723 |
|
724 |
;; srfi-1 |
725 |
|
726 |
(define (check-arg pred val caller) |
727 |
(let lp ((val val)) |
728 |
(if (pred val) val (lp (error "Bad argument" val pred caller))))) |
729 |
|
730 |
; Some macros and functions that the SRFI 1 reference implementation |
731 |
; requires that it does not define and are not part of R5RS. |
732 |
|
733 |
(define-macro let-optionals |
734 |
(lambda (input names . code) |
735 |
(let ((input-left (gensym))) |
736 |
`(let ((,input-left ,input)) |
737 |
,(let next ((names names)) |
738 |
(if (null? names) |
739 |
`(begin ,@code) |
740 |
`(let ((,input-left (if (null? ,input-left) |
741 |
'() |
742 |
(cdr ,input-left))) |
743 |
(,(caar names) (if (null? ,input-left) |
744 |
,(cadar names) |
745 |
(car ,input-left)))) |
746 |
,(next (cdr names))))))))) |
747 |
|
748 |
(define-macro receive |
749 |
(lambda (names values . code) |
750 |
`(call-with-values (lambda () ,values) |
751 |
(lambda ,names ,@code)))) |
752 |
|
753 |
|
754 |
(define (:optional data default) |
755 |
(if (null? data) |
756 |
default |
757 |
(car data))) |
758 |
|
759 |
(load "srfi-1.scm") |
760 |
|
761 |
;(load "srfi-55.scm") |
762 |
|
763 |
;(register-extension '(srfi 1) (lamba () (load "srfi-1.scm"))) |
764 |
;(register-extension '(srfi 23) (lamba () ())) ; error builtin |
765 |
;(register-extension '(srfi 55) (lamba () ())) ; always available |
766 |
|
767 |
;; end of init |
768 |
|
769 |
;(load "test.scm") |
770 |
|
771 |
(define $sc-put-cte #f) |
772 |
(define sc-expand #f) |
773 |
(define $make-environment #f) |
774 |
;(define environment? #f) |
775 |
;(define interaction-environment #f) |
776 |
(define identifier? #f) |
777 |
(define syntax->list #f) |
778 |
(define syntax->vector #f) |
779 |
(define syntax-object->datum #f) |
780 |
(define datum->syntax-object #f) |
781 |
(define generate-temporaries #f) |
782 |
(define free-identifier=? #f) |
783 |
(define bound-identifier=? #f) |
784 |
(define literal-identifier=? #f) |
785 |
(define syntax-error #f) |
786 |
(define $syntax-dispatch #f) |
787 |
|
788 |
(define void (lambda () (if #f #f))) |
789 |
|
790 |
(define andmap |
791 |
(lambda (f first . rest) |
792 |
(or (null? first) |
793 |
(if (null? rest) |
794 |
(let andmap ((first first)) |
795 |
(let ((x (car first)) (first (cdr first))) |
796 |
(if (null? first) |
797 |
(f x) |
798 |
(and (f x) (andmap first))))) |
799 |
(let andmap ((first first) (rest rest)) |
800 |
(let ((x (car first)) |
801 |
(xr (map car rest)) |
802 |
(first (cdr first)) |
803 |
(rest (map cdr rest))) |
804 |
(if (null? first) |
805 |
(apply f (cons x xr)) |
806 |
(and (apply f (cons x xr)) (andmap first rest))))))))) |
807 |
|
808 |
(define ormap |
809 |
(lambda (proc list1) |
810 |
(and (not (null? list1)) |
811 |
(or (proc (car list1)) (ormap proc (cdr list1)))))) |
812 |
|
813 |
(define *properties* '()) |
814 |
|
815 |
(define (putprop sym k v) |
816 |
(set! *properties* (acons (list sym k) v *properties*))) |
817 |
|
818 |
(define (remprop sym k) |
819 |
(putprop sym k #f)) |
820 |
|
821 |
(define (getprop sym k) |
822 |
(assoc (list sym k) *properties*)) |
823 |
|
824 |
(load "psyntax.pp") |
825 |
|
826 |
;(define *compile-hook* sc-expand) |
827 |
|
828 |
(load "test.scm") |
829 |
|