aboutsummaryrefslogtreecommitdiffstats
path: root/csc/cps-test.csc
blob: d51d10cc173348f377fe196da55c1a12377109aa (plain) (blame)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
(import (scheme base)
        (only (csc hash-map)
          map->alist)
        (only (csc ir1)
          make-call
          make-constant
          make-define-syntax
          make-if
          make-lambda
          make-lambda-case
          make-letrec
          make-lexical-ref
          make-lexical-set
          make-library-ref
          make-sequence)
        (only (csc ir2)
          ir2=?
          make-atom
          make-branch
          make-call-closure
          make-closure
          make-kargs
          make-klabel
          make-ktail
          make-lambda-args
          make-update)
        (only (csc loop)
          loop
          return)
        (only (csc sort) sort)
        (only (csc testing)
          assert-equal
          test)
        (csc cps))


(define (soup->alist s)
  (sort (lambda (x y) (< (car x) (car y))) (map->alist s)))


(define (soup=? x y)
  (and (= (length x) (length y))
       (loop for x* in x
             for y* in y
             unless (and (= (car x*) (car y*))
                         (ir2=? (cdr x*) (cdr y*)))
               return #f
             finally (return #t))))


(test atom-const
  (assert-equal soup=?
    (list
      (cons 0 (make-klabel
                (make-atom (make-constant 5) 1)))
      (cons 1 (make-ktail)))
    (soup->alist (ir1->ir2 (make-constant 5)))))


(test atom-lexical-ref
  (assert-equal soup=?
    (list
      (cons 0 (make-klabel
                (make-atom (make-lexical-ref 'var #f) 1)))
      (cons 1 (make-ktail)))
    (soup->alist (ir1->ir2 (make-lexical-ref 'var #f)))))


(test atom-library-ref
  (assert-equal soup=?
    (list
      (cons 0 (make-klabel
                (make-atom (make-library-ref 'var '(csc builtins)) 1)))
      (cons 1 (make-ktail)))
    (soup->alist (ir1->ir2 (make-library-ref 'var '(csc builtins))))))


(test lexical-set
  (assert-equal soup=?
    (list
      (cons 0 (make-klabel
                (make-atom (make-constant 5) 2)))
      (cons 1 (make-ktail))
      (cons 2 (make-kargs (list (make-lexical-ref 'generated-symbol #f))
                (make-update (make-lexical-ref 'var #f)
                             (make-lexical-ref 'generated-symbol #f) 1))))
    (soup->alist (ir1->ir2 (make-lexical-set (make-lexical-ref 'var #f) (make-constant 5))))))


(test no-op-define-syntax
  (assert-equal soup=?
    (list
      (cons 0 (make-klabel (make-atom (make-constant #f) 1)))
      (cons 1 (make-ktail)))
    (soup->alist (ir1->ir2 (make-define-syntax 'name '(transformer))))))


(test branch
  (assert-equal soup=?
    (list
      (cons 0 (make-klabel (make-atom (make-constant #t) 4)))
      (cons 1 (make-ktail))
      (cons 2 (make-klabel (make-atom (make-constant 1) 1)))
      (cons 3 (make-klabel (make-atom (make-constant 2) 1)))
      (cons 4 (make-kargs (list (make-lexical-ref 'generated-symbol #f))
                (make-branch (make-lexical-ref 'generated-symbol #f)
                  2 3))))
    (soup->alist (ir1->ir2 (make-if (make-constant #t)
                             (make-constant 1)
                             (make-constant 2))))))


(test call-closure
  (assert-equal soup=?
    (list
      (cons 0 (make-klabel (make-atom (make-lexical-ref 'f #f) 4)))
      (cons 1 (make-ktail))
      (cons 2 (make-kargs (list (make-lexical-ref 'generated-symbol #f))
                (make-call-closure (make-lexical-ref 'generated-symbol #f)
                  (list (make-lexical-ref 'generated-symbol #f)
                        (make-lexical-ref 'generated-symbol #f))
                  1)))
      (cons 3 (make-kargs (list (make-lexical-ref 'generated-symbol #f))
                (make-atom (make-constant 2) 2)))
      (cons 4 (make-kargs (list (make-lexical-ref 'generated-symbol #f))
                (make-atom (make-constant 1) 3))))
    (soup->alist (ir1->ir2 (make-call (make-lexical-ref 'f #f) (list (make-constant 1) (make-constant 2)))))))


(test sequence
  (assert-equal soup=?
    (list
      (cons 0 (make-klabel (make-atom (make-constant 1) 2)))
      (cons 1 (make-ktail))
      (cons 2 (make-klabel (make-atom (make-constant 2) 1))))
    (soup->alist (ir1->ir2 (make-sequence (make-constant 1)
                                          (make-constant 2))))))


(test closure
  (assert-equal soup=?
    (list
      (cons 0 (make-klabel
                (make-closure
                  (list (make-lambda-args 2 #t 3))
                  1)))
      (cons 1 (make-ktail))
      (cons 2 (make-ktail))
      (cons 3 (make-kargs (list (make-lexical-ref 'a #f)
                                (make-lexical-ref 'b #f)
                                (make-lexical-ref 'c #f))
                (make-atom (make-constant 5) 2))))
    (soup->alist (ir1->ir2 (make-lambda
                             (make-lambda-case
                               '(a b)
                               'c
                               '(#f #f #f)
                               (make-constant 5)
                               #f))))))


(test letrec-to-lambda
  (assert-equal soup=?
    (list
      ; God help you when it's time to debug this test.
      (cons 0 (make-klabel
                (make-closure
                  (list
                    (make-lambda-args 1 #f 7))
                  3)))
      (cons 1 (make-ktail))
      (cons 2 (make-kargs (list (make-lexical-ref 'generated-symbol #f))
                (make-call-closure
                  (make-lexical-ref 'generated-symbol #f)
                  (list (make-lexical-ref 'generated-symbol #f))
                  1)))
      (cons 3 (make-kargs (list (make-lexical-ref 'generated-symbol #f))
                (make-atom (make-constant #f) 2)))
      (cons 4 (make-ktail))
      (cons 5 (make-klabel
                (make-atom (make-constant 2) 4)))
      (cons 6 (make-kargs (list (make-lexical-ref 'generated-symbol #f))
                (make-update (make-lexical-ref 'a #f) (make-lexical-ref 'generated-symbol #f) 5)))
      (cons 7 (make-kargs (list (make-lexical-ref 'a #f))
                 (make-atom (make-constant 1) 6))))
    (soup->alist (ir1->ir2 (make-letrec
                             #t
                             '(a)
                             '(#f)
                             (list (make-constant 1))
                             (make-constant 2))))))