-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy path40.scm
306 lines (254 loc) · 9.71 KB
/
40.scm
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
(load-relative "../libs/init.scm")
(load-relative "./base/typed-oo/lang.scm")
(load-relative "./base/typed-oo/test.scm")
(load-relative "./base/typed-oo/store.scm")
(load-relative "./base/typed-oo/interp.scm")
(load-relative "./base/typed-oo/checker.scm")
(load-relative "./base/typed-oo/environments.scm")
(load-relative "./base/typed-oo/classes.scm")
(load-relative "./base/typed-oo/static-classes.scm")
(load-relative "./base/typed-oo/data-structures.scm")
(load-relative "./base/typed-oo/static-data-structures.scm")
(load-relative "./base/typed-oo/tests.scm")
;; see new stuff
;; Modify the design of the language so that every field declaration
;; contains an expression that is used to initialize the field.
;; Such a design has the advantage that a checked program will never refer
;; to an uninitialized value.
;;;;;;;;;;;;;;;; grammatical specification ;;;;;;;;;;;;;;;;
(define the-lexical-spec
'((whitespace (whitespace) skip)
(comment ("%" (arbno (not #\newline))) skip)
(identifier
(letter (arbno (or letter digit "_" "-" "?")))
symbol)
(number (digit (arbno digit)) number)
(number ("-" digit (arbno digit)) number)
))
(define the-grammar
'((program ((arbno class-decl) expression) a-program)
(expression (number) const-exp)
(expression
("-" "(" expression "," expression ")")
diff-exp)
(expression
("+" "(" expression "," expression ")")
sum-exp)
(expression
("zero?" "(" expression ")")
zero?-exp)
(expression
("if" expression "then" expression "else" expression)
if-exp)
(expression (identifier) var-exp)
(expression
("let" (arbno identifier "=" expression) "in" expression)
let-exp)
(expression
("proc" "(" (separated-list identifier ":" type ",") ")" expression)
proc-exp)
(expression
("(" expression (arbno expression) ")")
call-exp)
(expression
("letrec"
(arbno type identifier "(" (separated-list identifier ":" type ",") ")"
"=" expression)
"in" expression)
letrec-exp)
(expression
("begin" expression (arbno ";" expression) "end")
begin-exp)
(expression
("set" identifier "=" expression)
assign-exp)
;; non-empty lists for typechecked version
(expression
("list" "(" expression (arbno "," expression) ")" )
list-exp)
;; new productions for oop
(class-decl
("class" identifier
"extends" identifier
(arbno "implements" identifier)
(arbno "field" type identifier "=" expression)
(arbno method-decl)
)
a-class-decl)
(method-decl
("method" type identifier
"(" (separated-list identifier ":" type ",") ")" ; method formals
expression
)
a-method-decl)
(expression
("new" identifier "(" (separated-list expression ",") ")")
new-object-exp)
;; this is special-cased to prevent it from mutation
(expression
("self")
self-exp)
(expression
("send" expression identifier
"(" (separated-list expression ",") ")")
method-call-exp)
(expression
("super" identifier "(" (separated-list expression ",") ")")
super-call-exp)
;; new productions for typed-oo
(class-decl
("interface" identifier (arbno abstract-method-decl))
an-interface-decl)
(abstract-method-decl
("method" type identifier
"(" (separated-list identifier ":" type ",") ")" )
an-abstract-method-decl)
(expression
("cast" expression identifier)
cast-exp)
(expression
("instanceof" expression identifier)
instanceof-exp)
(type ("int") int-type)
(type ("bool") bool-type)
(type ("void") void-type)
(type
("(" (separated-list type "*") "->" type ")")
proc-type)
(type
("listof" type)
list-type)
(type (identifier) class-type) ;; new for typed oo
))
;;;;;;;;;;;;;;;; sllgen boilerplate ;;;;;;;;;;;;;;;;
(sllgen:make-define-datatypes the-lexical-spec the-grammar)
(define show-the-datatypes
(lambda () (sllgen:list-define-datatypes the-lexical-spec the-grammar)))
(define scan&parse
(sllgen:make-string-parser the-lexical-spec the-grammar))
(define just-scan
(sllgen:make-string-scanner the-lexical-spec the-grammar))
;; new stuff
(define check-type!
(lambda (ty1 ty2)
(if (equal? ty1 ty2)
#t
(error 'check-type!
"Type didn't match: ~s != ~s"
ty1 ty2))))
;; new stuff
(define check-for-field!
(lambda (f-types f-exps tenv)
(letrec ((check-equal-type-list
(lambda (l1 l2)
(if (null? l1)
(null? l2)
(begin
(check-type!
(car l1) (car l2))
(check-equal-type-list (cdr l1) (cdr l2)))))))
(let ((f-exp-types (map (lambda(e) (type-of e tenv))
f-exps)))
(check-equal-type-list f-types f-exp-types)))))
;;;;;;;;;;;;;;;; syntactic operations on types ;;;;;;;;;;;;;;;;
(define add-class-decl-to-static-class-env!
(lambda (c-decl)
(cases class-decl c-decl
(an-interface-decl (i-name abs-m-decls)
(let ((m-tenv
(abs-method-decls->method-tenv abs-m-decls)))
(check-no-dups! (map car m-tenv) i-name)
(add-static-class-binding!
i-name (an-interface m-tenv))))
(a-class-decl (c-name s-name i-names
f-types f-names f-exps m-decls)
(let ((i-names
(append
(static-class->interface-names
(lookup-static-class s-name))
i-names))
(f-names
(append-field-names
(static-class->field-names
(lookup-static-class s-name))
f-names))
(f-types
(append
(static-class->field-types
(lookup-static-class s-name))
f-types))
(method-tenv
(let ((local-method-tenv
(method-decls->method-tenv m-decls)))
(check-no-dups!
(map car local-method-tenv) c-name)
(merge-method-tenvs
(static-class->method-tenv
(lookup-static-class s-name))
local-method-tenv))))
(check-no-dups! i-names c-name)
(check-no-dups! f-names c-name)
(check-for-initialize! method-tenv c-name)
;;new stuff
(check-for-field! f-types f-exps method-tenv)
(add-static-class-binding! c-name
(a-static-class
s-name i-names f-names f-types method-tenv)))))))
;; initialize-class-decl! : ClassDecl -> Unspecified
(define initialize-class-decl!
(lambda (c-decl)
(cases class-decl c-decl
;; interfaces don't affect runtime
(an-interface-decl (interface-name method-decls) '())
;; new stuff
(a-class-decl (class-name super-name interface-names field-types field-names field-exps method-decls)
(let ((field-names
(append-field-names
(class->field-names (lookup-class super-name))
field-names)))
(add-to-class-env!
class-name
(a-class
super-name
field-names
(merge-method-envs
(class->method-env (lookup-class super-name))
(method-decls->method-env
method-decls super-name field-names)))))))))
;; check-class-decl! : ClassDecl -> Unspecified
(define check-class-decl!
(lambda (c-decl)
(cases class-decl c-decl
(an-interface-decl (i-name abs-method-decls)
#t)
(a-class-decl (class-name super-name i-names
field-types field-names field-exps method-decls)
(let ((sc (lookup-static-class class-name)))
(for-each
(lambda (method-decl)
(check-method-decl! method-decl
class-name super-name
(static-class->field-names sc)
(static-class->field-types sc)))
method-decls))
(for-each
(lambda (i-name)
(check-if-implements! class-name i-name))
i-names)
))))
(run " class c1 extends object
field int val1 = 1
field bool val2 = zero?(1)
method int initialize() 1
method int m1()1
let o1 = new c1()
in send o1 m1()")
(check " class c1 extends object
field int val1 = 1
field bool val2 = zero?(1) % this will report a error when val2 initialize with int
method int initialize() 1
method int m1() 1
let o1 = new c1()
in send o1 m1()")
;; run-all, check-all will have some fail cases
;; because modification on lang