1 | """Methods on YSH Dict"""
|
2 |
|
3 | from __future__ import print_function
|
4 |
|
5 | from _devbuild.gen.value_asdl import (value, value_e, value_t, Obj)
|
6 |
|
7 | from core import error
|
8 | from core import vm
|
9 | from frontend import typed_args
|
10 | from mycpp import mylib
|
11 | from mycpp.mylib import log, tagswitch
|
12 |
|
13 | from typing import cast, List
|
14 |
|
15 | _ = log
|
16 |
|
17 |
|
18 | class Keys(vm._Callable):
|
19 |
|
20 | def __init__(self):
|
21 | # type: () -> None
|
22 | pass
|
23 |
|
24 | def Call(self, rd):
|
25 | # type: (typed_args.Reader) -> value_t
|
26 |
|
27 | dictionary = rd.PosDict()
|
28 | rd.Done()
|
29 |
|
30 | keys = [value.Str(k) for k in dictionary.keys()] # type: List[value_t]
|
31 | return value.List(keys)
|
32 |
|
33 |
|
34 | class Values(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 | dictionary = rd.PosDict()
|
44 | rd.Done()
|
45 |
|
46 | values = dictionary.values() # type: List[value_t]
|
47 | return value.List(values)
|
48 |
|
49 |
|
50 | class Erase(vm._Callable):
|
51 |
|
52 | def __init__(self):
|
53 | # type: () -> None
|
54 | pass
|
55 |
|
56 | def Call(self, rd):
|
57 | # type: (typed_args.Reader) -> value_t
|
58 |
|
59 | dictionary = rd.PosDict()
|
60 | key = rd.PosStr()
|
61 | rd.Done()
|
62 |
|
63 | mylib.dict_erase(dictionary, key)
|
64 | return value.Null
|
65 |
|
66 |
|
67 | class Get(vm._Callable):
|
68 |
|
69 | def __init__(self):
|
70 | # type: () -> None
|
71 | pass
|
72 |
|
73 | def Call(self, rd):
|
74 | # type: (typed_args.Reader) -> value_t
|
75 |
|
76 | obj = rd.PosValue()
|
77 | key = rd.PosStr()
|
78 | default_value = rd.OptionalValue()
|
79 | rd.Done()
|
80 |
|
81 | UP_obj = obj
|
82 | with tagswitch(obj) as case:
|
83 | if case(value_e.Dict):
|
84 | obj = cast(value.Dict, UP_obj)
|
85 | d = obj.d
|
86 | elif case(value_e.Obj):
|
87 | obj = cast(Obj, UP_obj)
|
88 | d = obj.d
|
89 | else:
|
90 | raise error.TypeErr(obj, 'get() expected Dict or Obj',
|
91 | rd.BlamePos())
|
92 |
|
93 | if default_value is None:
|
94 | default_value = value.Null
|
95 | return d.get(key, default_value)
|