1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
--- flow_dsl.lua — pure-Lua internal DSL for the flow.ir wire vocabulary.
---
--- `F = require("flow_dsl")`. Wraps Expr / Node AST construction behind a
--- small, chainable Lua API; every builder emits a plain Lua table shaped
--- exactly like the flow.ir wire format (see the field-name source of
--- truth: `mse://guides/blueprint-authoring` § "Expr ops" / "Flow node
--- kinds"). flow_dsl has zero dependencies — it does not `require`
--- `bp_dsl` (bp_dsl depends on flow_dsl, never the reverse).
---
--- ## Expr wrapper
---
--- `F.p"$.x"` / `F.lit(v)` return an Expr wrapper with a method-chain of
--- comparison / boolean helpers (`E:eq(v)`, `E:And(other)`, ...) plus
--- arithmetic operator overloads (`+ - * / %`). Comparison operators
--- (`< <= == ...`) are deliberately NOT overloaded: the Lua VM coerces
--- their result to a VM-level boolean before any metamethod return value
--- could survive as an AST table, so `:lt()` / `:eq()` / ... are the only
--- way to build a comparison Expr.
---
--- Anywhere an Expr is expected, a raw Lua value (auto-`lit`) or another
--- Expr wrapper is also accepted — see `F.unwrap`.
---
--- ## Node builders
---
--- `F.step` / `F.seq` / `F.branch` / `F.loop_` / `F.assign` / `F.try_`
--- return plain Lua tables (no metatable) shaped like flow.ir `Node`s.
--- `loop` / `try` / `in` are Lua reserved words, hence the `loop_` / `try_`
--- suffixes on the Node builders and the `input` rename on `F.step`;
--- `then` / `else` (Branch) are reserved too, hence `on_true` / `on_false`.
local M =
local Expr =
Expr. = Expr
local
local
--- `F.raw(t)` — treat an arbitrary raw AST table as an Expr wrapper
--- passthrough (no validation; `t` is emitted verbatim as this Expr's
--- AST). This is the general escape hatch for ops with no dedicated
--- builder.
--- `F.call_extern(name, args)` — a `call_extern` Expr: `name` is the
--- extern registry key (wire field `ref`); `args` is a Lua list of
--- Expr wrappers or raw values (auto-`lit`, same convention as
--- `F.all`/`F.any`), evaluated before the extern call.
--- `F.p(path_str)` — a `path` Expr, e.g. `F.p"$.x"`.
--- `F.lit(v)` — a `lit` Expr wrapping a literal JSON-serializable value.
--- `F.unwrap(x)` — Expr wrapper -> its raw AST table; any other raw Lua
--- value -> a `lit` AST (auto-lit). Every builder that accepts "Expr or
--- raw value" funnels its argument through this function; it is exposed
--- directly for DSL authors who build `F.raw()` tables by hand.
local
Expr. = binop
Expr. = binop
Expr. = binop
Expr. = binop
Expr. = binop
Expr. = binop
--- `self:And(other)` — variadic `and` Expr over exactly `{self, other}`.
--- For 3+ operands in a single node use `F.all{...}` instead (this method
--- always emits a 2-element `args`, matching the pairwise chain form
--- `a:And(b):And(c)` produces nested `and` nodes, not a flattened one).
--- `self:Or(other)` — see `Expr:And`'s note (same variadic-but-pairwise
--- shape; use `F.any{...}` for a single flat N-ary node).
--- `self:contains(needle)` — `in` Expr: true iff `needle` is a member of
--- `self` (the haystack, expected to evaluate to a JSON array).
-- Arithmetic metamethods. `a`/`b` may be an Expr wrapper OR a raw value
-- (Lua invokes a metamethod as soon as either operand has one, so the
-- other operand is passed through as-is and must go through M.unwrap too).
local
Expr. = arith
Expr. = arith
Expr. = arith
Expr. = arith
Expr. = arith
--- `F.all{e1, e2, ...}` — a single, N-ary `and` Expr (flow-ir's `And.args`
--- is natively variadic, so this is one AST node regardless of list
--- length, not a left-fold of binary nodes).
--- `F.any{e1, e2, ...}` — a single, N-ary `or` Expr (see `F.all`'s note).
-- ── Empty-object marker ──────────────────────────────────────────────────
--- Marker key used by `F.obj()` below. Lua's table type cannot itself
--- distinguish an empty array from an empty object, and
--- `build_bp_from_script` (`crates/mlua-swarm-cli/src/dsl/mod.rs`)
--- converts every zero-length Lua table to a JSON array — so a
--- one-key table carrying this marker is used as a stand-in wherever a
--- field must serialize as an empty JSON *object* instead, and
--- `build_bp_from_script` replaces every occurrence of this exact
--- single-key shape with a genuine empty JSON object as a post-pass over
--- the converted value. `M.EMPTY_OBJECT_MARKER_KEY` is exposed so the
--- Rust side can reference the same literal rather than duplicating it.
M. = "__mse_empty_object__"
--- `F.obj()` — a marker value that serializes to an empty JSON object
--- (`{}`). See `M.EMPTY_OBJECT_MARKER_KEY`'s doc comment for why this
--- indirection is needed instead of a bare `{}` Lua table literal.
-- ── Node builders (plain tables, no metatable) ──────────────────────────
--- `F.step{id=, agent=, input=, out=}` — a `step` Node.
---
--- - `id` is an optional author-facing label (NOT part of the flow.ir
--- wire format; discarded — flow.ir Steps have no identity of their own,
--- only structural position within a `seq`).
--- - `agent` -> wire `ref` (the dispatch key).
--- - `input` -> wire `in` (`in` is a Lua reserved word, hence the
--- DSL-level rename); accepts an Expr or a raw value (auto-`lit`).
--- - `out` -> wire `out`; must resolve to a `path` Expr (a write target)
--- — flow_dsl does not enforce this, it is the author's responsibility.
--- `F.seq{node1, node2, ...}` — a `seq` Node (children evaluated in
--- order, threading ctx through each).
--- `F.branch{cond=, on_true=, on_false=}` — a `branch` Node. `then` /
--- `else` are Lua reserved words, hence `on_true` / `on_false`.
--- `F.loop_{counter=, cond=, max=, body=}` — a `loop` Node. `loop` is a
--- Lua reserved word, hence the trailing underscore.
--- `F.assign{at=, value=}` — an `assign` Node (pure ctx transform, no
--- agent dispatch).
--- `F.try_{body=, catch=, err_at=}` — a `try` Node. `try` is a Lua
--- reserved word, hence the trailing underscore. `err_at` is optional
--- (omitted entirely from the emitted table when not given, matching the
--- wire schema's `#[serde(default)]` field).
return M