OILS / ysh / val_ops.py View on Github | oilshell.org

542 lines, 314 significant
1from __future__ import print_function
2
3from errno import EINTR
4
5from _devbuild.gen.syntax_asdl import loc, loc_t, command_t
6from _devbuild.gen.value_asdl import (value, value_e, value_t, eggex_ops,
7 eggex_ops_t, regex_match, RegexMatch)
8from core import error
9from core.error import e_die
10from display import ui
11from mycpp import mops
12from mycpp import mylib
13from mycpp.mylib import tagswitch, log
14from ysh import regex_translate
15
16from typing import TYPE_CHECKING, cast, Dict, List, Optional
17
18import libc
19import posix_ as posix
20
21_ = log
22
23if TYPE_CHECKING:
24 from core import state
25
26
27def ToInt(val, msg, blame_loc):
28 # type: (value_t, str, loc_t) -> int
29 UP_val = val
30 if val.tag() == value_e.Int:
31 val = cast(value.Int, UP_val)
32 return mops.BigTruncate(val.i)
33
34 raise error.TypeErr(val, msg, blame_loc)
35
36
37def ToFloat(val, msg, blame_loc):
38 # type: (value_t, str, loc_t) -> float
39 UP_val = val
40 if val.tag() == value_e.Float:
41 val = cast(value.Float, UP_val)
42 return val.f
43
44 raise error.TypeErr(val, msg, blame_loc)
45
46
47def ToStr(val, msg, blame_loc):
48 # type: (value_t, str, loc_t) -> str
49 UP_val = val
50 if val.tag() == value_e.Str:
51 val = cast(value.Str, UP_val)
52 return val.s
53
54 raise error.TypeErr(val, msg, blame_loc)
55
56
57def ToList(val, msg, blame_loc):
58 # type: (value_t, str, loc_t) -> List[value_t]
59 UP_val = val
60 if val.tag() == value_e.List:
61 val = cast(value.List, UP_val)
62 return val.items
63
64 raise error.TypeErr(val, msg, blame_loc)
65
66
67def ToDict(val, msg, blame_loc):
68 # type: (value_t, str, loc_t) -> Dict[str, value_t]
69 UP_val = val
70 if val.tag() == value_e.Dict:
71 val = cast(value.Dict, UP_val)
72 return val.d
73
74 raise error.TypeErr(val, msg, blame_loc)
75
76
77def ToCommandFrag(val, msg, blame_loc):
78 # type: (value_t, str, loc_t) -> command_t
79 UP_val = val
80 if val.tag() == value_e.CommandFrag:
81 val = cast(value.CommandFrag, UP_val)
82 return val.c
83
84 raise error.TypeErr(val, msg, blame_loc)
85
86
87def Stringify(val, blame_loc, op_desc):
88 # type: (value_t, loc_t, str) -> str
89 """
90 Args:
91 op_desc: could be empty string ''
92 or 'Expr Sub ' or 'Expr Splice ', with trailing space
93
94 Used by:
95
96 $[x] Expr Sub - stringify operator
97 @[x] Expr splice - each element is stringified
98 @x Splice value
99
100 str() Builtin function
101 join() Each element is stringified, e.g. join([1,2])
102 Not sure I like join([null, true]), but it's consistent
103 Str.replace() ^"x = $x" after eggex conversion function
104 """
105 if blame_loc is None:
106 blame_loc = loc.Missing
107
108 UP_val = val
109 with tagswitch(val) as case:
110 if case(value_e.Str): # trivial case
111 val = cast(value.Str, UP_val)
112 return val.s
113
114 elif case(value_e.Null):
115 s = 'null' # JSON spelling
116
117 elif case(value_e.Bool):
118 val = cast(value.Bool, UP_val)
119 s = 'true' if val.b else 'false' # JSON spelling
120
121 elif case(value_e.Int):
122 val = cast(value.Int, UP_val)
123 # e.g. decimal '42', the only sensible representation
124 s = mops.ToStr(val.i)
125
126 elif case(value_e.Float):
127 val = cast(value.Float, UP_val)
128 s = str(val.f)
129
130 elif case(value_e.Eggex):
131 val = cast(value.Eggex, UP_val)
132 s = regex_translate.AsPosixEre(val) # lazily converts to ERE
133
134 else:
135 pass # mycpp workaround
136
137 if val.tag() == value_e.List:
138 # Special error message for using the wrong sigil, or maybe join
139 raise error.TypeErrVerbose(
140 "%sgot a List, which can't be stringified (OILS-ERR-203)" %
141 op_desc, blame_loc)
142
143 raise error.TypeErr(
144 val,
145 "%sexpected one of (Null Bool Int Float Str Eggex)" % op_desc,
146 blame_loc)
147
148 return s
149
150
151def ToShellArray(val, blame_loc, prefix=''):
152 # type: (value_t, loc_t, str) -> List[str]
153 """
154 Used by
155
156 @[x] expression splice
157 @x splice value
158
159 Dicts do NOT get spliced, but they iterate over their keys
160 So this function NOT use Iterator.
161 """
162 UP_val = val
163 with tagswitch(val) as case2:
164 if case2(value_e.List):
165 val = cast(value.List, UP_val)
166 strs = [] # type: List[str]
167 # Note: it would be nice to add the index to the error message
168 # prefix, WITHOUT allocating a string for every item
169 for item in val.items:
170 strs.append(Stringify(item, blame_loc, prefix))
171
172 # I thought about getting rid of this to keep OSH and YSH separate,
173 # but:
174 # - readarray/mapfile returns bash array (ysh-user-feedback depends on it)
175 # - ysh-options tests parse_at too
176 elif case2(value_e.BashArray):
177 val = cast(value.BashArray, UP_val)
178 strs = val.strs
179
180 else:
181 raise error.TypeErr(val, "%sexpected List" % prefix, blame_loc)
182
183 return strs
184
185
186class Iterator(object):
187 """Interface for various types of for loop."""
188
189 def __init__(self):
190 # type: () -> None
191 self.i = 0
192
193 def Index(self):
194 # type: () -> int
195 return self.i
196
197 def Next(self):
198 # type: () -> None
199 self.i += 1
200
201 def FirstValue(self):
202 # type: () -> Optional[value_t]
203 """Return a value, or None if done
204
205 e.g. return Dict key or List value
206 """
207 raise NotImplementedError()
208
209 def SecondValue(self):
210 # type: () -> value_t
211 """Return Dict value or FAIL"""
212 raise AssertionError("Shouldn't have called this")
213
214
215class StdinIterator(Iterator):
216 """ for x in <> { """
217
218 def __init__(self, blame_loc):
219 # type: (loc_t) -> None
220 Iterator.__init__(self)
221 self.blame_loc = blame_loc
222 self.f = mylib.Stdin()
223
224 def FirstValue(self):
225 # type: () -> Optional[value_t]
226
227 # line, eof = read_osh.ReadLineSlowly(None, with_eol=False)
228 try:
229 line = self.f.readline()
230 except (IOError, OSError) as e: # signals
231 if e.errno == EINTR:
232 # Caller will can run traps with cmd_ev, like ReadLineSlowly
233 return value.Interrupted
234 else:
235 # For possible errors from f.readline(), see
236 # man read
237 # man getline
238 # e.g.
239 # - ENOMEM getline() allocation failure
240 # - EISDIR getline() read from directory descriptor!
241 #
242 # Note: the read builtin returns status 1 for EISDIR.
243 #
244 # We'll raise a top-level error like Python. (Awk prints a
245 # warning message)
246 e_die("I/O error in for <> loop: %s" % posix.strerror(e.errno),
247 self.blame_loc)
248
249 if len(line) == 0:
250 return None # Done
251 elif line.endswith('\n'):
252 # TODO: optimize this to prevent extra garbage
253 line = line[:-1]
254
255 return value.Str(line)
256
257
258class ArrayIter(Iterator):
259 """ for x in 1 2 3 { """
260
261 def __init__(self, strs):
262 # type: (List[str]) -> None
263 Iterator.__init__(self)
264 self.strs = strs
265 self.n = len(strs)
266
267 def FirstValue(self):
268 # type: () -> Optional[value_t]
269 if self.i == self.n:
270 return None
271 return value.Str(self.strs[self.i])
272
273
274class RangeIterator(Iterator):
275 """ for x in (m:n) { """
276
277 def __init__(self, val):
278 # type: (value.Range) -> None
279 Iterator.__init__(self)
280 self.val = val
281
282 def FirstValue(self):
283 # type: () -> Optional[value_t]
284 if self.val.lower + self.i >= self.val.upper:
285 return None
286
287 # TODO: range should be BigInt too
288 return value.Int(mops.IntWiden(self.val.lower + self.i))
289
290
291class ListIterator(Iterator):
292 """ for x in (mylist) { """
293
294 def __init__(self, val):
295 # type: (value.List) -> None
296 Iterator.__init__(self)
297 self.val = val
298 self.n = len(val.items)
299
300 def FirstValue(self):
301 # type: () -> Optional[value_t]
302 if self.i == self.n:
303 return None
304 return self.val.items[self.i]
305
306
307class DictIterator(Iterator):
308 """ for x in (mydict) { """
309
310 def __init__(self, val):
311 # type: (value.Dict) -> None
312 Iterator.__init__(self)
313
314 # TODO: Don't materialize these Lists
315 self.keys = val.d.keys() # type: List[str]
316 self.values = val.d.values() # type: List[value_t]
317
318 self.n = len(val.d)
319 assert self.n == len(self.keys)
320
321 def FirstValue(self):
322 # type: () -> value_t
323 if self.i == self.n:
324 return None
325 return value.Str(self.keys[self.i])
326
327 def SecondValue(self):
328 # type: () -> value_t
329 return self.values[self.i]
330
331
332def ToBool(val):
333 # type: (value_t) -> bool
334 """Convert any value to a boolean.
335
336 TODO: expose this as Bool(x), like Python's bool(x).
337 """
338 UP_val = val
339 with tagswitch(val) as case:
340 if case(value_e.Undef):
341 return False
342
343 elif case(value_e.Null):
344 return False
345
346 elif case(value_e.Str):
347 val = cast(value.Str, UP_val)
348 return len(val.s) != 0
349
350 # OLD TYPES
351 elif case(value_e.BashArray):
352 val = cast(value.BashArray, UP_val)
353 return len(val.strs) != 0
354
355 elif case(value_e.BashAssoc):
356 val = cast(value.BashAssoc, UP_val)
357 return len(val.d) != 0
358
359 elif case(value_e.Bool):
360 val = cast(value.Bool, UP_val)
361 return val.b
362
363 elif case(value_e.Int):
364 val = cast(value.Int, UP_val)
365 return not mops.Equal(val.i, mops.BigInt(0))
366
367 elif case(value_e.Float):
368 val = cast(value.Float, UP_val)
369 return val.f != 0.0
370
371 elif case(value_e.List):
372 val = cast(value.List, UP_val)
373 return len(val.items) > 0
374
375 elif case(value_e.Dict):
376 val = cast(value.Dict, UP_val)
377 return len(val.d) > 0
378
379 else:
380 return True # all other types are Truthy
381
382
383def ExactlyEqual(left, right, blame_loc):
384 # type: (value_t, value_t, loc_t) -> bool
385
386 if left.tag() == value_e.Float or right.tag() == value_e.Float:
387 raise error.TypeErrVerbose(
388 "Equality isn't defined on Float values (OILS-ERR-202)", blame_loc)
389
390 if left.tag() != right.tag():
391 return False
392
393 UP_left = left
394 UP_right = right
395 with tagswitch(left) as case:
396 if case(value_e.Undef):
397 return True # there's only one Undef
398
399 elif case(value_e.Null):
400 return True # there's only one Null
401
402 elif case(value_e.Bool):
403 left = cast(value.Bool, UP_left)
404 right = cast(value.Bool, UP_right)
405 return left.b == right.b
406
407 elif case(value_e.Int):
408 left = cast(value.Int, UP_left)
409 right = cast(value.Int, UP_right)
410 return mops.Equal(left.i, right.i)
411
412 elif case(value_e.Float):
413 raise AssertionError()
414
415 elif case(value_e.Str):
416 left = cast(value.Str, UP_left)
417 right = cast(value.Str, UP_right)
418 return left.s == right.s
419
420 elif case(value_e.BashArray):
421 left = cast(value.BashArray, UP_left)
422 right = cast(value.BashArray, UP_right)
423 if len(left.strs) != len(right.strs):
424 return False
425
426 for i in xrange(0, len(left.strs)):
427 if left.strs[i] != right.strs[i]:
428 return False
429
430 return True
431
432 elif case(value_e.List):
433 left = cast(value.List, UP_left)
434 right = cast(value.List, UP_right)
435 if len(left.items) != len(right.items):
436 return False
437
438 for i in xrange(0, len(left.items)):
439 if not ExactlyEqual(left.items[i], right.items[i], blame_loc):
440 return False
441
442 return True
443
444 elif case(value_e.BashAssoc):
445 left = cast(value.Dict, UP_left)
446 right = cast(value.Dict, UP_right)
447 if len(left.d) != len(right.d):
448 return False
449
450 for k in left.d.keys():
451 if k not in right.d or right.d[k] != left.d[k]:
452 return False
453
454 return True
455
456 elif case(value_e.Dict):
457 left = cast(value.Dict, UP_left)
458 right = cast(value.Dict, UP_right)
459 if len(left.d) != len(right.d):
460 return False
461
462 for k in left.d.keys():
463 if (k not in right.d or
464 not ExactlyEqual(right.d[k], left.d[k], blame_loc)):
465 return False
466
467 return True
468
469 raise error.TypeErrVerbose(
470 "Can't compare two values of type %s" % ui.ValType(left), blame_loc)
471
472
473def Contains(needle, haystack):
474 # type: (value_t, value_t) -> bool
475 """Haystack must be a Dict.
476
477 We should have mylist->find(x) !== -1 for searching through a List.
478 Things with different perf characteristics should look different.
479 """
480 UP_haystack = haystack
481 with tagswitch(haystack) as case:
482 if case(value_e.Dict):
483 haystack = cast(value.Dict, UP_haystack)
484 s = ToStr(needle, "LHS of 'in' should be Str", loc.Missing)
485 return s in haystack.d
486
487 else:
488 raise error.TypeErr(haystack, "RHS of 'in' should be Dict",
489 loc.Missing)
490
491 return False
492
493
494def MatchRegex(left, right, mem):
495 # type: (value_t, value_t, Optional[state.Mem]) -> bool
496 """
497 Args:
498 mem: Whether to set or clear matches
499 """
500 UP_right = right
501
502 with tagswitch(right) as case:
503 if case(value_e.Str): # plain ERE
504 right = cast(value.Str, UP_right)
505
506 right_s = right.s
507 regex_flags = 0
508 capture = eggex_ops.No # type: eggex_ops_t
509
510 elif case(value_e.Eggex):
511 right = cast(value.Eggex, UP_right)
512
513 right_s = regex_translate.AsPosixEre(right)
514 regex_flags = regex_translate.LibcFlags(right.canonical_flags)
515 capture = eggex_ops.Yes(right.convert_funcs, right.convert_toks,
516 right.capture_names)
517
518 else:
519 raise error.TypeErr(right, 'Expected Str or Regex for RHS of ~',
520 loc.Missing)
521
522 UP_left = left
523 left_s = None # type: str
524 with tagswitch(left) as case:
525 if case(value_e.Str):
526 left = cast(value.Str, UP_left)
527 left_s = left.s
528 else:
529 raise error.TypeErrVerbose('LHS must be a string', loc.Missing)
530
531 indices = libc.regex_search(right_s, regex_flags, left_s, 0)
532 if indices is not None:
533 if mem:
534 mem.SetRegexMatch(RegexMatch(left_s, indices, capture))
535 return True
536 else:
537 if mem:
538 mem.SetRegexMatch(regex_match.No)
539 return False
540
541
542# vim: sw=4