tagged release 0.6.4
[parrot.git] / languages / tcl / runtime / builtin / lindex.pir
blobcbdd7cd7a129b281dbd8e1b467bbca7cd99b2edd
1 ###
2 # [source]
4 .HLL 'Tcl', 'tcl_group'
5 .namespace []
7 .sub '&lindex'
8   .param pmc argv :slurpy
10   .local int argc
11   argc = argv
12   if argc < 1 goto bad_args
14   .local pmc __list, __index
15   __list  = get_root_global ['_tcl'], '__list'
16   __index = get_root_global ['_tcl'], '__index'
18   .local pmc list
19   list = argv[0]
20   list = __list(list)
22 have_list:
23   if argc == 1 goto done
25   $I0 = 0
26 select_elem:
27   inc $I0
28   if $I0 == argc goto done
30   $P0 = argv[$I0]
31   .local pmc indices
32   push_eh not_a_list
33     indices = __list($P0)
34   pop_eh
35   goto select
37 not_a_list:
38   indices = new 'FixedPMCArray'
39   indices = 1
40   indices[0] = $P0
42 select:
43   .local int index
44   .local int elems
45   elems = elements indices
46   $I1 = 0
47 select_loop:
48   if $I1 >= elems goto select_elem
49   list = __list(list)
51   $P0 = indices[$I1]
52   index = __index($P0, list)
54   $I2 = elements list
55   if index >= $I2 goto empty
56   if index < 0    goto empty
58   list  = list[index]
60   inc $I1
61   goto select_loop
63 done:
64   .return(list)
66 empty:
67   .return('')
69 bad_args:
70   tcl_error 'wrong # args: should be "lindex list ?index...?"'
71 .end
73 # Local Variables:
74 #   mode: pir
75 #   fill-column: 100
76 # End:
77 # vim: expandtab shiftwidth=4 ft=pir: