-
Notifications
You must be signed in to change notification settings - Fork 0
/
uncover-get.rkt
67 lines (62 loc) · 1.81 KB
/
uncover-get.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
#lang racket
(require "type-check-Lif.rkt")
(require "interp-Lif.rkt")
(require "utilities.rkt")
(provide uncover-get)
(define (collect-set! e)
(match e
[(Var x) (set)]
[(Int n) (set)]
[(Bool b) (set)]
[(Prim op es)
(for/fold ([acc (set)]) ([e es])
(set-union (collect-set! e) acc))]
[(Let x rhs body)
(set-union (collect-set! rhs) (collect-set! body))]
[(If cnd then-clause else-clause)
(set-union
(collect-set! cnd)
(collect-set! then-clause)
(collect-set! else-clause))]
[(SetBang var rhs)
(set-union (set var) (collect-set! rhs))]
[(Begin es body)
(for/fold ([acc (collect-set! body)]) ([e es])
(set-union (collect-set! e) acc))]
[(WhileLoop cnd body)
(set-union (collect-set! cnd) (collect-set! body))]
[(Void) (set)]))
(define (uncover-get!-exp mutable-vars e)
(define (uncover-get-aux e)
(match e
[(Var x)
(if (set-member? mutable-vars x)
(GetBang x)
(Var x))]
[(Int n) (Int n)]
[(Bool b) (Bool b)]
[(Prim op es)
(Prim op (map uncover-get-aux es))]
[(Let x rhs body)
(Let
x
(uncover-get-aux rhs)
(uncover-get-aux body))]
[(If cnd then-clause else-clause)
(If
(uncover-get-aux cnd)
(uncover-get-aux then-clause)
(uncover-get-aux else-clause))]
[(SetBang var rhs)
(SetBang var (uncover-get-aux rhs))]
[(Begin es body)
(Begin (map uncover-get-aux es) (uncover-get-aux body))]
[(WhileLoop cnd body)
(WhileLoop (uncover-get-aux cnd) (uncover-get-aux body))]
[(Void) (Void)]))
(uncover-get-aux e))
(define (uncover-get p)
(match p
[(Program '() exp)
(define mutable-vars (collect-set! exp))
(Program '() (uncover-get!-exp mutable-vars exp))]))