File: tiny.scm

package info (click to toggle)
scheme48 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,232 kB
  • sloc: lisp: 88,907; ansic: 87,519; sh: 3,224; makefile: 771
file content (205 lines) | stat: -rw-r--r-- 5,364 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
; Part of Scheme 48 1.9.  See file COPYING for notices and license.

; Authors: Richard Kelsey, Jonathan Rees, Mike Sperber

; Tiny image and simple I/O test system

(define (start arg in in-encoding out out-encoding error-out error-out-encoding resumer-records)
  (letrec ((fib (lambda (n)
		  (if (< n 2)
		      1
		      (+ (fib (- n 1))
			 (fib (- n 2) ))))))
    (fib 32)
    0)

  (write-string "Hello " out)

  (write-string (read-string in) out)
  (newline out)

  (if (vector? arg)
      (if (< 0 (vector-length arg))
          (write-byte-vector (vector-ref arg 0) out)))
  (newline out)
  (let ((c #f))
    (foo (lambda ()
           (if (call-with-current-continuation (lambda (cc)
                                                 (set! c cc)
                                                 10))
               'a
               'b)))
    (collect)
    (collect)
    (symbol-check in out)
    (collect)
    (if c 1 2)
    (write-image "test.image" start "Hello?" error-out)
    0))

(define (foo thunk)
  (+ (begin (thunk) 1)
     2))

(define (wrapper thunk out)
  (call-with-current-continuation
    (lambda (c)
      (write-string "[in]" out)
      (newline out)
      (call-with-values
       (let ((x thunk)) (set! thunk #f) x)
       (lambda results
	 (write-string "[out with " out)
	 (display-list results out)
	 (write-string "]" out)
	 (newline out)
	 (apply values results))))))

(define (display-list c out)
  (if (eq? c '())
      (write-string "()" out)
      (begin
	(write-string "(" out)
	(write-string (symbol->string (car c)) out)
	(let loop ((c (cdr c)))
	  (if (eq? c '())
	      (write-string ")" out)
	      (begin
		(write-string " " out)
		(write-string (symbol->string (car c)) out)
		(loop (cdr c))))))))

(define (call-with-current-continuation proc)
  (primitive-cwcc
    (lambda (cont)
      (proc (continuation->procedure cont)))))

(define (primitive-cwcc p)
  (primitive-catch (lambda (cont)
		     (p (loophole :escape cont))))) ;?

(define (continuation->procedure cont)
  (lambda results
    (with-continuation cont
      (lambda ()
	(apply values results)))))

(define *the-symbols* '())

(define (lookup s)
  (let loop ((syms *the-symbols*))
    (cond ((eq? syms '())
	   (set! *the-symbols*
		 (cons (lookup-shared-binding s #f)
		       *the-symbols*))
	   #f)
	  ((string=? s (shared-binding-name (car syms)))
	   (car syms))
	  (else
	   (loop (cdr syms))))))

(define (symbol-check in out)
  (let loop ()
    (write-string "> " out)
    (let ((s (read-string in)))
      (if (< 0 (string-length s))
	  (let ((sym (lookup s)))
	    (write-string (if sym
			      (if (eq? sym (lookup-shared-binding s #f))
				  "[old, eq]"
				  "[old, not eq]")
			      "[new]")
			  out)
	    (newline out)
	    (loop))))))

(define (write-string string . channel-option)  ; test n-ary procedures
  (write-byte-vector (string->byte-vector string)
		     (car channel-option)))

(define (write-byte-vector bytes channel)
  (channel-maybe-write channel
		       bytes
		       0
		       (byte-vector-length bytes)))

(define (string->byte-vector string)
  (let* ((size (string-length string))
	 (bytes (make-byte-vector size 0)))
    (do ((i 0 (+ 1 i)))
	((>= i size))
      (byte-vector-set! bytes i (char->scalar-value (string-ref string i))))
    bytes))

(define (string->low-string string)
  (let* ((size (string-length string))
	 (bytes (make-byte-vector (+ 1 size) 0)))
    (do ((i 0 (+ 1 i)))
	((>= i size))
      (byte-vector-set! bytes i (char->scalar-value (string-ref string i))))
    bytes))

(define (newline channel)
  (write-string "
" channel))

(define (read-string in)
  ((lambda (buffer)
     (letrec ((loop (lambda (have)
		      ((lambda (got)
			 (if (eq? got (eof-object))
			     "eof"
			     ((lambda (len)
				(if len
				    ((lambda (string)
				       (copy-bytes-to-string! buffer string len)
				       string)
				     (make-string len #\space))
				    (loop (+ have got))))
			      (has-newline buffer have got))))
		       (channel-maybe-read in buffer have (- 80 have) #f)))))
       (loop 0)))
   (make-byte-vector 80 (char->scalar-value #\space))))

(define (has-newline bytes start count)
  (letrec ((loop (lambda (i)
		   (if (= i count)
		       #f
		       (if (char=? #\newline
				   (scalar-value->char
				    (byte-vector-ref bytes (+ start i))))
			   (+ start i)
			   (loop (+ i 1)))))))
    (loop 0)))

(define (copy-bytes-to-string! from to count)
  (letrec ((loop (lambda (i)
		   (if (< i count)
		       (begin
			 (string-set! to i
				      (scalar-value->char (byte-vector-ref from i)))
			 (loop (+ i 1)))))))
    (loop 0)))

;----------------

(define (write-image filename start-procedure message error-out)
  (let ((undumpable (make-vector 1000 #f)))
    (write-image-low (string->low-string filename)
		     start-procedure (string->low-string message)
		     undumpable)
    (if (vector-ref undumpable 0)
	(begin
	  (write-string "undumpable records written in image:" error-out)
	  (display-list (vector-prefix->list undumpable) error-out)
	  (newline error-out)))))

; Return a list containing the non-#F values at the beginning of VECTOR.

(define (vector-prefix->list vector)
  (do ((i 0 (+ i 1))
       (losers '() (cons (vector-ref vector i) losers)))
      ((or (= i (vector-length vector))
	   (if (vector-ref vector i) #f #t))
       losers)))