Skip to content

Commit

Permalink
Adjust List!generate to always (lazily) flatten RPAs and Parcels
Browse files Browse the repository at this point in the history
in the values of the list, even if no 'flatten' flag is set on
them.  This fixes empty [] to create an empty Array, as well as
reduce the number of places that we need to set the 'flatten' flag.
  • Loading branch information
pmichaud committed Jan 21, 2010
1 parent c36717d commit 47ef21b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
3 changes: 0 additions & 3 deletions src/builtins/Array.pir
Expand Up @@ -56,9 +56,6 @@ Arrays are the mutable form of Lists.
.namespace []
.sub '&circumfix:<[ ]>'
.param pmc values :slurpy
.local pmc true
true = get_hll_global ['Bool'], 'True'
setprop values, 'flatten', true
$P0 = new ['Array']
$P0.'!STORE'(values)
.return ($P0)
Expand Down
37 changes: 22 additions & 15 deletions src/builtins/List.pir
Expand Up @@ -119,50 +119,57 @@ Creates a List from its arguments.
Ask the list to make at least the first C<n> elements as non-lazy
as it can, then return its entire list of elements as a
ResizablePMCArray or Parcel. If C<n> isn't given, then eagerly
generate the entire list.
generate the entire list. Any RPAs (incl. Parcels) in $!values
always flatten here, even if not marked with the 'flatten' flag.
=cut
.namespace ['List']
.sub '!generate' :method
.param int n :optional
.param int has_n :opt_flag
.local pmc values, gen
.local pmc values
values = getattribute self, '$!values'
unless null values goto have_values
values = new ['ResizablePMCArray']
setattribute self, '$!values', values
have_values:
gen = getattribute self, '$!gen'
.local pmc gen
gen = getattribute self, '$!gen'
unless null gen goto have_gen
gen = box 0
setattribute self, '$!gen', gen
have_gen:
gen_loop:
.local int gen_i, values_i
.local int gen_i
gen_i = gen
values_i = elements values
if has_n goto gen_elem
n = values_i
gen_elem:
unless gen_i < n goto gen_done
if gen_i >= values_i goto gen_done
gen_loop:
$I0 = elements values
if gen_i >= $I0 goto gen_done
.local pmc elem
elem = values[gen_i]
if null elem goto gen_next
# RPAs (incl Parcel) always flatten
$I1 = isa elem, ['ResizablePMCArray']
if $I1 goto have_flat_elem
# see if we have a flattening element
$P0 = getprop 'flatten', elem
if null $P0 goto gen_next
# see if we need to flatten a certain number of elems
if has_n goto gen_elem_n
elem = elem.'!generate'()
goto have_flat_elem
gen_elem_n:
$I0 = n - gen_i
$I1 = isa elem, ['ResizablePMCArray']
if $I1 goto have_flat_elem
elem = elem.'!generate'($I0)
have_flat_elem:
splice values, elem, gen_i, 1
goto gen_loop
gen_next:
inc gen
goto gen_loop
inc gen_i
unless has_n goto gen_loop
if gen_i < n goto gen_loop
gen_done:
gen = gen_i
.return (values)
.end
Expand Down
6 changes: 3 additions & 3 deletions src/builtins/Parcel.pir
Expand Up @@ -55,6 +55,9 @@ to a List.
setattribute parcel, '$!values', values
gen = box 0
setattribute parcel, '$!gen', gen
# mark the List as a flattening object
$P0 = get_hll_global ['Bool'], 'True'
setprop parcel, 'flatten', $P0
.return (parcel)
.end

Expand All @@ -78,9 +81,6 @@ The canonical operator for creating a Parcel.
parcel = new ['Parcel']
transform_to_p6opaque parcel
splice parcel, args, 0, 0
# mark the Parcel as flattening objects
$P0 = get_hll_global ['Bool'], 'True'
setprop parcel, 'flatten', $P0
.return (parcel)
.end

Expand Down

0 comments on commit 47ef21b

Please sign in to comment.