65
65
(define (str-bits? v)
66
66
(zero? (bitwise-xor (bitwise-and v imm-mask) type-str)))
67
67
68
+ ;; TODO: Expand Int to Byte and UInt
68
69
(define types
69
70
'(Int Bool Char Str Vector Eof Empty Box Cons Void Any))
70
71
71
72
(define (is-member e list)
72
73
(if (eq? (member e list) #f ) #f #t ))
73
74
75
+ (define (append-element lst elem)
76
+ (append lst (list elem)))
77
+
78
+ ;; Only consider single item
79
+ ;; If pass (list type), check each of them
74
80
(define (type? types x)
75
- (match x
76
- ['() #t ]
77
- [(? symbol?) (is-member x types)]
78
- [(list 'Listof s) (type? types s)]
79
- [(cons t ts) (and (type? types t) (type? types ts))]
80
- [_ #f ]))
81
+ (match x
82
+ ['() #t ]
83
+ [(? symbol?) (is-member x types)]
84
+ [(list 'Listof s) (type? types s)]
85
+ [(cons t ts) (and (type? types t) (type? types ts))]
86
+ [_ #f ]))
87
+
88
+ ;; (type-replace-id xts id pt)
89
+ ;; (type-rule-out-id xts id pt)
90
+
91
+ (define (type-replace-id xts id pt)
92
+ (append (reverse (list-tail (reverse xts) (- (length xts) id))) `(,pt) (list-tail xts (+ id 1 ))))
81
93
94
+ (define (type-rule-out-id xts id pt)
95
+ (append (reverse (list-tail (reverse xts) (- (length xts) id))) `(,(type-minus (list-ref xts id) pt)) (list-tail xts (+ id 1 ))))
96
+
97
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
98
+ ;; TODO: support check (Listof, Cons)
99
+
100
+ ;; Only consider when type-query isn't a list
82
101
;; possible type-format:
83
102
;; 1. A single type
84
103
;; 2. List of type
85
104
;; 3. (Listof type)
86
105
106
+ ;; Special:
107
+ ;; 1. Any contains anything
108
+ ;; 2. Empty can be contained in Vector/String
109
+ ;; 3. Listof can be contained in Cons
110
+
87
111
(define (type-contain type-format type-query)
88
112
(match type-format
89
113
['() #f ]
90
114
[(list 'Listof tf)
91
- (match type-query
92
- [(list 'Listof tq) (type-contain tf tq)]
93
- [_ #f ])]
94
- [(? symbol?) (if (eq? type-format 'Any ) #t (if (eq? type-query 'Empty ) (or (eq? type-format 'Vector ) (eq? type-format 'Str )) (eq? type-format type-query)))]
115
+ (match type-query
116
+ [(list 'Listof tq) (type-contain tf tq)]
117
+ [_ #f ])]
118
+ [(? symbol?)
119
+ (if (eq? type-format 'Any ) #t
120
+ (if (eq? type-query 'Empty ) (or (eq? type-format 'Vector ) (eq? type-format 'Str ) (eq? type-format 'Empty ))
121
+ (if (eq? type-format 'Cons )
122
+ (match type-query
123
+ [(list 'Listof tq) #t ]
124
+ ['Cons #t ]
125
+ [_ #f ]) (eq? type-format type-query))))]
95
126
[(cons type-a type-format) (or (type-contain type-a type-query) (type-contain type-format type-query))]
96
127
[_ #f ]))
97
128
110
141
(match type-origin-list
111
142
['() '() ]
112
143
['Any
113
- (match type-ruled-out
114
- [(? symbol?) (append (remove* '(Any type-ruled-out) types) '((Listof Any)))]
115
- [(list 'Listof tf) (append (remove 'Any types) `((Listof ,(type-minus 'Any tf))))]
116
- [_ (error "type-error " "invalid type: ~a " type-ruled-out)])]
144
+ (match type-ruled-out
145
+ [(? symbol?) (append (remove* '(Any type-ruled-out) types) '((Listof Any)))]
146
+ [(list 'Listof tf) (append (remove 'Any types) `((Listof ,(type-minus 'Any tf))))]
147
+ [_ (error "type-error " "invalid type: ~a " type-ruled-out)])]
117
148
[(? symbol?) (if (eq? type-origin-list type-ruled-out) '() type-origin-list)]
118
149
[(list 'Listof tf)
119
- (match type-ruled-out
120
- [(? symbol?) type-origin-list]
121
- [(list 'Listof tr) (let ([empty-ele-lst (type-minus tf tr)])
122
- (if (eq? empty-ele-lst '() ) '()
123
- (if (eq? (length empty-ele-lst) 1 )
124
- (list 'Listof (list-ref empty-ele-lst 0 ))
125
- (list 'Listof empty-ele-lst))))]
126
- [_ (error "type-error " "invalid type: ~a " type-ruled-out)])]
150
+ (match type-ruled-out
151
+ [(? symbol?) type-origin-list]
152
+ [(list 'Listof tr) (let ([empty-ele-lst (type-minus tf tr)])
153
+ (if (eq? empty-ele-lst '() ) '()
154
+ (if (eq? (length empty-ele-lst) 1 )
155
+ (list 'Listof (list-ref empty-ele-lst 0 ))
156
+ (list 'Listof empty-ele-lst))))]
157
+ [_ (error "type-error " "invalid type: ~a " type-ruled-out)])]
127
158
[(cons type-first types-remaining)
128
- (let ([ele-listele-lst (type-minus type-first type-ruled-out)])
129
- (match ele-listele-lst
130
- [(? symbol? ele-listele-lst) (append `(,ele-listele-lst) (type-minus types-remaining type-ruled-out))]
131
- [(list 'Listof _ ) (append `(,ele-listele-lst) (type-minus types-remaining type-ruled-out))]
132
- [_ (append ele-listele-lst (type-minus types-remaining type-ruled-out))]))]))
159
+ (let ([ele-listele-lst (type-minus type-first type-ruled-out)])
160
+ (match ele-listele-lst
161
+ [(? symbol? ele-listele-lst) (append `(,ele-listele-lst) (type-minus types-remaining type-ruled-out))]
162
+ [(list 'Listof _ ) (append `(,ele-listele-lst) (type-minus types-remaining type-ruled-out))]
163
+ [_ (append ele-listele-lst (type-minus types-remaining type-ruled-out))]))]))
164
+
165
+ ;; possible Input:
166
+ ;; 1. '()
167
+ ;; 2. 'Any
168
+ ;; 3. 'symbol
169
+ ;; 4. (Listof xx)
170
+ ;; 5. (list Type)
171
+
172
+ (define (type-add type-or-lst-1 type-or-lst-2)
173
+ (match type-or-lst-1
174
+ ['() type-or-lst-2]
175
+ ['Any 'Any ]
176
+ [(? symbol?)
177
+ (match type-or-lst-2
178
+ ['() type-or-lst-1]
179
+ ['Any 'Any ]
180
+ [(? symbol?) (if (eq? type-or-lst-1 type-or-lst-2) type-or-lst-1 `(,type-or-lst-1 ,type-or-lst-2))]
181
+ [(list 'Listof tf) `(,type-or-lst-1 ,type-or-lst-2)]
182
+ [(cons type-first types-remaining)
183
+ (if (eq? types-remaining '() ) `(,type-or-lst-1 ,type-first)
184
+ (match type-first
185
+ [(? symbol?) (if (type-contain type-first type-or-lst-1) type-or-lst-2 (append `(,type-first) (type-add type-or-lst-1 types-remaining)))]
186
+ [(list 'Listof tf) (append `(,type-first) (type-add type-or-lst-1 types-remaining))]))])]
187
+ [(list 'Listof tf)
188
+ (match type-or-lst-2
189
+ ['() type-or-lst-1]
190
+ ['Any 'Any ]
191
+ [(list 'Listof tf2)
192
+ (if (type-contain tf tf2) type-or-lst-1
193
+ (if (type-contain tf2 tf) type-or-lst-2
194
+ `(Listof ,(type-add tf tf2))))]
195
+ [(? symbol?) `(,type-or-lst-1 ,type-or-lst-2)]
196
+ [(cons type-first types-remaining)
197
+ (match type-first
198
+ [(list 'Listof tf2) (if (type-contain type-first type-or-lst-1) type-or-lst-2 (type-add `(Listof ,(type-add tf tf2)) types-remaining))]
199
+ [(? symbol?) (if (eq? types-remaining '() ) `(,type-first ,type-or-lst-1) (type-add type-first (type-add type-or-lst-1 types-remaining)))])
200
+ ])]
201
+ [(cons type-first types-remaining)
202
+ (match type-or-lst-2
203
+ ['() type-or-lst-1]
204
+ ['Any 'Any ]
205
+ [(list 'Listof tf)
206
+ (match type-first
207
+ [(list 'Listof tf2)
208
+ (if (type-contain tf tf2) type-or-lst-1
209
+ (if (type-contain tf2 tf) (type-add type-or-lst-2 types-remaining)
210
+ (type-add `(Listof ,(type-add tf tf2)) types-remaining)))]
211
+ [(? symbol?) (type-add type-first (type-add type-or-lst-2 types-remaining))])]
212
+ [(? symbol?)
213
+ (match type-first
214
+ [(list 'Listof tf2) (append `(,type-first) (type-add type-or-lst-2 types-remaining))]
215
+ [(? symbol?) (if (type-contain type-first type-or-lst-2) (type-or-lst-1)
216
+ (if (type-contain type-or-lst-2 type-first) (type-add type-or-lst-2 types-remaining)
217
+ (append `(,type-first) (type-add type-or-lst-2 types-remaining))))])]
218
+ [(cons type-first-2 types-remaining-2) (type-add types-remaining (type-add type-first type-or-lst-2))])]))
0 commit comments