-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator.py
More file actions
233 lines (137 loc) · 5 KB
/
Copy pathoperator.py
File metadata and controls
233 lines (137 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
@namespace("operator")
from Promethium import List, ValueError
# A small, opt-in subset of Python's operator module.
#
# Import this module explicitly from Promethium code. It deliberately does not
# alter built-ins or provide target-specific behavior.
#
# `floordiv` implements CPython's floor-toward-negative-infinity semantics by
# hand rather than Promethium's `//` operator (unconfirmed to exist/behave
# the same way here — no file in this codebase uses it), using only `/` and
# `%` (both already relied on elsewhere: see `mod` above and
# PromethiumBaseLibrary's own `range()`), which are assumed to truncate
# toward zero like C/C#/Java's integer division.
#
# `contains`/`concat`/`countOf`/`getitem`/`setitem`/`indexOf` operate on
# `List[T]` generically, but deliberately avoid `List.index()`: Toffee's
# underlying `indexOfObject:` returns Cocoa's `NSNotFound` sentinel (not
# `-1`) when the item is missing, so a portable "not found" check can't
# just compare `index() >= 0`. `contains`/`countOf` use the already-portable
# `List.count()` instead, and `indexOf` does its own linear scan with
# `.Equals`/`.isEqual` — the same pattern `collections`' classes already use
# for exactly this reason (see `Counter.py`'s `_index_of`). `setitem` is a
# thin wrapper over `List`'s own `__setitem__` (bracket assignment).
def add(left: int, right: int) -> int:
return left + right
def add(left: float, right: float) -> float:
return left + right
def sub(left: int, right: int) -> int:
return left - right
def sub(left: float, right: float) -> float:
return left - right
def mul(left: int, right: int) -> int:
return left * right
def mul(left: float, right: float) -> float:
return left * right
def truediv(left: float, right: float) -> float:
return left / right
def mod(left: int, right: int) -> int:
return left % right
def mod(left: float, right: float) -> float:
return left % right
def neg(value: int) -> int:
return -value
def neg(value: float) -> float:
return -value
def pos(value: int) -> int:
return +value
def pos(value: float) -> float:
return +value
def eq(left: int, right: int) -> bool:
return left == right
def eq(left: float, right: float) -> bool:
return left == right
def eq(left: bool, right: bool) -> bool:
return left == right
def ne(left: int, right: int) -> bool:
return left != right
def ne(left: float, right: float) -> bool:
return left != right
def ne(left: bool, right: bool) -> bool:
return left != right
def lt(left: int, right: int) -> bool:
return left < right
def lt(left: float, right: float) -> bool:
return left < right
def le(left: int, right: int) -> bool:
return left <= right
def le(left: float, right: float) -> bool:
return left <= right
def gt(left: int, right: int) -> bool:
return left > right
def gt(left: float, right: float) -> bool:
return left > right
def ge(left: int, right: int) -> bool:
return left >= right
def ge(left: float, right: float) -> bool:
return left >= right
def not_(value: bool) -> bool:
return not value
def truth(value: bool) -> bool:
return value
def abs(value: int) -> int:
if value < 0:
return -value
return value
def abs(value: float) -> float:
if value < 0.0:
return -value
return value
def and_(left: int, right: int) -> int:
return left & right
def and_(left: bool, right: bool) -> bool:
return left and right
def or_(left: int, right: int) -> int:
return left | right
def or_(left: bool, right: bool) -> bool:
return left or right
def xor(left: int, right: int) -> int:
return left ^ right
def xor(left: bool, right: bool) -> bool:
return left != right
def invert(value: int) -> int:
return ~value
def lshift(value: int, count: int) -> int:
return value << count
def rshift(value: int, count: int) -> int:
return value >> count
def floordiv(left: int, right: int) -> int:
quotient: int = left / right
remainder: int = left % right
if remainder != 0 and (remainder < 0) != (right < 0):
quotient -= 1
return quotient
def getitem[T](container: List[T], index: int) -> T:
return container.__getitem__(index)
def setitem[T](container: List[T], index: int, value: T):
container[index] = value
def concat[T](a: List[T], b: List[T]) -> List[T]:
result: List[T] = a.copy()
result.extend(b)
return result
def contains[T](container: List[T], item: T) -> bool:
return container.count(item) > 0
def countOf[T](container: List[T], item: T) -> int:
return container.count(item)
def indexOf[T](container: List[T], item: T) -> int:
position: int = 0
while position < len(container):
candidate: T = container.__getitem__(position)
if defined("TOFFEE"):
if candidate.isEqual(item):
return position
else:
if candidate.Equals(item):
return position
position += 1
raise ValueError("sequence.index(x): x not in sequence")