OILS / core / value.asdl View on Github | oils.pub

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