OILS / core / value.asdl View on Github | oilshell.org

198 lines, 79 significant
1# Runtime value
2
3module value
4{
5 # import from frontend/syntax.asdl
6 use frontend syntax {
7 loc Token
8 expr command
9 DoubleQuoted
10 re proc_sig
11 Func
12 NameType
13 EggexFlag
14 BraceGroup SourceLine
15 }
16
17 use core runtime {
18 Cell
19 }
20
21 IntBox = (int i)
22
23 ProcDefaults = (
24 List[value]? for_word, # all of them are value.Str
25 List[value]? for_typed,
26 Dict[str, value]? for_named,
27 value? for_block,
28 )
29
30 LeftName = (str name, loc blame_loc)
31
32 # for setvar, and value.Place
33 y_lvalue =
34 # e.g. read (&x)
35 Local %LeftName
36 # e.g. &a[0][1].key -- we evaluate a[0][1] first
37 | Container(value obj, value index)
38
39 # An sh_lvalue is for things mutation that happen with dynamic scope
40 #
41 # - sh_expr_eval uses this for unset / printf -v
42 # - word_eval uses this for ${a[0]=}
43 # - expr_eval / cmd_eval use this for setvar a[i] = 42
44 sh_lvalue =
45 Var %LeftName
46 | Indexed(str name, int index, loc blame_loc)
47 | Keyed(str name, str key, loc blame_loc)
48
49 eggex_ops =
50 # for BASH_REMATCH or ~ with a string
51 No
52 # These lists are indexed by group number, and will have None entries
53 | Yes(List[value?] convert_funcs, List[Token?] convert_toks,
54 List[str?] capture_names)
55
56 RegexMatch = (str s, List[int] indices, eggex_ops ops)
57
58 regex_match =
59 No
60 | Yes %RegexMatch
61
62 # Retain references to lines
63 LiteralBlock = (BraceGroup brace_group, List[SourceLine] lines)
64
65 # TODO: should Expr also have backing lines?
66 cmd_frag =
67 LiteralBlock %LiteralBlock # p { echo hi } has backing lines
68 | Expr(command c) # var b = ^(echo hi)
69
70 # Arbitrary objects, where attributes are looked up on the prototype chain.
71 Obj = (Obj? prototype, Dict[str, value] d)
72
73 # Commands, words, and expressions from syntax.asdl are evaluated to a VALUE.
74 # value_t instances are stored in state.Mem().
75 value =
76 # Only used for val_ops.StdinIterator. (It would be nice if we could
77 # express iter_value.{Eof,Interrupted,Str,Int,...} in ASDL)
78 Interrupted
79 | Stdin
80
81 # Methods on state.Mem return value.Undef, but it's not visible in YSH.
82 #
83 # A var bound to Undef is different than no binding because of dynamic
84 # scope. Undef can shadow values lower on the stack.
85 | Undef
86
87 | Str(str s)
88
89 # "holes" in the array are represented by None
90 | BashArray(List[str] strs)
91 # TODO: Switch to this more efficient representation?
92 # max_index makes append-sparse workload faster, and normal append loops too
93 | SparseArray(Dict[BigInt, str] d, BigInt max_index)
94
95 | BashAssoc(Dict[str, str] d)
96
97 # DATA model for YSH follows JSON. Note: YSH doesn't have 'undefined' and
98 # 'null' like JavaScript, just 'null'.
99 | Null
100 | Bool(bool b)
101 | Int(BigInt i)
102 | Float(float f)
103 | List(List[value] items)
104 | Dict(Dict[str, value] d)
105
106 # for polymorphism - should replace value.{IO,Module} too
107 # because they have attributes (functions), methods - not just methods
108 | Obj %Obj
109
110 # CODE types
111 # unevaluated: Eggex, Expr, Template, Command/Block
112 # callable, in separate namespaces: Func, BoundFunc, Proc
113
114 # expr is spliced
115 # / d+; ignorecase / -> '[[:digit:]]+' REG_ICASE
116 | Eggex(re spliced, str canonical_flags,
117 List[value?] convert_funcs, List[Token?] convert_toks,
118 # str? is because some groups are not named
119 str? as_ere, List[str?] capture_names)
120
121 # The indices list has 2 * (num_group + 1) entries. Group 0 is the whole
122 # match, and each group has both a start and end index.
123 # It's flat to reduce allocations. The group() start() end() funcs/methods
124 # provide a nice interface.
125 | Match %RegexMatch
126
127 # ^[42 + a[i]]
128 | Expr(expr e)
129
130 # This is an UNBOUND command, like
131 # ^(echo 1; echo 2) and cd { echo 1; echo 2 }
132 | CommandFrag(command c)
133
134 # Bound command
135 | Command(cmd_frag frag, Dict[str, Cell] captured_frame)
136
137 # A place has an additional stack frame where the value is evaluated.
138 # The frame MUST be lower on the stack at the time of use.
139 | Place(y_lvalue lval, Dict[str, Cell] frame)
140
141 # for io->evalToDict(), which uses ctx_FrontFrame(), which is distinct from
142 # ctx_Eval()
143 # TODO: ASDL should let us "collapse" this Dict directly into value_t
144 | Frame(Dict[str, Cell] frame)
145
146 # callable is vm._Callable.
147 # TODO: ASDL needs some kind of "extern" to declare vm._Callable and
148 # cmd_eval.CommandEvaluator. I think it would just generate a forward
149 # declaration.
150 | BuiltinFunc(any callable)
151 | BoundFunc(value me, value func)
152
153 | Func(str name, Func parsed,
154 List[value] pos_defaults, Dict[str, value] named_defaults,
155 # module is where "global" lookups happen
156 Dict[str, Cell] module_frame)
157
158 # command.ShFunction and command.Proc evaluate to value.Proc
159 # They each have name, name_tok, and body.
160 #
161 # YSH procs disable dynamic scope, have default args to evaluate, and
162 # different @ARGV.
163
164 # builtin is vm._Builtin, this can be introspected
165 | BuiltinProc(any builtin)
166 | Proc(str name, Token name_tok, proc_sig sig, command body,
167 ProcDefaults? defaults, bool sh_compat,
168 # module is where "global" lookups happen
169 Dict[str, Cell] module_frame)
170
171 # for i in (1:n) { echo $i } # both ends are required
172 | Range(int lower, int upper)
173
174 # internal detail - can't be instantied by users
175 # a[3:5] a[:10] a[3:] a[:] # both ends are optional
176 | Slice(IntBox? lower, IntBox? upper)
177
178 # Other introspection
179 # __builtins__ - Dict[str, value_t] - I would like to make this read-only
180 # __modules__ - Dict[str, Obj] - read-only to prevent non-Obj
181 # __sh_funcs__ - Dict[str, value.Proc] - read-only to prevent non-Proc
182 # __traps__ - Dict[str, command_t] ?
183 # __builtin_procs__ - Dict[str, BuiltinProc] - builtin commands - special
184 # and non-special? and assignment?
185 # __aliases__ - Dict[str, str]
186 # __jobs__ - maybe nicer that jobs -p
187 # __stack__ - replaces pp stacks_, frame_vars_
188 #
189 # More:
190 # - dir stack pushd/popd - read-only variable
191 # - there is a hidden mem.pwd, in addition to $PWD
192 # - completion hooks and spec
193 # - getopts state
194 # - command cache - hash builtin
195}
196
197# vim: sw=2
198