OILS / builtin / method_list.py View on Github | oilshell.org

138 lines, 76 significant
1"""Methods on YSH List"""
2
3from __future__ import print_function
4
5from _devbuild.gen.value_asdl import (value, value_t)
6
7from core import num
8from core import vm
9from frontend import typed_args
10from mycpp import mops
11from mycpp.mylib import log
12from ysh import val_ops
13
14_ = log
15
16
17class Append(vm._Callable):
18
19 def __init__(self):
20 # type: () -> None
21 pass
22
23 def Call(self, rd):
24 # type: (typed_args.Reader) -> value_t
25
26 items = rd.PosList()
27 to_append = rd.PosValue()
28 rd.Done()
29
30 items.append(to_append)
31 return value.Null
32
33
34class Clear(vm._Callable):
35
36 def __init__(self):
37 # type: () -> None
38 pass
39
40 def Call(self, rd):
41 # type: (typed_args.Reader) -> value_t
42
43 li = rd.PosList()
44 rd.Done()
45
46 del li[:]
47
48 return value.Null
49
50
51class Extend(vm._Callable):
52
53 def __init__(self):
54 # type: () -> None
55 pass
56
57 def Call(self, rd):
58 # type: (typed_args.Reader) -> value_t
59
60 a = rd.PosList()
61 b = rd.PosList()
62 rd.Done()
63
64 a.extend(b)
65 return value.Null
66
67
68class Pop(vm._Callable):
69
70 def __init__(self):
71 # type: () -> None
72 pass
73
74 def Call(self, rd):
75 # type: (typed_args.Reader) -> value_t
76
77 items = rd.PosList()
78 rd.Done()
79
80 return items.pop()
81
82
83class Reverse(vm._Callable):
84
85 def __init__(self):
86 # type: () -> None
87 pass
88
89 def Call(self, rd):
90 # type: (typed_args.Reader) -> value_t
91
92 li = rd.PosList()
93 rd.Done()
94
95 li.reverse()
96
97 return value.Null
98
99
100class IndexOf(vm._Callable):
101
102 def __init__(self):
103 # type: () -> None
104 pass
105
106 def Call(self, rd):
107 # type: (typed_args.Reader) -> value_t
108
109 li = rd.PosList()
110 needle = rd.PosValue()
111 rd.Done()
112 i = 0
113 while i < len(li):
114 if val_ops.ExactlyEqual(li[i], needle, rd.LeftParenToken()):
115 return num.ToBig(i)
116 i += 1
117 return value.Int(mops.MINUS_ONE)
118
119
120class LastIndexOf(vm._Callable):
121
122 def __init__(self):
123 # type: () -> None
124 pass
125
126 def Call(self, rd):
127 # type: (typed_args.Reader) -> value_t
128
129 li = rd.PosList()
130 needle = rd.PosValue()
131 rd.Done()
132
133 i = len(li) - 1
134 while i > -1:
135 if val_ops.ExactlyEqual(li[i], needle, rd.LeftParenToken()):
136 return num.ToBig(i)
137 i -= 1
138 return value.Int(mops.MINUS_ONE)