This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
forked from iambrj/imin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type-check-Cvar.rkt
50 lines (42 loc) · 1.66 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
#lang racket
(require "utilities.rkt" "type-check-Rvar.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-exp type-equal? check-type-equal?)
(define/public ((type-check-stmt env) 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)])]))
(define/public ((type-check-tail env block-env G) 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 G) t)]))
(define/override (type-check-program p)
(match p
[(CProgram info G)
(define env (make-hash))
(define block-env (make-hash))
(define t ((type-check-tail env block-env G)
(dict-ref G '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 G)]
[else (super type-check-program p)]))
))
(define type-check-Cvar-class (type-check-Cvar-mixin type-check-Rvar-class))
(define (type-check-Cvar p)
(send (new type-check-Cvar-class) type-check-program p))