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