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

744 lines, 429 significant
1#!/usr/bin/env python2
2"""
3func_misc.py
4"""
5from __future__ import print_function
6
7from _devbuild.gen.value_asdl import (value, value_e, value_t, value_str, Obj)
8
9from core import error
10from core import num
11from display import pp_value
12from display import ui
13from core import vm
14from data_lang import j8
15from frontend import match
16from frontend import typed_args
17from mycpp import mops
18from mycpp import mylib
19from mycpp.mylib import NewDict, iteritems, log, tagswitch
20from ysh import val_ops
21
22from typing import TYPE_CHECKING, Dict, List, Optional, cast
23if TYPE_CHECKING:
24 from osh import glob_
25 from osh import split
26
27_ = log
28
29
30class Object(vm._Callable):
31 """Create a value.Obj
32
33 The order of params follows JavaScript's Object.create():
34 var obj = Object(prototype, props)
35 """
36
37 def __init__(self):
38 # type: () -> None
39 pass
40
41 def Call(self, rd):
42 # type: (typed_args.Reader) -> value_t
43
44 prototype = rd.PosValue()
45 proto_loc = rd.BlamePos()
46
47 props = rd.PosDict()
48 rd.Done()
49
50 chain = None # type: Optional[Obj]
51 UP_prototype = prototype
52 with tagswitch(prototype) as case:
53 if case(value_e.Null):
54 pass
55 elif case(value_e.Obj):
56 prototype = cast(Obj, UP_prototype)
57 chain = prototype
58 else:
59 raise error.TypeErr(prototype, 'Object() expected Obj or Null',
60 proto_loc)
61
62 return Obj(chain, props)
63
64
65class Prototype(vm._Callable):
66 """Get an object's prototype."""
67
68 def __init__(self):
69 # type: () -> None
70 pass
71
72 def Call(self, rd):
73 # type: (typed_args.Reader) -> value_t
74 obj = rd.PosObj()
75 rd.Done()
76
77 if obj.prototype is None:
78 return value.Null
79
80 return obj.prototype
81
82
83class PropView(vm._Callable):
84 """Get a Dict view of an object's properties."""
85
86 def __init__(self):
87 # type: () -> None
88 pass
89
90 def Call(self, rd):
91 # type: (typed_args.Reader) -> value_t
92 obj = rd.PosObj()
93 rd.Done()
94
95 return value.Dict(obj.d)
96
97
98class Len(vm._Callable):
99
100 def __init__(self):
101 # type: () -> None
102 pass
103
104 def Call(self, rd):
105 # type: (typed_args.Reader) -> value_t
106
107 x = rd.PosValue()
108 rd.Done()
109
110 UP_x = x
111 with tagswitch(x) as case:
112 if case(value_e.List):
113 x = cast(value.List, UP_x)
114 return num.ToBig(len(x.items))
115
116 elif case(value_e.Dict):
117 x = cast(value.Dict, UP_x)
118 return num.ToBig(len(x.d))
119
120 elif case(value_e.Str):
121 x = cast(value.Str, UP_x)
122 return num.ToBig(len(x.s))
123
124 raise error.TypeErr(x, 'len() expected Str, List, or Dict',
125 rd.BlamePos())
126
127
128class Type(vm._Callable):
129
130 def __init__(self):
131 # type: () -> None
132 pass
133
134 def Call(self, rd):
135 # type: (typed_args.Reader) -> value_t
136
137 val = rd.PosValue()
138 rd.Done()
139
140 # TODO: assert it's not Undef, Interrupted, Slice
141 # Then return an Obj type
142 #
143 # It would be nice if they were immutable, if we didn't have to create
144 # 23-24 dicts and 23-24 Obj on startup?
145 return value.Str(ui.ValType(val))
146
147
148class Join(vm._Callable):
149 """Both free function join() and List->join() method."""
150
151 def __init__(self):
152 # type: () -> None
153 pass
154
155 def Call(self, rd):
156 # type: (typed_args.Reader) -> value_t
157
158 li = rd.PosList()
159 delim = rd.OptionalStr(default_='')
160 rd.Done()
161
162 strs = [] # type: List[str]
163 for i, el in enumerate(li):
164 strs.append(val_ops.Stringify(el, rd.LeftParenToken(), 'join() '))
165
166 return value.Str(delim.join(strs))
167
168
169class Maybe(vm._Callable):
170
171 def __init__(self):
172 # type: () -> None
173 pass
174
175 def Call(self, rd):
176 # type: (typed_args.Reader) -> value_t
177
178 val = rd.PosValue()
179 rd.Done()
180
181 if val == value.Null:
182 return value.List([])
183
184 s = val_ops.ToStr(
185 val, 'maybe() expected Str, but got %s' % value_str(val.tag()),
186 rd.LeftParenToken())
187 if len(s):
188 return value.List([val]) # use val to avoid needlessly copy
189
190 return value.List([])
191
192
193class Bool(vm._Callable):
194
195 def __init__(self):
196 # type: () -> None
197 pass
198
199 def Call(self, rd):
200 # type: (typed_args.Reader) -> value_t
201
202 val = rd.PosValue()
203 rd.Done()
204
205 return value.Bool(val_ops.ToBool(val))
206
207
208class Int(vm._Callable):
209
210 def __init__(self):
211 # type: () -> None
212 pass
213
214 def Call(self, rd):
215 # type: (typed_args.Reader) -> value_t
216
217 val = rd.PosValue()
218 rd.Done()
219
220 UP_val = val
221 with tagswitch(val) as case:
222 if case(value_e.Int):
223 return val
224
225 elif case(value_e.Bool):
226 val = cast(value.Bool, UP_val)
227 return value.Int(mops.FromBool(val.b))
228
229 elif case(value_e.Float):
230 val = cast(value.Float, UP_val)
231 ok, big_int = mops.FromFloat(val.f)
232 if ok:
233 return value.Int(big_int)
234 else:
235 raise error.Expr(
236 "Can't convert float %s to Int" %
237 pp_value.FloatString(val.f), rd.BlamePos())
238
239 elif case(value_e.Str):
240 val = cast(value.Str, UP_val)
241 if not match.LooksLikeYshInt(val.s):
242 raise error.Expr("Can't convert %s to Int" % val.s,
243 rd.BlamePos())
244
245 s = val.s.replace('_', '')
246 ok, big_int = mops.FromStr2(s)
247 if not ok:
248 raise error.Expr("Integer too big: %s" % val.s,
249 rd.BlamePos())
250
251 return value.Int(big_int)
252
253 raise error.TypeErr(val, 'int() expected Bool, Int, Float, or Str',
254 rd.BlamePos())
255
256
257class Float(vm._Callable):
258
259 def __init__(self):
260 # type: () -> None
261 pass
262
263 def Call(self, rd):
264 # type: (typed_args.Reader) -> value_t
265
266 val = rd.PosValue()
267 rd.Done()
268
269 UP_val = val
270 with tagswitch(val) as case:
271 if case(value_e.Int):
272 val = cast(value.Int, UP_val)
273 return value.Float(mops.ToFloat(val.i))
274
275 elif case(value_e.Float):
276 return val
277
278 elif case(value_e.Str):
279 val = cast(value.Str, UP_val)
280 if not match.LooksLikeYshFloat(val.s):
281 raise error.Expr('Cannot convert %s to Float' % val.s,
282 rd.BlamePos())
283
284 return value.Float(float(val.s))
285
286 raise error.TypeErr(val, 'float() expected Int, Float, or Str',
287 rd.BlamePos())
288
289
290class Str_(vm._Callable):
291
292 def __init__(self):
293 # type: () -> None
294 pass
295
296 def Call(self, rd):
297 # type: (typed_args.Reader) -> value_t
298
299 val = rd.PosValue()
300 rd.Done()
301
302 with tagswitch(val) as case:
303 # Avoid extra allocation
304 if case(value_e.Str):
305 return val
306 else:
307 s = val_ops.Stringify(val, rd.LeftParenToken(), 'str() ')
308 return value.Str(s)
309
310
311class List_(vm._Callable):
312
313 def __init__(self):
314 # type: () -> None
315 pass
316
317 def Call(self, rd):
318 # type: (typed_args.Reader) -> value_t
319
320 val = rd.PosValue()
321 rd.Done()
322
323 l = [] # type: List[value_t]
324 it = None # type: val_ops.Iterator
325 UP_val = val
326 with tagswitch(val) as case:
327 if case(value_e.List):
328 val = cast(value.List, UP_val)
329 it = val_ops.ListIterator(val)
330
331 elif case(value_e.Dict):
332 val = cast(value.Dict, UP_val)
333 it = val_ops.DictIterator(val)
334
335 elif case(value_e.Range):
336 val = cast(value.Range, UP_val)
337 it = val_ops.RangeIterator(val)
338
339 else:
340 raise error.TypeErr(val,
341 'list() expected Dict, List, or Range',
342 rd.BlamePos())
343
344 assert it is not None
345 while True:
346 first = it.FirstValue()
347 if first is None:
348 break
349 l.append(first)
350 it.Next()
351
352 return value.List(l)
353
354
355class DictFunc(vm._Callable):
356
357 def __init__(self):
358 # type: () -> None
359 pass
360
361 def Call(self, rd):
362 # type: (typed_args.Reader) -> value_t
363
364 val = rd.PosValue()
365 rd.Done()
366
367 UP_val = val
368 with tagswitch(val) as case:
369 if case(value_e.Dict):
370 d = NewDict() # type: Dict[str, value_t]
371 val = cast(value.Dict, UP_val)
372 for k, v in iteritems(val.d):
373 d[k] = v
374
375 return value.Dict(d)
376
377 elif case(value_e.Obj):
378 d = NewDict()
379 val = cast(Obj, UP_val)
380 for k, v in iteritems(val.d):
381 d[k] = v
382
383 return value.Dict(d)
384
385 elif case(value_e.BashAssoc):
386 d = NewDict()
387 val = cast(value.BashAssoc, UP_val)
388 for k, s in iteritems(val.d):
389 d[k] = value.Str(s)
390
391 return value.Dict(d)
392
393 raise error.TypeErr(val, 'dict() expected Dict, Obj, or BashAssoc',
394 rd.BlamePos())
395
396
397class Runes(vm._Callable):
398
399 def __init__(self):
400 # type: () -> None
401 pass
402
403 def Call(self, rd):
404 # type: (typed_args.Reader) -> value_t
405 return value.Null
406
407
408class EncodeRunes(vm._Callable):
409
410 def __init__(self):
411 # type: () -> None
412 pass
413
414 def Call(self, rd):
415 # type: (typed_args.Reader) -> value_t
416 return value.Null
417
418
419class Bytes(vm._Callable):
420
421 def __init__(self):
422 # type: () -> None
423 pass
424
425 def Call(self, rd):
426 # type: (typed_args.Reader) -> value_t
427 return value.Null
428
429
430class EncodeBytes(vm._Callable):
431
432 def __init__(self):
433 # type: () -> None
434 pass
435
436 def Call(self, rd):
437 # type: (typed_args.Reader) -> value_t
438 return value.Null
439
440
441class Split(vm._Callable):
442
443 def __init__(self, splitter):
444 # type: (split.SplitContext) -> None
445 vm._Callable.__init__(self)
446 self.splitter = splitter
447
448 def Call(self, rd):
449 # type: (typed_args.Reader) -> value_t
450 s = rd.PosStr()
451
452 ifs = rd.OptionalStr()
453
454 rd.Done()
455
456 l = [
457 value.Str(elem)
458 for elem in self.splitter.SplitForWordEval(s, ifs=ifs)
459 ] # type: List[value_t]
460 return value.List(l)
461
462
463class FloatsEqual(vm._Callable):
464
465 def __init__(self):
466 # type: () -> None
467 pass
468
469 def Call(self, rd):
470 # type: (typed_args.Reader) -> value_t
471 left = rd.PosFloat()
472 right = rd.PosFloat()
473 rd.Done()
474
475 return value.Bool(left == right)
476
477
478class Glob(vm._Callable):
479
480 def __init__(self, globber):
481 # type: (glob_.Globber) -> None
482 vm._Callable.__init__(self)
483 self.globber = globber
484
485 def Call(self, rd):
486 # type: (typed_args.Reader) -> value_t
487 s = rd.PosStr()
488 rd.Done()
489
490 out = [] # type: List[str]
491 self.globber._Glob(s, out)
492
493 l = [value.Str(elem) for elem in out] # type: List[value_t]
494 return value.List(l)
495
496
497class ToJson8(vm._Callable):
498
499 def __init__(self, is_j8):
500 # type: (bool) -> None
501 self.is_j8 = is_j8
502
503 def Call(self, rd):
504 # type: (typed_args.Reader) -> value_t
505
506 val = rd.PosValue()
507 space = mops.BigTruncate(rd.NamedInt('space', 0))
508 rd.Done()
509
510 # Convert from external JS-like API to internal API.
511 if space <= 0:
512 indent = -1
513 else:
514 indent = space
515
516 buf = mylib.BufWriter()
517 try:
518 if self.is_j8:
519 j8.PrintMessage(val, buf, indent)
520 else:
521 j8.PrintJsonMessage(val, buf, indent)
522 except error.Encode as e:
523 # status code 4 is special, for encode/decode errors.
524 raise error.Structured(4, e.Message(), rd.LeftParenToken())
525
526 return value.Str(buf.getvalue())
527
528
529class FromJson8(vm._Callable):
530
531 def __init__(self, is_j8):
532 # type: (bool) -> None
533 self.is_j8 = is_j8
534
535 def Call(self, rd):
536 # type: (typed_args.Reader) -> value_t
537
538 s = rd.PosStr()
539 rd.Done()
540
541 p = j8.Parser(s, self.is_j8)
542 try:
543 val = p.ParseValue()
544 except error.Decode as e:
545 # Right now I'm not exposing the original string, because that
546 # could lead to a memory leak in the _error Dict.
547 # The message quotes part of the string, and we could improve
548 # that. We could have a substring with context.
549 props = {
550 'start_pos': num.ToBig(e.start_pos),
551 'end_pos': num.ToBig(e.end_pos),
552 } # type: Dict[str, value_t]
553 # status code 4 is special, for encode/decode errors.
554 raise error.Structured(4, e.Message(), rd.LeftParenToken(), props)
555
556 return val
557
558
559class BashArrayToSparse(vm._Callable):
560 """
561 value.BashArray -> value.SparseArray, for testing
562 """
563
564 def __init__(self):
565 # type: () -> None
566 pass
567
568 def Call(self, rd):
569 # type: (typed_args.Reader) -> value_t
570
571 strs = rd.PosBashArray()
572 rd.Done()
573
574 d = {} # type: Dict[mops.BigInt, str]
575 max_index = mops.MINUS_ONE # max index for empty array
576 for i, s in enumerate(strs):
577 if s is not None:
578 big_i = mops.IntWiden(i)
579 d[big_i] = s
580 if mops.Greater(big_i, max_index):
581 max_index = big_i
582
583 return value.SparseArray(d, max_index)
584
585
586class SparseOp(vm._Callable):
587 """
588 All ops on value.SparseArray, for testing performance
589 """
590
591 def __init__(self):
592 # type: () -> None
593 pass
594
595 def Call(self, rd):
596 # type: (typed_args.Reader) -> value_t
597
598 sp = rd.PosSparseArray()
599 d = sp.d
600 #i = mops.BigTruncate(rd.PosInt())
601 op_name = rd.PosStr()
602
603 no_str = None # type: str
604
605 if op_name == 'len': # ${#a[@]}
606 rd.Done()
607 return num.ToBig(len(d))
608
609 elif op_name == 'get': # ${a[42]}
610 index = rd.PosInt()
611 rd.Done()
612
613 s = d.get(index)
614 if s is None:
615 return value.Null
616 else:
617 return value.Str(s)
618
619 elif op_name == 'set': # a[42]=foo
620 index = rd.PosInt()
621 s = rd.PosStr()
622 rd.Done()
623
624 d[index] = s
625
626 if mops.Greater(index, sp.max_index):
627 sp.max_index = index
628
629 return value.Int(mops.ZERO)
630
631 elif op_name == 'unset': # unset 'a[1]'
632 index = rd.PosInt()
633 rd.Done()
634
635 mylib.dict_erase(d, index)
636
637 max_index = mops.MINUS_ONE # Note: this works if d is not empty
638 for i1 in d:
639 if mops.Greater(i1, max_index): # i1 > max_index
640 max_index = i1
641 sp.max_index = max_index
642
643 return value.Int(mops.ZERO)
644
645 elif op_name == 'subst': # "${a[@]}"
646 # Algorithm to expand a Dict[BigInt, Str]
647 #
648 # 1. Copy the integer keys into a new List
649 # 2. Sort them in numeric order
650 # 3. Create a List[str] that's the same size as the keys
651 # 4. Loop through sorted keys, look up value, and populate list
652 #
653 # There is another possible algorithm:
654 #
655 # 1. Copy the VALUES into a new list
656 # 2. Somehow sort them by the CORRESPONDING key, which depends on
657 # Slab<> POSITION. I think this does not fit within the
658 # std::sort() model. I think we would have to write a little custom
659 # sort algorithm.
660
661 keys = d.keys()
662 mylib.BigIntSort(keys)
663 # Pre-allocate
664 items = [no_str] * len(d) # type: List[str]
665 j = 0
666 for i in keys:
667 s = d.get(i)
668 assert s is not None
669 items[j] = s
670 j += 1
671 return value.BashArray(items)
672
673 elif op_name == 'keys': # "${!a[@]}"
674 keys = d.keys()
675 mylib.BigIntSort(keys)
676 items = [mops.ToStr(k) for k in keys]
677
678 # TODO: return SparseArray
679 return value.BashArray(items)
680
681 elif op_name == 'slice': # "${a[@]:0:5}"
682 start = rd.PosInt()
683 end = rd.PosInt()
684 rd.Done()
685
686 n = mops.BigTruncate(mops.Sub(end, start))
687 #log('start %d - end %d', start.i, end.i)
688
689 # Pre-allocate
690 items2 = [no_str] * n # type: List[str]
691
692 # Iterate from start to end. Note that this algorithm is
693 # theoretically slower than bash in the case where the array is
694 # sparse (in the part selected by the slice)
695 #
696 # e.g. if you do ${a[@]:1:1000} e.g. to SHIFT, and there are only 3
697 # elements, OSH will iterate through 999 integers and do 999 dict
698 # lookups, while bash will follow 3 pointers.
699 #
700 # However, in practice, I think iterating through integers is
701 # cheap.
702
703 j = 0
704 i = start
705 while mops.Greater(end, i): # i < end
706 s = d.get(i)
707 #log('s %s', s)
708 if s is not None:
709 items2[j] = s
710 j += 1
711
712 i = mops.Add(i, mops.ONE) # i += 1
713
714 # TODO: return SparseArray
715 return value.BashArray(items2)
716
717 elif op_name == 'append': # a+=(x y)
718 strs = rd.PosBashArray()
719
720 # TODO: We can maintain the max index in the value.SparseArray(),
721 # so that it's O(1) to append rather than O(n)
722 # - Update on 'set' is O(1)
723 # - Update on 'unset' is potentially O(n)
724
725 if 0:
726 max_index = mops.MINUS_ONE # Note: this works for empty arrays
727 for i1 in d:
728 if mops.Greater(i1, max_index): # i1 > max_index
729 max_index = i1
730 else:
731 max_index = sp.max_index
732
733 i2 = mops.Add(max_index, mops.ONE) # i2 = max_index + 1
734 for s in strs:
735 d[i2] = s
736 i2 = mops.Add(i2, mops.ONE) # i2 += 1
737
738 # sp.max_index += len(strs)
739 sp.max_index = mops.Add(sp.max_index, mops.IntWiden(len(strs)))
740 return value.Int(mops.ZERO)
741
742 else:
743 print('Invalid SparseArray operation %r' % op_name)
744 return value.Int(mops.ZERO)