/
NQPSetting.pm
389 lines (335 loc) · 7.82 KB
/
NQPSetting.pm
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
387
388
389
knowhow NQPStr is repr('P6str') {
method new() {
nqp::instance_of(self.WHAT);
}
method Bool() {
nqp::logical_not_int(nqp::equal_strs(self, "")) &&
nqp::logical_not_int(nqp::equal_strs(self, "0"))
}
method Int() {
nqp::coerce_str_to_int(self, NQPInt)
}
method Num() {
nqp::coerce_str_to_num(self, NQPNum)
}
method Numeric() {
nqp::coerce_str_to_num(self, NQPNum)
}
method Str() {
self
}
method Stringy() {
self
}
method defined() {
nqp::repr_defined(self)
}
}
knowhow NQPInt is repr('P6int') {
method new() {
nqp::instance_of(self.WHAT);
}
method Bool() {
nqp::logical_not_int(nqp::equal_ints(self, 0))
}
method Int() {
self
}
method Num() {
nqp::coerce_int_to_num(self, NQPNum)
}
method Numeric() {
self
}
method Str() {
nqp::coerce_int_to_str(self, NQPStr)
}
method Stringy() {
nqp::coerce_int_to_str(self, NQPStr)
}
method defined() {
nqp::repr_defined(self)
}
}
knowhow NQPNum is repr('P6num') {
method new() {
nqp::instance_of(self.WHAT);
}
method Bool() {
nqp::logical_not_int(nqp::equal_nums(self, 0.0))
}
method Int() {
nqp::coerce_num_to_int(self, NQPStr)
}
method Num() {
self
}
method Numeric() {
self
}
method Str() {
nqp::coerce_num_to_str(self, NQPStr)
}
method Stringy() {
nqp::coerce_num_to_str(self, NQPStr)
}
method defined() {
nqp::repr_defined(self)
}
}
knowhow NQPList is repr('P6list') {
method new() {
nqp::instance_of(self.WHAT)
}
method elems() {
nqp::lllist_elems(self)
}
method Numeric() {
self.elems
}
method at_pos($idx) {
nqp::lllist_get_at_pos(self, $idx.Int)
}
method defined() {
nqp::repr_defined(self)
}
}
knowhow NQPArray is repr('P6list') {
method new() {
nqp::instance_of(self.WHAT)
}
method Numeric() {
self.elems
}
method elems() {
nqp::lllist_elems(self)
}
method at_pos($idx) {
nqp::lllist_get_at_pos(self, $idx.Int)
}
method bind_pos($idx, $value) {
nqp::lllist_bind_at_pos(self, $idx.Int, $value)
}
method defined() {
nqp::repr_defined(self)
}
}
knowhow NQPHash is repr('P6mapping') {
method new() {
nqp::instance_of(self.WHAT)
}
method Numeric() {
self.elems
}
method elems() {
nqp::llmapping_elems(self)
}
method at_key($key) {
nqp::llmapping_get_at_key(self, $key.Str)
}
method bind_key($key, $value) {
nqp::llmapping_bind_at_key(self, $key.Str, $value)
}
method defined() {
nqp::repr_defined(self)
}
}
knowhow NQPCode is repr('RakudoCodeRef') {
method leave($with) {
nqp::leave_block(self, $with)
}
method defined() {
nqp::repr_defined(self)
}
}
## XXX Need coercive Any fallbacks too.
proto sub &infix:<==>($x, $y) { * }
multi sub &infix:<==>(NQPInt $x, NQPInt $y) {
nqp::equal_ints($x, $y)
}
multi sub &infix:<==>(NQPNum $x, NQPNum $y) {
nqp::equal_nums($x, $y)
}
proto sub &infix:<!=>($x, $y) { * }
multi sub &infix:<!=>(NQPInt $x, NQPInt $y) {
nqp::logical_not_int(nqp::equal_ints($x, $y))
}
multi sub &infix:<!=>(NQPNum $x, NQPNum $y) {
nqp::logical_not_int(nqp::equal_nums($x, $y))
}
sub &infix:<eq>($x, $y) {
nqp::equal_strs($x.Str, $y.Str)
}
sub &infix:<ne>($x, $y) {
!nqp::equal_strs($x.Str, $y.Str)
}
sub &infix:<=:=>($x, $y) {
nqp::equal_refs($x, $y)
}
sub &prefix:<!>($x) {
nqp::logical_not_int($x.Bool)
}
sub &prefix:<?>($x) {
$x.Bool
}
sub &prefix:<~>($x) {
$x.Stringy
}
sub &prefix:<+>($x) {
$x.Numeric
}
proto sub &infix:<+>($x, $y) { * }
multi sub &infix:<+>(NQPInt $x, NQPInt $y) {
nqp::add_int($x, $y);
}
proto sub &infix:<->($x, $y) { * }
multi sub &infix:<->(NQPInt $x, NQPInt $y) {
nqp::sub_int($x, $y);
}
proto sub &infix:<*>($x, $y) { * }
multi sub &infix:<*>(NQPInt $x, NQPInt $y) {
nqp::mul_int($x, $y);
}
proto sub &infix:</>($x, $y) { * }
multi sub &infix:</>(NQPInt $x, NQPInt $y) {
nqp::div_int($x, $y);
}
proto sub &infix:<%>($x, $y) { * }
multi sub &infix:<%>(NQPInt $x, NQPInt $y) {
nqp::mod_int($x, $y);
}
sub &infix:<~>($x, $y) {
nqp::concat($x.Str, $y.Str);
}
# A basic, fairly bare-bones exception object.
knowhow NQPException {
has $!message;
has $!resumable;
method new($message) {
nqp::instance_of(self.WHAT).BUILD(:message($message))
}
method BUILD(:$message) {
$!message := $message;
$!resumable := 0;
self;
}
method defined() {
nqp::repr_defined(self)
}
method resumable() {
$!resumable
}
method resume() {
$!resumable := 1;
}
method Str() {
$!message
}
}
sub die($message) {
nqp::throw_dynamic(NQPException.new($message), 0)
}
# For tests.
my $count := NQPInt.new();
sub plan($n) {
print("1..");
say($n);
}
sub ok($check, $diag?) {
$count := $count + 1;
unless $check { print("not ") }
print("ok ");
say($count);
}
# Here comes the start of a heavily under construction ClassHOW.
knowhow NQPClassHOW {
##
## Attributes
##
# Name of the class.
has $!name;
# Attributes, methods, parents and roles directly added.
has %!attributes;
has %!methods;
has @!parents;
has @!roles;
# Vtable and mapping of method names to slots.
has @!vtable;
has %!method-vtable-slots;
# Have we been composed?
has $!composed;
# Cached MRO (list of the type objects).
has @!mro;
# Full list of roles that we do.
has @!done;
##
## Declarative.
##
# Creates a new instance of this meta-class.
method new() {
nqp::instance_of(self).BUILD()
}
method BUILD() {
$!composed := 0;
%!methods := NQPHash.new;
self;
}
# Create a new meta-class instance, and then a new type object
# to go with it, and return that.
# XXX TODO :$repr named parameter defaulting to P6opaque (don't
# have default values yet implemented).
method new_type() {
my $metaclass := self.new();
nqp::type_object_for($metaclass, 'P6opaque');
}
method add_method($obj, $name, $code_obj) {
# XXX TODO This crashes due to lack of auto-viv. :/
#if %!methods{$name} {
# die("This class already has a method named " ~ $name);
#}
%!methods{$name} := $code_obj;
}
method compose($obj) {
# XXX TODO: Compose roles, compose attributes.
# Some things we only do if we weren't already composed once, like
# building the MRO.
unless $!composed {
@!mro := compute_c3_mro($obj);
$!composed := 1;
}
$obj
}
# XXX TODO: Get enough working to bring over the C3 implementation that
# we run on 6model on Parrot.
sub compute_c3_mro($obj) {
my @mro;
@mro[0] := $obj;
@mro;
}
##
## Introspecty
##
method method_table($obj) {
%!methods
}
method defined() {
nqp::repr_defined(self)
}
##
## Dispatchy
##
method find_method($obj, $name) {
# XXX TODO Epic cheat, replace with that's in the ClassHOW that we
# run on Parrot. Needs auto-viv, .defined, return.
my %meths := @!mro[0].HOW.method_table($obj);
%meths{$name}
}
}
# XXX Bad hack, we'll replace this later.
knowhow Any {
}
# GLOBAL stash.
# (XXX Really want one per compilation unit and unify, but this will get us
# started. Also really want a stash type that knows its name rather than just
# a hash, I guess.)
::GLOBAL := NQPHash.new();