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