Source

pynie / src / classes / pynielist.pir

Full commit
.sub '' :anon :init :load
    $P0 = subclass 'ResizablePMCArray', [ 'list' ]

    $P1 = get_global [ 'type' ], 'wrap_class'
    $P1('list', $P0)

    $P2 = get_class 'ResizablePMCArray'
    $P1 = getinterp
    $P1.'hll_map'($P2, $P0)
.end


.namespace [ 'list' ]


=item list([sequence])

Return a list whose items are the same and in the same order as sequence's items.
sequence may be either a sequence, a container that supports iteration, or an
iterator object. If sequence is already a list, a copy is made and returned,
similar to sequence[:]. For instance, list('abc') returns ['a', 'b', 'c'] and
list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, returns a new
empty list, [].

=cut

.sub '__init__' :method
    .param pmc sequence :optional
    .param int has_seq :opt_flag

    unless has_seq goto no_seq
    self.'extend'(sequence)
  no_seq:
    # do nothing, just an empty list
.end

.sub 'get_string' :vtable
    .local pmc iterator
    .local string result

    iterator = iter self

    result = '['
    unless iterator goto done

    # first element separately, to handle commas properly
    $P0 = shift iterator
    $S0 = 'repr'($P0)
    result .= $S0

loop:
    unless iterator goto done
    result .= ', '

    $P0 = shift iterator
    $S0 = 'repr'($P0)
    result .= $S0
    goto loop

done:
    result .= ']'
    .return (result)
.end


.sub 'is_equal' :vtable
    .param pmc other

    $I0 = isa other, [ 'list' ]
    unless $I0 goto no

    $I0 = elements self
    $I1 = elements other
    if $I0 != $I1 goto no

    .local pmc i, j, item1, item2
    i = iter self
    j = iter other
loop:
    # if no more elements and everything matched, then yes
    unless i goto yes
    item1 = shift i
    item2 = shift j
    if item1 != item2 goto no
    goto loop

yes:
    .return (1)

no:
    .return (0)
.end


.sub 'append' :method
    .param pmc x
    push self, x
.end


.sub 'count' :method
    .param pmc value

    .local pmc ret, iterator
    ret = new 'Integer'
    ret = 0
    
    iterator = iter self
loop:
    unless iterator goto done
    $P0 = shift iterator
    if $P0 != value goto loop
    inc ret
    goto loop

done:
    .return (ret)
.end


.sub 'extend' :method
    .param pmc iterable
    $I0 = elements self
    splice self, iterable, $I0, 0
.end


.sub 'index' :method
    .param pmc value
    .param pmc start        :optional
    .param int has_start    :opt_flag
    .param pmc stop         :optional
    .param int has_stop     :opt_flag

    if has_start goto not_yet
    if has_stop goto not_yet

    .local pmc iterator
    .local int idx
    
    idx = -1
    iterator = iter self
loop:
    unless iterator goto not_found
    inc idx
    $P0 = shift iterator
    unless $P0 == value goto loop

    # found!
    .return (idx)

not_found:
    $P0 = new 'Exception'
    $P0 = 'ValueError'
    throw $P0

not_yet:
    $P0 = new 'Exception'
    $P0 = 'NotImplementedError'      # XXX
    throw $P0
.end


.sub 'insert' :method
    .param pmc index
    .param pmc object

    $I0 = index
    $P0 = new 'ResizablePMCArray'
    push $P0, object
    splice self, $P0, $I0, 0
.end

.sub 'pop' :method
    .param int idx      :optional
    .param int has_idx  :opt_flag

    unless has_idx goto no_idx
    $P1 = self[idx]
    delete self[idx]
    .return($P1)

no_idx:
    $P1 = pop self
    .return($P1)
.end


.sub 'remove' :method
    .param pmc value
    $I0 = self.'index'(value)
    delete self[$I0]
.end


.sub 'reverse' :method
    .local int i, j
    .local pmc a, b

    i = 0
    j = elements self
    dec j

loop:
    if i >= j goto done

    a = self[i]
    b = self[j]
    self[i] = b
    self[j] = a

    inc i
    dec j
    goto loop

done:
.end


.sub 'sort' :method
    .param pmc key          :named :optional
    .param pmc reverse      :named :optional
    
    $P0 = new 'Exception'
    $P0 = 'NotImplementedError'      # XXX
    throw $P0
.end

.namespace []