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

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