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

217 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 #
77 # Implementation details
78 #
79
80 # Only used for io.stdin aka val_ops.StdinIterator. (It would be nice if
81 # we could express iter_value.{Eof,Interrupted,Str,Int,...} in ASDL)
82 Interrupted
83 | Stdin
84 # Can't be instantiated by users
85 # a[3:5] a[:10] a[3:] a[:] # both ends are optional
86 | Slice(IntBox? lower, IntBox? upper)
87
88 #
89 # OSH/Bash types
90 #
91
92 # Methods on state::Mem return value.Undef, but it's not visible in YSH.
93 # Note: A var bound to Undef is different than no binding because of
94 # dynamic scope. Undef can shadow values lower on the stack.
95 | Undef
96
97 | Str(str s)
98
99 # "holes" in the array are represented by None
100 | BashArray(List[str] strs)
101 # TODO: Switch to this more efficient representation. max_index makes
102 # append-sparse workload faster, and normal append loops too
103 | SparseArray(Dict[BigInt, str] d, BigInt max_index)
104
105 | BashAssoc(Dict[str, str] d)
106
107 # The DATA model for YSH follows JSON. Note: YSH doesn't have 'undefined'
108 # and 'null' like JavaScript, just 'null'.
109 | Null
110 | Bool(bool b)
111 | Int(BigInt i)
112 | Float(float f)
113 | List(List[value] items)
114 | Dict(Dict[str, value] d)
115
116 # Possible types
117 # value.Htm8 - a string that can be queried, with lazily materialized "views"
118 # value.Tsv8 - ditto
119 # value.Json8 - some kind of jq or JSONPath query language
120
121 # Objects are for for polymorphism
122 | Obj %Obj
123
124 # for i in (0 .. n) { echo $i } # both ends are required
125 # TODO: BigInt
126 | Range(int lower, int upper)
127
128 # expr is spliced
129 # / d+; ignorecase / -> '[[:digit:]]+' REG_ICASE
130 | Eggex(re spliced, str canonical_flags,
131 List[value?] convert_funcs, List[Token?] convert_toks,
132 # str? is because some groups are not named
133 str? as_ere, List[str?] capture_names)
134
135 # The indices list has 2 * (num_group + 1) entries. Group 0 is the whole
136 # match, and each group has both a start and end index.
137 # It's flat to reduce allocations. The group() start() end() funcs/methods
138 # provide a nice interface.
139 | Match %RegexMatch
140
141 # A place has an additional stack frame where the value is evaluated.
142 # The frame MUST be lower on the stack at the time of use.
143 | Place(y_lvalue lval, Dict[str, Cell] frame)
144
145 # for io->evalToDict(), which uses ctx_FrontFrame(), which is distinct from
146 # ctx_Eval()
147 # TODO: ASDL should let us "collapse" this Dict directly into value_t
148 | Frame(Dict[str, Cell] frame)
149
150 #
151 # Code units: BoundFunc, BuiltinFunc, Func, BuiltinProc, Proc
152 #
153
154 # for obj.method and obj->mutatingMethod
155 | BoundFunc(value me, value func)
156 # callable is vm._Callable.
157 # TODO: ASDL needs some kind of "extern" to declare vm._Callable,
158 # vm._Builtin. I think it would just generate a forward declaration.
159 | BuiltinFunc(any callable)
160
161 | Func(str name, Func parsed,
162 List[value] pos_defaults, Dict[str, value] named_defaults,
163 # module is where "global" lookups happen
164 Dict[str, Cell] module_frame)
165
166 # command.ShFunction and command.Proc evaluate to value.Proc
167 # They each have name, name_tok, and body.
168 #
169 # YSH procs disable dynamic scope, have default args to evaluate, and
170 # different @ARGV.
171
172 # builtin is vm._Builtin, this can be introspected
173 | BuiltinProc(any builtin)
174 | Proc(str name, Token name_tok, proc_sig sig, command body,
175 ProcDefaults? defaults, bool sh_compat,
176 # module is where "global" lookups happen
177 Dict[str, Cell] module_frame)
178
179 #
180 # Unevaluated CODE types: ExprFrag, Expr, CommandFrag, Command
181 #
182
183 # This can be the output of parseExpr()?
184 #| ExprFrag(expr e)
185
186 # var x = ^[42 + a[i]]
187 # my-ls | where [size > 10]
188 | Expr(expr e, Dict[str, Cell] captured_frame)
189
190 # This is an UNBOUND command, like
191 # ^(echo 1; echo 2) and cd { echo 1; echo 2 }
192 | CommandFrag(command c)
193
194 # Bound command
195 | Command(cmd_frag frag, Dict[str, Cell] captured_frame)
196
197 # Other introspection
198 # __builtins__ - Dict[str, value_t] - I would like to make this read-only
199 # __modules__ - Dict[str, Obj] - read-only to prevent non-Obj
200 # __sh_funcs__ - Dict[str, value.Proc] - read-only to prevent non-Proc
201 # __traps__ - Dict[str, command_t] ?
202 # __builtin_procs__ - Dict[str, BuiltinProc] - builtin commands - special
203 # and non-special? and assignment?
204 # __aliases__ - Dict[str, str]
205 # __jobs__ - maybe nicer that jobs -p
206 # __stack__ - replaces pp stacks_, frame_vars_
207 #
208 # More:
209 # - dir stack pushd/popd - read-only variable
210 # - there is a hidden mem.pwd, in addition to $PWD
211 # - completion hooks and spec
212 # - getopts state
213 # - command cache - hash builtin
214}
215
216# vim: sw=2
217