cvsuser 04/06/16 00:11:54
Modified: classes iterator.pmc
t/pmc iter.t
Log:
[perl #27694] PATCH] Index access and get_string for Iterator PMC
I have been looking into integer keyed access to the Iterator PMC.
I tried to make work several 'get_*_keyed_int', 'exists_keyed_int' and
'defined_keyed_int' ops.
Most likely this will only be useful for array like PMCs.
Courtesy of Bernhard Schmalhofer <[EMAIL PROTECTED]>
Revision Changes Path
1.19 +78 -27 parrot/classes/iterator.pmc
Index: iterator.pmc
===================================================================
RCS file: /cvs/public/parrot/classes/iterator.pmc,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -w -r1.18 -r1.19
--- iterator.pmc 22 Feb 2004 17:48:41 -0000 1.18
+++ iterator.pmc 16 Jun 2004 07:11:51 -0000 1.19
@@ -1,6 +1,6 @@
/*
Copyright: 2001-2003 The Perl Foundation. All Rights Reserved.
-$Id: iterator.pmc,v 1.18 2004/02/22 17:48:41 mikescott Exp $
+$Id: iterator.pmc,v 1.19 2004/06/16 07:11:51 leo Exp $
=head1 NAME
@@ -88,12 +88,32 @@
=item C<INTVAL get_integer()>
+Get number of remaining elements. Does not work for hashes yet.
+TODO: keep track of current position and direction
+
=cut
*/
INTVAL get_integer () {
- return VTABLE_get_integer(INTERP, (PMC *)PMC_pmc_val(SELF));
+ PMC *agg = PMC_pmc_val(SELF);
+ return VTABLE_get_integer(INTERP, agg );
+ }
+
+/*
+
+=item C<STRING *get_string()>
+
+Returns the number of remaining elements as a Parrot string.
+TODO: keep track of current position and direction
+
+=cut
+
+*/
+
+ STRING* get_string () {
+ PMC *agg = PMC_pmc_val(SELF);
+ return string_from_int(INTERP, VTABLE_get_integer(INTERP, agg ) );
}
/*
@@ -110,15 +130,18 @@
/*
-=item C<INTVAL get_integer_keyed_int(INTVAL key)>
+=item C<INTVAL get_integer_keyed_int(INTVAL index)>
+
+Get integer value of current position plus index.
=cut
*/
- INTVAL get_integer_keyed_int (INTVAL key) {
- /* XXX adjust index */
- return VTABLE_get_integer_keyed_int(INTERP, (PMC *)PMC_pmc_val(SELF), key);
+ INTVAL get_integer_keyed_int (INTVAL index) {
+ PMC *key = PMC_struct_val(SELF);
+ PMC *agg = PMC_pmc_val(SELF);
+ return VTABLE_get_integer_keyed_int(INTERP, agg, PMC_int_val(key) + index );
}
/*
@@ -135,15 +158,18 @@
/*
-=item C<FLOATVAL get_number_keyed_int(INTVAL key)>
+=item C<FLOATVAL get_number_keyed_int(INTVAL index)>
+
+Get number value of current position plus index.
=cut
*/
- FLOATVAL get_number_keyed_int (INTVAL key) {
- /* XXX adjust index */
- return VTABLE_get_number_keyed_int(INTERP, (PMC *)PMC_pmc_val(SELF), key);
+ FLOATVAL get_number_keyed_int (INTVAL index) {
+ PMC *key = PMC_struct_val(SELF);
+ PMC *agg = PMC_pmc_val(SELF);
+ return VTABLE_get_number_keyed_int(INTERP, agg, PMC_int_val(key) + index );
}
/*
@@ -160,16 +186,18 @@
/*
-=item C<BIGNUM *get_bignum_keyed_int(INTVAL key)>
+=item C<BIGNUM *get_bignum_keyed_int(INTVAL index)>
-Returns the current index.
+Get bignum value of current position plus index.
=cut
*/
- BIGNUM* get_bignum_keyed_int (INTVAL key) {
- return VTABLE_get_bignum_keyed_int(INTERP, (PMC *)PMC_pmc_val(SELF), key);
+ BIGNUM* get_bignum_keyed_int (INTVAL index) {
+ PMC *key = PMC_struct_val(SELF);
+ PMC *agg = PMC_pmc_val(SELF);
+ return VTABLE_get_bignum_keyed_int(INTERP, agg, PMC_int_val(key) + index );
}
/*
@@ -186,16 +214,18 @@
/*
-=item C<STRING *get_string_keyed_int(INTVAL key)>
+=item C<STRING *get_string_keyed_int(INTVAL index)>
-Returns the current key.
+Get string value of current position plus index.
=cut
*/
- STRING* get_string_keyed_int (INTVAL key) {
- return VTABLE_get_string_keyed_int(INTERP, (PMC *)PMC_pmc_val(SELF), key);
+ STRING* get_string_keyed_int (INTVAL index) {
+ PMC *key = PMC_struct_val(SELF);
+ PMC *agg = PMC_pmc_val(SELF);
+ return VTABLE_get_string_keyed_int(INTERP, agg, PMC_int_val(key) + index );
}
/*
@@ -251,8 +281,10 @@
*/
- PMC* get_pmc_keyed_int (INTVAL key) {
- return VTABLE_get_pmc_keyed_int(INTERP, (PMC *)PMC_pmc_val(SELF), key);
+ PMC* get_pmc_keyed_int (INTVAL index) {
+ PMC *key = PMC_struct_val(SELF);
+ PMC *agg = PMC_pmc_val(SELF);
+ return VTABLE_get_pmc_keyed_int(INTERP, agg, PMC_int_val(key) + index );
}
/*
@@ -474,16 +506,18 @@
/*
-=item C<INTVAL exists_keyed_int(INTVAL key)>
+=item C<INTVAL exists_keyed_int(INTVAL index)>
-Returns whether an element for C<key> exists in the aggregate.
+Returns whether an element for C<index> exists in the aggregate.
=cut
*/
- INTVAL exists_keyed_int (INTVAL key) {
- return VTABLE_exists_keyed_int(INTERP, (PMC *)PMC_pmc_val(SELF), key);
+ INTVAL exists_keyed_int (INTVAL index) {
+ PMC *key = PMC_struct_val(SELF);
+ PMC *agg = PMC_pmc_val(SELF);
+ return VTABLE_exists_keyed_int(INTERP, agg, PMC_int_val(key) + index );
}
/*
@@ -522,8 +556,10 @@
*/
- INTVAL defined_keyed_int (INTVAL key) {
- return VTABLE_defined_keyed_int(INTERP, (PMC *)PMC_pmc_val(SELF), key);
+ INTVAL defined_keyed_int (INTVAL index) {
+ PMC *key = PMC_struct_val(SELF);
+ PMC *agg = PMC_pmc_val(SELF);
+ return VTABLE_defined_keyed_int(INTERP, agg, PMC_int_val(key) + index );
}
/*
@@ -539,6 +575,21 @@
INTVAL type_keyed (PMC* key) {
return VTABLE_type_keyed(INTERP, (PMC *)PMC_pmc_val(SELF), key);
}
+
+
+=item C<INTVAL type_keyed_int(PMC *index)>
+
+Returns the result of calling C<type_keyed(key)> on the aggregate.
+
+=cut
+
+*/
+
+ INTVAL type_keyed_int (INTVAL index) {
+ PMC *key = PMC_struct_val(SELF);
+ PMC *agg = PMC_pmc_val(SELF);
+ return VTABLE_type_keyed_int(INTERP, agg, PMC_int_val(key) + index );
+ }
}
/*
1.13 +181 -10 parrot/t/pmc/iter.t
Index: iter.t
===================================================================
RCS file: /cvs/public/parrot/t/pmc/iter.t,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -w -r1.12 -r1.13
--- iter.t 8 Mar 2004 00:20:09 -0000 1.12
+++ iter.t 16 Jun 2004 07:11:54 -0000 1.13
@@ -1,6 +1,6 @@
#! perl -w
# Copyright: 2001-2003 The Perl Foundation. All Rights Reserved.
-# $Id: iter.t,v 1.12 2004/03/08 00:20:09 chromatic Exp $
+# $Id: iter.t,v 1.13 2004/06/16 07:11:54 leo Exp $
=head1 NAME
@@ -16,8 +16,9 @@
=cut
-use Parrot::Test tests => 10;
+use Parrot::Test tests => 12;
use Test::More qw(skip);
+
output_is(<<'CODE', <<'OUTPUT', "new iter");
new P2, .PerlArray
new P1, .Iterator, P2
@@ -325,12 +326,180 @@
reached end
OUTPUT
+output_is(<<'CODE', <<'OUTPUT', "Index access for Iterator on PerlString");
+##PIR##
+.include "iterator.pasm"
+.sub _main
+ .local pmc string_1
+ string_1 = new PerlString
+ string_1 = "abcd\x65\x66\x67"
+ print 'PerlString new: '
+ print string_1
+ print "\n"
+
+ .local pmc iter_1
+ iter_1 = new Iterator, string_1
+ iter_1 = .ITERATE_FROM_START
+
+ .local int code_point_1
+ .local float code_point_2
+ .local string code_point_3
+ .local pmc code_point_4
+
+ print 'Iterator shift_integer: '
+ shift code_point_1, iter_1
+ print code_point_1
+ print "\n"
+
+ print 'Iterator get_integer_keyed_int 2: '
+ code_point_1 = iter_1[2]
+ print code_point_1
+ print "\n"
+
+ print 'Iterator get_integer_keyed_int 0: '
+ code_point_1 = iter_1[0]
+ print code_point_1
+ print "\n"
+
+ print 'Iterator get_integer_keyed_int -1: '
+ code_point_1 = iter_1[-1]
+ print code_point_1
+ print "\n"
+
+ end
+.end
+CODE
+PerlString new: abcdefg
+Iterator shift_integer: 97
+Iterator get_integer_keyed_int 2: 100
+Iterator get_integer_keyed_int 0: 98
+Iterator get_integer_keyed_int -1: 97
+OUTPUT
+
+output_is(<< 'CODE', << 'OUTPUT', "Index access for Iterator on PerlArray");
+##PIR##
+.include "iterator.pasm"
+.sub _main
+ .local pmc array_1
+ array_1 = new PerlArray
+ push array_1, 'a'
+ push array_1, 'b'
+ push array_1, 'c'
+ push array_1, 'd'
+ push array_1, 'e'
+ push array_1, 'f'
+ push array_1, '7'
+ push array_1, '-8.8'
+
+ print 'PerlArray get_string: '
+ print array_1
+ print "\n"
+
+ .local pmc iter_1
+ iter_1 = new Iterator, array_1
+ iter_1 = .ITERATE_FROM_START
+
+ print 'Iterator get_string: '
+ print iter_1
+ print "\n"
+
+ .local string elem_1
+
+ print 'Iterator shift_string: '
+ shift elem_1, iter_1
+ print elem_1
+ print "\n"
+
+ print 'Iterator get_string_keyed_int 2: '
+ elem_1 = iter_1[2]
+ print elem_1
+ print "\n"
+
+ print 'Iterator get_string_keyed_int -1: '
+ elem_1 = iter_1[-1]
+ print elem_1
+ print "\n"
+
+ print 'Iterator get_string_keyed_int 0: '
+ elem_1 = iter_1[0]
+ print elem_1
+ print "\n"
+
+ print 'Iterator get_pmc_keyed_int 3: '
+ .local pmc elem_2
+ elem_2 = iter_1[3]
+ print elem_2
+ print "\n"
+
+ .local int flag
+
+ print 'Iterator exists_keyed_int 3: '
+ flag = exists iter_1[3]
+ print flag
+ print "\n"
+
+ print 'Iterator exists_keyed_int 28: '
+ flag = exists iter_1[28]
+ print flag
+ print "\n"
+
+
+ print 'Iterator defined_keyed_int 3: '
+ flag = defined iter_1[3]
+ print flag
+ print "\n"
+
+ print 'Iterator defined_keyed_int -1278: '
+ flag = defined iter_1[-1278]
+ print flag
+ print "\n"
+
+ .local pmc iter_2
+ iter_2 = new Iterator, array_1
+ iter_2 = .ITERATE_FROM_END
+
+ print 'Iterator get_string: '
+ print iter_1
+ print "\n"
+
+ print 'Iterator shift_float: '
+ .local float elem_2
+ shift elem_2, iter_2
+ print elem_2
+ print "\n"
+
+ print 'Iterator get_integer: '
+ .local int elem_3
+ elem_3 = iter_2[-1]
+ print elem_3
+ print "\n"
+
+ end
+.end
+CODE
+PerlArray get_string: 8
+Iterator get_string: 8
+Iterator shift_string: a
+Iterator get_string_keyed_int 2: d
+Iterator get_string_keyed_int -1: a
+Iterator get_string_keyed_int 0: b
+Iterator get_pmc_keyed_int 3: e
+Iterator exists_keyed_int 3: 1
+Iterator exists_keyed_int 28: 0
+Iterator defined_keyed_int 3: 1
+Iterator defined_keyed_int -1278: 0
+Iterator get_string: 8
+Iterator shift_float: -8.800000
+Iterator get_integer: 7
+OUTPUT
+
+
SKIP: {
-skip("N/Y: get_keyed_int gets rest of array", 1);
+skip("N/Y: length of rest of array ", 1);
output_is(<<'CODE', <<'OUTPUT', "shift + index access");
.include "iterator.pasm"
- new P2, .PerlArray # array with 2 elements
+ new P2, .PerlArray # array with 4 elements
push P2, 10
push P2, 20
push P2, 30
@@ -349,22 +518,24 @@
print "not "
ok2: print "ok 2\n"
- set I0, P1[0] # first element of iter = next
+ shift I0, P1 # get one
eq I0, 20, ok3
- print I0
print " not "
ok3: print "ok 3\n"
set I0, P1 # arr.length of rest
- eq I0, 3, ok4
+ eq I0, 2, ok6
print I0
print " not "
-ok4: print "ok 4\n"
+ok6: print "ok 6\n"
+
+ print P1
end
CODE
ok 1
ok 2
ok 3
-ok 4
+ok 6
+2
OUTPUT
}