Debian Codegraph @

scheme/ikarus.symbols.ss

raw symbols SCIP
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17 (library (ikarus.symbols)
18 (export gensym gensym? gensym->unique-string gensym-prefix
19 gensym-count print-gensym symbol->string
20 getprop putprop remprop property-list
21 top-level-value top-level-bound? set-top-level-value!
22 symbol-value symbol-bound? set-symbol-value!
23 $unintern-gensym
24 reset-symbol-proc! system-value system-value-gensym)
25 (import
26 (except (ikarus system $symbols) $unintern-gensym)
27 (ikarus system $pairs)
28 (ikarus system $fx)
29 (except (ikarus) gensym gensym? gensym->unique-string
30 gensym-prefix gensym-count print-gensym system-value
31 symbol->string getprop putprop remprop property-list
32 top-level-value top-level-bound? set-top-level-value!
33 symbol-value symbol-bound? set-symbol-value! reset-symbol-proc!))
34
35 (define gensym
36 (case-lambda
37 [() ($make-symbol #f)]
38 [(s)
39 (if (string? s)
40 ($make-symbol s)
41 (if (symbol? s)
42 ($make-symbol ($symbol-string s))
43 (die 'gensym "neither a string nor a symbol" s)))]))
44
45 (define gensym?
46 (lambda (x)
47 (and (symbol? x)
48 (let ([s ($symbol-unique-string x)])
49 (and s #t)))))
50
51 (define ($unintern-gensym x)
52 (if (symbol? x)
53 (begin (foreign-call "ikrt_unintern_gensym" x) (void))
54 (die 'unintern-gensym "not a symbol" x)))
55
56 (define top-level-value
57 (lambda (x)
58 (unless (symbol? x)
59 (die 'top-level-value "not a symbol" x))
60 (let ([v ($symbol-value x)])
61 (when ($unbound-object? v)
62 (raise
63 (condition
64 (make-undefined-violation)
65 (make-who-condition 'eval)
66 (make-message-condition "unbound variable")
67 (make-irritants-condition
68 (list (string->symbol (symbol->string x)))))))
69 v)))
70
71 (define top-level-bound?
72 (lambda (x)
73 (unless (symbol? x)
74 (die 'top-level-bound? "not a symbol" x))
75 (not ($unbound-object? ($symbol-value x)))))
76
77 (define set-top-level-value!
78 (lambda (x v)
79 (unless (symbol? x)
80 (die 'set-top-level-value! "not a symbol" x))
81 ($set-symbol-value! x v)))
82
83 (define symbol-value
84 (lambda (x)
85 (unless (symbol? x)
86 (die 'symbol-value "not a symbol" x))
87 (let ([v ($symbol-value x)])
88 (when ($unbound-object? v)
89 (die 'symbol-value "unbound" x))
90 v)))
91
92 (define symbol-bound?
93 (lambda (x)
94 (unless (symbol? x)
95 (die 'symbol-bound? "not a symbol" x))
96 (not ($unbound-object? ($symbol-value x)))))
97
98 (define set-symbol-value!
99 (lambda (x v)
100 (unless (symbol? x)
101 (die 'set-symbol-value! "not a symbol" x))
102 ($set-symbol-value! x v)
103 ($set-symbol-proc! x
104 (if (procedure? v) v
105 (lambda args
106 (die 'apply "not a procedure"
107 ($symbol-value x)))))))
108
109 (define reset-symbol-proc!
110 (lambda (x)
111 (let ([v ($symbol-value x)])
112 ($set-symbol-proc! x
113 (if (procedure? v)
114 v
115 (lambda args
116 (die 'apply "not a procedure"
117 (top-level-value x))))))))
118
119 #;
120 (define string->symbol
121 (lambda (x)
122 (unless (string? x)
123 (die 'string->symbol "not a string" x))
124 (foreign-call "ikrt_string_to_symbol" x)))
125
126 (define symbol->string
127 (lambda (x)
128 (unless (symbol? x)
129 (die 'symbol->string "not a symbol" x))
130 (let ([str ($symbol-string x)])
131 (or str
132 (let ([ct (gensym-count)])
133 ;;; FIXME: what if gensym-count is a bignum?
134 (let ([str (string-append (gensym-prefix) (fixnum->string ct))])
135 ($set-symbol-string! x str)
136 (gensym-count ($fxadd1 ct))
137 str))))))
138
139 (define putprop
140 (lambda (x k v)
141 (unless (symbol? x) (die 'putprop "not a symbol" x))
142 (unless (symbol? k) (die 'putprop "not a symbol" k))
143 (let ([p ($symbol-plist x)])
144 (cond
145 [(assq k p) => (lambda (x) (set-cdr! x v))]
146 [else
147 ($set-symbol-plist! x (cons (cons k v) p))]))))
148
149 (define getprop
150 (lambda (x k)
151 (unless (symbol? x) (die 'getprop "not a symbol" x))
152 (unless (symbol? k) (die 'getprop "not a symbol" k))
153 (let ([p ($symbol-plist x)])
154 (cond
155 [(assq k p) => cdr]
156 [else #f]))))
157
158 (define remprop
159 (lambda (x k)
160 (unless (symbol? x) (die 'remprop "not a symbol" x))
161 (unless (symbol? k) (die 'remprop "not a symbol" k))
162 (let ([p ($symbol-plist x)])
163 (unless (null? p)
164 (let ([a ($car p)])
165 (cond
166 [(eq? ($car a) k) ($set-symbol-plist! x ($cdr p))]
167 [else
168 (let f ([q p] [p ($cdr p)])
169 (unless (null? p)
170 (let ([a ($car p)])
171 (cond
172 [(eq? ($car a) k)
173 ($set-cdr! q ($cdr p))]
174 [else
175 (f p ($cdr p))]))))]))))))
176
177 (define property-list
178 (lambda (x)
179 (unless (symbol? x)
180 (die 'property-list "not a symbol" x))
181 (letrec ([f
182 (lambda (ls ac)
183 (cond
184 [(null? ls) ac]
185 [else
186 (let ([a ($car ls)])
187 (f ($cdr ls)
188 (cons ($car a) (cons ($cdr a) ac))))]))])
189 (f ($symbol-plist x) '()))))
190
191 (define gensym->unique-string
192 (lambda (x)
193 (unless (symbol? x)
194 (die 'gensym->unique-string "not a gensym" x))
195 (let ([us ($symbol-unique-string x)])
196 (cond
197 [(string? us) us]
198 [(not us)
199 (die 'gensym->unique-string "not a gensym" x)]
200 [else
201 (let f ([x x])
202 (let ([id (uuid)])
203 ($set-symbol-unique-string! x id)
204 (cond
205 [(foreign-call "ikrt_intern_gensym" x) id]
206 [else (f x)])))]))))
207
208 (define gensym-prefix
209 (make-parameter
210 "g"
211 (lambda (x)
212 (unless (string? x)
213 (die 'gensym-prefix "not a string" x))
214 x)))
215
216 (define gensym-count
217 (make-parameter
218 0
219 (lambda (x)
220 (unless (and (fixnum? x) ($fx>= x 0))
221 (die 'gensym-count "not a valid count" x))
222 x)))
223
224 (define print-gensym
225 (make-parameter
226 #t
227 (lambda (x)
228 (unless (or (boolean? x) (eq? x 'pretty))
229 (die 'print-gensym "not in #t|#f|pretty" x))
230 x)))
231
232 (define system-value-gensym (gensym))
233
234 (define (system-value x)
235 (unless (symbol? x)
236 (die 'system-value "not a symbol" x))
237 (cond
238 [(getprop x system-value-gensym) =>
239 (lambda (g)
240 (let ([v ($symbol-value g)])
241 (when ($unbound-object? v)
242 (die 'system-value "not a system symbol" x))
243 v))]
244 [else (die 'system-value "not a system symbol" x)]))
245
246
247
248 )
249