cvsuser     04/07/07 08:19:54

  Modified:    classes  default.pmc iterator.pmc
               languages/python pie-thon.pl
               languages/python/t/basic func.t
               src      py_func.c sub.c
               t/pmc    iter.t
  Log:
  Pie-thon 33 - yet another iterator class: Enumerate
  
  Revision  Changes    Path
  1.92      +22 -1     parrot/classes/default.pmc
  
  Index: default.pmc
  ===================================================================
  RCS file: /cvs/public/parrot/classes/default.pmc,v
  retrieving revision 1.91
  retrieving revision 1.92
  diff -u -w -r1.91 -r1.92
  --- default.pmc       27 Jun 2004 15:29:40 -0000      1.91
  +++ default.pmc       7 Jul 2004 15:19:39 -0000       1.92
  @@ -1,6 +1,6 @@
   /*
   Copyright: 2001-2003 The Perl Foundation.  All Rights Reserved.
  -$Id: default.pmc,v 1.91 2004/06/27 15:29:40 leo Exp $
  +$Id: default.pmc,v 1.92 2004/07/07 15:19:39 leo Exp $
   
   =head1 NAME
   
  @@ -756,6 +756,10 @@
   
   Returns SELF. A PMC is it's own class.
   
  +=item C<PMC* get_attr_str(STRING* attr)>
  +
  +Look for NCI methods and propertys.
  +
   =cut
   
   */
  @@ -763,6 +767,23 @@
           return SELF;
       }
   
  +    PMC* get_attr_str(STRING* name) {
  +        /* lets look for props first */
  +        PMC *p;
  +        HashBucket* b;
  +        if (SELF->pmc_ext && PMC_metadata(SELF)) {
  +             b = hash_get_bucket(INTERP, (Hash*) PMC_metadata(SELF), name);
  +             if (b)
  +                 return b->value;
  +        }
  +        /* may be NCI? */
  +        p = Parrot_find_method_with_cache(INTERP, SELF, name);
  +        if (!p) {
  +            /* TODO AttributeError */
  +        }
  +        return p;
  +    }
  +
   /*
   
   =item C<void visit(visit_info *info)>
  
  
  
  1.25      +18 -5     parrot/classes/iterator.pmc
  
  Index: iterator.pmc
  ===================================================================
  RCS file: /cvs/public/parrot/classes/iterator.pmc,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -w -r1.24 -r1.25
  --- iterator.pmc      6 Jul 2004 16:19:58 -0000       1.24
  +++ iterator.pmc      7 Jul 2004 15:19:40 -0000       1.25
  @@ -1,6 +1,6 @@
   /*
   Copyright: 2001-2003 The Perl Foundation.  All Rights Reserved.
  -$Id: iterator.pmc,v 1.24 2004/07/06 16:19:58 leo Exp $
  +$Id: iterator.pmc,v 1.25 2004/07/07 15:19:40 leo Exp $
   
   =head1 NAME
   
  @@ -22,6 +22,13 @@
   
   pmclass Iterator {
   
  +    void class_init() {
  +        if (pass) {
  +            enter_nci_method(INTERP, enum_class_Iterator,
  +                    F2DPTR(Parrot_Iterator_shift_pmc), "next", "PIO");
  +        }
  +    }
  +
   /*
   
   =item C<void init()>
  @@ -451,21 +458,23 @@
           PMC *agg, *key, *res;
           INTVAL ires;
   
  -        REG_INT(1) = REG_INT(2) = REG_INT(3) = REG_INT(3) = 0;
  +        REG_INT(1) = REG_INT(2) = REG_INT(3) = REG_INT(4) = 0;
           if (!key)
               return next;
  -        if (PMC_int_val(key) == -1)
  +        if (PMC_int_val(key) == -1) {
  +            real_exception(INTERP, NULL, E_StopIteration, "StopIteration");
               return next;
  +        }
           key = PMC_struct_val(SELF);
           agg = PMC_pmc_val(SELF);
           switch (agg->vtable->base_type) {
               case enum_class_IntList:
  -                ires = SELF.shift_integer();
  +                ires = DYNSELF.shift_integer();
                   REG_INT(1) = 1;
                   REG_INT(5) = ires;
                   return next;
               default:
  -                res = SELF.shift_pmc();
  +                res = DYNSELF.shift_pmc();
                   REG_INT(3) = 1;
                   REG_PMC(5) = res;
                   return next;
  @@ -644,6 +653,10 @@
           PMC *agg = PMC_pmc_val(SELF);
           return VTABLE_type_keyed_int(INTERP, agg, PMC_int_val(key) + idx );
       }
  +
  +    PMC* get_iter () {
  +        return SELF;
  +    }
   }
   
   /*
  
  
  
  1.18      +48 -6     parrot/languages/python/pie-thon.pl
  
  Index: pie-thon.pl
  ===================================================================
  RCS file: /cvs/public/parrot/languages/python/pie-thon.pl,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -w -r1.17 -r1.18
  --- pie-thon.pl       7 Jul 2004 03:37:48 -0000       1.17
  +++ pie-thon.pl       7 Jul 2004 15:19:44 -0000       1.18
  @@ -24,6 +24,7 @@
       callable => 1,
       chr => 1,
       hash => 1,
  +    enumerate => 1,
       filter => 1,
       map => 1,
       range => 'v', # var args
  @@ -124,7 +125,7 @@
   }
   
   my ($code_l, %params, %lexicals, %names, %def_args, %arg_count,
  -    @code, %globals, %classes);
  +    @code, %globals, %classes, @loops);
   
   sub decode_line {
       my $l = shift;
  @@ -1001,8 +1002,13 @@
   sub SETUP_LOOP
   {
       my ($n, $c, $cmt) = @_;
  +    my $targ = "pc_xxx";
  +    if ($c =~ /to (\d+)/) {
  +     $targ = "pc_$1";
  +    }
  +    push @loops, $targ;
       print <<EOC;
  -     # TODO $cmt
  +     # -> $targ $cmt
   EOC
   }
   sub GET_ITER
  @@ -1033,20 +1039,36 @@
   EOC
       push @stack, [-1, $var, 'P']
   }
  +
   sub POP_BLOCK
   {
       my ($n, $c, $cmt) = @_;
  +    if (@loops) {
  +     my $pc = pop @loops;
  +     print <<EOC;
  +     # $pc  $cmt
  +EOC
  +    }
  +    else {
       print <<EOC;
  -     # TODO $cmt
  +     \t\t$cmt
   EOC
   }
  +}
   
   sub UNPACK_SEQUENCE
   {
       my ($n, $c, $cmt) = @_;
  +    my $tos = pop @stack;
  +    my $seq = $tos->[1];
  +    my ($p, $i);
  +    for ($i = $n-1; $i >= 0; $i--) {
  +     $p = temp('P');
       print <<EOC;
  -     # TODO $cmt
  +     $p = $seq\[$i\] $cmt
   EOC
  +     push @stack, [-1, $p, 'P'];
  +    }
   }
   
   sub DUP_TOP
  @@ -1132,3 +1154,23 @@
   EOC
       push @stack, ['class $tos->[1]', $cl, 'P'];
   }
  +
  +sub BREAK_LOOP
  +{
  +    my ($n, $c, $cmt) = @_;
  +    my $pc = pop @loops;
  +    print <<EOC;
  +     goto $pc $cmt
  +EOC
  +}
  +
  +sub LOAD_ATTR
  +{
  +    my ($n, $c, $cmt) = @_;
  +    my $tos = pop @stack;  # object
  +    my $attr = temp('P');
  +    print <<EOC;
  +      $attr = getattribute $tos->[1], "$c" $cmt
  +EOC
  +    push @stack, [-1, $attr, 'P'];
  +}
  
  
  
  1.8       +19 -2     parrot/languages/python/t/basic/func.t
  
  Index: func.t
  ===================================================================
  RCS file: /cvs/public/parrot/languages/python/t/basic/func.t,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -w -r1.7 -r1.8
  --- func.t    5 Jul 2004 19:57:07 -0000       1.7
  +++ func.t    7 Jul 2004 15:19:47 -0000       1.8
  @@ -1,9 +1,9 @@
  -# $Id: func.t,v 1.7 2004/07/05 19:57:07 leo Exp $
  +# $Id: func.t,v 1.8 2004/07/07 15:19:47 leo Exp $
   
   use strict;
   use lib '../../lib';
   
  -use Parrot::Test tests => 21;
  +use Parrot::Test tests => 23;
   
   sub test {
       language_output_is('python', $_[0], '', $_[1]);
  @@ -201,3 +201,20 @@
   if __name__ == '__main__':
       main()
   CODE
  +
  +test(<<'CODE', 'enumerate');
  +if __name__ == '__main__':
  +    for i,j in enumerate("abc"):
  +     print i,j
  +CODE
  +
  +test(<<'CODE', 'enumerate - break');
  +if __name__ == '__main__':
  +    it = iter("abcdef")
  +    for i, c in enumerate(it):
  +        print i, c
  +        if i == 2:
  +            break
  +    print "Ok"
  +CODE
  +
  
  
  
  1.8       +22 -13    parrot/src/py_func.c
  
  Index: py_func.c
  ===================================================================
  RCS file: /cvs/public/parrot/src/py_func.c,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -w -r1.7 -r1.8
  --- py_func.c 6 Jul 2004 16:20:11 -0000       1.7
  +++ py_func.c 7 Jul 2004 15:19:50 -0000       1.8
  @@ -1,6 +1,6 @@
   /*
   Copyright: 2001-2004 The Perl Foundation.  All Rights Reserved.
  -$Id: py_func.c,v 1.7 2004/07/06 16:20:11 leo Exp $
  +$Id: py_func.c,v 1.8 2004/07/07 15:19:50 leo Exp $
   
   =head1 NAME
   
  @@ -61,6 +61,13 @@
   }
   
   static PMC *
  +parrot_py_enumerate(Interp *interpreter, PMC *list)
  +{
  +    PMC *res = pmc_new_init(interpreter, enum_class_Enumerate, list);
  +    return res;
  +}
  +
  +static PMC *
   parrot_py_filter(Interp *interpreter, PMC *func, PMC *list)
   {
       PMC *res, *iter;
  @@ -328,11 +335,9 @@
   
       STRING *callable     = CONST_STRING(interpreter, "callable");
       STRING *chr     = CONST_STRING(interpreter, "chr");
  -    STRING *hash     = CONST_STRING(interpreter, "hash");
  -    STRING *iter     = CONST_STRING(interpreter, "iter");  /* XXX op */
  -    STRING *iter_sig = CONST_STRING(interpreter, "PIP");
  +    STRING *enumerate= CONST_STRING(interpreter, "enumerate");
       STRING *filter     = CONST_STRING(interpreter, "filter");
  -
  +    STRING *hash     = CONST_STRING(interpreter, "hash");
       STRING *map     = CONST_STRING(interpreter, "map");
       STRING *range     = CONST_STRING(interpreter, "range");
       STRING *reduce     = CONST_STRING(interpreter, "reduce");
  @@ -340,9 +345,9 @@
   
       parrot_py_global(interpreter, F2DPTR(parrot_py_callable), callable, pip);
       parrot_py_global(interpreter, F2DPTR(parrot_py_chr), chr, pip);
  -    parrot_py_global(interpreter, F2DPTR(parrot_py_hash), hash, pip);
  -    parrot_py_global(interpreter, F2DPTR(parrot_py_iter), iter, pip);
  +    parrot_py_global(interpreter, F2DPTR(parrot_py_enumerate), enumerate, pip);
       parrot_py_global(interpreter, F2DPTR(parrot_py_filter), filter, pipp);
  +    parrot_py_global(interpreter, F2DPTR(parrot_py_hash), hash, pip);
       parrot_py_global(interpreter, F2DPTR(parrot_py_map), map, pipp);
       parrot_py_global(interpreter, F2DPTR(parrot_py_range), range, pip);
       parrot_py_global(interpreter, F2DPTR(parrot_py_reduce), reduce, pipp);
  @@ -373,6 +378,10 @@
       s = CONST_STRING(interpreter, "Exception");
       Parrot_store_global(interpreter, NULL, s, ex);
   
  +    ex = ex_list[E_StopIteration];
  +    s = CONST_STRING(interpreter, "StopIteration");
  +    Parrot_store_global(interpreter, NULL, s, ex);
  +
       ex = ex_list[E_RuntimeError];
       s = CONST_STRING(interpreter, "RuntimeError");
       Parrot_store_global(interpreter, NULL, s, ex);
  
  
  
  1.65      +5 -3      parrot/src/sub.c
  
  Index: sub.c
  ===================================================================
  RCS file: /cvs/public/parrot/src/sub.c,v
  retrieving revision 1.64
  retrieving revision 1.65
  diff -u -w -r1.64 -r1.65
  --- sub.c     6 Jul 2004 18:11:47 -0000       1.64
  +++ sub.c     7 Jul 2004 15:19:51 -0000       1.65
  @@ -1,6 +1,6 @@
   /*
   Copyright: 2001-2003 The Perl Foundation.  All Rights Reserved.
  -$Id: sub.c,v 1.64 2004/07/06 18:11:47 leo Exp $
  +$Id: sub.c,v 1.65 2004/07/07 15:19:51 leo Exp $
   
   =head1 NAME
   
  @@ -388,8 +388,10 @@
       ctx = &co->ctx;
       save_context(interp, ctx);
   
  -    /* we have separate register stacks */
  -    setup_register_stacks(interp, ctx);
  +    /* we have separate register stacks
  +     * - or not, with our single item stack
  +     */
  +    /* setup_register_stacks(interp, ctx); */
   
       /* create new (pad ??) and control stacks,
        * when invoking the coroutine the real stacks are
  
  
  
  1.25      +27 -2     parrot/t/pmc/iter.t
  
  Index: iter.t
  ===================================================================
  RCS file: /cvs/public/parrot/t/pmc/iter.t,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -w -r1.24 -r1.25
  --- iter.t    6 Jul 2004 06:38:11 -0000       1.24
  +++ iter.t    7 Jul 2004 15:19:54 -0000       1.25
  @@ -1,6 +1,6 @@
   #! perl -w
   # Copyright: 2001-2003 The Perl Foundation.  All Rights Reserved.
  -# $Id: iter.t,v 1.24 2004/07/06 06:38:11 leo Exp $
  +# $Id: iter.t,v 1.25 2004/07/07 15:19:54 leo Exp $
   
   =head1 NAME
   
  @@ -16,7 +16,7 @@
   
   =cut
   
  -use Parrot::Test tests => 42;
  +use Parrot::Test tests => 43;
   use Test::More qw(skip);
   
   output_is(<<'CODE', <<'OUTPUT', "new iter");
  @@ -1371,3 +1371,28 @@
   ok
   OUTPUT
   
  +output_is(<<'CODE', <<'OUTPUT', "enumerate class");
  +   new P0, .PerlString
  +   set P0, "abcdef"
  +   new P1, .Enumerate, P0
  +   print "ok 1\n"
  +lp:unless P1, ex
  +   shift P2, P1
  +   set P10, P2[0]
  +   print P10
  +   print " "
  +   set P10, P2[1]
  +   print P10
  +   print "\n"
  +   branch lp
  +ex:
  +   end
  +CODE
  +ok 1
  +0 a
  +1 b
  +2 c
  +3 d
  +4 e
  +5 f
  +OUTPUT
  
  
  

Reply via email to