Commits

Anonymous committed 0501ae3

Add Pynie;list class

Comments (0)

Files changed (7)

Lib/test/parrot/class-list.py

+#!./parrot
+
+print('test: len')
+a = [1,2,3]
+if len(a) == 3: print('success: len')
+else: print('failure: len')
+
+print('test: equality')
+a = [0, 1, 2]
+if a == [0, 1, 2] and a != [7, 8, 9]: print('success: equality')
+else: print('failure: equality')
+
+print('test: append')
+a = []
+for i in range(3): a.append(i)
+if a == [0, 1, 2]: print('success: append')
+else: print('failure: append')
+
+print('test: count')
+a = [1,2,5,6,5,5,5,5,6]
+if a.count(5) == 5 and a.count(6) == 2: print('success: count')
+else: print('failure: count')
+
+print('test: extend')
+a = [0, 1, 2]
+b = [7, 8, 9]
+a.extend(b)
+# b should be added to a, but should not be affected
+if a == [0, 1, 2, 7, 8, 9] and b == [7, 8, 9]: print('success: extend')
+else: print('failure: extend')
+
+print('test: index')
+a = [5,8,7]
+# XXX check for ValueError if value doesn't exist
+if a.index(8) == 1: print('success: index')
+else: print('failure: index')
+
+print('test: insert')
+a = [0, 1, 2]
+a.insert(1, 8)
+if a == [0, 8, 1, 2]: print('success: insert')
+else: print('failure: insert')
+
+print('test: pop')
+a = [0, 1, 2]
+i = a.pop()     # pop the 2
+j = a.pop(0)    # pop the 0
+if i == 2 and j == 0: print('success: pop')
+else: print('failure: pop')
+
+print('test: remove')
+a = [5,8,7]
+# XXX check for ValueError if value doesn't exist
+a.remove(8)
+if a == [5, 7]: print('success: remove')
+else: print('failure: remove')
+
+print('test: reverse (empty)')
+a = []
+a.reverse()
+if a == []: print('success: reverse (empty)')
+else: print('failure: reverse (empty)')
+
+print('test: reverse (even element count)')
+a = [1, 2, 3, 4, 5, 6]
+a.reverse()
+if a == [6,5,4,3,2,1]: print('success: reverse (even element count)')
+else: print('failure: reverse (even element count)')
+
+print('test: reverse (odd element count)')
+a = [1, 2, 3, 4, 5]
+a.reverse()
+if a == [5,4,3,2,1]: print('success: reverse (odd element count)')
+else: print('failure: reverse (odd element count)')
+
+print('test: str(list)')
+a = [0, 1, 2]
+if str(a) == '[0, 1, 2]': print('success: str(list)')
+else: print('failure: str(list)')
+
+print('test: repr(list)')
+a = [0, 1, 2]
+if repr(a) == '[0, 1, 2]': print('success: repr(list)')
+else: print('failure: repr(list)')

Lib/test/parrot/control-flow.py

 print('test: nested fors')
 lst = [ ]
 for i in range(3):
-    # XXX ResizablePMCArray has a 'push' method, use list.append
     for j in range(3):
-        lst.push((i + 1) * (j + 1))
+        lst.append((i + 1) * (j + 1))
 compare = 1
 lst2 = [ 1, 2, 3,  2, 4, 6,  3, 6, 9 ]
 for i in range(9):
     def test_logic_ops_compare(self):
         """pynie.pbc Lib/test/parrot/op-logic-compare.py"""
 
+    def test_class_list(self):
+        """pynie.pbc Lib/test/parrot/class-list.py"""
+
     def test_grammar(self):
         """pynie.pbc Lib/test/parrot/grammar.py"""
 
   'src/builtins/funcs.pir', 
   'src/builtins/io.pir', 
   'src/builtins/lists.pir', 
-  'src/builtins/oper.pir' ]
+  'src/builtins/oper.pir',
+  'src/classes/pynielist.pir' ]
 
 CLEANUPS = GEN_SOURCES + [
   'pynie.pbc',

src/builtins/funcs.pir

 
 
     # Now iterate over all the lists together
-    result = new 'ResizablePMCArray'
+    result = new [ 'Pynie' ; 'list' ]
 loop:
     func_params = new 'FixedPMCArray'
     func_params = num_lists
      .local pmc tmp
 
      $I0 = 0
-     lst = new 'ResizablePMCArray'
+     lst = new [ 'Pynie' ; 'list' ]
      stopn = stop
 loop:
      unless $I0 < stopn goto done
      tmp = new 'Integer'
      tmp = $I0
-     lst.'push'(tmp)
+     push lst, tmp
 
      inc $I0
      goto loop

src/builtins/lists.pir

 .sub 'listmaker'
     .param pmc args  :slurpy
     unless null args goto have_args
-    args = new 'ResizablePMCArray'
+    args = new [ 'Pynie' ; 'list' ]
   have_args:
     .return (args)
 .end

src/classes/pynielist.pir

+.sub '' :anon :init :load
+    $P0 = subclass 'ResizablePMCArray', [ 'Pynie' ; 'list' ]
+
+    $P2 = get_class 'ResizablePMCArray'
+    $P1 = getinterp
+    $P1.'hll_map'($P2, $P0)
+.end
+
+
+.namespace [ 'Pynie' ; 'list' ]
+
+
+.sub 'get_string' :vtable
+    .local pmc iter
+    .local string result
+
+    iter = new 'Iterator', self
+
+    result = '['
+    unless iter goto done
+
+    # first element separately, to handle commas properly
+    $P0 = shift iter
+    $S0 = 'repr'($P0)
+    result .= $S0
+
+loop:
+    unless iter goto done
+    result .= ', '
+
+    $P0 = shift iter
+    $S0 = 'repr'($P0)
+    result .= $S0
+    goto loop
+
+done:
+    result .= ']'
+    .return (result)
+.end
+
+
+.sub 'is_equal' :vtable
+    .param pmc other
+
+    $I0 = isa other, [ 'Pynie' ; 'list' ]
+    unless $I0 goto no
+
+    $I0 = elements self
+    $I1 = elements other
+    if $I0 != $I1 goto no
+
+    .local pmc i, j, item1, item2
+    i = new 'Iterator', self
+    j = new 'Iterator', 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, iter
+    ret = new 'Integer'
+    ret = 0
+    
+    iter = new 'Iterator', self
+loop:
+    unless iter goto done
+    $P0 = shift iter
+    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 iter
+    .local int idx
+    
+    idx = -1
+    iter = new 'Iterator', self
+loop:
+    unless iter goto not_found
+    inc idx
+    $P0 = shift iter
+    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 []