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
/
uniquify.rkt
55 lines (50 loc) · 1.9 KB
/
uniquify.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
#lang racket
(require "utilities.rkt"
"utils.rkt")
(provide uniquify)
(define ((uniquify-exp env) e)
(match e
[(Void) (Void)]
[(Var x) (Var (dict-ref env x))]
[(Int n) (Int n)]
[(Bool b) (Bool b)]
[(FunRef f) (FunRef f)]
[(GlobalValue g) (GlobalValue g)]
[(Allocate l t) (Allocate l t)]
[(Collect bytes) (Collect bytes)]
[(Apply fun arg*) (Apply ((uniquify-exp env) fun)
(map (uniquify-exp env) arg*))]
[(HasType e t) (HasType ((uniquify-exp env) e) t)]
[(If c t e)
(let ([u (uniquify-exp env)])
(If (u c) (u t) (u e)))]
[(Let x e body)
(let* ([x1 (gensym x)]
[e1 ((uniquify-exp env) e)]
[env1 (dict-set env x x1)]
[body1 ((uniquify-exp env1) body)])
(Let x1 e1 body1))]
[(Prim op es)
(Prim op (for/list ([e es]) ((uniquify-exp env) e)))]))
(define (param-type p)
(match p
[`(,_ : ,t) t]
[else (error "param-type passed non-param : " p)]))
(define (uniquify p)
(match p
[(ProgramDefs info def*)
(let ([fun* (foldr (lambda (def a)
(cons (cons def def) a))
'()
def*)])
(ProgramDefs info
(map (lambda (d)
(let ([param* (foldr (lambda (param a)
(cons (cons (param-name param)
(param-name param)) a))
'()
(Def-param* d))])
(struct-copy Def d
[body ((uniquify-exp (append fun*
param*)) (Def-body d))])))
def*)))]))