-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.rkt
177 lines (147 loc) · 4.18 KB
/
test.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
#lang typed/racket/base
(require
racket/match
typed/rackunit
"scope.rkt"
"core-lang.rkt"
"scanner.rkt"
"env.rkt"
"eval.rkt"
"expander.rkt"
"transformers.rkt"
"initial-state.rkt"
(submod "scanner.rkt" test)
(submod "parser.rkt" test)
(submod "eval.rkt" test)
(submod "transformers.rkt" test)
)
(define-values (initial-eval-env initial-expand-env initial-state)
(make-default-initial-state
#:expand expand
#:quote quote-transform
#:syntax syntax-transform
#:lambda fun-transform
#:let-syntax let-syntax-transform))
;;; Evaluation
(define (eval i)
(define-values (state expanded)
(expand initial-state initial-expand-env (Stx-scan i) #:phase 0 #:prune (empty-SetofScopes)))
(define-values (state* result)
(Ast-eval (CompState-parse state expanded #:phase 0) initial-eval-env state (empty-Env) #f #:phase 0))
result)
(define (check-eval i o)
(check-true (equal? (eval i) (scan o))))
(define (eval-equal? i o)
(check-true (equal? (eval i) (scan o))))
(check eval-equal?
'cons
'#%cons)
(check eval-equal?
'(cons '1 '())
'(1))
(check eval-equal?
'((lambda (y) y) '1)
'1)
(check eval-equal?
'(list-ref '(a b c) '0)
'a)
(check eval-equal?
'(list-ref (list 'a 'b 'c) '0)
'a)
(check eval-equal?
'(car (list '1 '2))
'1)
(check eval-equal?
'(cdr (cons '1 (list '2)))
'(2))
(check eval-equal?
'(cdr (list '1 '2))
'(2))
(check eval-equal?
'(list)
'())
(check eval-equal?
'(((lambda (x) (lambda () x)) '5))
'5)
(check eval-equal?
'((lambda (y) ((lambda (x) y) '0)) '1)
'1)
;;; Macros
;;; NOTE: In 'Macros that Work Together', all functions take one
;;; argument, so the thunk macro may be a little confusing: it takes
;;; one ignored argument.
(check eval-equal?
'(let-syntax thunk (lambda (e)
(mk-stx
(list #'lambda #'(a)
(car (cdr (stx-e e))))
e))
((thunk (+ '1 '2)) '0))
'3)
(check eval-equal?
'(let-syntax thunk (lambda (e)
(mk-stx
(list #'lambda #'(a)
(car (cdr (stx-e e))))
e))
(((lambda (a) (thunk (+ a '1))) '5) '0))
;; Unhygienic answer:
;;'1
;; Hygienic answer:
'6
)
;; Here are the "Scope Examples" from 3.6.1 of *Macros that Work
;; Together*:
(check eval-equal?
'(((lambda (x)
(let-syntax m (lambda (stx) #'x)
(lambda (x) (+ (m) x))))
'1)
'42)
'43)
(check eval-equal?
'(((lambda (x)
(let-syntax n (lambda (stx)
; expand (n e) to (lambda (x) (+ e x))
(mk-stx
(list #'lambda (mk-stx (list #'x) stx)
(mk-stx
(list #'+ (car (cdr (stx-e stx))) #'x)
stx))
stx))
(n '1)))
'1)
'42)
'43)
;; The lvalue primitive should only be available during the
;; application of a macro transformer:
(check-exn #rx"expand: unbound identifier*" (lambda () (eval 'lvalue)))
;; Testing the lvalue primitive:
(check eval-equal?
'((lambda (x)
(let-syntax n #'x
(let-syntax m (lambda (stx) (lvalue #'n))
m)))
'42)
42)
;; The lexpand primitive should only be available during the
;; application of a macro transformer:
(check-exn #rx"expand: unbound identifier*" (lambda () (eval 'lexpand)))
;; Testing the lexpand primitive:
#;(check eval-equal?
'(let-syntax public (lambda (e) (syntax-error))
(let-syntax class (lambda (e)
((lambda (e2) (car (cdr (stx-e e2))))
(lexpand (car (cdr (stx-e e))) (list #'public))))
(class (public '8))))
8)
#;(check-exn
#rx"expand: unbound identifier*"
(lambda ()
(eval
'(let-syntax stop (lambda (e) (car (cdr (stx-e e))))
(let-syntax ex (lambda (e) (lexpand (car (cdr (stx-e e)))
(list #'stop)))
(ex (lambda (x)
(let-syntax arg (lambda (e) #'(stop x))
(arg)))))))))