Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 635 lines (429 sloc) 12.77 kB
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
1 /*
f149e74 @coke #39217 - copyright cleanup. (src/)
coke authored
2 Copyright (C) 2001-2003, The Perl Foundation.
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
3 $Id$
4
5 =head1 NAME
6
f474cd7 @bschmalhofer [perl #38235] Rename (dyn)?classes to (dyn)?pmc
bschmalhofer authored
7 src/pmc/fixedintegerarray.pmc - fixed size array for integers only
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
8
9 =head1 DESCRIPTION
10
6c8f30b @ambs 80 columns cuts in all pmcs
ambs authored
11 This class, FixedIntegerArray, implements an array of fixed size,
12 which stored INTVALs, it uses Integer PMCs for all of the conversions
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
13
14 =head2 Functions
15
16 =over 4
17
18 =cut
19
20 */
21
22 #include "parrot/parrot.h"
23
24 pmclass FixedIntegerArray need_ext does array {
d1f7c98 JIT/i386 ord_i_s*
Leopold Toetsch authored
25
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
26 /*
27
28 =back
29
30 =head2 Methods
31
32 =over 4
33
34 =item C<void init()>
35
36 Initializes the array.
37
38 =cut
39
40 */
41
42 void init () {
43 PMC_int_val(SELF) = 0;
44 PMC_data(SELF) = NULL;
45 }
46
47 /*
48
2760c33 update =pod for new_from_string
Leopold Toetsch authored
49 =item C<PMC new_from_string(STRING *rep, INTVAL flags)>
1af9ad9 fixed integer array new_from_string
Leopold Toetsch authored
50
51 Class method to construct an array from the string representation C<rep>,
bf41b66 @particle [PDD07]: ws cleanup in src/, src/dynpmc/, and src/pmc/
particle authored
52 which is a string I<"(el0, el1, ...)">. C<flags> may have the
2760c33 update =pod for new_from_string
Leopold Toetsch authored
53 C<PObj_constant_FLAG> bit set, which will be honored to create a PMC
54 in the constant PMC pool.
1af9ad9 fixed integer array new_from_string
Leopold Toetsch authored
55
56 =cut
57
58 */
59 PMC* new_from_string(STRING *rep, INTVAL flags) {
60 INTVAL type, n, elem;
61 char *p, *start;
62 UINTVAL i, l;
63 int base;
64
65 type = SELF->vtable->base_type;
66 if (flags & PObj_constant_FLAG)
67 SELF = constant_pmc_new(INTERP, type);
68 else
69 SELF = pmc_new(INTERP, type);
d2478f6 fix argument passing ops w/o parens in signature
Leopold Toetsch authored
70 l = string_length(INTERP, rep);
71 if (!l)
1af9ad9 fixed integer array new_from_string
Leopold Toetsch authored
72 return SELF;
73 if (rep->encoding != Parrot_fixed_8_encoding_ptr)
74 real_exception(INTERP, NULL, E_ValueError,
75 "unhandled string encoding in constructor");
d2478f6 fix argument passing ops w/o parens in signature
Leopold Toetsch authored
76 if (l <= 2 && ((char*)rep->strstart)[0] == '(') /* "()" - no args */
77 return SELF;
1af9ad9 fixed integer array new_from_string
Leopold Toetsch authored
78 /* count commas */
79 p = rep->strstart;
80 for (i = l, n = 0; i; --i, ++p) {
81 if (*p == ',')
82 ++n;
83 }
84 /* presize the array */
85 SELF.set_integer_native(n + 1);
86 /* parse string */
87 p = rep->strstart;
88 for (i = l, n = 0; i; --i, ++p) {
89 switch (*p) {
90 case ' ': continue;
91 case '\t': continue;
92 case '(': continue;
93 case ')': break;
94 case ',':
95 n++;
96 break;
97 default:
98 base = 10;
99 if (*p == '0') {
100 ++p;
101 --i;
102 if (*p == 'b' || *p == 'B') {
103 base = 2;
104 ++p;
105 --i;
106 }
107 else if (*p == 'x' || *p == 'X') {
108 base = 16;
109 ++p;
110 --i;
111 }
112 }
113 start = p;
114 elem = strtoul(p, &p, base);
115 --p;
116 i -= (p - start);
117 SELF.set_integer_keyed_int(n, elem);
118 break;
119 }
120 }
121 return SELF;
122 }
123
124 /*
125
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
126 =item C<void morph(INTVAL type)>
127
f474cd7 @bschmalhofer [perl #38235] Rename (dyn)?classes to (dyn)?pmc
bschmalhofer authored
128 Const PMCs must have a C<morph()>.
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
129
130 =cut
131
132 */
133
134 void morph(INTVAL type) {
135 SUPER(type);
136 }
137
138
139 /*
140
141 =item C<void destroy()>
142
143 Destroys the array.
144
145 =cut
146
147 */
148
149 void destroy () {
150 if (PMC_data(SELF))
151 mem_sys_free(PMC_data(SELF));
152 PMC_data(SELF) = NULL;
153 PMC_int_val(SELF) = 0;
154 }
155
156 /*
157
158 =item C<PMC *clone()>
159
160 Creates and returns a copy of the array.
161
162 =cut
163
164 */
165
166 PMC* clone () {
167 INTVAL size;
9822a24 var_args 8 - verify signature during bytecode creation
Leopold Toetsch authored
168 PMC * dest;
169 /* a quick hack to create a clone in the constant PMC arena
170 * this is needed for the call signatures
171 *
172 * a better way would be probably to supply a flag to the clone
173 * vtable
174 */
175
176 if (PObj_constant_TEST(SELF))
177 dest = constant_pmc_new(INTERP, SELF->vtable->base_type);
178 else
179 dest = pmc_new(INTERP, SELF->vtable->base_type);
d1f7c98 JIT/i386 ord_i_s*
Leopold Toetsch authored
180
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
181 if (!PMC_data(SELF))
182 return dest;
183 size = PMC_int_val(SELF);
184 PMC_int_val(dest) = size;
d1f7c98 JIT/i386 ord_i_s*
Leopold Toetsch authored
185
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
186 PMC_data(dest) = mem_sys_allocate(size * sizeof(INTVAL));
187 mem_sys_memcopy(PMC_data(dest), PMC_data(SELF), size*sizeof(INTVAL));
188 PObj_active_destroy_SET(dest);
189 return dest;
190 }
191
192 /*
193
194 =item C<INTVAL get_bool()>
195
196 Returns whether the array has any elements (meaning been initialized, for a
197 fixed sized array).
198
199 =cut
200
201 */
202 INTVAL get_bool () {
ae8baa5 @bschmalhofer DYNDYNSELF in the Fixed*Array PMC looks like a typo. Using SELF propably
bschmalhofer authored
203 INTVAL size = SELF.elements();
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
204 return (INTVAL)(size != 0);
205 }
206
207 /*
208
209 =item C<INTVAL elements()>
210
211 =cut
212
213 */
214
215 INTVAL elements () {
216 return PMC_int_val(SELF);
217 }
218
219 /*
220
221 =item C<INTVAL get_integer()>
222
223 Returns the number of elements in the array.
224
225 =cut
226
227 */
228
229 INTVAL get_integer () {
ae8baa5 @bschmalhofer DYNDYNSELF in the Fixed*Array PMC looks like a typo. Using SELF propably
bschmalhofer authored
230 return SELF.elements();
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
231 }
232
233
234 /*
235
236 =item C<INTVAL get_integer_keyed_int(INTVAL key)>
237
238 Returns the integer value of the element at index C<key>.
239
240 =cut
241
242 */
243
244 INTVAL get_integer_keyed_int (INTVAL key) {
245 INTVAL *data;
246 if (key < 0 || key >= PMC_int_val(SELF))
717a913 @particle PMC: FixedIntegerArray
particle authored
247 real_exception(interpreter, NULL, E_IndexError,
d185452 @chromatic Standardize internal exception calls (James deBoer, james at huronbox…
chromatic authored
248 "FixedIntegerArray: index out of bounds!");
d1f7c98 JIT/i386 ord_i_s*
Leopold Toetsch authored
249
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
250 data = (INTVAL *)PMC_data(SELF);
251 return data[key];
252 }
253
254 /*
255
256 =item C<INTVAL get_integer_keyed(PMC *key)>
257
258 Returns the integer value of the element at index C<*key>.
259
260 =cut
261
262 */
263
264 INTVAL get_integer_keyed (PMC* key) {
265 /* simple int keys only */
266 INTVAL k = key_integer(INTERP, key);
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
267 return DYNSELF.get_integer_keyed_int(k);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
268 }
269
270
271 /*
272
273 =item C<FLOATVAL get_number_keyed_int(INTVAL key)>
274
275 Returns the floating-point value of the element at index C<key>.
276
277 =cut
278
279 */
280
281 FLOATVAL get_number_keyed_int (INTVAL key) {
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
282 return (FLOATVAL)DYNSELF.get_integer_keyed_int(key);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
283 }
284
285 /*
286
287 =item C<FLOATVAL get_number_keyed (PMC* key)>
288
289 Returns the floating-point value of the element at index C<*key>.
290
291 =cut
292
293 */
294
295 FLOATVAL get_number_keyed (PMC* key) {
296 INTVAL k = key_integer(INTERP, key);
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
297 return DYNSELF.get_number_keyed_int(k);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
298 }
299
300 /*
301
302 =item C<STRING *get_string_keyed_int(INTVAL key)>
303
304 Returns the Parrot string value of the element at index C<key>.
305
306 =cut
307
308 */
309
310 STRING* get_string_keyed_int (INTVAL key) {
311 PMC *temp;
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
312 temp = DYNSELF.get_pmc_keyed_int(key);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
313 return VTABLE_get_string(INTERP, temp);
314 }
bf41b66 @particle [PDD07]: ws cleanup in src/, src/dynpmc/, and src/pmc/
particle authored
315
5a67dac @tewk Add get_repr to fixedintegerarray and integer
tewk authored
316 /*
317
318 =item C<STRING *get_repr()>
319
320 Returns the Parrot string representation C<key>.
321
322 =cut
323
324 */
325
326 STRING* get_repr () {
327 STRING *res;
328 INTVAL j, n;
329 PMC *val;
330
331 res = string_from_cstring(INTERP, "[ ", 0);
332 n = VTABLE_elements(INTERP, SELF);
333 for (j = 0; j < n; ++j) {
334 val = SELF.get_pmc_keyed_int(j);
335 res = string_append(INTERP, res, VTABLE_get_repr(INTERP, val), 0);
336 if (j < n - 1)
337 res = string_append(INTERP, res, const_string(INTERP, ", "), 0);
338 }
339 res = string_append(INTERP, res, const_string(INTERP, " ]"), 0);
340 return res;
341 }
342
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
343
344 /*
345
346 =item C<STRING *get_string_keyed(PMC *key)>
347
348 Returns the Parrot string value of the element at index C<*key>.
349
350 =cut
351
352 */
353
354 STRING* get_string_keyed(PMC* key) {
355 INTVAL k = key_integer(INTERP, key);
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
356 return DYNSELF.get_string_keyed_int(k);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
357 }
358
359
360 /*
361
362 =item C<PMC *get_pmc_keyed_int(INTVAL key)>
363
364 Returns the PMC value of the element at index C<key>.
365
366 =cut
367
368 */
369
370 PMC* get_pmc_keyed_int (INTVAL key) {
371 PMC *ret;
372 INTVAL val;
373
374 ret = pmc_new(INTERP, enum_class_Integer);
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
375 val = DYNSELF.get_integer_keyed_int(key);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
376 VTABLE_set_integer_native(INTERP, ret, val);
377 return ret;
378 }
379
380 /*
381
382 =item C<PMC *get_pmc_keyed(PMC *key)>
383
384 Returns the PMC value of the element at index C<*key>.
385
386 =cut
387
388 */
389
390 PMC* get_pmc_keyed(PMC* key) {
391 INTVAL k = key_integer(INTERP, key);
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
392 return DYNSELF.get_pmc_keyed_int(k);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
393 }
394
395 /*
396
397 =item C<void set_integer_native(INTVAL size)>
398
399 Resizes the array to C<size> elements.
400
401 =cut
402
403 */
404
405 void set_integer_native (INTVAL size) {
406 if (PMC_int_val(SELF) || size < 1)
7298186 @particle pmc: convert Fixed*Array and Resizable*Array internal_exceptions to r…
particle authored
407 real_exception(INTERP, NULL, E_IndexError,
d1f7c98 JIT/i386 ord_i_s*
Leopold Toetsch authored
408 "FixedIntegerArray: Can't resize!");
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
409 PMC_int_val(SELF) = size;
d1f7c98 JIT/i386 ord_i_s*
Leopold Toetsch authored
410 PMC_data(SELF) = mem_sys_allocate_zeroed(size * sizeof(INTVAL));
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
411 PObj_active_destroy_SET(SELF);
412 }
413
414 /*
415
416 =item C<void set_integer_keyed_int(INTVAL key, INTVAL value)>
417
418 Sets the integer value of the element at index C<key> to C<value>.
419
420 =cut
421
422 */
423
424 void set_integer_keyed_int (INTVAL key, INTVAL value) {
425 INTVAL *data;
426 if (key < 0 || key >= PMC_int_val(SELF))
717a913 @particle PMC: FixedIntegerArray
particle authored
427 real_exception(interpreter, NULL, E_IndexError,
d185452 @chromatic Standardize internal exception calls (James deBoer, james at huronbox…
chromatic authored
428 "FixedIntegerArray: index out of bounds!");
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
429
430 data = (INTVAL*)PMC_data(SELF);
431 data[key] = value;
432 }
433
434 /*
435
436 =item C<void set_integer_keyed(PMC *key, INTVAL value)>
437
438 Sets the integer value of the element at index C<key> to C<value>.
439
440 =cut
441
442 */
443
444 void set_integer_keyed (PMC *key, INTVAL value) {
445 INTVAL k;
446 k = key_integer(INTERP, key);
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
447 DYNSELF.set_integer_keyed_int(k, value);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
448 }
449
450 /*
451
452 =item C<void set_number_keyed_int(INTVAL key, FLOATVAL value)>
453
454 Sets the floating-point value of the element at index C<key> to
455 C<value>.
456
457 =cut
458
459 */
460
461 void set_number_keyed_int (INTVAL key, FLOATVAL value) {
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
462 DYNSELF.set_integer_keyed_int(key, (INTVAL)value);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
463 }
464
465 /*
466
467 =item C<void set_number_keyed(PMC *key, FLOATVAL value)>
468
469 Sets the floating-point value of the element at index C<key> to
470 C<value>.
471
472 =cut
473
474 */
475
476 void set_number_keyed(PMC *key, FLOATVAL value) {
477 INTVAL k;
478 k = key_integer(INTERP, key);
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
479 DYNSELF.set_number_keyed_int(k, value);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
480 }
d1f7c98 JIT/i386 ord_i_s*
Leopold Toetsch authored
481
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
482 /*
483
484 =item C<void set_string_keyed_int(INTVAL key, STRING *value)>
485
486 Sets the Parrot string value of the element at index C<key> to C<value>.
487
488 =cut
489
490 */
491
492 void set_string_keyed_int (INTVAL key, STRING* value) {
493 INTVAL tempInt;
494 PMC *tempPMC;
495
496 tempPMC = pmc_new(INTERP, enum_class_Integer);
497 VTABLE_set_string_native(INTERP, tempPMC, value);
498 tempInt = VTABLE_get_integer(INTERP, tempPMC);
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
499 DYNSELF.set_integer_keyed_int(key, tempInt);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
500 }
501
502 /*
503
504 =item C<void set_string_keyed(PMC *key, STRING* value)>
505
506 Sets the string value of the element at index C<key> to
507 C<value>.
508
509 =cut
510
511 */
512
513 void set_string_keyed(PMC *key, STRING* value) {
514 INTVAL k;
515 k = key_integer(INTERP, key);
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
516 DYNSELF.set_string_keyed_int(k, value);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
517 }
518
519 /*
520
521 =item C<void set_pmc_keyed_int(INTVAL key, PMC *src)>
522
523 Sets the PMC value of the element at index C<key> to C<*src>.
524
525 =cut
526
527 */
528
529 void set_pmc_keyed_int (INTVAL key, PMC* src) {
530 INTVAL tempInt;
531
532 tempInt = VTABLE_get_integer(INTERP, src);
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
533 DYNSELF.set_integer_keyed_int(key, tempInt);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
534 }
535
536 /*
537
538 =item C<void set_pmc_keyed(PMC *key, PMC* value)>
539
540 Sets the string value of the element at index C<key> to
541 C<value>.
542
543 =cut
544
545 */
546
547 void set_pmc_keyed(PMC *key, PMC* value) {
548 INTVAL k;
549 k = key_integer(INTERP, key);
6a54672 This patch adds Resizable*Array pmcs as the counterparts to Fixed*Array
Dan Sugalski authored
550 DYNSELF.set_pmc_keyed_int(k, value);
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
551 }
552
6287ae3 add missing fixedintegerarray.freeze and .thaw
Leopold Toetsch authored
553 /*
554
555 =item C<void visit(visit_info *info)>
556
557 This is used by freeze/thaw to visit the contents of the array.
558
559 C<*info> is the visit info, (see F<include/parrot/pmc_freeze.h>).
560
561 =item C<void freeze(visit_info *info)>
562
563 Used to archive the array.
564
565 =item C<void thaw(visit_info *info)>
566
567 Used to unarchive the array.
568
569 =cut
570
571 */
572
573 void visit(visit_info *info) {
574 SUPER(info);
575 }
576
577 void freeze(visit_info *info) {
578 IMAGE_IO *io = info->image_io;
579 INTVAL i, n;
580 INTVAL *ar;
581
582 SUPER(info);
583 n = VTABLE_elements(INTERP, SELF);
584 io->vtable->push_integer(INTERP, io, n);
585 ar = (INTVAL *)PMC_data(SELF);
586 for (i = 0; i < n; ++i) {
587 io->vtable->push_integer(INTERP, io, ar[i]);
588 }
589 }
590
591 void thaw(visit_info *info) {
592 IMAGE_IO *io = info->image_io;
593 INTVAL i, n;
594 INTVAL *ar;
595
596 if (info->extra_flags == EXTRA_IS_NULL) {
597 PMC_int_val(SELF) = 0;
598 PMC_data(SELF) = NULL;
599 n = io->vtable->shift_integer(INTERP, io);
600 if (!n)
601 return;
602 DYNSELF.set_integer_native(n);
603 ar = (INTVAL *)PMC_data(SELF);
604 for (i = 0; i < n; ++i) {
605 ar[i] = io->vtable->shift_integer(INTERP, io);
606 }
607 }
608 else
609 SUPER(info);
610 }
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
611 }
612
613 /*
614
615 =back
616
617 =head1 SEE ALSO
618
1e8a4ab this patch adds some relevant 'push' ops to the resizable PMCs, descr…
Dan Sugalski authored
619 F<docs/pdds/pdd17_basic_types.pod>.
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
620
621 =head1 HISTORY
622
623 Initial version 2004.06.11 by Matt Fowles
624
625 =cut
626
627 */
628
629 /*
630 * Local variables:
72575ae @bschmalhofer #40481: [PATCH] C-code coda in pmc files
bschmalhofer authored
631 * c-file-style: "parrot"
4498fea This patch adds the above Fixed*Array classes. They are basic tests for
Dan Sugalski authored
632 * End:
633 * vim: expandtab shiftwidth=4:
72575ae @bschmalhofer #40481: [PATCH] C-code coda in pmc files
bschmalhofer authored
634 */
Something went wrong with that request. Please try again.