Skip to content
Newer
Older
100644 662 lines (581 sloc) 15.8 KB
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
1 my knowhow NQPStr is repr('P6str') {
2f806d3 @jnthn Here starteth the NQP setting.
authored
2 method new() {
3 nqp::instance_of(self.WHAT);
4 }
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
5 method Bool() {
3881965 @jnthn Turns out that the always pass ops type object for result type strate…
authored
6 nqp::logical_not_int(nqp::equal_strs(self, "")) &&
7 nqp::logical_not_int(nqp::equal_strs(self, "0"))
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
8 }
ebb368a @jnthn [common] NQPArray.bind_pos plus some missing coercion methods.
authored
9 method Int() {
10 nqp::coerce_str_to_int(self, NQPInt)
11 }
12 method Num() {
13 nqp::coerce_str_to_num(self, NQPNum)
14 }
cf07e79 @jnthn [common] Some initial cuts of prefix + and ~, Stringy and Numeric in …
authored
15 method Numeric() {
16 nqp::coerce_str_to_num(self, NQPNum)
17 }
8c6b5ae @jnthn Add Str methods to setting for NQPNum and NQPInt.
authored
18 method Str() {
3e7ec36 @jnthn A little setting re-ordering; put NQPStr first, since we need strings…
authored
19 self
8c6b5ae @jnthn Add Str methods to setting for NQPNum and NQPInt.
authored
20 }
cf07e79 @jnthn [common] Some initial cuts of prefix + and ~, Stringy and Numeric in …
authored
21 method Stringy() {
22 self
23 }
ac08da5 @jnthn [common] Add .defined methods to the various built-in types.
authored
24 method defined() {
25 nqp::repr_defined(self)
26 }
2f806d3 @jnthn Here starteth the NQP setting.
authored
27 }
28
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
29 my knowhow NQPInt is repr('P6int') {
2f806d3 @jnthn Here starteth the NQP setting.
authored
30 method new() {
31 nqp::instance_of(self.WHAT);
32 }
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
33 method Bool() {
3881965 @jnthn Turns out that the always pass ops type object for result type strate…
authored
34 nqp::logical_not_int(nqp::equal_ints(self, 0))
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
35 }
3e7ec36 @jnthn A little setting re-ordering; put NQPStr first, since we need strings…
authored
36 method Int() {
2f806d3 @jnthn Here starteth the NQP setting.
authored
37 self
38 }
3e7ec36 @jnthn A little setting re-ordering; put NQPStr first, since we need strings…
authored
39 method Num() {
40 nqp::coerce_int_to_num(self, NQPNum)
41 }
cf07e79 @jnthn [common] Some initial cuts of prefix + and ~, Stringy and Numeric in …
authored
42 method Numeric() {
43 self
44 }
3e7ec36 @jnthn A little setting re-ordering; put NQPStr first, since we need strings…
authored
45 method Str() {
46 nqp::coerce_int_to_str(self, NQPStr)
47 }
cf07e79 @jnthn [common] Some initial cuts of prefix + and ~, Stringy and Numeric in …
authored
48 method Stringy() {
49 nqp::coerce_int_to_str(self, NQPStr)
50 }
ac08da5 @jnthn [common] Add .defined methods to the various built-in types.
authored
51 method defined() {
52 nqp::repr_defined(self)
53 }
2f806d3 @jnthn Here starteth the NQP setting.
authored
54 }
55
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
56 my knowhow NQPNum is repr('P6num') {
2f806d3 @jnthn Here starteth the NQP setting.
authored
57 method new() {
58 nqp::instance_of(self.WHAT);
59 }
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
60 method Bool() {
3881965 @jnthn Turns out that the always pass ops type object for result type strate…
authored
61 nqp::logical_not_int(nqp::equal_nums(self, 0.0))
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
62 }
63 method Int() {
64 nqp::coerce_num_to_int(self, NQPStr)
65 }
2f806d3 @jnthn Here starteth the NQP setting.
authored
66 method Num() {
67 self
68 }
cf07e79 @jnthn [common] Some initial cuts of prefix + and ~, Stringy and Numeric in …
authored
69 method Numeric() {
70 self
71 }
8c6b5ae @jnthn Add Str methods to setting for NQPNum and NQPInt.
authored
72 method Str() {
73 nqp::coerce_num_to_str(self, NQPStr)
74 }
cf07e79 @jnthn [common] Some initial cuts of prefix + and ~, Stringy and Numeric in …
authored
75 method Stringy() {
76 nqp::coerce_num_to_str(self, NQPStr)
77 }
ac08da5 @jnthn [common] Add .defined methods to the various built-in types.
authored
78 method defined() {
79 nqp::repr_defined(self)
80 }
2f806d3 @jnthn Here starteth the NQP setting.
authored
81 }
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
82
2fd812d @jnthn [dotnet] Make sure at_pos and at_key never return null, but rather so…
authored
83 # XXX Bad hack, we'll replace this later.
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
84 my knowhow Any {
2fd812d @jnthn [dotnet] Make sure at_pos and at_key never return null, but rather so…
authored
85 method defined() { 0 }
86 }
87
08510b7 @jnthn [common] Add an NQPMapIter that just knows how to eagerly reify a lis…
authored
88 my knowhow NQPMapIter {
89 has $!block;
90 has @!list;
91 method new($block, @list) {
92 my $iter := nqp::instance_of(self.WHAT);
93 $iter.BUILD($block, @list);
94 $iter
95 }
96 method BUILD($block, @list) {
97 $!block := $block;
98 @!list := @list;
99 }
100 method eager() {
101 my $i := 0;
102 my $elems := +@!list;
103 my @result;
104 while $i < $elems {
105 @result.push($!block(@!list[$i]));
106 $i := $i + 1;
107 }
108 @result
109 }
110 }
111
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
112 my knowhow NQPList is repr('P6list') {
7e36dac @jnthn Give NQPList a .new method.
authored
113 method new() {
114 nqp::instance_of(self.WHAT)
115 }
d88a87b @jnthn Flesh out NQPList a little bit.
authored
116 method elems() {
117 nqp::lllist_elems(self)
118 }
ea3a9cb @jnthn [common] Lists and arrays numify to number of elements.
authored
119 method Numeric() {
120 self.elems
121 }
d88a87b @jnthn Flesh out NQPList a little bit.
authored
122 method at_pos($idx) {
2fd812d @jnthn [dotnet] Make sure at_pos and at_key never return null, but rather so…
authored
123 nqp::vivify(nqp::lllist_get_at_pos(self, $idx.Int), Any)
d88a87b @jnthn Flesh out NQPList a little bit.
authored
124 }
ac08da5 @jnthn [common] Add .defined methods to the various built-in types.
authored
125 method defined() {
126 nqp::repr_defined(self)
127 }
08510b7 @jnthn [common] Add an NQPMapIter that just knows how to eagerly reify a lis…
authored
128 method map($block) {
129 NQPMapIter.new($block, self)
130 }
eae223f @jnthn Add a first cut of a P6list representation and a list sub that makes …
authored
131 }
132
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
133 my knowhow NQPArray is repr('P6list') {
7e36dac @jnthn Give NQPList a .new method.
authored
134 method new() {
135 nqp::instance_of(self.WHAT)
136 }
ea3a9cb @jnthn [common] Lists and arrays numify to number of elements.
authored
137 method Numeric() {
138 self.elems
139 }
7e36dac @jnthn Give NQPList a .new method.
authored
140 method elems() {
141 nqp::lllist_elems(self)
142 }
e7e4edb @diakopter [dotnet] implement push, pop, unshift, shift in NQPArray
diakopter authored
143 method pop() {
144 nqp::lllist_pop(self)
145 }
146 method push($item) {
147 nqp::lllist_push(self, $item)
148 }
149 method shift() {
150 nqp::lllist_shift(self)
151 }
152 method unshift($item) {
153 nqp::lllist_unshift(self, $item)
154 }
7e36dac @jnthn Give NQPList a .new method.
authored
155 method at_pos($idx) {
2fd812d @jnthn [dotnet] Make sure at_pos and at_key never return null, but rather so…
authored
156 nqp::vivify(nqp::lllist_get_at_pos(self, $idx.Int), Any)
7e36dac @jnthn Give NQPList a .new method.
authored
157 }
ebb368a @jnthn [common] NQPArray.bind_pos plus some missing coercion methods.
authored
158 method bind_pos($idx, $value) {
159 nqp::lllist_bind_at_pos(self, $idx.Int, $value)
160 }
ac08da5 @jnthn [common] Add .defined methods to the various built-in types.
authored
161 method defined() {
162 nqp::repr_defined(self)
163 }
08510b7 @jnthn [common] Add an NQPMapIter that just knows how to eagerly reify a lis…
authored
164 method map($block) {
165 NQPMapIter.new($block, self)
166 }
eae223f @jnthn Add a first cut of a P6list representation and a list sub that makes …
authored
167 }
168
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
169 my knowhow NQPHash is repr('P6mapping') {
435bd24 @jnthn [common] Stub in NQPHash. Doesn't do anything yet other that .new.
authored
170 method new() {
171 nqp::instance_of(self.WHAT)
172 }
88190a8 @jnthn [common] Fill out NQPHash a bit and...we have basically functioning h…
authored
173 method Numeric() {
174 self.elems
175 }
176 method elems() {
177 nqp::llmapping_elems(self)
178 }
179 method at_key($key) {
2fd812d @jnthn [dotnet] Make sure at_pos and at_key never return null, but rather so…
authored
180 nqp::vivify(nqp::llmapping_get_at_key(self, $key.Str), Any)
88190a8 @jnthn [common] Fill out NQPHash a bit and...we have basically functioning h…
authored
181 }
182 method bind_key($key, $value) {
183 nqp::llmapping_bind_at_key(self, $key.Str, $value)
184 }
ac08da5 @jnthn [common] Add .defined methods to the various built-in types.
authored
185 method defined() {
186 nqp::repr_defined(self)
187 }
435bd24 @jnthn [common] Stub in NQPHash. Doesn't do anything yet other that .new.
authored
188 }
189
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
190 my knowhow NQPCode is repr('RakudoCodeRef') {
3c156f0 @jnthn [common] Add a NQPCode with a leave method.
authored
191 method leave($with) {
192 nqp::leave_block(self, $with)
193 }
ac08da5 @jnthn [common] Add .defined methods to the various built-in types.
authored
194 method defined() {
195 nqp::repr_defined(self)
196 }
3c156f0 @jnthn [common] Add a NQPCode with a leave method.
authored
197 }
198
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
199 ## XXX Need coercive Any fallbacks too.
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
200
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
201 proto sub &infix:<==>($x, $y) { * }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
202 multi sub &infix:<==>(NQPInt $x, NQPInt $y) {
203 nqp::equal_ints($x, $y)
204 }
205 multi sub &infix:<==>(NQPNum $x, NQPNum $y) {
206 nqp::equal_nums($x, $y)
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
207 }
208
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
209 proto sub &infix:<!=>($x, $y) { * }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
210 multi sub &infix:<!=>(NQPInt $x, NQPInt $y) {
dcf5ee5 @jnthn Make our != rather more efficient.
authored
211 nqp::logical_not_int(nqp::equal_ints($x, $y))
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
212 }
213 multi sub &infix:<!=>(NQPNum $x, NQPNum $y) {
dcf5ee5 @jnthn Make our != rather more efficient.
authored
214 nqp::logical_not_int(nqp::equal_nums($x, $y))
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
215 }
216
ee58ecd @diakopter implemented (copypaste) >,>=,<,<= for NQPInts & NQPNums
diakopter authored
217 proto sub &infix:«<=»($x, $y) { * }
218 multi sub &infix:«<=»(NQPInt $x, NQPInt $y) {
219 nqp::less_than_or_equal_ints($x, $y)
220 }
221 multi sub &infix:«<=»(NQPNum $x, NQPNum $y) {
222 nqp::less_than_or_equal_nums($x, $y)
223 }
224
225 proto sub &infix:«<»($x, $y) { * }
226 multi sub &infix:«<»(NQPInt $x, NQPInt $y) {
227 nqp::less_than_ints($x, $y)
228 }
229 multi sub &infix:«<»(NQPNum $x, NQPNum $y) {
230 nqp::less_than_nums($x, $y)
231 }
925f29e @jnthn [weird] Looks like diakopter++'s commits and my git disagree on line …
authored
232
233 proto sub &infix:«>=»($x, $y) { * }
234 multi sub &infix:«>=»(NQPInt $x, NQPInt $y) {
235 nqp::greater_than_or_equal_ints($x, $y)
236 }
237 multi sub &infix:«>=»(NQPNum $x, NQPNum $y) {
238 nqp::greater_than_or_equal_nums($x, $y)
239 }
240
241 proto sub &infix:«>»($x, $y) { * }
242 multi sub &infix:«>»(NQPInt $x, NQPInt $y) {
243 nqp::greater_than_ints($x, $y)
244 }
245 multi sub &infix:«>»(NQPNum $x, NQPNum $y) {
246 nqp::greater_than_nums($x, $y)
247 }
ee58ecd @diakopter implemented (copypaste) >,>=,<,<= for NQPInts & NQPNums
diakopter authored
248
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
249 sub &infix:<eq>($x, $y) {
3881965 @jnthn Turns out that the always pass ops type object for result type strate…
authored
250 nqp::equal_strs($x.Str, $y.Str)
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
251 }
252
253 sub &infix:<ne>($x, $y) {
3881965 @jnthn Turns out that the always pass ops type object for result type strate…
authored
254 !nqp::equal_strs($x.Str, $y.Str)
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
255 }
256
0d13a6c @diakopter [dotnet] implement coercive lt, le, gt, ge; add tests to 10-cmp.t
diakopter authored
257 sub &infix:<ge>($x, $y) {
258 nqp::greater_than_or_equal_strs($x.Str, $y.Str)
259 }
260
261 sub &infix:<gt>($x, $y) {
262 nqp::greater_than_strs($x.Str, $y.Str)
263 }
264
265 sub &infix:<le>($x, $y) {
266 nqp::less_than_or_equal_strs($x.Str, $y.Str)
267 }
268
269 sub &infix:<lt>($x, $y) {
270 nqp::less_than_strs($x.Str, $y.Str)
271 }
272
98e707b @jnthn [common] =:=
authored
273 sub &infix:<=:=>($x, $y) {
274 nqp::equal_refs($x, $y)
275 }
276
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
277 sub &prefix:<!>($x) {
3881965 @jnthn Turns out that the always pass ops type object for result type strate…
authored
278 nqp::logical_not_int($x.Bool)
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
279 }
280
281 sub &prefix:<?>($x) {
282 $x.Bool
283 }
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
284
cf07e79 @jnthn [common] Some initial cuts of prefix + and ~, Stringy and Numeric in …
authored
285 sub &prefix:<~>($x) {
286 $x.Stringy
287 }
288
289 sub &prefix:<+>($x) {
290 $x.Numeric
291 }
292
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
293 proto sub &infix:<+>($x, $y) { * }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
294 multi sub &infix:<+>(NQPInt $x, NQPInt $y) {
295 nqp::add_int($x, $y);
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
296 }
297
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
298 proto sub &infix:<->($x, $y) { * }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
299 multi sub &infix:<->(NQPInt $x, NQPInt $y) {
300 nqp::sub_int($x, $y);
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
301 }
302
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
303 proto sub &infix:<*>($x, $y) { * }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
304 multi sub &infix:<*>(NQPInt $x, NQPInt $y) {
305 nqp::mul_int($x, $y);
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
306 }
307
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
308 proto sub &infix:</>($x, $y) { * }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
309 multi sub &infix:</>(NQPInt $x, NQPInt $y) {
310 nqp::div_int($x, $y);
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
311 }
312
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
313 proto sub &infix:<%>($x, $y) { * }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
314 multi sub &infix:<%>(NQPInt $x, NQPInt $y) {
315 nqp::mod_int($x, $y);
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
316 }
317
318 sub &infix:<~>($x, $y) {
3881965 @jnthn Turns out that the always pass ops type object for result type strate…
authored
319 nqp::concat($x.Str, $y.Str);
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
320 }
a64ea75 @jnthn Implement plan and ok for getting us more tests.
authored
321
6a07cb2 @jnthn [common] Stub in a simple NQPException exception object and a die sub.
authored
322 # A basic, fairly bare-bones exception object.
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
323 my knowhow NQPException {
6a07cb2 @jnthn [common] Stub in a simple NQPException exception object and a die sub.
authored
324 has $!message;
325 has $!resumable;
326
327 method new($message) {
328 nqp::instance_of(self.WHAT).BUILD(:message($message))
329 }
330
331 method BUILD(:$message) {
332 $!message := $message;
333 $!resumable := 0;
334 self;
335 }
336
ac08da5 @jnthn [common] Add .defined methods to the various built-in types.
authored
337 method defined() {
338 nqp::repr_defined(self)
339 }
340
6a07cb2 @jnthn [common] Stub in a simple NQPException exception object and a die sub.
authored
341 method resumable() {
342 $!resumable
343 }
344
345 method resume() {
346 $!resumable := 1;
347 }
348
349 method Str() {
350 $!message
351 }
352 }
353
354 sub die($message) {
355 nqp::throw_dynamic(NQPException.new($message), 0)
356 }
357
a64ea75 @jnthn Implement plan and ok for getting us more tests.
authored
358 # For tests.
359 my $count := NQPInt.new();
360 sub plan($n) {
361 print("1..");
362 say($n);
363 }
7e9d2b8 @jnthn [common] $diag arg of ok should be optional.
authored
364 sub ok($check, $diag?) {
a64ea75 @jnthn Implement plan and ok for getting us more tests.
authored
365 $count := $count + 1;
366 unless $check { print("not ") }
367 print("ok ");
368 say($count);
369 }
cbdd49b @jnthn [dotnet] Out with one bad hack, in with a slightly less bad one for u…
authored
370
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
371 # Here comes the start of a heavily under construction ClassHOW.
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
372 my knowhow NQPClassHOW {
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
373 ##
374 ## Attributes
375 ##
376
377 # Name of the class.
378 has $!name;
379
380 # Attributes, methods, parents and roles directly added.
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
381 has @!attributes;
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
382 has %!methods;
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
383 has @!multi_methods_to_incorporate;
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
384 has @!parents;
385 has @!roles;
386
387 # Vtable and mapping of method names to slots.
388 has @!vtable;
389 has %!method-vtable-slots;
390
391 # Have we been composed?
392 has $!composed;
393
394 # Cached MRO (list of the type objects).
395 has @!mro;
396
397 # Full list of roles that we do.
398 has @!done;
399
400 ##
401 ## Declarative.
402 ##
403
404 # Creates a new instance of this meta-class.
b76e080 @jnthn [common] Allow NQPClassHOW to take and store the name of the class, a…
authored
405 method new(:$name) {
406 nqp::instance_of(self).BUILD(:name($name))
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
407 }
408
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
409 method CREATE($obj) {
410 nqp::instance_of($obj)
411 }
412
b76e080 @jnthn [common] Allow NQPClassHOW to take and store the name of the class, a…
authored
413 method BUILD(:$name) {
414 $!name := $name;
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
415 $!composed := 0;
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
416 %!methods := NQPHash.new;
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
417 @!multi_methods_to_incorporate := NQPArray.new;
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
418 @!attributes := NQPArray.new;
419 @!parents := NQPArray.new;
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
420 self;
421 }
422
423 # Create a new meta-class instance, and then a new type object
424 # to go with it, and return that.
425 # XXX TODO :$repr named parameter defaulting to P6opaque (don't
426 # have default values yet implemented).
b76e080 @jnthn [common] Allow NQPClassHOW to take and store the name of the class, a…
authored
427 method new_type(:$name = '<anon>', :$repr = 'P6opaque') {
428 my $metaclass := self.new(:name($name));
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
429 nqp::type_object_for($metaclass, 'P6opaque');
430 }
431
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
432 method add_method($obj, $name, $code_obj) {
2fd812d @jnthn [dotnet] Make sure at_pos and at_key never return null, but rather so…
authored
433 if %!methods{$name}.defined {
434 die("This class already has a method named " ~ $name);
435 }
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
436 %!methods{$name} := $code_obj;
437 }
438
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
439 method add_multi_method($obj, $name, $code_obj) {
440 # We can't incorporate these right away as we don't know all
441 # parents yet, maybe, which influences whether we even can
442 # have multis, need to generate a proto or worreva. So just
443 # queue them up in a todo list and we handle it at class
444 # composition time.
445 my %todo;
446 %todo<name> := $name;
447 %todo<code> := $code_obj;
448 @!multi_methods_to_incorporate[+@!multi_methods_to_incorporate] := %todo;
449 $code_obj;
450 }
451
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
452 method add_attribute($obj, $meta_attr) {
453 if $!composed {
454 die("NQPClassHOW does not support adding attributes after being composed.");
455 }
456 my $i := 0;
457 while $i != +@!attributes {
458 if @!attributes[$i].name eq $meta_attr.name {
459 die("Already have an attribute named " ~ $meta_attr.name);
460 }
461 $i := $i + 1;
462 }
463 @!attributes[+@!attributes] := $meta_attr;
464 }
465
466 method add_parent($obj, $parent) {
467 if $!composed {
468 pir::die("NQPClassHOW does not support adding parents after being composed.");
469 }
470 my $i := 0;
471 while $i != +@!parents {
472 if @!parents[$i] =:= $parent {
473 die("Already have " ~ $parent ~ " as a parent class.");
474 }
475 $i := $i + 1;
476 }
477 @!parents[+@!parents] := $parent;
478 }
479
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
480 method compose($obj) {
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
481 # XXX TODO: Compose roles (must come before we make MRO,
482 # and may provide multi candidates.)
b18a4f4 @jnthn [common] If no parent class is specified, use Mu (for now and in NQP,…
authored
483
484 # If we have no parents and we're not called Mu (XXX that's a
485 # tad fragile, I guess...) then add Mu as our parent.
486 if +@!parents == 0 && $!name ne 'Mu' {
487 self.add_parent($obj, Mu)
488 }
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
489
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
490 # Some things we only do if we weren't already composed once, like
491 # building the MRO.
492 unless $!composed {
493 @!mro := compute_c3_mro($obj);
494 $!composed := 1;
495 }
496
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
497 # Incorporate any new multi candidates (needs MRO built).
498 self.incorporate_multi_candidates($obj);
499
500 # XXX TODO: Compose attributes.
501
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
502 $obj
503 }
504
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
505 method incorporate_multi_candidates($obj) {
506 my $num_todo := +@!multi_methods_to_incorporate;
507 my $i := 0;
508 while $i != $num_todo {
509 # Get method name and code.
510 my $name := @!multi_methods_to_incorporate[$i]<name>;
511 my $code := @!multi_methods_to_incorporate[$i]<code>;
512
513 # Do we have anything in the methods table already in
514 # this class?
515 my $dispatcher := %!methods{$name};
516 if $dispatcher.defined {
517 # Yes. Only or dispatcher, though? If only, error. If
518 # dispatcher, simply add new dispatchee.
519 if nqp::is_dispatcher($dispatcher) {
520 nqp::push_dispatchee($dispatcher, $code);
521 }
522 else {
523 die("Cannot have a multi candidate for $name when an only method is also in the class");
524 }
525 }
526 else {
dc36346 @jnthn [common] Implement searching for a proto in parent classes too, and i…
authored
527 # Go hunting in the MRO for a proto.
528 my $j := 1;
529 my $found := 0;
530 while $j != +@!mro && !$found {
531 my $parent := @!mro[$j];
532 my %meths := $parent.HOW.method_table($parent);
533 my $dispatcher := %meths{$name};
534 if $dispatcher.defined {
535 # Found a possible - make sure it's a dispatcher, not
536 # an only.
537 if nqp::is_dispatcher($dispatcher) {
538 # Clone it and install it in our method table.
539 my @new_dispatchees;
540 @new_dispatchees[0] := $code;
541 %!methods{$name} := nqp::create_dispatch_and_add_candidates($dispatcher, @new_dispatchees);
542 $found := 1;
543 }
544 else {
545 die("Could not find a proto for multi $name (it may exist, but an only is hiding it if so)");
546 }
547 }
548 }
549 unless $found {
550 die("Could not find a proto for multi $name, and proto generation is NYI");
551 }
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
552 }
553 $i := $i + 1;
554 }
555 }
556
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
557 # XXX TODO: Get enough working to bring over the C3 implementation that
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
558 # we run on 6model on Parrot. For now, we only build it for single
559 # inheritance since it's obvious how to do it.
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
560 sub compute_c3_mro($obj) {
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
561 # MRO starts with this object.
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
562 my @mro;
563 @mro[0] := $obj;
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
564
565 # Now add all parents until we have none.
566 my $cur_obj := $obj;
567 my @parents := $cur_obj.HOW.parents($cur_obj, :local(1));
568 while +@parents {
569 if +@parents == 1 {
570 @mro[+@mro] := $cur_obj := @parents[0];
571 @parents := $cur_obj.HOW.parents($cur_obj, :local(1));
572 }
573 else {
574 die("Sorry, multiple inheritance is not yet implemented.");
575 }
576 }
577
578 # Return MRO.
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
579 @mro;
580 }
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
581
582 ##
583 ## Introspecty
584 ##
585
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
586 method attributes($obj, :$local!) {
587 @!attributes
588 }
589
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
590 method method_table($obj) {
591 %!methods
592 }
593
b76e080 @jnthn [common] Allow NQPClassHOW to take and store the name of the class, a…
authored
594 method name($obj) {
595 $!name
596 }
597
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
598 method parents($obj, :$local!) {
599 @!parents
600 }
601
ac08da5 @jnthn [common] Add .defined methods to the various built-in types.
authored
602 method defined() {
603 nqp::repr_defined(self)
604 }
605
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
606 ##
ea111e5 @jnthn [common] Implement .isa in NQPClassHOW.
authored
607 ## Czechy
608 ##
609
610 method isa($obj, $checkee) {
611 my $i := 0;
612 my $mro_length := +@!mro;
613 while $i != $mro_length {
614 if @!mro[$i] =:= $checkee {
615 return 1;
616 }
617 $i := $i + 1;
618 }
619 0;
620 }
621
622 ##
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
623 ## Dispatchy
624 ##
625
626 method find_method($obj, $name) {
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
627 my $i := 0;
628 my $mro_length := +@!mro;
629 while $i != $mro_length {
630 my %meths := @!mro[$i].HOW.method_table($obj);
631 my $found := %meths{$name};
632 if $found.defined {
633 return $found;
634 }
635 $i := $i + 1;
636 }
637 die("Could not find method " ~ $name);
638 }
639 }
640
641 # A simple attribute meta-object.
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
642 my knowhow NQPAttribute {
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
643 has $!name;
644 method new(:$name) {
645 my $obj := nqp::instance_of(self);
646 $obj.BUILD(:name($name));
647 $obj
648 }
649 method BUILD(:$name) {
650 $!name := $name
651 }
652 method name() {
653 $!name
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
654 }
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
655 }
656
4f3c7e0 @jnthn [common] Set up a GLOBAL.
authored
657 # GLOBAL stash.
658 # (XXX Really want one per compilation unit and unify, but this will get us
659 # started. Also really want a stash type that knows its name rather than just
660 # a hash, I guess.)
661 ::GLOBAL := NQPHash.new();
Something went wrong with that request. Please try again.