-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmake-semantics.rkt
143 lines (127 loc) · 4.94 KB
/
make-semantics.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
#lang racket
(require (only-in racket/struct make-constructor-style-printer))
(require (only-in mystery-languages/utils observe))
(require mystery-languages/common)
(provide ML-error? ML-error-ex ML-okay? ML-okay-val)
(struct ML-error (ex) #:transparent
#:property prop:custom-print-quotable 'never
#:methods gen:custom-write
[(define write-proc
(make-constructor-style-printer
(lambda (obj) 'ML-error)
(lambda (obj) (list (exn-message (ML-error-ex obj))))))])
(struct ML-okay (val) #:transparent)
(require racket/pretty)
(pretty-print-size-hook
(lambda (v display? the-port)
(if (procedure? v)
(string-length "#<procedure>")
#f)))
(pretty-print-print-hook
(lambda (v display? the-port)
(display "#<procedure>" the-port)))
(port-write-handler (current-output-port) (lambda (v port) (pretty-write v port #:newline? #f)))
(port-display-handler (current-output-port) (lambda (v port) (pretty-display v port #:newline? #f)))
(port-print-handler (current-output-port) (lambda (v port) (pretty-print v port #:newline? #f)))
(require rackunit)
(provide make-namespaces-and-lang-print-names show-output test-output)
(define (make-namespaces-and-lang-print-names language-specs)
(define lpns
(foldr (λ (e past)
(cons (string-append "L" (number->string e) ": ")
past))
empty
(range 1 (add1 (length language-specs)))))
(define nss (map (λ (_) (make-base-empty-namespace)) language-specs))
(for-each (λ (ls ns)
(parameterize ([current-namespace ns])
(namespace-require ls)))
language-specs nss)
(values nss lpns))
(define (run/okay-or-error expr n)
(with-handlers ([exn? (λ (ex) (ML-error ex))])
(ML-okay (observe (eval expr n)))))
(define (run-multiple e ns)
(map (λ (n) (run/okay-or-error e n)) ns))
(define (test-multiple e ns cs)
(map (λ (n c)
(c (thunk (observe (eval e n)))))
ns cs))
(define (show-output e namespaces lang-print-names)
(writeln e)
(let ([results (run-multiple e namespaces)])
(unless (andmap (λ (r)
(and (ML-okay? r) (void? (ML-okay-val r))))
results)
(for-each (λ (r lpn)
(display lpn)
(flush-output)
(cond
[(ML-okay? r) (writeln (ML-okay-val r))]
[(ML-error? r)
(let ([the-exn (ML-error-ex r)])
((error-display-handler) (exn-message the-exn) #f))]
[else (error 'multi-runner "shouldn't have gotten here: ~a" r)]))
results lang-print-names)))
(newline)
(flush-output))
(define (test-output e expecteds namespaces)
(display "••••• TESTING ") (write e) (displayln " (blank if all tests pass)")
(flush-output)
(unless (= (length namespaces) (length expecteds))
(error 'TEST "number of results ~a does not match number of result terms ~a"
(length namespaces)
(length expecteds)))
(test-multiple e namespaces (map checker-of-expected expecteds))
(newline))
(define ((checker-of-expected expected) do)
(define (make-checker neg? expected)
(match expected
[`(not ,expected)
(make-checker (not neg?) expected)]
[(or 'failure 'error)
(if neg?
(check-not-exn do)
(check-exn any/c do))]
[(or 'void 'void?)
(if neg?
(check-not-equal? (do) (void))
(check-equal? (do) (void)))]
[(or 'procedure 'procedure?)
(if neg?
(check-pred (not/c procedure?) (do) "expecting a non-procedure")
(check-pred procedure? (do) "expecting a procedure"))]
[(or 'number 'number?)
(if neg?
(check-pred (not/c number?) (do) "expecting a non-number")
(check-pred number? (do) "expecting a number"))]
[(or 'boolean 'boolean?)
(if neg?
(check-pred (not/c boolean?) (do) "expecting a non-boolean")
(check-pred boolean? (do) "expecting a boolean"))]
[(or 'string 'string?)
(if neg?
(check-pred (not/c string?) (do) "expecting a non-string")
(check-pred string? (do) "expecting a string"))]
[(or 'object 'object?)
(if neg?
(check-pred (not/c an-object?) (do) "expecting a non-object")
(check-pred an-object? (do) "expecting an object"))]
[val
#:when ((or/c number? string? boolean? char?) val)
(if neg?
(check-not-equal? (do) val)
(check-equal? (do) val))]
[other
(fail (format "~a is not a valid way to specify the result. Please check the documentation" other))]))
(with-check-info*
(list
(make-check-location
(list
(syntax-source expected)
(syntax-line expected)
(syntax-column expected)
(syntax-position expected)
(syntax-span expected))))
(thunk
(make-checker #f (syntax->datum expected)))))