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