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