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 (inexact? x) (and (real? x) (not (integer? x)))) |
35 |
(define (even? n) (= (remainder n 2) 0)) |
36 |
(define (odd? n) (not (= (remainder n 2) 0))) |
37 |
(define (zero? n) (= n 0)) |
38 |
(define (positive? n) (> n 0)) |
39 |
(define (negative? n) (< n 0)) |
40 |
(define complex? number?) |
41 |
(define rational? real?) |
42 |
(define (abs n) (if (>= n 0) n (- n))) |
43 |
(define (exact->inexact n) (* n 1.0)) |
44 |
(define (<> n1 n2) (not (= n1 n2))) |
45 |
|
46 |
; min and max must return inexact if any arg is inexact; use (+ n 0.0) |
47 |
(define (max . lst) |
48 |
(foldr (lambda (a b) |
49 |
(if (> a b) |
50 |
(if (exact? b) a (+ a 0.0)) |
51 |
(if (exact? a) b (+ b 0.0)))) |
52 |
(car lst) (cdr lst))) |
53 |
(define (min . lst) |
54 |
(foldr (lambda (a b) |
55 |
(if (< a b) |
56 |
(if (exact? b) a (+ a 0.0)) |
57 |
(if (exact? a) b (+ b 0.0)))) |
58 |
(car lst) (cdr lst))) |
59 |
|
60 |
(define (succ x) (+ x 1)) |
61 |
(define (pred x) (- x 1)) |
62 |
(define gcd |
63 |
(lambda a |
64 |
(if (null? a) |
65 |
0 |
66 |
(let ((aa (abs (car a))) |
67 |
(bb (abs (cadr a)))) |
68 |
(if (= bb 0) |
69 |
aa |
70 |
(gcd bb (remainder aa bb))))))) |
71 |
(define lcm |
72 |
(lambda a |
73 |
(if (null? a) |
74 |
1 |
75 |
(let ((aa (abs (car a))) |
76 |
(bb (abs (cadr a)))) |
77 |
(if (or (= aa 0) (= bb 0)) |
78 |
0 |
79 |
(abs (* (quotient aa (gcd aa bb)) bb))))))) |
80 |
|
81 |
|
82 |
(define (string . charlist) |
83 |
(list->string charlist)) |
84 |
|
85 |
(define (list->string charlist) |
86 |
(let* ((len (length charlist)) |
87 |
(newstr (make-string len)) |
88 |
(fill-string! |
89 |
(lambda (str i len charlist) |
90 |
(if (= i len) |
91 |
str |
92 |
(begin (string-set! str i (car charlist)) |
93 |
(fill-string! str (+ i 1) len (cdr charlist))))))) |
94 |
(fill-string! newstr 0 len charlist))) |
95 |
|
96 |
(define (string-fill! s e) |
97 |
(let ((n (string-length s))) |
98 |
(let loop ((i 0)) |
99 |
(if (= i n) |
100 |
s |
101 |
(begin (string-set! s i e) (loop (succ i))))))) |
102 |
|
103 |
(define (string->list s) |
104 |
(let loop ((n (pred (string-length s))) (l '())) |
105 |
(if (= n -1) |
106 |
l |
107 |
(loop (pred n) (cons (string-ref s n) l))))) |
108 |
|
109 |
(define (string-copy str) |
110 |
(string-append str)) |
111 |
|
112 |
(define (string->anyatom str pred) |
113 |
(let* ((a (string->atom str))) |
114 |
(if (pred a) a |
115 |
(error "string->xxx: not a xxx" a)))) |
116 |
|
117 |
(define (string->number str . base) |
118 |
(let ((n (string->atom str (if (null? base) 10 (car base))))) |
119 |
(if (number? n) n #f))) |
120 |
|
121 |
(define (anyatom->string n pred) |
122 |
(if (pred n) |
123 |
(atom->string n) |
124 |
(error "xxx->string: not a xxx" n))) |
125 |
|
126 |
(define (number->string n . base) |
127 |
(atom->string n (if (null? base) 10 (car base)))) |
128 |
|
129 |
|
130 |
(define (char-cmp? cmp a b) |
131 |
(cmp (char->integer a) (char->integer b))) |
132 |
(define (char-ci-cmp? cmp a b) |
133 |
(cmp (char->integer (char-downcase a)) (char->integer (char-downcase b)))) |
134 |
|
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 |
(define (char<=? a b) (char-cmp? <= a b)) |
139 |
(define (char>=? a b) (char-cmp? >= a b)) |
140 |
|
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 |
(define (char-ci<=? a b) (char-ci-cmp? <= a b)) |
145 |
(define (char-ci>=? a b) (char-ci-cmp? >= a b)) |
146 |
|
147 |
; Note the trick of returning (cmp x y) |
148 |
(define (string-cmp? chcmp cmp a b) |
149 |
(let ((na (string-length a)) (nb (string-length b))) |
150 |
(let loop ((i 0)) |
151 |
(cond |
152 |
((= i na) |
153 |
(if (= i nb) (cmp 0 0) (cmp 0 1))) |
154 |
((= i nb) |
155 |
(cmp 1 0)) |
156 |
((chcmp = (string-ref a i) (string-ref b i)) |
157 |
(loop (succ i))) |
158 |
(else |
159 |
(chcmp cmp (string-ref a i) (string-ref b i))))))) |
160 |
|
161 |
|
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 |
(define (string<=? a b) (string-cmp? char-cmp? <= a b)) |
166 |
(define (string>=? a b) (string-cmp? char-cmp? >= a b)) |
167 |
|
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 |
(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 |
|
174 |
(define (list . x) x) |
175 |
|
176 |
(define (foldr f x lst) |
177 |
(if (null? lst) |
178 |
x |
179 |
(foldr f (f x (car lst)) (cdr lst)))) |
180 |
|
181 |
(define (unzip1-with-cdr . lists) |
182 |
(unzip1-with-cdr-iterative lists '() '())) |
183 |
|
184 |
(define (unzip1-with-cdr-iterative lists cars cdrs) |
185 |
(if (null? lists) |
186 |
(cons cars cdrs) |
187 |
(let ((car1 (caar lists)) |
188 |
(cdr1 (cdar lists))) |
189 |
(unzip1-with-cdr-iterative |
190 |
(cdr lists) |
191 |
(append cars (list car1)) |
192 |
(append cdrs (list cdr1)))))) |
193 |
|
194 |
(define (map proc . lists) |
195 |
(if (null? lists) |
196 |
(apply proc) |
197 |
(if (null? (car lists)) |
198 |
'() |
199 |
(let* ((unz (apply unzip1-with-cdr lists)) |
200 |
(cars (car unz)) |
201 |
(cdrs (cdr unz))) |
202 |
(cons (apply proc cars) (apply map (cons proc cdrs))))))) |
203 |
|
204 |
(define (for-each proc . lists) |
205 |
(if (null? lists) |
206 |
(apply proc) |
207 |
(if (null? (car lists)) |
208 |
#t |
209 |
(let* ((unz (apply unzip1-with-cdr lists)) |
210 |
(cars (car unz)) |
211 |
(cdrs (cdr unz))) |
212 |
(apply proc cars) (apply map (cons proc cdrs)))))) |
213 |
|
214 |
(define (list-tail x k) |
215 |
(if (zero? k) |
216 |
x |
217 |
(list-tail (cdr x) (- k 1)))) |
218 |
|
219 |
(define (list-ref x k) |
220 |
(car (list-tail x k))) |
221 |
|
222 |
(define (last-pair x) |
223 |
(if (pair? (cdr x)) |
224 |
(last-pair (cdr x)) |
225 |
x)) |
226 |
|
227 |
(define (head stream) (car stream)) |
228 |
|
229 |
(define (tail stream) (force (cdr stream))) |
230 |
|
231 |
(define (vector-equal? x y) |
232 |
(and (vector? x) (vector? y) (= (vector-length x) (vector-length y)) |
233 |
(let ((n (vector-length x))) |
234 |
(let loop ((i 0)) |
235 |
(if (= i n) |
236 |
#t |
237 |
(and (equal? (vector-ref x i) (vector-ref y i)) |
238 |
(loop (succ i)))))))) |
239 |
|
240 |
(define (list->vector x) |
241 |
(apply vector x)) |
242 |
|
243 |
(define (vector-fill! v e) |
244 |
(let ((n (vector-length v))) |
245 |
(let loop ((i 0)) |
246 |
(if (= i n) |
247 |
v |
248 |
(begin (vector-set! v i e) (loop (succ i))))))) |
249 |
|
250 |
(define (vector->list v) |
251 |
(let loop ((n (pred (vector-length v))) (l '())) |
252 |
(if (= n -1) |
253 |
l |
254 |
(loop (pred n) (cons (vector-ref v n) l))))) |
255 |
|
256 |
;; The following quasiquote macro is due to Eric S. Tiedemann. |
257 |
;; Copyright 1988 by Eric S. Tiedemann; all rights reserved. |
258 |
;; |
259 |
;; Subsequently modified to handle vectors: D. Souflis |
260 |
|
261 |
(macro |
262 |
quasiquote |
263 |
(lambda (l) |
264 |
(define (mcons f l r) |
265 |
(if (and (pair? r) |
266 |
(eq? (car r) 'quote) |
267 |
(eq? (car (cdr r)) (cdr f)) |
268 |
(pair? l) |
269 |
(eq? (car l) 'quote) |
270 |
(eq? (car (cdr l)) (car f))) |
271 |
(if (or (procedure? f) (number? f) (string? f)) |
272 |
f |
273 |
(list 'quote f)) |
274 |
(if (eqv? l vector) |
275 |
(apply l (eval r)) |
276 |
(list 'cons l r) |
277 |
))) |
278 |
(define (mappend f l r) |
279 |
(if (or (null? (cdr f)) |
280 |
(and (pair? r) |
281 |
(eq? (car r) 'quote) |
282 |
(eq? (car (cdr r)) '()))) |
283 |
l |
284 |
(list 'append l r))) |
285 |
(define (foo level form) |
286 |
(cond ((not (pair? form)) |
287 |
(if (or (procedure? form) (number? form) (string? form)) |
288 |
form |
289 |
(list 'quote form)) |
290 |
) |
291 |
((eq? 'quasiquote (car form)) |
292 |
(mcons form ''quasiquote (foo (+ level 1) (cdr form)))) |
293 |
(#t (if (zero? level) |
294 |
(cond ((eq? (car form) 'unquote) (car (cdr form))) |
295 |
((eq? (car form) 'unquote-splicing) |
296 |
(error "Unquote-splicing wasn't in a list:" |
297 |
form)) |
298 |
((and (pair? (car form)) |
299 |
(eq? (car (car form)) 'unquote-splicing)) |
300 |
(mappend form (car (cdr (car form))) |
301 |
(foo level (cdr form)))) |
302 |
(#t (mcons form (foo level (car form)) |
303 |
(foo level (cdr form))))) |
304 |
(cond ((eq? (car form) 'unquote) |
305 |
(mcons form ''unquote (foo (- level 1) |
306 |
(cdr form)))) |
307 |
((eq? (car form) 'unquote-splicing) |
308 |
(mcons form ''unquote-splicing |
309 |
(foo (- level 1) (cdr form)))) |
310 |
(#t (mcons form (foo level (car form)) |
311 |
(foo level (cdr form))))))))) |
312 |
(foo 0 (car (cdr l))))) |
313 |
|
314 |
;;;;;Helper for the dynamic-wind definition. By Tom Breton (Tehom) |
315 |
(define (shared-tail x y) |
316 |
(let ((len-x (length x)) |
317 |
(len-y (length y))) |
318 |
(define (shared-tail-helper x y) |
319 |
(if |
320 |
(eq? x y) |
321 |
x |
322 |
(shared-tail-helper (cdr x) (cdr y)))) |
323 |
|
324 |
(cond |
325 |
((> len-x len-y) |
326 |
(shared-tail-helper |
327 |
(list-tail x (- len-x len-y)) |
328 |
y)) |
329 |
((< len-x len-y) |
330 |
(shared-tail-helper |
331 |
x |
332 |
(list-tail y (- len-y len-x)))) |
333 |
(#t (shared-tail-helper x y))))) |
334 |
|
335 |
;;;;;Dynamic-wind by Tom Breton (Tehom) |
336 |
|
337 |
;;Guarded because we must only eval this once, because doing so |
338 |
;;redefines call/cc in terms of old call/cc |
339 |
(unless (defined? 'dynamic-wind) |
340 |
(let |
341 |
;;These functions are defined in the context of a private list of |
342 |
;;pairs of before/after procs. |
343 |
( (*active-windings* '()) |
344 |
;;We'll define some functions into the larger environment, so |
345 |
;;we need to know it. |
346 |
(outer-env (current-environment))) |
347 |
|
348 |
;;Poor-man's structure operations |
349 |
(define before-func car) |
350 |
(define after-func cdr) |
351 |
(define make-winding cons) |
352 |
|
353 |
;;Manage active windings |
354 |
(define (activate-winding! new) |
355 |
((before-func new)) |
356 |
(set! *active-windings* (cons new *active-windings*))) |
357 |
(define (deactivate-top-winding!) |
358 |
(let ((old-top (car *active-windings*))) |
359 |
;;Remove it from the list first so it's not active during its |
360 |
;;own exit. |
361 |
(set! *active-windings* (cdr *active-windings*)) |
362 |
((after-func old-top)))) |
363 |
|
364 |
(define (set-active-windings! new-ws) |
365 |
(unless (eq? new-ws *active-windings*) |
366 |
(let ((shared (shared-tail new-ws *active-windings*))) |
367 |
|
368 |
;;Define the looping functions. |
369 |
;;Exit the old list. Do deeper ones last. Don't do |
370 |
;;any shared ones. |
371 |
(define (pop-many) |
372 |
(unless (eq? *active-windings* shared) |
373 |
(deactivate-top-winding!) |
374 |
(pop-many))) |
375 |
;;Enter the new list. Do deeper ones first so that the |
376 |
;;deeper windings will already be active. Don't do any |
377 |
;;shared ones. |
378 |
(define (push-many new-ws) |
379 |
(unless (eq? new-ws shared) |
380 |
(push-many (cdr new-ws)) |
381 |
(activate-winding! (car new-ws)))) |
382 |
|
383 |
;;Do it. |
384 |
(pop-many) |
385 |
(push-many new-ws)))) |
386 |
|
387 |
;;The definitions themselves. |
388 |
(eval |
389 |
`(define call-with-current-continuation |
390 |
;;It internally uses the built-in call/cc, so capture it. |
391 |
,(let ((old-c/cc call-with-current-continuation)) |
392 |
(lambda (func) |
393 |
;;Use old call/cc to get the continuation. |
394 |
(old-c/cc |
395 |
(lambda (continuation) |
396 |
;;Call func with not the continuation itself |
397 |
;;but a procedure that adjusts the active |
398 |
;;windings to what they were when we made |
399 |
;;this, and only then calls the |
400 |
;;continuation. |
401 |
(func |
402 |
(let ((current-ws *active-windings*)) |
403 |
(lambda (x) |
404 |
(set-active-windings! current-ws) |
405 |
(continuation x))))))))) |
406 |
outer-env) |
407 |
;;We can't just say "define (dynamic-wind before thunk after)" |
408 |
;;because the lambda it's defined to lives in this environment, |
409 |
;;not in the global environment. |
410 |
(eval |
411 |
`(define dynamic-wind |
412 |
,(lambda (before thunk after) |
413 |
;;Make a new winding |
414 |
(activate-winding! (make-winding before after)) |
415 |
(let ((result (thunk))) |
416 |
;;Get rid of the new winding. |
417 |
(deactivate-top-winding!) |
418 |
;;The return value is that of thunk. |
419 |
result))) |
420 |
outer-env))) |
421 |
|
422 |
(define call/cc call-with-current-continuation) |
423 |
|
424 |
|
425 |
;;;;; atom? and equal? written by a.k |
426 |
|
427 |
;;;; atom? |
428 |
(define (atom? x) |
429 |
(not (pair? x))) |
430 |
|
431 |
;;;; equal? |
432 |
(define (equal? x y) |
433 |
(cond |
434 |
((pair? x) |
435 |
(and (pair? y) |
436 |
(equal? (car x) (car y)) |
437 |
(equal? (cdr x) (cdr y)))) |
438 |
((vector? x) |
439 |
(and (vector? y) (vector-equal? x y))) |
440 |
((string? x) |
441 |
(and (string? y) (string=? x y))) |
442 |
(else (eqv? x y)))) |
443 |
|
444 |
;;;; (do ((var init inc) ...) (endtest result ...) body ...) |
445 |
;; |
446 |
(macro do |
447 |
(lambda (do-macro) |
448 |
(apply (lambda (do vars endtest . body) |
449 |
(let ((do-loop (gensym))) |
450 |
`(letrec ((,do-loop |
451 |
(lambda ,(map (lambda (x) |
452 |
(if (pair? x) (car x) x)) |
453 |
`,vars) |
454 |
(if ,(car endtest) |
455 |
(begin ,@(cdr endtest)) |
456 |
(begin |
457 |
,@body |
458 |
(,do-loop |
459 |
,@(map (lambda (x) |
460 |
(cond |
461 |
((not (pair? x)) x) |
462 |
((< (length x) 3) (car x)) |
463 |
(else (car (cdr (cdr x)))))) |
464 |
`,vars))))))) |
465 |
(,do-loop |
466 |
,@(map (lambda (x) |
467 |
(if (and (pair? x) (cdr x)) |
468 |
(car (cdr x)) |
469 |
'())) |
470 |
`,vars))))) |
471 |
do-macro))) |
472 |
|
473 |
;;;; generic-member |
474 |
(define (generic-member cmp obj lst) |
475 |
(cond |
476 |
((null? lst) #f) |
477 |
((cmp obj (car lst)) lst) |
478 |
(else (generic-member cmp obj (cdr lst))))) |
479 |
|
480 |
(define (memq obj lst) |
481 |
(generic-member eq? obj lst)) |
482 |
(define (memv obj lst) |
483 |
(generic-member eqv? obj lst)) |
484 |
(define (member obj lst) |
485 |
(generic-member equal? obj lst)) |
486 |
|
487 |
;;;; generic-assoc |
488 |
(define (generic-assoc cmp obj alst) |
489 |
(cond |
490 |
((null? alst) #f) |
491 |
((cmp obj (caar alst)) (car alst)) |
492 |
(else (generic-assoc cmp obj (cdr alst))))) |
493 |
|
494 |
(define (assq obj alst) |
495 |
(generic-assoc eq? obj alst)) |
496 |
(define (assv obj alst) |
497 |
(generic-assoc eqv? obj alst)) |
498 |
(define (assoc obj alst) |
499 |
(generic-assoc equal? obj alst)) |
500 |
|
501 |
(define (acons x y z) (cons (cons x y) z)) |
502 |
|
503 |
;;;; Handy for imperative programs |
504 |
;;;; Used as: (define-with-return (foo x y) .... (return z) ...) |
505 |
(macro (define-with-return form) |
506 |
`(define ,(cadr form) |
507 |
(call/cc (lambda (return) ,@(cddr form))))) |
508 |
|
509 |
;;;; Simple exception handling |
510 |
; |
511 |
; Exceptions are caught as follows: |
512 |
; |
513 |
; (catch (do-something to-recover and-return meaningful-value) |
514 |
; (if-something goes-wrong) |
515 |
; (with-these calls)) |
516 |
; |
517 |
; "Catch" establishes a scope spanning multiple call-frames |
518 |
; until another "catch" is encountered. |
519 |
; |
520 |
; Exceptions are thrown with: |
521 |
; |
522 |
; (throw "message") |
523 |
; |
524 |
; If used outside a (catch ...), reverts to (error "message) |
525 |
|
526 |
(define *handlers* (list)) |
527 |
|
528 |
(define (push-handler proc) |
529 |
(set! *handlers* (cons proc *handlers*))) |
530 |
|
531 |
(define (pop-handler) |
532 |
(let ((h (car *handlers*))) |
533 |
(set! *handlers* (cdr *handlers*)) |
534 |
h)) |
535 |
|
536 |
(define (more-handlers?) |
537 |
(pair? *handlers*)) |
538 |
|
539 |
(define (throw . x) |
540 |
(if (more-handlers?) |
541 |
(apply (pop-handler)) |
542 |
(apply error x))) |
543 |
|
544 |
(macro (catch form) |
545 |
(let ((label (gensym))) |
546 |
`(call/cc (lambda (exit) |
547 |
(push-handler (lambda () (exit ,(cadr form)))) |
548 |
(let ((,label (begin ,@(cddr form)))) |
549 |
(pop-handler) |
550 |
,label))))) |
551 |
|
552 |
(define *error-hook* throw) |
553 |
|
554 |
|
555 |
;;;;; Definition of MAKE-ENVIRONMENT, to be used with two-argument EVAL |
556 |
|
557 |
(macro (make-environment form) |
558 |
`(apply (lambda () |
559 |
,@(cdr form) |
560 |
(current-environment)))) |
561 |
|
562 |
(define-macro (eval-polymorphic x . envl) |
563 |
(display envl) |
564 |
(let* ((env (if (null? envl) (current-environment) (eval (car envl)))) |
565 |
(xval (eval x env))) |
566 |
(if (closure? xval) |
567 |
(make-closure (get-closure-code xval) env) |
568 |
xval))) |
569 |
|
570 |
; Redefine this if you install another package infrastructure |
571 |
; Also redefine 'package' |
572 |
(define *colon-hook* eval) |
573 |
|
574 |
;;;;; I/O |
575 |
|
576 |
(define (input-output-port? p) |
577 |
(and (input-port? p) (output-port? p))) |
578 |
|
579 |
(define (close-port p) |
580 |
(cond |
581 |
((input-output-port? p) (close-input-port (close-output-port p))) |
582 |
((input-port? p) (close-input-port p)) |
583 |
((output-port? p) (close-output-port p)) |
584 |
(else (throw "Not a port" p)))) |
585 |
|
586 |
(define (call-with-input-file s p) |
587 |
(let ((inport (open-input-file s))) |
588 |
(if (eq? inport #f) |
589 |
#f |
590 |
(let ((res (p inport))) |
591 |
(close-input-port inport) |
592 |
res)))) |
593 |
|
594 |
(define (call-with-output-file s p) |
595 |
(let ((outport (open-output-file s))) |
596 |
(if (eq? outport #f) |
597 |
#f |
598 |
(let ((res (p outport))) |
599 |
(close-output-port outport) |
600 |
res)))) |
601 |
|
602 |
(define (with-input-from-file s p) |
603 |
(let ((inport (open-input-file s))) |
604 |
(if (eq? inport #f) |
605 |
#f |
606 |
(let ((prev-inport (current-input-port))) |
607 |
(set-input-port inport) |
608 |
(let ((res (p))) |
609 |
(close-input-port inport) |
610 |
(set-input-port prev-inport) |
611 |
res))))) |
612 |
|
613 |
(define (with-output-to-file s p) |
614 |
(let ((outport (open-output-file s))) |
615 |
(if (eq? outport #f) |
616 |
#f |
617 |
(let ((prev-outport (current-output-port))) |
618 |
(set-output-port outport) |
619 |
(let ((res (p))) |
620 |
(close-output-port outport) |
621 |
(set-output-port prev-outport) |
622 |
res))))) |
623 |
|
624 |
(define (with-input-output-from-to-files si so p) |
625 |
(let ((inport (open-input-file si)) |
626 |
(outport (open-input-file so))) |
627 |
(if (not (and inport outport)) |
628 |
(begin |
629 |
(close-input-port inport) |
630 |
(close-output-port outport) |
631 |
#f) |
632 |
(let ((prev-inport (current-input-port)) |
633 |
(prev-outport (current-output-port))) |
634 |
(set-input-port inport) |
635 |
(set-output-port outport) |
636 |
(let ((res (p))) |
637 |
(close-input-port inport) |
638 |
(close-output-port outport) |
639 |
(set-input-port prev-inport) |
640 |
(set-output-port prev-outport) |
641 |
res))))) |
642 |
|
643 |
; Random number generator (maximum cycle) |
644 |
(define *seed* 1) |
645 |
(define (random-next) |
646 |
(let* ((a 16807) (m 2147483647) (q (quotient m a)) (r (modulo m a))) |
647 |
(set! *seed* |
648 |
(- (* a (- *seed* |
649 |
(* (quotient *seed* q) q))) |
650 |
(* (quotient *seed* q) r))) |
651 |
(if (< *seed* 0) (set! *seed* (+ *seed* m))) |
652 |
*seed*)) |
653 |
|
654 |
;; SRFI-0 |
655 |
;; COND-EXPAND |
656 |
;; Implemented as a macro |
657 |
(define *features* '(srfi-0)) |
658 |
|
659 |
(define-macro (cond-expand . cond-action-list) |
660 |
(cond-expand-runtime cond-action-list)) |
661 |
|
662 |
(define (cond-expand-runtime cond-action-list) |
663 |
(if (null? cond-action-list) |
664 |
#t |
665 |
(if (cond-eval (caar cond-action-list)) |
666 |
`(begin ,@(cdar cond-action-list)) |
667 |
(cond-expand-runtime (cdr cond-action-list))))) |
668 |
|
669 |
(define (cond-eval-and cond-list) |
670 |
(foldr (lambda (x y) (and (cond-eval x) (cond-eval y))) #t cond-list)) |
671 |
|
672 |
(define (cond-eval-or cond-list) |
673 |
(foldr (lambda (x y) (or (cond-eval x) (cond-eval y))) #f cond-list)) |
674 |
|
675 |
(define (cond-eval condition) |
676 |
(cond |
677 |
((symbol? condition) |
678 |
(if (member condition *features*) #t #f)) |
679 |
((eq? condition #t) #t) |
680 |
((eq? condition #f) #f) |
681 |
(else (case (car condition) |
682 |
((and) (cond-eval-and (cdr condition))) |
683 |
((or) (cond-eval-or (cdr condition))) |
684 |
((not) (if (not (null? (cddr condition))) |
685 |
(error "cond-expand : 'not' takes 1 argument") |
686 |
(not (cond-eval (cadr condition))))) |
687 |
(else (error "cond-expand : unknown operator" (car condition))))))) |
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 |
|
830 |
(gc-verbose #f) |