ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/microscheme/init.scm
Revision: 1.22
Committed: Tue Dec 1 04:57:49 2015 UTC (8 years, 6 months ago) by root
Branch: MAIN
Changes since 1.21: +47 -6 lines
Log Message:
*** empty log message ***

File Contents

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