Skip to content

Commit af006f5

Browse files
committed
fixes
1 parent 737cd02 commit af006f5

File tree

6 files changed

+68
-17
lines changed

6 files changed

+68
-17
lines changed

conditionals.rkt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@
164164
(verbose "flatten" e)
165165
(match e
166166
;; For atomic stuff, we keep the has-type annotation. -Jeremy
167+
[`(has-type (void) ,t)
168+
(values `(has-type (void) ,t) '())]
167169
[`(has-type ,e ,t) #:when (or (symbol? e) (integer? e) (boolean? e))
168170
(values `(has-type ,e ,t) '())]
169171

dynamic-typing.rkt

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,45 @@
2424
(define/override (type-check env)
2525
(lambda (e)
2626
(match e
27+
[`(vector-ref ,(app (type-check env) e t) ,i)
28+
(match t
29+
[`(Vector ,ts ...)
30+
(unless (and (exact-nonnegative-integer? i)
31+
(i . < . (length ts)))
32+
(error 'type-check "invalid index ~a" i))
33+
(let ([t (list-ref ts i)])
34+
(values `(has-type (vector-ref ,e (has-type ,i Integer)) ,t) t))]
35+
[`(Vectorof ,t)
36+
(unless (exact-nonnegative-integer? i)
37+
(error 'type-check "invalid index ~a" i))
38+
(values `(has-type (vector-ref ,e (has-type ,i Integer)) ,t) t)]
39+
[else (error "expected a vector in vector-ref, not" t)])]
40+
[`(vector-set! ,e-vec ,i ,e-arg)
41+
(define-values (e-vec^ t-vec) ((type-check env) e-vec))
42+
(define-values (e-arg^ t-arg) ((type-check env) e-arg))
43+
(match t-vec
44+
[`(Vector ,ts ...)
45+
(unless (and (exact-nonnegative-integer? i)
46+
(i . < . (length ts)))
47+
(error 'type-check "invalid index ~a" i))
48+
(unless (equal? (list-ref ts i) t-arg)
49+
(error 'type-check "type mismatch in vector-set! ~a ~a"
50+
(list-ref ts i) t-arg))
51+
(values `(has-type (vector-set! ,e-vec^
52+
(has-type ,i Integer)
53+
,e-arg^) Void) 'Void)]
54+
[`(Vectorof ,t)
55+
(unless (exact-nonnegative-integer? i)
56+
(error 'type-check "invalid index ~a" i))
57+
(unless (equal? t t-arg)
58+
(error 'type-check "type mismatch in vector-set! ~a ~a"
59+
t t-arg))
60+
(values `(has-type (vector-set! ,e-vec^
61+
(has-type ,i Integer)
62+
,e-arg^) Void) 'Void)]
63+
[else (error 'type-check
64+
"expected a vector in vector-set!, not ~a"
65+
t-vec)])]
2766
[`(inject ,e ,ty)
2867
(define-values (new-e e-ty) ((type-check env) e))
2968
(cond
@@ -127,6 +166,7 @@
127166
(define (any-tag ty)
128167
(match ty
129168
['Integer 0]
169+
['Void 0]
130170
['Boolean 1]
131171
[`(Vector ,ts ...) 2]
132172
[`(Vectorof ,t) 2]
@@ -238,7 +278,7 @@
238278
[`(- ,e) `(inject (- (project ,((cast-insert) e) Integer)) Integer)]
239279
[`(let ([,x ,e1]) ,e2) `(let ([,x ,((cast-insert) e1)]) ,((cast-insert) e2))]
240280
[#t `(inject #t Boolean)]
241-
[#t `(inject #f Boolean)]
281+
[#f `(inject #f Boolean)]
242282
[`(and ,e1 ,e2) (let ([gen (gensym)])
243283
`(let ([,gen ,((cast-insert) e1)])
244284
(if (eq? ,gen (inject #f Boolean))
@@ -247,9 +287,9 @@
247287
[`(not ,e) `(inject (not (project ,((cast-insert) e) Boolean)) Boolean)]
248288
[`(eq? ,e1 ,e2) `(inject (eq? ,((cast-insert) e1),((cast-insert) e2)) Boolean)]
249289
[`(if ,eq ,et ,ef) `(if (eq? ,((cast-insert) eq) (inject #t Boolean)) ,((cast-insert) et) ,((cast-insert) ef))]
250-
[`(vector ,es ...) `(inject (vector ,@(map (cast-insert) es)) (Vectorof Any))]
251-
[`(vector-ref ,e1 ,n) `(vector-ref (project ,((cast-insert) e1) (Vectorof Any)) n)]
252-
[`(vector-set! ,e1 ,n ,e2) `(vector-set! (project ,((cast-insert) e1) (Vectorof Any)) n ,((cast-insert) e2))]
290+
[`(vector ,es ...) `(inject (vector ,@(map (cast-insert) es)) (Vector ,@(map (lambda (x) 'Any) es)))]
291+
[`(vector-ref ,e1 ,n) `(vector-ref (project ,((cast-insert) e1) (Vectorof Any)) ,n)]
292+
[`(vector-set! ,e1 ,n ,e2) `(vector-set! (project ,((cast-insert) e1) (Vectorof Any)) ,n ,((cast-insert) e2))]
253293
[`(void) `(inject (void) Void)] ; ???
254294
[`(lambda (,xs ...) ,e) `(inject (lambda: (,@(map (lambda (x) `[,x : Any]) xs)) : Any ,((cast-insert) e)) (,@(map (lambda (x) 'Any) xs) -> Any))]
255295
[`(app ,e ,es ...) `(app (project ,((cast-insert) e) (,@(map (lambda (x) 'Any) es) -> Any)) ,@(map (cast-insert) es))]
@@ -335,6 +375,7 @@
335375
(cond
336376
[(lookup e funs #f) `(function-ref ,e ,(lookup e funs))]
337377
[else e])]
378+
['(void) '(void)]
338379
[`(program ,ds ... ,body) #:when (or (null? ds) (not (eq? (caar ds) 'type)))
339380
(define funs
340381
(for/list ([d ds])

functions.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
(define/public (non-apply-ast)
1616
(set-union (primitives)
17-
(set 'if 'let 'define 'program)))
17+
(set 'if 'let 'define 'program 'void)))
1818

1919
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2020
;; type-check : env -> S3 -> S3 (for programs)

int_exp.rkt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818
(lambda (e)
1919
(define recur (uniquify env))
2020
(match e
21-
[(? symbol?) (cdr (assq e env))]
22-
[(? integer?) e]
23-
[`(let ([,x ,e]) ,body)
24-
(define new-x (gensym (racket-id->c-id x)))
25-
(define new-e (recur e))
26-
`(let ([,new-x ,new-e])
21+
['(void) '(void)]
22+
[(? symbol?) (cdr (assq e env))]
23+
[(? integer?) e]
24+
[`(let ([,x ,e]) ,body)
25+
(define new-x (gensym (racket-id->c-id x)))
26+
(define new-e (recur e))
27+
`(let ([,new-x ,new-e])
2728
,((uniquify (cons (cons x new-x) env)) body))]
28-
[`(program ,body)
29-
`(program ,(recur body))]
30-
[`(,op ,es ...) #:when (set-member? (primitives) op)
31-
`(,op ,@(map recur es))]
32-
[else (error "uniquify couldn't match" e)])))
29+
[`(program ,body)
30+
`(program ,(recur body))]
31+
[`(,op ,es ...) #:when (set-member? (primitives) op)
32+
`(,op ,@(map recur es))]
33+
[else (error "uniquify couldn't match" e)])))
3334

3435
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3536
;; flatten : Bool -> S0 -> C0-expr x (C0-stmt list)

run-tests.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
(3 . ,(range 1 20))
4343
(4 . ,(range 0 8))
4444
(6 . ,(range 0 8))
45-
(7 . ,(range 0 3))
45+
(7 . ,(range 0 8))
4646
))
4747

4848
(define (suite-range x)

vectors.rkt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
(lambda (e)
2222
(vomit "vectors/type-check" e env)
2323
(match e
24+
['(void) (values '(has-type (void) Void) 'Void)]
2425
[`(vector ,(app (type-check env) e* t*) ...)
2526
(let ([t `(Vector ,@t*)])
2627
(values `(has-type (vector ,@e*) ,t) t))]
@@ -62,9 +63,13 @@
6263
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6364
;; uniqueify : S1 -> C1-expr x (C1-stmt list)
6465

66+
67+
6568
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6669
;; flatten : S1 -> C1-expr x (C1-stmt list)
6770

71+
72+
6873
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6974
;; expose allocation : C1 -> ?
7075

@@ -165,6 +170,7 @@
165170
(define/public (uncover-call-live-roots-exp e)
166171
(vomit "uncover-call-live-roots-exp" e)
167172
(match e
173+
[`(has-type (void) ,t) (set)]
168174
[`(has-type ,(? symbol? x) ,t)
169175
(if (root-type? t)
170176
(set x)
@@ -239,6 +245,7 @@
239245
(lambda (x)
240246
(vomit "select instructions" x)
241247
(match x
248+
[`(void) `(int 0)]
242249
[`(assign ,lhs (has-type (allocate ,length) (Vector ,ts ...)))
243250
(define lhs^ ((select-instructions) lhs))
244251
;; Add one quad word for the meta info tag

0 commit comments

Comments
 (0)