-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcj-symbol.scm
251 lines (213 loc) · 6.08 KB
/
cj-symbol.scm
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
;;; Copyright 2010-2018 by Christian Jaeger <[email protected]>
;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License (GPL) as published
;;; by the Free Software Foundation, either version 2 of the License, or
;;; (at your option) any later version.
(require define-macro-star
cj-symbol-with
test
cj-env
(fixnum inc)
(cj-env on symbol-value-or))
(export symbol<?
gensym
interned-symbol?
unsafe-string->uninterned-symbol
string->uninterned-symbol
gensym?
syntax-equal?
syntax-equal?*
(macro with-gensyms)
(macro with-gensym)
(macro symbol-case)
#!optional
cj-gensym?
cj-gensym-maybe-name
gensym-count
make-syntax-equal?)
;;(include "cj-standarddeclares.scm")
;; don't have cj-struct yet, so can't use ^
(declare (block)
(standard-bindings)
(extended-bindings))
;; COPY to avoid circular dependency on: (cj-functional false/0)
(define (false/0)
#f)
;; /COPY
(define symbol<?
(on symbol->string string<?))
;; a gensym that returns interned symbols
(define gensym-prefix "GEN:")
(define gensym-count (make-parameter 0))
;; parameter not for thread safety, but for easy temporary override
;; XXX TODO: these are not actually safe when considering the
;; possibility of loading already-compiled object files. How to solve?
;; Either forced namespaces, or maintaining counter value as part of
;; the build process.
(define (gensym-count-next)
(let ((v (gensym-count)))
(gensym-count (inc v))
v))
(define (gensym #!optional maybe-name)
(let* ((name (or maybe-name ""))
(cnt (number->string (gensym-count-next))))
(symbol-append gensym-prefix name "-" cnt)))
(define (interned-symbol? v)
(and (symbol? v)
(not (uninterned-symbol? v))))
(define unsafe-string->uninterned-symbol
(symbol-value-or
'##make-uninterned-symbol
(lambda ()
(symbol-value-or
'##string->uninterned-symbol
(lambda args (error "could not find uninterned symbol constructor"))))))
(define (string->uninterned-symbol str)
(if (string? str)
(unsafe-string->uninterned-symbol str)
(error "string->uninterned-symbol: need string, got:" str)))
(TEST
> (%try-error (string->uninterned-symbol 'inc))
#(error "string->uninterned-symbol: need string, got:" inc)
;; > (string->uninterned-symbol "inc")
;; #:inc -- ah fails since equal? returns #f
> (symbol? (string->uninterned-symbol "inc"))
#t
> (interned-symbol? (string->uninterned-symbol "inc"))
#f
> (symbol->string (string->uninterned-symbol "inc"))
"inc")
(define (cj-gensym? v)
(and (interned-symbol? v)
(let* ((s (symbol->string v))
(len (string-length s))
(plen (string-length gensym-prefix)))
(and (> len plen)
;; ^ do *not* require 1 more character! as writing GEN:a
;; seems quite natural and is decided to be accepted.
(string=? (substring s 0 plen)
gensym-prefix)))))
(TEST
> (cj-gensym? '|GEN:|)
#f
> (cj-gensym? '|GEN:-|)
#t
> (cj-gensym? '|GEN:-1|)
#t)
(define (cj-gensym-maybe-name v)
(if (cj-gensym? v)
;; what string libraries do we have available at this
;; point? assume that none?
(let* ((str (symbol->string v))
(len (string-length str))
(startpos (string-length gensym-prefix))
(endpos (let lp ((i startpos))
(if (eq? (string-ref str i) #\-)
i
(lp (inc i))))))
(if (= startpos endpos)
#f
(substring str startpos endpos)))
(error "not a cj-gensym:" v)))
(TEST
> (cj-gensym-maybe-name 'GEN:abc-3349)
"abc"
> (cj-gensym-maybe-name 'GEN:-3349)
#f)
(define (gensym? v)
(or (uninterned-symbol? v)
(cj-gensym? v)))
(define (make-syntax-equal? TYPE?)
(lambda (a b)
(let ((equivalent?
;; allow alpha conversion between a and b
(let ((a->b (make-table test: eq?))
(b->a (make-table test: eq?)))
(lambda (a b)
(let ((check
(lambda (a->b a b)
(cond ((table-ref a->b a #f)
=> (lambda (expected)
(eq? b expected)))
(else
;; fresh name, accept
(table-set! a->b a b)
#t)))))
(and (check a->b a b)
(check b->a b a)))))))
(let rec ((a a)
(b b))
;; check for TYPE *first*, because in that case we have to
;; *forbid* comparison with equal?/eq?.
(cond ((and (TYPE? a) (TYPE? b))
(equivalent? a b))
((and (pair? a) (pair? b))
(and (rec (car a)
(car b))
(rec (cdr a)
(cdr b))))
((and (vector? a) (vector? b))
(rec (vector->list a)
(vector->list b)))
((and (box? a) (box? b))
(rec (unbox a)
(unbox b)))
(else
(equal? a b)))))))
(define syntax-equal? (make-syntax-equal? gensym?))
(define syntax-equal?* (make-syntax-equal? symbol?))
(TEST
> (syntax-equal? 'a 'b)
#f
> (syntax-equal?* 'a 'b)
#t
> (syntax-equal? 'a 'a)
#t
> (syntax-equal?* 'a 'a)
#t
> (syntax-equal? '(a b c) '(a b "c"))
#f
> (syntax-equal?* '(a b c) '(a b "c"))
#f
> (syntax-equal? '(a b "c") '(a b "c"))
#t
> (syntax-equal?* '(a b "c") '(a b "c"))
#t
> (syntax-equal? '(a GEN:b "c") '(a GEN:b "c"))
#t
> (syntax-equal? '(a GEN:b "c") '(a GEN:x "c"))
#t
> (syntax-equal?* '(a GEN:b "c") '(a GEN:x "c"))
#t
> (syntax-equal? '(a GEN:b "c") '(a #:g23 "c"))
#t
> (syntax-equal? '(a GEN:b GEN:c "c") '(a #:g23 #:g23 "c"))
#t
> (syntax-equal? '(a GEN:b GEN:b "c") '(a #:g23 #:g23 "c"))
#f
> (syntax-equal? '(a GEN:b GEN:c "c") (let ((v (##gensym))) `(a ,v ,v "c")))
#f
> (syntax-equal? '(a GEN:b GEN:b "c") (let ((v (##gensym))) `(a ,v ,v "c")))
#t
> (syntax-equal? '(GEN:a GEN:b GEN:c) '(GEN:a GEN:c GEN:b))
#t
> (syntax-equal? '(GEN:a GEN:b GEN:b GEN:c) '(GEN:a GEN:c GEN:b GEN:b))
#f
> (syntax-equal?* '(a b c) '(a c b))
#t
> (syntax-equal?* '(a b b c) '(a f g g))
#f
> (syntax-equal?* '(a b b c) '(a c b b))
#f
)
;; related, thus put here, too:
(define-macro* (symbol-case expr . clauses)
(with-gensyms
(V V*)
`(let ((,V ,expr))
(assert* symbol? ,V
(lambda (,V*)
(case ,V*
;; XX should check clauses for whether they really
;; check for symbols...
,@clauses))))))