/
types.pir
386 lines (311 loc) · 6.15 KB
/
types.pir
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
.HLL 'Arc'
.namespace [ ]
.sub '_types_init' :anon :init :load
## Cons cell
$P0 = subclass 'ArcCons', 'Cons'
## Nil & T
$P0 = new 'ArcNil'
set_hll_global 'nil', $P0
$P0 = new 'ArcT'
set_hll_global 't', $P0
## Annotated
$P0 = subclass 'Array', 'Tagged'
## hash
$P0 = subclass 'Hash', 'ArcHash'
## I/O ports
$P0 = newclass 'Inport'
addattribute $P0, 'stream'
## default input port
$P0 = new 'Inport'
$P1 = getstdin
setattribute $P0, 'stream', $P1
set_hll_global 'stdin*', $P0
$P0 = newclass 'Outport'
addattribute $P0, 'stream'
## default output port
$P0 = new 'Outport'
$P1 = getstdout
setattribute $P0, 'stream', $P1
set_hll_global 'stdout*', $P0
## default error port
$P0 = new 'Outport'
$P1 = getstderr
setattribute $P0, 'stream', $P1
set_hll_global 'stderr*', $P0
$P0 = newclass 'Eof'
$P0 = newclass 'Socketport'
addattribute $P0, 'fd'
## threading
$P0 = subclass 'ParrotThread', 'Thread'
.return ()
.end
.namespace ['ArcHash']
.sub 'name' :vtable
.return ("hash")
.end
.sub 'to_string' :method
.return ("#hash()")
.end
.namespace ['Cons']
.sub 'name' :vtable :method
.return ("cons")
.end
.sub 'to_string' :method
.local string str
.local pmc nil
str = "("
$P0 = self.'car'()
$S0 = $P0.'to_string'()
str .= $S0
$P0 = self # $P0 holds current cons cell
nil = get_hll_global 'nil'
cdr_to_str:
$P1 = $P0.'cdr'()
$I3 = issame $P1, nil
if $I3 goto end # check if the list is finished
$S1 = typeof $P1 # type of the cdr
if $S1 == 'cons' goto to_list # is it another cons?
str .= " . "
$P2 = $P0.'cdr'()
$S0 = $P2.'to_string'()
str .= $S0 # add the non-cons object and finish
goto end
to_list:
str .= " "
$P0 = $P0.'cdr'() # advance to next cons cell
$P2 = $P0.'car'()
$S0 = $P2.'to_string'()
str .= $S0 # add the car
goto cdr_to_str
end:
str .= ")"
.return (str)
.end
.namespace ['Tagged']
.sub 'to_string' :method
$S0 = "#3(tagged "
$P0 = self[0]
$S1 = $P0.'to_string'()
$S0 .= $S1
$S0 .= " "
$P0 = self[1]
$S1 = $P0.'to_string'()
$S0 .= $S1
$S0 .= ")"
.return ($S0)
.end
.namespace ['Inport']
.sub 'get_string' :method
.return ("#<input port>")
.end
## compatibility with ReadStream
.sub 'is_eof' :method
$P0 = getattribute self, 'stream'
$I0 = $P0.'eof'()
if $I0 goto true
.return (0)
true:
.return (1)
.end
.sub 'peek1' :method
$P0 = getattribute self, 'stream'
$S0 = peek $P0
$I0 = $P0.'eof'()
if $I0 goto end
.return ($S0)
end:
$P0 = get_hll_global 'nil'
.return ($P0)
.end
.sub 'get1' :method
$P0 = getattribute self, 'stream'
$S0 = $P0.'read'(1)
$I0 = $P0.'eof'()
if $I0 goto end
.return ($S0)
end:
$P0 = get_hll_global 'nil'
.return ($P0)
.end
.namespace ['Outport']
.sub 'to_string' :method
.return ("#<output port>")
.end
.namespace ['Eof']
.sub 'to_string' :method
.return ("#<eof>")
.end
.namespace ['Socketport']
.sub 'to_string' :method
.return ("#<socket>")
.end
.namespace ['Thread']
.sub 'to_string' :method
.return ("#<thread>")
.end
## functions accessible to the user
.namespace [ ]
.sub 'cons'
.param pmc car
.param pmc cdr
$P0 = new 'Cons'
$P0.'scar'(car)
$P0.'scdr'(cdr)
.return ($P0)
.end
.sub 'car'
.param pmc cell
$P0 = cell.'car'()
.return ($P0)
.end
.sub 'scar'
.param pmc cell
.param pmc val
cell.'scar'(val)
.return (val)
.end
.sub 'cdr'
.param pmc cell
$P0 = cell.'cdr'()
.return ($P0)
.end
.sub 'scdr'
.param pmc cell
.param pmc val
cell.'scdr'(val)
.return (val)
.end
.sub 'list'
.param pmc elems :slurpy
.local pmc res
.local pmc last
.local pmc iter
.local pmc nil
nil = get_hll_global 'nil'
last = nil
res = nil
iter = new 'Iterator', elems
start:
unless iter goto end
$P0 = shift iter
$I0 = issame last, nil
if $I0 goto first
$P0 = cons($P0, nil)
scdr(last, $P0)
last = $P0
goto start
first:
last = cons($P0, nil)
res = last
goto start
end:
.return (res)
.end
.sub '_list_to_array'
.param pmc lst
.local pmc res
.local pmc nil
res = new 'ResizablePMCArray'
nil = get_hll_global 'nil'
loop:
$I0 = issame lst, nil
if $I0 goto end
$P0 = car(lst)
push res, $P0
lst = cdr(lst)
goto loop
end:
.return (res)
.end
## there is no plist in Arc...
.sub 'plist'
.param pmc sym
$P0 = sym[1]
.return ($P0)
.end
.sub 'annotate'
.param pmc type
.param pmc rep
$P0 = new 'Tagged'
$P0 = 2
$P0[0] = type
$P0[1] = rep
.return ($P0)
.end
.sub 'rep'
.param pmc annotation
$P0 = annotation[1]
.return ($P0)
.end
.sub 'table'
$P0 = new 'ArcHash'
.return ($P0)
.end
.sub 'sref' :multi(ArcHash)
.param pmc h
.param pmc val
.param pmc key
$S0 = typeof key
unless $S0 == 'string' goto tostring
$S0 = "\""
$S1 = key
$S0 .= $S1
$S0 .= "\""
goto go
tostring:
$S0 = key.'to_string'()
go:
h[$S0] = val
.return (val)
.end
.sub 'sref' :multi(ArcStr)
.param pmc str
.param pmc val
.param pmc ind
$S0 = typeof val
unless $S0 == 'char' goto type_err
$S0 = typeof ind
unless $S0 == 'int' goto type_err2
$S0 = val.'pr_repr'()
str[ind] = $S0
.return (val)
type_err:
.tailcall 'err'("Wrong type passed as value to sref (string)")
type_err2:
.tailcall 'err'("Wrong type passed as index to sref (string)")
.end
.sub 'sref' :multi(Cons)
.param pmc cell
.param pmc val
.param pmc ind
$S0 = typeof ind
unless $S0 == 'int' goto type_err
.local pmc nil
$I0 = ind
if $I0 < 0 goto neg_index
nil = get_hll_global 'nil'
loop:
$I1 = issame cell, nil
if $I1 goto too_large
if $I0 == 0 goto end
cell = cdr(cell)
$I0 -= 1
goto loop
end:
.tailcall scar(cell, val)
neg_index:
.tailcall 'err'("Negative index!")
too_large:
.tailcall 'err'("Index too large!")
type_err:
.tailcall 'err'("Wrong type passed as index to sref (cons)")
.end
.sub 'type'
.param pmc what
$S0 = typeof what
if $S0 == 'Tagged' goto tagged
.tailcall 'intern'($S0)
tagged:
$P0 = what[0]
.return ($P0)
.end