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

169 lines, 69 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 LiteralBlock Func
12 NameType
13 EggexFlag
14 }
15
16 use core runtime {
17 Cell
18 }
19
20 IntBox = (int i)
21
22 ProcDefaults = (
23 List[value]? for_word, # all of them are value.Str
24 List[value]? for_typed, # block default is also here
25 Dict[str, value]? for_named,
26 )
27
28 LeftName = (str name, loc blame_loc)
29
30 # for setvar, and value.Place
31 y_lvalue =
32 # e.g. read (&x)
33 Local %LeftName
34 # e.g. &a[0][1].key -- we evaluate a[0][1] first
35 | Container(value obj, value index)
36
37 # An sh_lvalue is for things mutation that happen with dynamic scope
38 #
39 # - sh_expr_eval uses this for unset / printf -v
40 # - word_eval uses this for ${a[0]=}
41 # - expr_eval / cmd_eval use this for setvar a[i] = 42
42 sh_lvalue =
43 Var %LeftName
44 | Indexed(str name, int index, loc blame_loc)
45 | Keyed(str name, str key, loc blame_loc)
46
47 eggex_ops =
48 # for BASH_REMATCH or ~ with a string
49 No
50 # These lists are indexed by group number, and will have None entries
51 | Yes(List[value?] convert_funcs, List[Token?] convert_toks,
52 List[str?] capture_names)
53
54 RegexMatch = (str s, List[int] indices, eggex_ops ops)
55
56 regex_match =
57 No
58 | Yes %RegexMatch
59
60 # Commands, words, and expressions from syntax.asdl are evaluated to a VALUE.
61 # value_t instances are stored in state.Mem().
62 value =
63 # Methods on state.Mem return value.Undef, but it's not visible in YSH.
64 #
65 # A var bound to Undef is different than no binding because of dynamic
66 # scope. Undef can shadow values lower on the stack.
67 Undef
68
69 | Str(str s)
70
71 # "holes" in the array are represented by None
72 # TODO: Use Dict[int, str] representation
73 | BashArray(List[str] strs)
74 | BashAssoc(Dict[str, str] d)
75
76 # DATA model for YSH follows JSON. Note: YSH doesn't have 'undefined' and
77 # 'null' like JavaScript, just 'null'.
78 | Null
79 | Bool(bool b)
80 | Int(BigInt i)
81 #| Int(int i)
82 | Float(float f)
83 | List(List[value] items)
84 | Dict(Dict[str, value] d)
85
86 # CODE types
87 # unevaluated: Eggex, Expr, Template, Command/Block
88 # callable, in separate namespaces: Func, BoundFunc, Proc
89
90 # expr is spliced
91 # / d+; ignorecase / -> '[[:digit:]]+' REG_ICASE
92 | Eggex(re spliced, str canonical_flags,
93 List[value?] convert_funcs, List[Token?] convert_toks,
94 # str? is because some groups are not named
95 str? as_ere, List[str?] capture_names)
96
97 # The indices list has 2 * (num_group + 1) entries. Group 0 is the whole
98 # match, and each group has both a start and end index.
99 # It's flat to reduce allocations. The group() start() end() funcs/methods
100 # provide a nice interface.
101 | Match %RegexMatch
102
103 # ^[42 + a[i]]
104 | Expr(expr e)
105
106 # ^"$1 and $2"
107 # TODO: fix bug with shared variant across files, then collapse this
108 | Template(DoubleQuoted dq)
109
110 # ^(echo 1; echo 2) and cd { echo 1; echo 2 }
111 | Command(command c)
112
113 # for Hay to get the backing lines
114 # TODO: Consolidate value.Command and value.LiteralBlock. All Command
115 # instance should have backing lines.
116
117 # TODO: ASDL doesn't support shared variant across module
118 # This would be more efficient
119 # | LiteralBlock %LiteralBlock
120 | Block(LiteralBlock block)
121
122 # A place has an additional stack frame where the value is evaluated.
123 # The frame MUST be lower on the stack at the time of use.
124 | Place(y_lvalue lval, Dict[str, Cell] frame)
125
126 # for Flags/flag and Flags/arg?
127 # for json read/write ?
128 # Possibly unify Hay and modules/namespaces
129 | Module(Dict[str, value] defs)
130
131 # The ability to use operating system functions. Right now some functions
132 # leak, like glob().
133 | IO(any cmd_ev, any prompt_ev)
134
135 # _guts->heapId() can be used to detect object cycles.
136 # It's considered impure; it depends on VM implementation details. The =
137 # operator and 'pp value' also print the heap ID.
138 | Guts(any vm)
139
140 # callable is vm._Callable.
141 # TODO: ASDL needs some kind of "extern" to declare vm._Callable and
142 # cmd_eval.CommandEvaluator. I think it would just generate a forward
143 # declaration.
144 | BuiltinFunc(any callable)
145 | BoundFunc(value me, value func)
146
147 # command.ShFunction and command.Proc evaluate to ProcValue.
148 # Procs have default args to evaluate, and no dynamic scope.
149 #
150 # TODO: this can also have frame.
151 # Perhaps divide this into Proc and ShFunction
152
153 | Proc(str name, Token name_tok, proc_sig sig, command body,
154 ProcDefaults? defaults, bool dynamic_scope)
155
156 # module may be a frame where defined
157 | Func(str name, Func parsed,
158 List[value] pos_defaults, Dict[str, value] named_defaults,
159 Dict[str, Cell]? module_)
160
161 # a[3:5] a[:10] a[3:] a[:] # both ends are optional
162 | Slice(IntBox? lower, IntBox? upper)
163
164 # for i in (1:n) { echo $i } # both ends are required
165 | Range(int lower, int upper)
166}
167
168# vim: sw=2
169