-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfy.icn
307 lines (277 loc) · 8 KB
/
cfy.icn
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
307
#
# cfy - undocumented gobbledy gook that we have to reverse engineer.
#
#
procedure cfy(ifname, ofname)
local fi, fo, nvars, ctor, stem
local i, s, m, n, r1, r2, numargs, prev
dbg := 1
fi := open(ifname, "r") | {
write("cfy: cannot access \"", ifname, "\".")
fail
}
fo := open(ofname, "w") | {
write("cfy: cannot access \"", ofname, "\".")
close(fi)
fail
}
while s := read(fi) do {
if not (cfy_match_nonws(s, "record"||(" "|"\t"))) then {
write(fo, s)
next
}
else if trim(s,,0) == "record" then {
write(&errout, "cfy: needs a better parser for multiline record decls")
close(fi); close(fo)
fail
}
if not (r1 := cfy_recdecl_parse(s)) then {
write(&errout, "cfy: can't parse (state vector expected): ", image(s))
close(fi); close(fo)
fail
}
if not (cfy_is_classrec(r1)) then {
write(fo, s)
next
}
# have __state; read __methods.
s := read(fi)
if not (r2 := cfy_recdecl_parse(s)) then {
write(&errout, "cfy: can't parse (methods vector expected): ",image(s))
close(fi); close(fo)
fail
}
stem := r1.name[1:-7]
# pop var __s off of r1.flds
pop(r1.flds)
# get the number of vars in this class
varc := *r1.flds - 1 # omit __s in count of vars
# get the number of methods in this class
methodc := *r2.flds
if \dbg then {
write(fo, "# ", stem, "_mdw_inst_mdw: varc: ", varc)
every i := 1 to varc do
write(fo, "# var ", i-1, ": ", r1.flds[i])
write(fo, "# ", stem, "_mdw_inst_mdw: methodc: ", methodc)
every i := 1 to methodc do
write(fo, "# method ", i-1, ": ", r2.flds[i])
}
#
# merge __state and __methods recs into r1
#
r1.xform := (r1.name[1:-5] || "mdw_inst_mdw")
pop(r1.flds) # discard __s
nvars := *r1.flds
every i := 1 to *r2.flds do
put(r1.flds, pop(r2.flds))
#
# emit the xxx_methods rec-decl so that the
# xxxinitialize method can use it to init xxx_oprec.
#
write(fo, s)
#
# emit xformed instance rec-decl
#
if \dbg then
write(fo, "# ", stem, "_mdw_inst_mdw: *r1.flds: ", *r1.flds)
writes(fo, "record " || r1.xform || "(")
every i := 1 to *r1.flds do {
writes(fo, r1.flds[i])
if i < *r1.flds then
writes(fo, ",")
}
write(fo, ")")
#
# skip oprec defn
#
write(fo, read(fi))
#
# get the ctor signature
#
while ctor := read(fi) do {
if i := cfy_match_nonws(ctor, "procedure ") then
break
write(fo, ctor)
}
write(fo, ctor); # emit untainted ctor signature
ctor := ctor[find("(", ctor)+1:-1] # peel off what we need
#
# determine how many args are in the ctor
#
s := ctor;
ctorargc := 0
while i := find(",", s) do {
ctorargc +:= 1
s := s[i+1:0]
}
if (ctorargc > 0) | (*ctor > 1) then
ctorargc +:= 1
if \dbg then
write(fo, "# ", stem, "_mdw_inst_mdw: ctor-argc: ", ctorargc)
#
# read, save, and emit all ctor lines appearing
# in the initial clause.
#
ctorinitlines := list()
while s := read(fi) do {
put(ctorinitlines, s)
write(fo, s)
if cfy_match_nonws(s, "}") then
break
}
if \dbg then
write(fo, "# ", stem, "_mdw_inst_mdw: varc: ", varc, " nvars: ", nvars,
" r1.flds: ", *r1.flds)
#
# read all ctor lines (past initial clause) and save them
#
ctorlines := list()
while s := read(fi) do {
if cfy_match_nonws(s, "end") then
break
put(ctorlines, s)
}
put(ctorlines, s)
#
# read all xxxinitialize() lines and save them
#
initlines := list()
while s := read(fi) do {
if cfy_match_nonws(s, "end") then
break
put(initlines, s)
}
put(initlines, s)
#
# extract any base-class initializers from the ctor's initial clause
#
bcinits := cfy_baseclass_inits_get(cfy_classname(r1), ctorinitlines)
if \dbg then
write(fo, "# class ", cfy_classname(r1), " has ", *bcinits,
" bc-inits.")
#
# xform "self := xxx_state(...)"
#
s := ctorlines[1]
m := find("__state", s)
writes(fo, s[1:m], "__mdw_inst_mdw(")
# use state-rec ctor to init inst-rec ctor vars...
numargs := 0
if m := find("__oprec,", s) then {
# count num args in the state-rec ctor
numargs := cfy_numargs(s[m+8:-1])
# emit the vars from the state-rec ctor
writes(fo, s[m+8:-1])
}
every i := numargs to varc-1 do
writes(fo, ",")
# get the methods to init the inst-rec ctor
# from the xxxinitialize() call...
s := initlines[3]
m := find("(", s)
if numargs >= 1 then
writes(fo, ",")
write(fo, s[m+1:0])
write(fo, "# numargs: ", numargs, " varc: ", varc);
#
# initialize any baseclass instance vars found
#
every i := 1 to *bcinits do {
s := bcinits[i]
write(fo, " self." || s || " := " || s || "__oprec")
}
#
# xform "self.__m.initially()..."
#
s := ctorlines[3]
m := find("__m.", s)
s[m:m+4] := ""
write(fo, s)
#
# write rest of ctor
#
write(fo, ctorlines[4])
write(fo, ctorlines[5])
#
# emit untainted xxxinitialize()
#
every i := 1 to *initlines do
write(fo, initlines[i])
}
close(fi)
close(fo)
return
end
procedure cfy_baseclass_inits_get(clsnm, lines)
local i, n, s, t, rslt;
rslt := []
every i := 1 to *lines do {
s := lines[i]
s ? {
tab(many(" \t"))
if n := match(clsnm || "__oprec.") then {
t := s[find(".")+1 : find(":=")]
put(rslt, trim(t))
}
}
}
return rslt
end
procedure cfy_classname(decl)
if decl.name[-7:0] == "__state" then
return decl.name[1:-7]
if decl.name[-9:0] == "__methods" then
return decl.name[1:-9]
end
procedure cfy_ctor_has_initially(lines)
every i := 1 to *lines do {
if cfy_match_nonws(lines[i], "self.__m.initially(", lines[i]) then
return
}
end
procedure cfy_is_classrec(decl)
if (decl.name[-7:0] == "__state") | (decl.name[-9:0] == "__methods") then
return
end
procedure cfy_match_nonws(sub, pat)
static ws
initial ws := ' \f\n\r\t'
&subject := sub
tab(many(ws))
return match(pat)
end
procedure cfy_numargs(s)
local n, rslt, i
rslt := 0
every i := find(",", s) do rslt +:= 1
s := s[\i+1:0]
s := trim(s, , 0)
while *s > 0 do {
if member(&letters, s[1]) then {
rslt +:= 1
break
}
pop(s)
}
return rslt
end
procedure cfy_recdecl_parse(s)
local m, n, flds, rslt
flds := []
rslt := recdecl()
if not (cfy_match_nonws(s, "record")) then {
fail
}
m := find(" ", s)
n := find("(", s)
rslt.name := s[\m+1:\n]
s := s[\n+1:0]
while m := find(",", s) do {
put(flds, s[1:m])
s := s[\m+1:0]
}
m := find(")", s)
put(flds, s[1:m])
rslt.flds := flds
return rslt
end