-
Notifications
You must be signed in to change notification settings - Fork 1
/
type-check-Lvec.rkt
92 lines (83 loc) · 3.66 KB
/
type-check-Lvec.rkt
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
#lang racket
(require "utilities.rkt")
(require "type-check-Cvar.rkt")
(require "type-check-Lwhile.rkt")
(provide type-check-Lvec type-check-Lvec-class)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tuples (aka Vectors) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; type-check-Lvec
(define type-check-Lvec-class
(class type-check-Lwhile-class
(super-new)
(inherit check-type-equal?)
(define/override (type-equal? t1 t2)
(debug 'type-equal? "lenient" t1 t2)
(match* (t1 t2)
[(`(Vector ,ts1 ...) `(Vector ,ts2 ...))
(for/and ([t1 ts1] [t2 ts2])
(type-equal? t1 t2))]
[(other wise) (super type-equal? t1 t2)]))
(define/override (type-check-exp env)
(lambda (e)
(define recur (type-check-exp env))
(match e
[(Prim 'vector es)
(unless (<= (length es) 50)
(error 'type-check "vector too large ~a, max is 50" (length es)))
(define-values (e* t*) (for/lists (e* t*) ([e es]) (recur e)))
(define t `(Vector ,@t*))
(values (HasType (Prim 'vector e*) t) t)]
[(Prim 'vector-ref (list e1 (Int i)))
(define-values (e1^ t) (recur e1))
(match t
[`(Vector ,ts ...)
(unless (and (0 . <= . i) (i . < . (length ts)))
(error 'type-check "index ~a out of bounds\nin ~v" i e))
(values (Prim 'vector-ref (list e1^ (Int i))) (list-ref ts i))]
[else (error 'type-check "expect Vector, not ~a\nin ~v" t e)])]
[(Prim 'vector-set! (list e1 (Int i) arg) )
(define-values (e-vec t-vec) (recur e1))
(define-values (e-arg^ t-arg) (recur arg))
(match t-vec
[`(Vector ,ts ...)
(unless (and (0 . <= . i) (i . < . (length ts)))
(error 'type-check "index ~a out of bounds\nin ~v" i e))
(check-type-equal? (list-ref ts i) t-arg e)
(values (Prim 'vector-set! (list e-vec (Int i) e-arg^)) 'Void)]
[else (error 'type-check "expect Vector, not ~a\nin ~v" t-vec e)])]
[(Prim 'vector-length (list e))
(define-values (e^ t) (recur e))
(match t
[`(Vector ,ts ...)
(values (Prim 'vector-length (list e^)) 'Integer)]
[else (error 'type-check "expect Vector, not ~a\nin ~v" t e)])]
[(Prim 'eq? (list arg1 arg2))
(define-values (e1 t1) (recur arg1))
(define-values (e2 t2) (recur arg2))
(match* (t1 t2)
[(`(Vector ,ts1 ...) `(Vector ,ts2 ...)) (void)]
[(other wise) (check-type-equal? t1 t2 e)])
(values (Prim 'eq? (list e1 e2)) 'Boolean)]
[(HasType (Prim 'vector es) t)
((type-check-exp env) (Prim 'vector es))]
[(HasType e1 t)
(define-values (e1^ t^) (recur e1))
(check-type-equal? t t^ e)
(values (HasType e1^ t) t)]
[(GlobalValue name)
(values (GlobalValue name) 'Integer)]
[(Allocate size t)
(values (Allocate size t) t)]
[(Collect size)
(values (Collect size) 'Void)]
[else ((super type-check-exp env) e)]
)))
))
(define (type-check-Lvec p)
(send (new type-check-Lvec-class) type-check-program p))
#;(define (type-check-exp env)
(send (new type-check-Lvec-class) type-check-exp env))
#;(define (type-equal? t1 t2)
(send (new type-check-Lvec-class) type-equal? t1 t2))