-
Notifications
You must be signed in to change notification settings - Fork 1
/
type-check-Cvar.rkt
71 lines (61 loc) · 2.64 KB
/
type-check-Cvar.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
#lang racket
(require "utilities.rkt" "type-check-Lvar.rkt")
(provide type-check-Cvar type-check-Cvar-mixin)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; type-check-Cvar
(define (type-check-Cvar-mixin super-class)
(class super-class
(super-new)
(inherit type-check-op type-equal? check-type-equal?)
(define/public ((type-check-atm env) e)
(match e
[(Var x) (values (Var x) (dict-ref env x))]
[(Int n) (values (Int n) 'Integer)]
[else (error 'type-check-atm "expected a Cvar atm, not ~a" e)]))
(define/override ((type-check-exp env) e)
(debug 'type-check-exp "Cvar ~a" e)
(match e
[(Var x) (values (Var x) (dict-ref env x))]
[(Int n) (values (Int n) 'Integer)]
[(Prim op es)
(define-values (new-es ts)
(for/lists (exprs types) ([e es]) ((type-check-atm env) e)))
(values (Prim op new-es) (type-check-op op ts e))]
[else (error 'type-check-exp "expected a C exp, not ~a" e)]))
(define/public ((type-check-stmt env) s)
(debug 'type-check-stmt "Cvar ~a" s)
(match s
[(Assign (Var x) e)
(define-values (e^ t) ((type-check-exp env) e))
(cond [(dict-has-key? env x)
(check-type-equal? t (dict-ref env x) s)]
[else (dict-set! env x t)])]
[else (error 'type-check-stmt "expected a Cvar stmt, not ~a" s)]))
(define/public ((type-check-tail env block-env blocks) t)
(debug 'type-check-tail "Cvar ~a" t)
(match t
[(Return e)
(define-values (e^ t) ((type-check-exp env) e))
t]
[(Seq s t)
((type-check-stmt env) s)
((type-check-tail env block-env blocks) t)]
[else (error 'type-check-tail "expected a Cvar tail, not ~a" t)]))
(define/override (type-check-program p)
(match p
[(CProgram info blocks)
(define env (make-hash))
(define block-env (make-hash))
(define t ((type-check-tail env block-env blocks)
(dict-ref blocks 'start)))
(unless (type-equal? t 'Integer)
(error "return type of program must be Integer, not" t))
(define locals-types (for/list ([(x t) (in-dict env)])
(cons x t)))
(define new-info (dict-set info 'locals-types locals-types))
(CProgram new-info blocks)]
[else (error 'type-check-program "expected a C program, not ~a" p)]))
))
(define type-check-Cvar-class (type-check-Cvar-mixin type-check-Lvar-class))
(define (type-check-Cvar p)
(send (new type-check-Cvar-class) type-check-program p))