Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 765 lines (678 sloc) 19.035 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
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
199 proto sub &infix:<==>($x, $y) { * }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
200 multi sub &infix:<==>($x, $y) {
201 nqp::equal_nums($x.Num, $y.Num)
202 }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
203 multi sub &infix:<==>(NQPInt $x, NQPInt $y) {
204 nqp::equal_ints($x, $y)
205 }
206 multi sub &infix:<==>(NQPNum $x, NQPNum $y) {
207 nqp::equal_nums($x, $y)
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
208 }
209
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
210 proto sub &infix:<!=>($x, $y) { * }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
211 multi sub &infix:<!=>($x, $y) {
212 nqp::logical_not_int(nqp::equal_nums($x.Num, $y.Num))
213 }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
214 multi sub &infix:<!=>(NQPInt $x, NQPInt $y) {
dcf5ee5 @jnthn Make our != rather more efficient.
authored
215 nqp::logical_not_int(nqp::equal_ints($x, $y))
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
216 }
217 multi sub &infix:<!=>(NQPNum $x, NQPNum $y) {
dcf5ee5 @jnthn Make our != rather more efficient.
authored
218 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
219 }
220
ee58ecd @diakopter implemented (copypaste) >,>=,<,<= for NQPInts & NQPNums
diakopter authored
221 proto sub &infix:«<=»($x, $y) { * }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
222 multi sub &infix:«<=»($x, $y) {
223 nqp::less_than_or_equal_nums($x.Num, $y.Num)
224 }
ee58ecd @diakopter implemented (copypaste) >,>=,<,<= for NQPInts & NQPNums
diakopter authored
225 multi sub &infix:«<=»(NQPInt $x, NQPInt $y) {
226 nqp::less_than_or_equal_ints($x, $y)
227 }
228 multi sub &infix:«<=»(NQPNum $x, NQPNum $y) {
229 nqp::less_than_or_equal_nums($x, $y)
230 }
231
232 proto sub &infix:«<»($x, $y) { * }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
233 multi sub &infix:«<»($x, $y) {
234 nqp::less_than_nums($x.Num, $y.Num)
235 }
ee58ecd @diakopter implemented (copypaste) >,>=,<,<= for NQPInts & NQPNums
diakopter authored
236 multi sub &infix:«<»(NQPInt $x, NQPInt $y) {
237 nqp::less_than_ints($x, $y)
238 }
239 multi sub &infix:«<»(NQPNum $x, NQPNum $y) {
240 nqp::less_than_nums($x, $y)
241 }
925f29e @jnthn [weird] Looks like diakopter++'s commits and my git disagree on line …
authored
242
243 proto sub &infix:«>=»($x, $y) { * }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
244 multi sub &infix:«>=»($x, $y) {
245 nqp::greater_than_or_equal_nums($x.Num, $y.Num)
246 }
925f29e @jnthn [weird] Looks like diakopter++'s commits and my git disagree on line …
authored
247 multi sub &infix:«>=»(NQPInt $x, NQPInt $y) {
248 nqp::greater_than_or_equal_ints($x, $y)
249 }
250 multi sub &infix:«>=»(NQPNum $x, NQPNum $y) {
251 nqp::greater_than_or_equal_nums($x, $y)
252 }
253
254 proto sub &infix:«>»($x, $y) { * }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
255 multi sub &infix:«>»($x, $y) {
256 nqp::greater_than_nums($x.Num, $y.Num)
257 }
925f29e @jnthn [weird] Looks like diakopter++'s commits and my git disagree on line …
authored
258 multi sub &infix:«>»(NQPInt $x, NQPInt $y) {
259 nqp::greater_than_ints($x, $y)
260 }
261 multi sub &infix:«>»(NQPNum $x, NQPNum $y) {
262 nqp::greater_than_nums($x, $y)
263 }
ee58ecd @diakopter implemented (copypaste) >,>=,<,<= for NQPInts & NQPNums
diakopter authored
264
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
265 sub &infix:<eq>($x, $y) {
3881965 @jnthn Turns out that the always pass ops type object for result type strate…
authored
266 nqp::equal_strs($x.Str, $y.Str)
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
267 }
268
269 sub &infix:<ne>($x, $y) {
3881965 @jnthn Turns out that the always pass ops type object for result type strate…
authored
270 !nqp::equal_strs($x.Str, $y.Str)
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
271 }
272
0d13a6c @diakopter [dotnet] implement coercive lt, le, gt, ge; add tests to 10-cmp.t
diakopter authored
273 sub &infix:<ge>($x, $y) {
274 nqp::greater_than_or_equal_strs($x.Str, $y.Str)
275 }
276
277 sub &infix:<gt>($x, $y) {
278 nqp::greater_than_strs($x.Str, $y.Str)
279 }
280
281 sub &infix:<le>($x, $y) {
282 nqp::less_than_or_equal_strs($x.Str, $y.Str)
283 }
284
285 sub &infix:<lt>($x, $y) {
286 nqp::less_than_strs($x.Str, $y.Str)
287 }
288
98e707b @jnthn [common] =:=
authored
289 sub &infix:<=:=>($x, $y) {
290 nqp::equal_refs($x, $y)
291 }
292
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
293 sub &prefix:<!>($x) {
3881965 @jnthn Turns out that the always pass ops type object for result type strate…
authored
294 nqp::logical_not_int($x.Bool)
5e82e89 @jnthn Fill out the setting with some more coercioin methods and a few equal…
authored
295 }
296
297 sub &prefix:<?>($x) {
298 $x.Bool
299 }
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
300
cf07e79 @jnthn [common] Some initial cuts of prefix + and ~, Stringy and Numeric in …
authored
301 sub &prefix:<~>($x) {
302 $x.Stringy
303 }
304
305 sub &prefix:<+>($x) {
306 $x.Numeric
307 }
308
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
309 proto sub &infix:<+>($x, $y) { * }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
310 multi sub &infix:<+>($x, $y) {
311 nqp::add_num($x.Num, $y.Num);
312 }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
313 multi sub &infix:<+>(NQPInt $x, NQPInt $y) {
314 nqp::add_int($x, $y);
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
315 }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
316 multi sub &infix:<+>(NQPNum $x, NQPNum $y) {
317 nqp::add_num($x, $y);
318 }
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
319
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
320 proto sub &infix:<->($x, $y) { * }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
321 multi sub &infix:<->($x, $y) {
322 nqp::sub_num($x.Num, $y.Num);
323 }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
324 multi sub &infix:<->(NQPInt $x, NQPInt $y) {
325 nqp::sub_int($x, $y);
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
326 }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
327 multi sub &infix:<->(NQPNum $x, NQPNum $y) {
328 nqp::sub_num($x, $y);
329 }
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
330
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
331 proto sub &infix:<*>($x, $y) { * }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
332 multi sub &infix:<*>($x, $y) {
333 nqp::mul_num($x.Num, $y.Num);
334 }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
335 multi sub &infix:<*>(NQPInt $x, NQPInt $y) {
336 nqp::mul_int($x, $y);
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
337 }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
338 multi sub &infix:<*>(NQPNum $x, NQPNum $y) {
339 nqp::mul_num($x, $y);
340 }
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
341
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
342 proto sub &infix:</>($x, $y) { * }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
343 multi sub &infix:</>($x, $y) {
344 nqp::div_num($x.Num, $y.Num);
345 }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
346 multi sub &infix:</>(NQPInt $x, NQPInt $y) {
347 nqp::div_int($x, $y);
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
348 }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
349 multi sub &infix:</>(NQPNum $x, NQPNum $y) {
350 nqp::div_num($x, $y);
351 }
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
352
413bae8 @jnthn [common] Replace explicit multi-dispatcher ops with a proto with a pl…
authored
353 proto sub &infix:<%>($x, $y) { * }
4382f0d @jnthn [common] Add various coercice multi variants to various ops, now that…
authored
354 multi sub &infix:<%>($x, $y) {
355 nqp::mod_int($x.Int, $y.Int);
356 }
10019ac @jnthn Start to add multi variants to the NQPSetting. Seems that even though…
authored
357 multi sub &infix:<%>(NQPInt $x, NQPInt $y) {
358 nqp::mod_int($x, $y);
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
359 }
360
361 sub &infix:<~>($x, $y) {
3881965 @jnthn Turns out that the always pass ops type object for result type strate…
authored
362 nqp::concat($x.Str, $y.Str);
d3865c1 @jnthn Add some more operators to the NQP setting.
authored
363 }
a64ea75 @jnthn Implement plan and ok for getting us more tests.
authored
364
f5cd961 @diakopter [dotnet] implement bitwise operators for infix NQPInt... can make coe…
diakopter authored
365 sub &infix:<+|>(NQPInt $x, NQPInt $y) {
366 nqp::bitwise_or_int($x, $y);
367 }
368 sub &infix:<+&>(NQPInt $x, NQPInt $y) {
369 nqp::bitwise_and_int($x, $y);
370 }
371 sub &infix:<+^>(NQPInt $x, NQPInt $y) {
372 nqp::bitwise_xor_int($x, $y);
373 }
374
6a07cb2 @jnthn [common] Stub in a simple NQPException exception object and a die sub.
authored
375 # A basic, fairly bare-bones exception object.
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
376 my knowhow NQPException {
6a07cb2 @jnthn [common] Stub in a simple NQPException exception object and a die sub.
authored
377 has $!message;
378 has $!resumable;
379
380 method new($message) {
381 nqp::instance_of(self.WHAT).BUILD(:message($message))
382 }
383
384 method BUILD(:$message) {
385 $!message := $message;
386 $!resumable := 0;
387 self;
388 }
389
ac08da5 @jnthn [common] Add .defined methods to the various built-in types.
authored
390 method defined() {
391 nqp::repr_defined(self)
392 }
393
6a07cb2 @jnthn [common] Stub in a simple NQPException exception object and a die sub.
authored
394 method resumable() {
395 $!resumable
396 }
397
398 method resume() {
399 $!resumable := 1;
400 }
401
402 method Str() {
403 $!message
404 }
405 }
406
407 sub die($message) {
408 nqp::throw_dynamic(NQPException.new($message), 0)
409 }
410
39ec4ec @diakopter [dotnet] add defined method to Mu
diakopter authored
411 sub substr(NQPStr $str, NQPInt $offset, NQPInt $length?) {
412 nqp::substr($str, $offset, $length)
413 }
414
a64ea75 @jnthn Implement plan and ok for getting us more tests.
authored
415 # For tests.
416 my $count := NQPInt.new();
417 sub plan($n) {
418 print("1..");
419 say($n);
420 }
7e9d2b8 @jnthn [common] $diag arg of ok should be optional.
authored
421 sub ok($check, $diag?) {
a64ea75 @jnthn Implement plan and ok for getting us more tests.
authored
422 $count := $count + 1;
423 unless $check { print("not ") }
424 print("ok ");
425 say($count);
426 }
cbdd49b @jnthn [dotnet] Out with one bad hack, in with a slightly less bad one for u…
authored
427
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
428 # Here comes the start of a heavily under construction ClassHOW.
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
429 my knowhow NQPClassHOW {
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
430 ##
431 ## Attributes
432 ##
433
434 # Name of the class.
435 has $!name;
436
437 # Attributes, methods, parents and roles directly added.
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
438 has @!attributes;
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
439 has %!methods;
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
440 has @!multi_methods_to_incorporate;
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
441 has @!parents;
442 has @!roles;
443
444 # Vtable and mapping of method names to slots.
445 has @!vtable;
446 has %!method-vtable-slots;
447
448 # Have we been composed?
449 has $!composed;
450
451 # Cached MRO (list of the type objects).
452 has @!mro;
453
454 # Full list of roles that we do.
455 has @!done;
456
457 ##
458 ## Declarative.
459 ##
460
461 # Creates a new instance of this meta-class.
b76e080 @jnthn [common] Allow NQPClassHOW to take and store the name of the class, a…
authored
462 method new(:$name) {
463 nqp::instance_of(self).BUILD(:name($name))
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
464 }
465
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
466 method CREATE($obj) {
467 nqp::instance_of($obj)
468 }
469
b76e080 @jnthn [common] Allow NQPClassHOW to take and store the name of the class, a…
authored
470 method BUILD(:$name) {
471 $!name := $name;
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
472 $!composed := 0;
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
473 %!methods := NQPHash.new;
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
474 @!multi_methods_to_incorporate := NQPArray.new;
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
475 @!attributes := NQPArray.new;
476 @!parents := NQPArray.new;
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
477 self;
478 }
479
480 # Create a new meta-class instance, and then a new type object
481 # to go with it, and return that.
482 # XXX TODO :$repr named parameter defaulting to P6opaque (don't
483 # have default values yet implemented).
b76e080 @jnthn [common] Allow NQPClassHOW to take and store the name of the class, a…
authored
484 method new_type(:$name = '<anon>', :$repr = 'P6opaque') {
485 my $metaclass := self.new(:name($name));
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
486 nqp::type_object_for($metaclass, 'P6opaque');
487 }
488
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
489 method add_method($obj, $name, $code_obj) {
2fd812d @jnthn [dotnet] Make sure at_pos and at_key never return null, but rather so…
authored
490 if %!methods{$name}.defined {
491 die("This class already has a method named " ~ $name);
492 }
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
493 %!methods{$name} := $code_obj;
494 }
495
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
496 method add_multi_method($obj, $name, $code_obj) {
497 # We can't incorporate these right away as we don't know all
498 # parents yet, maybe, which influences whether we even can
499 # have multis, need to generate a proto or worreva. So just
500 # queue them up in a todo list and we handle it at class
501 # composition time.
502 my %todo;
503 %todo<name> := $name;
504 %todo<code> := $code_obj;
505 @!multi_methods_to_incorporate[+@!multi_methods_to_incorporate] := %todo;
506 $code_obj;
507 }
508
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
509 method add_attribute($obj, $meta_attr) {
510 if $!composed {
511 die("NQPClassHOW does not support adding attributes after being composed.");
512 }
513 my $i := 0;
514 while $i != +@!attributes {
515 if @!attributes[$i].name eq $meta_attr.name {
516 die("Already have an attribute named " ~ $meta_attr.name);
517 }
518 $i := $i + 1;
519 }
520 @!attributes[+@!attributes] := $meta_attr;
521 }
522
523 method add_parent($obj, $parent) {
524 if $!composed {
525 pir::die("NQPClassHOW does not support adding parents after being composed.");
526 }
527 my $i := 0;
528 while $i != +@!parents {
529 if @!parents[$i] =:= $parent {
530 die("Already have " ~ $parent ~ " as a parent class.");
531 }
532 $i := $i + 1;
533 }
534 @!parents[+@!parents] := $parent;
535 }
536
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
537 method compose($obj) {
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
538 # XXX TODO: Compose roles (must come before we make MRO,
539 # and may provide multi candidates.)
b18a4f4 @jnthn [common] If no parent class is specified, use Mu (for now and in NQP,…
authored
540
541 # If we have no parents and we're not called Mu (XXX that's a
542 # tad fragile, I guess...) then add Mu as our parent.
543 if +@!parents == 0 && $!name ne 'Mu' {
544 self.add_parent($obj, Mu)
545 }
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
546
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
547 # Some things we only do if we weren't already composed once, like
548 # building the MRO.
549 unless $!composed {
550 @!mro := compute_c3_mro($obj);
551 $!composed := 1;
552 }
553
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
554 # Incorporate any new multi candidates (needs MRO built).
555 self.incorporate_multi_candidates($obj);
e2fc32f @jnthn [common] Implement type_check and publish a type cache in NQPClassHOW.
authored
556
557 # Publish type cache.
558 self.publish_type_cache($obj);
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
559
39ec4ec @diakopter [dotnet] add defined method to Mu
diakopter authored
560 # Compose attributes.
561 for @!attributes { $_.compose($obj) }
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
562
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
563 $obj
564 }
565
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
566 method incorporate_multi_candidates($obj) {
567 my $num_todo := +@!multi_methods_to_incorporate;
568 my $i := 0;
569 while $i != $num_todo {
570 # Get method name and code.
571 my $name := @!multi_methods_to_incorporate[$i]<name>;
572 my $code := @!multi_methods_to_incorporate[$i]<code>;
573
574 # Do we have anything in the methods table already in
575 # this class?
576 my $dispatcher := %!methods{$name};
577 if $dispatcher.defined {
578 # Yes. Only or dispatcher, though? If only, error. If
579 # dispatcher, simply add new dispatchee.
580 if nqp::is_dispatcher($dispatcher) {
581 nqp::push_dispatchee($dispatcher, $code);
582 }
583 else {
584 die("Cannot have a multi candidate for $name when an only method is also in the class");
585 }
586 }
587 else {
dc36346 @jnthn [common] Implement searching for a proto in parent classes too, and i…
authored
588 # Go hunting in the MRO for a proto.
589 my $j := 1;
590 my $found := 0;
591 while $j != +@!mro && !$found {
592 my $parent := @!mro[$j];
593 my %meths := $parent.HOW.method_table($parent);
594 my $dispatcher := %meths{$name};
595 if $dispatcher.defined {
596 # Found a possible - make sure it's a dispatcher, not
597 # an only.
598 if nqp::is_dispatcher($dispatcher) {
599 # Clone it and install it in our method table.
600 my @new_dispatchees;
601 @new_dispatchees[0] := $code;
602 %!methods{$name} := nqp::create_dispatch_and_add_candidates($dispatcher, @new_dispatchees);
603 $found := 1;
604 }
605 else {
606 die("Could not find a proto for multi $name (it may exist, but an only is hiding it if so)");
607 }
608 }
609 }
610 unless $found {
611 die("Could not find a proto for multi $name, and proto generation is NYI");
612 }
6e2fb41 @jnthn [dotnet] First cut of multi-methods in NQPClassHOW. At the moment, on…
authored
613 }
614 $i := $i + 1;
615 }
616 }
617
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
618 # 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
619 # we run on 6model on Parrot. For now, we only build it for single
620 # 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
621 sub compute_c3_mro($obj) {
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
622 # MRO starts with this object.
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
623 my @mro;
624 @mro[0] := $obj;
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
625
626 # Now add all parents until we have none.
627 my $cur_obj := $obj;
628 my @parents := $cur_obj.HOW.parents($cur_obj, :local(1));
629 while +@parents {
630 if +@parents == 1 {
631 @mro[+@mro] := $cur_obj := @parents[0];
632 @parents := $cur_obj.HOW.parents($cur_obj, :local(1));
633 }
634 else {
635 die("Sorry, multiple inheritance is not yet implemented.");
636 }
637 }
638
639 # Return MRO.
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
640 @mro;
641 }
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
642
e2fc32f @jnthn [common] Implement type_check and publish a type cache in NQPClassHOW.
authored
643 method publish_type_cache($obj) {
644 # XXX TODO: when we have roles, need these here too.
645 nqp::publish_type_check_cache($obj, @!mro)
646 }
647
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
648 ##
649 ## Introspecty
650 ##
651
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
652 method attributes($obj, :$local!) {
653 @!attributes
654 }
655
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
656 method method_table($obj) {
657 %!methods
658 }
659
b76e080 @jnthn [common] Allow NQPClassHOW to take and store the name of the class, a…
authored
660 method name($obj) {
661 $!name
662 }
663
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
664 method parents($obj, :$local!) {
665 @!parents
666 }
667
ac08da5 @jnthn [common] Add .defined methods to the various built-in types.
authored
668 method defined() {
669 nqp::repr_defined(self)
670 }
671
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
672 ##
ea111e5 @jnthn [common] Implement .isa in NQPClassHOW.
authored
673 ## Czechy
674 ##
675
676 method isa($obj, $checkee) {
677 my $i := 0;
678 my $mro_length := +@!mro;
679 while $i != $mro_length {
680 if @!mro[$i] =:= $checkee {
681 return 1;
682 }
683 $i := $i + 1;
684 }
685 0;
686 }
687
e2fc32f @jnthn [common] Implement type_check and publish a type cache in NQPClassHOW.
authored
688 method does($obj, $checkee) {
689 0 # XXX TODO
690 }
691
692 method type_check($obj, $checkee) {
693 self.isa($obj, $checkee) || self.does($obj, $checkee)
694 }
695
ea111e5 @jnthn [common] Implement .isa in NQPClassHOW.
authored
696 ##
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
697 ## Dispatchy
698 ##
699
700 method find_method($obj, $name) {
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
701 my $i := 0;
702 my $mro_length := +@!mro;
703 while $i != $mro_length {
704 my %meths := @!mro[$i].HOW.method_table($obj);
705 my $found := %meths{$name};
39ec4ec @diakopter [dotnet] add defined method to Mu
diakopter authored
706 if nqp::repr_defined($found) {
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
707 return $found;
708 }
709 $i := $i + 1;
710 }
711 die("Could not find method " ~ $name);
712 }
713 }
714
715 # A simple attribute meta-object.
2348cc1 @jnthn [common] Mark the various packages in the setting as lexical ones.
authored
716 my knowhow NQPAttribute {
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
717 has $!name;
39ec4ec @diakopter [dotnet] add defined method to Mu
diakopter authored
718 has $!has_accessor;
719 has $!has_mutator;
720 method new(:$name, :$has_accessor, :$has_mutator) {
721 my $obj := nqp::instance_of(self.WHAT);
722 $obj.BUILD(:name($name), :has_accessor($has_accessor),
723 :has_mutator($has_mutator));
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
724 $obj
725 }
39ec4ec @diakopter [dotnet] add defined method to Mu
diakopter authored
726
727 method BUILD(:$name, :$has_accessor, :$has_mutator) {
728 $!name := $name;
729 $!has_accessor := $has_accessor;
730 $!has_mutator := $has_mutator;
3c4309f @jnthn [common] Much more filling out of NQPClassHOW, plus add a very basic …
authored
731 }
732 method name() {
733 $!name
4356d78 @jnthn [common] Add a VERY hacky but working version of method addition and …
authored
734 }
39ec4ec @diakopter [dotnet] add defined method to Mu
diakopter authored
735 method has_accessor() {
736 $!has_accessor
737 }
738 method has_mutator() {
739 $!has_mutator
740 }
741 method compose($obj) {
742 my $long_name := $!name;
743 my $short_name := nqp::substr($!name, 2);
744 if $!has_accessor {
745 if $!has_mutator {
746 $obj.HOW.add_method($obj, $short_name, method ($val?) {
747 return nqp::repr_defined($val)
748 ?? nqp::bind_attr(self, $obj.WHAT, $long_name, $val)
749 !! nqp::get_attr(self, $obj.WHAT, $long_name);
750 });
751 } else {
752 $obj.HOW.add_method($obj, nqp::substr($short_name, 2), method () {
753 nqp::get_attr(self, $obj.WHAT, $long_name);
754 });
755 }
756 }
757 }
06d78a2 @jnthn [common] Start to stub in a very barebones ClassHOW. This is enough t…
authored
758 }
759
4f3c7e0 @jnthn [common] Set up a GLOBAL.
authored
760 # GLOBAL stash.
761 # (XXX Really want one per compilation unit and unify, but this will get us
762 # started. Also really want a stash type that knows its name rather than just
763 # a hash, I guess.)
764 ::GLOBAL := NQPHash.new();
Something went wrong with that request. Please try again.