Skip to content

HTTPS clone URL

Subversion checkout URL

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