tagged release 0.7.1
[parrot.git] / languages / tcl / runtime / builtin / lindex.pir
blobdcef83f844b80f5b22bbd64a9de822742576946d
1 .HLL 'Tcl', ''
2 .namespace []
4 .sub '&lindex'
5   .param pmc argv :slurpy
7   .local int argc
8   argc = elements argv
9   if argc < 1 goto bad_args
11   .local pmc toList, getIndex
12   toList  = get_root_global ['_tcl'], 'toList'
13   getIndex = get_root_global ['_tcl'], 'getIndex'
15   .local pmc list
16   list = argv[0]
17   list = toList(list)
19 have_list:
20   if argc == 1 goto done
22   $I0 = 0
23 select_elem:
24   inc $I0
25   if $I0 == argc goto done
27   $P0 = argv[$I0]
28   .local pmc indices
29   push_eh not_a_list
30     indices = toList($P0)
31   pop_eh
32   goto select
34 not_a_list:
35   indices = new 'FixedPMCArray'
36   indices = 1
37   indices[0] = $P0
39 select:
40   .local int index
41   .local int elems
42   elems = elements indices
43   $I1 = 0
44 select_loop:
45   if $I1 >= elems goto select_elem
46   list = toList(list)
48   $P0 = indices[$I1]
49   index = getIndex($P0, list)
51   $I2 = elements list
52   if index >= $I2 goto empty
53   if index < 0    goto empty
55   list  = list[index]
57   inc $I1
58   goto select_loop
60 done:
61   .return(list)
63 empty:
64   .return('')
66 bad_args:
67   die 'wrong # args: should be "lindex list ?index...?"'
68 .end
70 # Local Variables:
71 #   mode: pir
72 #   fill-column: 100
73 # End:
74 # vim: expandtab shiftwidth=4 ft=pir: