Skip to content

Commit edf18c3

Browse files
committed
doc: change to eager evaluation, and add lambda
1 parent e2ae2ab commit edf18c3

File tree

1 file changed

+126
-64
lines changed

1 file changed

+126
-64
lines changed

_docs/json-dsl.md

Lines changed: 126 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,28 @@
66
## Example of JSON DSL
77

88
```json
9-
["label",
9+
["$label",
1010
1,
1111
"installing plugins:",
12-
["vimdir/with-install",
13-
["parallel",
14-
["label",
12+
["$vimdir/with-install",
13+
["$parallel",
14+
["$label",
1515
2,
1616
" github.com/tyru/open-browser.vim ... {{if .Done}}done!{{end}}",
17-
["parallel",
17+
["$parallel",
1818
["lockjson/add",
1919
["repos/get", "github.com/tyru/open-browser.vim"],
2020
["$array", "default"]],
2121
["plugconf/install", "github.com/tyru/open-browser.vim"]]],
22-
["label",
22+
["$label",
2323
3,
2424
" github.com/tyru/open-browser-github.vim ... {{if .Done}}done!{{end}}",
25-
["parallel",
25+
["$parallel",
2626
["lockjson/add",
2727
["repos/get", "github.com/tyru/open-browser-github.vim"],
2828
["$array", "default"]],
29-
["plugconf/install", "github.com/tyru/open-browser-github.vim"]]]]]]
29+
["plugconf/install",
30+
"github.com/tyru/open-browser-github.vim"]]]]]]
3031
```
3132

3233
## Wordings
@@ -107,7 +108,7 @@ An array literal value is written using `$array` operator.
107108
This expression is evaluated to `[1, 2, 3]`.
108109

109110
Each expression has 0 or more parameters. And evaluation
110-
strategy is a non-strict evaluation.
111+
strategy is an eager evaluation.
111112

112113
Parameter types are
113114

@@ -117,7 +118,8 @@ Parameter types are
117118
* number
118119
* array
119120
* object
120-
* expression
121+
* lambda
122+
* expression (only macro can treat this)
121123

122124
But all values must be able to be serialized to JSON. Because AST of whole
123125
process is serialized and saved as a "transaction log file". The process can be
@@ -169,14 +171,29 @@ different with latest volt's JSON DSL when installing. this is a simplified
169171
version for easiness).
170172

171173
```json
172-
["vimdir/with-install",
173-
["do",
174+
["$vimdir/with-install",
175+
["$do",
174176
["lockjson/add",
175177
["repos/get", "github.com/tyru/caw.vim"],
176178
["$array", "default"]],
177179
["plugconf/install", "github.com/tyru/caw.vim"]]]
178180
```
179181

182+
At first, expands all macros (`["$array", "default"]` cannot be written in JSON
183+
notation, it is expanded to array but array literal is `["$array", "default"]`).
184+
185+
```json
186+
["vimdir/with-install",
187+
["fn", [],
188+
["do",
189+
["fn", [],
190+
["lockjson/add",
191+
["repos/get", "github.com/tyru/caw.vim"],
192+
["$array", "default"]]],
193+
["fn", [],
194+
["plugconf/install", "github.com/tyru/caw.vim"]]]]]
195+
```
196+
180197
I show you what happens in several steps when you "invert" the expression like
181198
`volt history undo`.
182199

@@ -185,41 +202,65 @@ At first, to invert the expression, `$invert` macro is used:
185202
```json
186203
["$invert",
187204
["vimdir/with-install",
188-
["do",
189-
["lockjson/add",
190-
["repos/get", "github.com/tyru/caw.vim"],
191-
["$array", "default"]],
192-
["plugconf/install", "github.com/tyru/caw.vim"]]]]
205+
["fn", [],
206+
["do",
207+
["fn", [],
208+
["lockjson/add",
209+
["repos/get", "github.com/tyru/caw.vim"],
210+
["$array", "default"]]],
211+
["fn", [],
212+
["plugconf/install", "github.com/tyru/caw.vim"]]]]]]
193213
```
194214

195-
`["$invert", ["vimdir/with-install", expr]]` is expanded to
196-
`["vimdir/with-install", ["$invert", expr]]`. Internally, it is implemented as
197-
calling `Invert()` method of `vimdir/with-install` operator struct. See "Go
198-
API" section.
215+
`["$invert", ["vimdir/with-install", thunk]]` is expanded to
216+
`["vimdir/with-install", ["$invert", thunk]]`. Internally, it is implemented as
217+
calling `InvertExpr()` method of `vimdir/with-install` operator struct.
199218

200219
```json
201220
["vimdir/with-install",
202221
["$invert",
203-
["do",
204-
["lockjson/add",
205-
["repos/get", "github.com/tyru/caw.vim"],
206-
["$array", "default"]],
207-
["plugconf/install", "github.com/tyru/caw.vim"]]]]
222+
["fn", [],
223+
["do",
224+
["fn", [],
225+
["lockjson/add",
226+
["repos/get", "github.com/tyru/caw.vim"],
227+
["$array", "default"]]],
228+
["fn", [],
229+
["plugconf/install", "github.com/tyru/caw.vim"]]]]]]
208230
```
209231

210-
And `["$invert", ["do", expr1, expr2]]` becomes
211-
`["do", ["$invert", expr2], ["$invert", expr1]]`.
212-
Note that `expr1` and `expr2` becomes reversed order.
232+
And `["$invert", ["fn", args, body]]` becomes
233+
`["fn", args, ["$invert", body]]`.
213234

214235
```json
215236
["vimdir/with-install",
216-
["do",
217-
["$invert",
218-
["plugconf/install", "github.com/tyru/caw.vim"]],
237+
["fn", [],
219238
["$invert",
220-
["lockjson/add",
221-
["repos/get", "github.com/tyru/caw.vim"],
222-
["$array", "default"]]]]]
239+
["do",
240+
["fn", [],
241+
["lockjson/add",
242+
["repos/get", "github.com/tyru/caw.vim"],
243+
["$array", "default"]]],
244+
["fn", [],
245+
["plugconf/install", "github.com/tyru/caw.vim"]]]]]]
246+
```
247+
248+
And `["$invert", ["do", thunk1, thunk2]]` becomes
249+
`["do", ["$invert", thunk2], ["$invert", thunk1]]`.
250+
Note that `thunk1` and `thunk2` become reversed order.
251+
252+
```json
253+
["vimdir/with-install",
254+
["fn", [],
255+
["do",
256+
["fn", [],
257+
["$invert",
258+
["lockjson/add",
259+
["repos/get", "github.com/tyru/caw.vim"],
260+
["$array", "default"]]]],
261+
["fn", [],
262+
["$invert",
263+
["plugconf/install", "github.com/tyru/caw.vim"]]]]]]
223264
```
224265

225266
And
@@ -230,38 +271,47 @@ And
230271

231272
```json
232273
["vimdir/with-install",
233-
["do",
234-
["plugconf/delete", ["$invert", "github.com/tyru/caw.vim"]],
235-
["lockjson/remove",
236-
["$invert", ["repos/get", "github.com/tyru/caw.vim"]],
237-
["$invert", ["$array", "default"]]]]]
274+
["fn", [],
275+
["do",
276+
["fn", [],
277+
["lockjson/add",
278+
["$invert", ["repos/get", "github.com/tyru/caw.vim"]],
279+
["$invert", ["$array", "default"]]]],
280+
["fn", [],
281+
["plugconf/install", ["$invert", "github.com/tyru/caw.vim"]]]]]]
238282
```
239283

240284
`["$invert", ["repos/get", path]]` becomes
241285
`["repos/delete", ["$invert", path]]`.
242286

243287
```json
244288
["vimdir/with-install",
245-
["do",
246-
["plugconf/delete", ["$invert", "github.com/tyru/caw.vim"]],
247-
["lockjson/remove",
248-
["repos/delete", ["$invert", "github.com/tyru/caw.vim"]],
249-
["$invert", ["$array", "default"]]]]]
289+
["fn", [],
290+
["do",
291+
["fn", [],
292+
["lockjson/add",
293+
["repos/delete", ["$invert", "github.com/tyru/caw.vim"]],
294+
["$invert", ["$array", "default"]]]],
295+
["fn", [],
296+
["plugconf/install", ["$invert", "github.com/tyru/caw.vim"]]]]]]
250297
```
251298

252299
And if `$invert` is applied to literals like string, JSON array, it just remains
253300
as-is.
254301

255302
```json
256303
["vimdir/with-install",
257-
["do",
258-
["plugconf/delete", "github.com/tyru/caw.vim"],
259-
["lockjson/remove",
260-
["repos/delete", "github.com/tyru/caw.vim"],
261-
["$array", "default"]]]]
304+
["fn", [],
305+
["do",
306+
["fn", [],
307+
["lockjson/add",
308+
["repos/delete", "github.com/tyru/caw.vim"],
309+
["$array", "default"]]],
310+
["fn", [],
311+
["plugconf/install", "github.com/tyru/caw.vim"]]]]]
262312
```
263313

264-
We can successfully evaluate the inverse expression of the first expression :)
314+
We can successfully evaluate the inverse expression of the first expression! :)
265315

266316
### Uninstall operation should be "Recovable"?
267317

@@ -317,7 +367,7 @@ As the above signature shows, operators must take care the following points:
317367

318368
TODO: Move to Godoc.
319369

320-
### Macro
370+
### Basic macros
321371

322372
All macros has `$` prefixed name for readability.
323373
Macros are not saved in transaction log (expanded before saving).
@@ -342,27 +392,39 @@ Macros are not saved in transaction log (expanded before saving).
342392

343393
### Basic operators
344394

345-
* `["label", linenum: number, tmpl string, expr Expr[* => R]] R`
395+
* `["$label", linenum: number, tmpl string, expr Expr[* => R]] R`
396+
* `["$label", linenum, tmpl, expr]` expands to
397+
`["label", linenum, tmpl, ["fn", [] expr]]`
398+
399+
* `["label", linenum: number, tmpl string, thunk Func[* => R]] R`
346400
* Render `tmpl` by text/template to `linenum` line (1-origin).
347-
Returns the evaluated value of `expr`.
401+
Returns the evaluated value of `thunk`.
348402
* e.g.
349-
* `["$invert", ["label", linenum, "msg", expr]]` = `["label", ["$invert", linenum], "revert: \"msg\"", ["$invert", expr]]`
403+
* `["$invert", ["label", linenum, "msg", thunk]]` = `["label", ["$invert", linenum], "revert: \"msg\"", ["$invert", thunk]]`
350404
* See `Label examples` section for more details
351405

352-
* `["do", expr1 R1, ..., expr_last R2] R2`
353-
* Executes multiple expressions in series.
354-
* Returns the evaluated value of the last expression.
406+
* `["$do", expr1 Expr[* => R1], ..., expr_last Expr[* => R2]] R2`
407+
* `["$do", expr1, expr2]` expands to
408+
`["do", ["fn", [], expr1], ["fn", [], expr2]]`
409+
410+
* `["do", thunk1 R1, ..., thunk_last R2] R2`
411+
* Executes multiple lambdas in series.
412+
* Returns the evaluated value of the last lambda.
355413
* e.g.
356-
* `["$invert", ["do", expr1, expr2]]` = `["do", ["$invert", expr1], ["$invert", expr2]]`
414+
* `["$invert", ["do", thunk1, thunk2]]` = `["do", ["$invert", thunk1], ["$invert", thunk2]]`
357415
* Note that the arguments are reversed.
358416

359-
* `["parallel", msg string, expr1 R1, ..., expr_last R2] R2`
360-
* Executes multiple expressions in parallel.
361-
* Returns the evaluated value of the last expression.
417+
* `["$parallel", expr1 Expr[* => R1], ..., expr_last Expr[* => R2]] R2`
418+
* `["$parallel", expr1, expr2]` expands to
419+
`["parallel", ["fn", [], expr1], ["fn", [], expr2]]`
420+
421+
* `["parallel", thunk1 Func[* => R1], ..., thunk_last Func[* => R2]] R2`
422+
* Executes multiple lambdas in parallel.
423+
* Returns the evaluated value of the last lambda.
362424
* e.g.
363-
* `["$invert", ["parallel", expr1, expr2]]` = `["parallel", ["$invert", expr2], ["$invert", expr1]]`
425+
* `["$invert", ["parallel", thunk1, thunk2]]` = `["parallel", ["$invert", thunk2], ["$invert", thunk1]]`
364426
* The arguments are **not** reversed because parallel does not care of
365-
execution order of given expressions.
427+
execution order of given value.
366428

367429
### Repository operators
368430

0 commit comments

Comments
 (0)