-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcj-struct-tag.scm
179 lines (138 loc) · 4.6 KB
/
cj-struct-tag.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
;;; Copyright 2010-2019 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
test
(cj-env define-if-not-defined) ;; in macro expansion
;; (predicates-1 list-of-length)
(cj-inline-1 define-inline.1) ;; cj-inline would give cycle
(cj-gambit-sys-0 @vector-ref @vector-length @vector-set!))
(export struct-tag?
;; ^ XX inconsistent: struct-type etc. are all talking about
;; 'type'; and yeah it should be that right?
maybe-struct-tag-name
struct-tag.name ;; stupid naming? (oo again, that isn't) (but
;; matches, above it may not be a struct-tag
;; and is ok)
cj-struct#vector? vector?
;; vector-like? see cj-struct.scm
;; and struct-metadata:
struct-metadata?
make-struct-metadata-accessor
struct-metadata
struct-metadata.constructor-name
@maybe-struct-tag-name
@struct-tag?
struct-tag-allocate!
maybe-struct-tag->metadata
struct-tag->metadata
#!optional
cj-struct:type->metadata ;; the table, shouldn't use directly
)
;;(include "cj-standarddeclares.scm")
;; no, as that would disable the modification to |vector?|
(declare (block)
(standard-bindings)
(extended-bindings))
;; A struct is a vector with an object in slot 0 that unambiguously
;; determines the struct type, implemented by using allocated,
;; non-interned objects ("tags", but they are not visual, but
;; invisibly unique; for now, sadly (no type language yet)).
;; Metadata
;; `struct-metadata` struct:
;; we can't use the struct-tag machinery yet, so hand-code the same
;; thing here for this type:
(define-if-not-defined struct-metadata:tag (list 'struct-metadata))
(define (struct-metadata? v)
(and (##vector? v)
(fx> (@vector-length v) 1)
(eq? (@vector-ref v 0) struct-metadata:tag)))
(define (make-struct-metadata-accessor slot-number)
(lambda (v)
(if (struct-metadata? v)
(vector-ref v slot-number)
(error "wrong type"))))
(define (struct-metadata constructor-name)
(vector struct-metadata:tag constructor-name))
(define struct-metadata.constructor-name (make-struct-metadata-accessor 1))
;; Mapping from tag objects to metadata
(define-if-not-defined cj-struct:type->metadata (make-table test: eq?))
(define-inline.1 (@maybe-struct-tag-name v)
(declare (block)
(standard-bindings)
(extended-bindings)
(not safe))
;; @maybe-struct-tag-name is also being inlined by joo.scm in unsafe
;; compilation mode.
(and (pair? v)
(null? (cdr v))
(let ((t (car v)))
(and (symbol? t)
t))))
(define (@struct-tag? v)
(and (@maybe-struct-tag-name v)
#t))
(define (struct-tag-allocate! s metadata)
(if (symbol? s)
(let ((t (list s)))
(table-set! cj-struct:type->metadata t metadata)
;; (table-set! cj-struct:lookup s t)
t)
(error "not a symbol:" s)))
(define (maybe-struct-tag->metadata t)
(table-ref cj-struct:type->metadata t #f))
(define (struct-tag->metadata t)
(or (maybe-struct-tag->metadata t)
(error "not a struct tag:" t)))
(define (struct-tag? v)
(and (maybe-struct-tag->metadata v)
#t))
(define (maybe-struct-tag-name v)
(and (struct-tag? v)
(@maybe-struct-tag-name v)))
(define (struct-tag.name v)
(if (struct-tag? v)
(@maybe-struct-tag-name v)
(error "not a struct tag:" v)))
;;
(TEST
> (define m (struct-metadata 'foo))
> (define t (struct-tag-allocate! 'kkfjkif3hnunnfgw56k m))
> (struct-tag? t)
#t
> (struct-tag? '(kkfjkif3hnunnfgw56k))
#f
> (@struct-tag? '(kkfjkif3hnunnfgw56k))
#t
> (equal? t '(kkfjkif3hnunnfgw56k))
#t
> (eq? t '(kkfjkif3hnunnfgw56k))
#f
> (maybe-struct-tag-name '(kkfjkif3hnunnfgw56k))
#f
> (@maybe-struct-tag-name '(kkfjkif3hnunnfgw56k))
kkfjkif3hnunnfgw56k
> (maybe-struct-tag-name t)
kkfjkif3hnunnfgw56k
> (@maybe-struct-tag-name t)
kkfjkif3hnunnfgw56k
> (eq? (struct-tag-allocate! 'kkfjkif3hnunnfgw56k m)
(struct-tag-allocate! 'kkfjkif3hnunnfgw56k m))
#f
;; cleanup, hacky
> (table-set! cj-struct:type->metadata t)
;; > (table-set! cj-struct:lookup 'kkfjkif3hnunnfgw56k #f)
)
;; Differentiate between "Scheme vectors" and those mis-used for
;; objects now:
;; (Offer in private namespace to make it accessible in standard
;; declaration mode, see in and keep in sync with
;; cj-standarddeclares.scm !)
(define (cj-struct#vector? v)
(and (##vector? v)
(if (fx>= (@vector-length v) 1)
(not (struct-tag? (@vector-ref v 0)))
#t)))
(set! vector? cj-struct#vector?)