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

100 lines, 55 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 Extend(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 a = rd.PosList()
44 b = rd.PosList()
45 rd.Done()
46
47 a.extend(b)
48 return value.Null
49
50
51class Pop(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 items = rd.PosList()
61 rd.Done()
62
63 return items.pop()
64
65
66class Reverse(vm._Callable):
67
68 def __init__(self):
69 # type: () -> None
70 pass
71
72 def Call(self, rd):
73 # type: (typed_args.Reader) -> value_t
74
75 li = rd.PosList()
76 rd.Done()
77
78 li.reverse()
79
80 return value.Null
81
82
83class IndexOf(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 needle = rd.PosValue()
94 rd.Done()
95 i = 0
96 while i < len(li):
97 if val_ops.ExactlyEqual(li[i], needle, rd.LeftParenToken()):
98 return num.ToBig(i)
99 i += 1
100 return value.Int(mops.MINUS_ONE)