generated from cmsc430/assign05-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rkt
208 lines (178 loc) · 7.25 KB
/
main.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#lang racket
(require "../interp.rkt" ; CHANGED THIS, ORIGINALLY POINTED TO ./interp.rkt, which doesn't have updated ast
rackunit)
(define (read-prog p)
(regexp-match "^#lang racket" p)
(sexpr->prog (read p))) ; use sexpr to prog function here to work with new AST representation
;; Code for submission needs to be in ".." directory
(require (only-in "../compile.rkt" compile)
(only-in "../asm/interp.rkt" asm-interp)
(only-in "../syntax.rkt" sexpr->prog sexpr->expr prog? expr? closed?))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Syntax tests
; made wrapper functions with the correct conversions
(define (check-expr? p) (expr? (sexpr->expr p))) ; use sexpr->expr because these are pre-Iniquity examples
(define (check-prog? p) (prog? (sexpr->prog p)))
(define (check-closed? p) (closed? (sexpr->prog p)))
(check-true (check-expr? 7))
(check-true (check-expr? "asdf"))
(check-true (check-expr? ""))
(check-true (check-expr? #t))
(check-true (check-expr? #t))
(check-true (check-expr? #\a))
(check-true (check-expr? '(add1 #f)))
(check-true (check-expr? '(sub1 #f)))
(check-true (check-expr? '(abs #f)))
(check-true (check-expr? '(- #f)))
(check-true (check-expr? '(zero? #f)))
(check-true (check-expr? '(integer->char #f)))
(check-true (check-expr? '(char->integer #f)))
(check-true (check-expr? '(char? #f)))
(check-true (check-expr? '(integer? #f)))
(check-true (check-expr? '(boolean? #f)))
(check-true (check-expr? '(box "adsf")))
(check-true (check-expr? '(+ 1 2)))
(check-true (check-expr? '(- 1)))
(check-true (check-expr? '(- 1 2)))
(check-true (check-expr? 'x))
(check-true (check-expr? '(let () x)))
(check-true (check-expr? '(let ((x 1)) x)))
(check-true (check-expr? '(let ((x 1) (y 2)) x)))
(check-true (check-expr? '(let ((x 1) (y 2) (z 3)) x)))
(check-true (check-expr? '(string-length "asdf")))
(check-true (check-expr? '(string-ref "asdf" 0)))
(check-true (check-expr? '(= #f #f)))
(check-true (check-expr? '(< #f #f)))
(check-true (check-expr? '(string? #f)))
(check-true (check-expr? '(box? #f)))
(check-true (check-expr? '(empty? #f)))
(check-true (check-expr? '(cons? #f)))
(check-true (check-expr? '(unbox #f)))
(check-true (check-expr? '(car #f)))
(check-true (check-expr? '(cdr #f)))
(check-true (check-expr? '(make-string #f #f)))
(check-true (check-expr? '(= #f #f)))
(check-true (check-expr? '(< #f #f)))
(check-true (check-expr? '(<= #f #f)))
(check-true (check-expr? '(char=? #f #f)))
(check-true (check-expr? '(boolean=? #f #f)))
(check-true (check-expr? '(+ #f #f)))
(check-true (check-expr? '(- #f #f)))
; I commented these out, but I think you just need to convert these to how you had them in assign05-test
;;; (check-false (check-expr? '(let 1))) ;; for instance, this becomes (check-exn exn:fail? (lambda () (expr? (sexpr->expr '(let 1)))))
(check-exn exn:fail? (lambda () (expr? (sexpr->expr '(let 1)))))
;;; (check-false (check-expr? '(let x 1)))
;;; (check-false (check-expr? '(let x y 1)))
;;; (check-false (check-expr? '(let (x y) 1)))
;;; (check-false (check-expr? '(let ((x)) 1)))
;;; (check-false (check-expr? '(let ((1 2)) 1)))
;;; (check-false (check-expr? '(let ((abs 1)) 1)))
;;; (check-false (check-expr? '(let ((string-ref 1)) 1)))
;;; (check-false (check-expr? '(let ((+ 1)) 1)))
;;; (check-false (check-expr? '(let ((string? 1)) 1)))
;;; (check-false (check-expr? '(1)))
;;; (check-false (check-expr? '(box)))
;;; (check-false (check-expr? '(string-ref "asdf")))
;;; (check-false (check-expr? '(+ 1 2 3)))
;;; (check-false (check-expr? '(make-string #f)))
;;; (check-false (check-expr? '(make-string #f #f #f)))
(check-true (check-prog? 5))
(check-true (check-prog? '(begin (define (f x) x)
(f 5))))
(check-true (check-prog? '(begin (define (f x y z) x)
(f 5 6 7))))
(check-true (check-prog? '(begin (define (f x y z) (g x))
(define (g q) q)
(f 5 6 7))))
; LISP 1 vs LISP 2, we fall back on Racket's conventions!
(check-true (check-prog? '(begin (define (f f) f)
(f 5))))
(check-true (check-closed? 7))
(check-true (check-closed? "asdf"))
(check-true (check-closed? ""))
(check-true (check-closed? #t))
(check-true (check-closed? #f))
(check-true (check-closed? #\a))
(check-true (check-closed? '(box "adsf")))
(check-true (check-closed? '(+ 1 2)))
(check-true (check-closed? '(- 1)))
(check-true (check-closed? '(- 1 2)))
(check-true (check-closed? '(let ((x 1)) x)))
(check-true (check-closed? '(let ((x 1) (y 2)) x)))
(check-true (check-closed? '(let ((x 1) (y 2) (z 3)) x)))
(check-true (check-closed? '(string-length "asdf")))
(check-true (check-closed? '(string-ref "asdf" 0)))
(check-true (check-closed? '(let ((x 1) (y 2))
(let ((z y))
(+ x z)))))
(check-false (check-closed? 'x))
(check-false (check-closed? '(let () x)))
(check-false (check-closed? '(let ((x 1)) y)))
(check-false (check-closed? '(let ((x 1) (y x)) y)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Compiler tests
(define (run p)
(asm-interp (compile (sexpr->prog p))))
(check-equal?
(run '(let ((x 1)) x))
(interp (sexpr->prog '(let ((x 1)) x))))
(check-equal? (run
'(begin (define (f x) x)
(f 5)))
5)
(check-equal? (run
'(begin (define (tri x)
(if (zero? x)
0
(+ x (tri (sub1 x)))))
(tri 9)))
45)
(check-equal? (run
'(begin (define (even? x)
(if (zero? x)
#t
(odd? (sub1 x))))
(define (odd? x)
(if (zero? x)
#f
(even? (sub1 x))))
(even? 101)))
#f)
(check-equal? (run
'(begin (define (map-add1 xs)
(if (empty? xs)
'()
(cons (add1 (car xs))
(map-add1 (cdr xs)))))
(map-add1 (cons 1 (cons 2 (cons 3 '()))))))
'(2 3 4))
(check-equal? (run
'(begin
(define (explode str)
(explode/i str 0))
(define (explode/i str i)
(if (= (string-length str) i)
'()
(cons (string-ref str i)
(explode/i str (add1 i)))))
(explode "fred")))
'(#\f #\r #\e #\d))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Random tests
(define parses
(parameterize ((current-directory "progs"))
(for/list ([fn (directory-list)])
(list fn (call-with-input-file fn read-prog)))))
(for ([p parses])
(match p
[(list fn p)
(check-true (and (prog? p)
(closed? p))
(list fn p))]))
(for ([p parses])
(match p
[(list fn p)
(check-equal? (with-handlers ([exn:fail? identity])
(asm-interp (compile p)))
(interp p)
(list fn p))]))