mambascript 0.5.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +326 -0
- package/README.md +509 -267
- package/bin/mamba +1 -258
- package/decision.js +1 -246
- package/docs/coverage.md +70 -0
- package/docs/features.md +3348 -0
- package/docs/perf.md +94 -0
- package/docs/reference.md +1295 -0
- package/docs/vs-coffeescript.md +1593 -0
- package/docs/vs-iced-coffeescript.md +626 -0
- package/docs/vs-typescript.md +107 -0
- package/esm.js +8 -0
- package/jest.js +33 -0
- package/lib/black.js +17 -13
- package/lib/browser.js +2 -4
- package/lib/cli.js +207 -136
- package/lib/compiler.js +1162 -93
- package/lib/decision.js +10 -10
- package/lib/diagnostics.js +219 -0
- package/lib/esm-hooks.js +60 -0
- package/lib/fast-parser.js +3469 -0
- package/lib/functional-helpers.js +26 -9
- package/lib/helpers.js +67 -50
- package/lib/js-nodes.js +151 -12
- package/lib/lexer.js +1499 -0
- package/lib/light-module.js +200 -63
- package/lib/mamba.js +372 -0
- package/lib/module.js +23 -172
- package/lib/nodes.js +84 -21
- package/lib/optimiser.js +29 -9
- package/lib/optparse.js +32 -15
- package/lib/parser.js +9433 -4840
- package/lib/preprocessor.js +3 -5
- package/lib/register.js +49 -56
- package/lib/repl.js +28 -9
- package/lib/reporter.js +106 -69
- package/lib/run.js +3 -5
- package/lib/string-scanner.js +94 -0
- package/lib/syntax-tables.js +480 -0
- package/lib/type-checker.js +258 -44
- package/lib/type-helpers.js +28 -10
- package/lib/type-resolver.js +256 -34
- package/lib/type-scope.js +10 -6
- package/lib/type-walker.js +664 -85
- package/lib/types.js +17 -7
- package/lib/underscore.js +170 -76
- package/lib/unicode-classes.js +122 -0
- package/mambaview.js +1 -0
- package/package.json +49 -46
- package/stdlib.js +1 -0
- package/vendor/README.md +40 -0
- package/vendor/escodegen/LICENSE.BSD +19 -0
- package/vendor/escodegen/escodegen.js +2289 -0
- package/vendor/estraverse/LICENSE.BSD +19 -0
- package/vendor/estraverse/estraverse.js +706 -0
- package/vendor/mambacodegen/LICENSE +24 -0
- package/vendor/mambacodegen/lib/mscodegen.js +620 -0
- package/vendor/patches/escodegen.diff +130 -0
- package/vendor/patches/estraverse.diff +51 -0
- package/vendor/patches/pegjs.diff +70 -0
- package/vendor/patches/source-map.diff +160 -0
- package/vendor/source-map/LICENSE +28 -0
- package/vendor/source-map/lib/source-map/array-set.js +92 -0
- package/vendor/source-map/lib/source-map/base64-vlq.js +140 -0
- package/vendor/source-map/lib/source-map/base64.js +38 -0
- package/vendor/source-map/lib/source-map/binary-search.js +77 -0
- package/vendor/source-map/lib/source-map/source-map-consumer.js +404 -0
- package/vendor/source-map/lib/source-map/source-map-generator.js +229 -0
- package/vendor/source-map/lib/source-map/source-node.js +201 -0
- package/vendor/source-map/lib/source-map/util.js +57 -0
- package/vendor/source-map/lib/source-map.js +8 -0
- package/bin/fix +0 -17
- package/bin/updateImports +0 -46
- package/build.js +0 -25
- package/lib/module-esm.js +0 -901
- package/lib/pegjs-kofu-plugin.js +0 -35
- package/lib/pegjs-mamba-plugin.js +0 -35
- package/lib/processor-esm.js +0 -507
- package/mamba +0 -259
- package/msast.js +0 -1
- package/utils/gulp-mamba.js +0 -29
- /package/{lib/esm.js → vendor/patches/mambacodegen.diff} +0 -0
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
# MambaScript against IcedCoffeeScript
|
|
2
|
+
|
|
3
|
+
IcedCoffeeScript (Max Krohn, 2011 onwards) is CoffeeScript with two
|
|
4
|
+
keywords, `await` and `defer`, that turn callback-passing code into
|
|
5
|
+
straight-line code. The compiler rewrites every function that contains
|
|
6
|
+
`await` into continuation-passing style and ships a small runtime (`iced`)
|
|
7
|
+
to do the bookkeeping. MambaScript 2.0 gets the same straight-line shape from
|
|
8
|
+
promises: `waitForIt` is JavaScript's `await`, `waitForAll` is
|
|
9
|
+
`await Promise.all`, and a function that uses either becomes `async` by
|
|
10
|
+
itself. This page shows how each Iced pattern is written in MambaScript, what
|
|
11
|
+
has no equivalent, and every CoffeeScript-level difference that carries over,
|
|
12
|
+
because Iced is a dialect of CoffeeScript 1 (the 1.x and 108.x releases track
|
|
13
|
+
CoffeeScript 1.12; a later "iced3" line tracked CoffeeScript 2, and I have not
|
|
14
|
+
checked it in the same detail).
|
|
15
|
+
|
|
16
|
+
How this page was made (2026-12-05): every `mamba` block was compiled with
|
|
17
|
+
MambaScript 2.0.0 on Node 24 (`mamba -c -p --strict` and `mambascript --js
|
|
18
|
+
--bare --strict --parser peg`) and the ones with output were run. The `coffee`
|
|
19
|
+
blocks are Iced and are not compiled here; they are written from the Iced
|
|
20
|
+
README and language notes, and where I am not sure of an Iced detail the
|
|
21
|
+
entry says so. Read docs/vs-coffeescript.md first for the CoffeeScript
|
|
22
|
+
differences; this page repeats only what changes for Iced.
|
|
23
|
+
|
|
24
|
+
Contents: [the Iced model](#1-the-iced-model-and-its-mambascript-shape) ·
|
|
25
|
+
[side by side](#2-side-by-side) · [what has no equivalent](#3-what-has-no-equivalent) ·
|
|
26
|
+
[CoffeeScript 1 differences that carry over](#4-coffeescript-1-differences-that-carry-over)
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 1. The Iced model and its MambaScript shape
|
|
31
|
+
|
|
32
|
+
### 1.1 `await` / `defer` (continuations) against `waitForIt` (promises)
|
|
33
|
+
|
|
34
|
+
**Iced**: `await` opens a block; inside it, each `defer slot` creates a
|
|
35
|
+
callback that fills `slot` when called; the code after the block runs once
|
|
36
|
+
every deferred callback has fired. The function containing `await` returns
|
|
37
|
+
immediately (its value is not meaningful) and the rest of its body runs
|
|
38
|
+
later; callers therefore pass a callback too.
|
|
39
|
+
|
|
40
|
+
```coffee
|
|
41
|
+
fetchUser = (id, cb) ->
|
|
42
|
+
await $.getJSON "/users/#{id}", defer user
|
|
43
|
+
cb user
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
**MambaScript 2.0**: `waitForIt` waits for a promise and gives its value;
|
|
47
|
+
the function is compiled as `async function` and returns a promise; callers
|
|
48
|
+
`waitForIt` it in turn. `await` is not a keyword, but `x = await p` is
|
|
49
|
+
rejected with MS1124 "await is JavaScript; MambaScript spells it waitForIt";
|
|
50
|
+
`defer` is a plain name you may use.
|
|
51
|
+
|
|
52
|
+
```mamba
|
|
53
|
+
fetchUser = (id) ->
|
|
54
|
+
res = waitForIt fetch "/users/#{id}"
|
|
55
|
+
waitForIt res.json()
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
fetchUser = async function (id) {
|
|
60
|
+
var res;
|
|
61
|
+
res = await fetch('/users/' + id);
|
|
62
|
+
return await res.json();
|
|
63
|
+
};
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
**Why**: promises are the asynchronous value JavaScript settled on after Iced
|
|
67
|
+
was designed; `async`/`await` in the engine do what Iced's CPS transform did
|
|
68
|
+
in the compiler, with no runtime and with ordinary return values. The cost:
|
|
69
|
+
callback-style APIs (Node's `fs.readFile(path, cb)`, old client libraries)
|
|
70
|
+
need a promise wrapper first (2.4), which Iced's `defer` did for free.
|
|
71
|
+
|
|
72
|
+
### 1.2 `autocb`
|
|
73
|
+
|
|
74
|
+
**Iced**: naming a parameter `autocb` makes the function call it
|
|
75
|
+
automatically when the body finishes, with the function's return value;
|
|
76
|
+
`return x` inside it means `autocb x`.
|
|
77
|
+
|
|
78
|
+
```coffee
|
|
79
|
+
delay = (ms, autocb) ->
|
|
80
|
+
await setTimeout defer(), ms
|
|
81
|
+
'done'
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**MambaScript 2.0**: an async function's return value *is* what the caller
|
|
85
|
+
gets, so there is nothing to name: the last expression (or `return`) resolves
|
|
86
|
+
the promise.
|
|
87
|
+
|
|
88
|
+
```mamba
|
|
89
|
+
delay = (ms) ->
|
|
90
|
+
waitForIt make Promise (resolve) -> setTimeout resolve, ms
|
|
91
|
+
'done'
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
delay = async function (ms) {
|
|
96
|
+
await new Promise(function (resolve) {
|
|
97
|
+
return setTimeout(resolve, ms);
|
|
98
|
+
});
|
|
99
|
+
return 'done';
|
|
100
|
+
};
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### 1.3 The `iced` runtime
|
|
104
|
+
|
|
105
|
+
**Iced**: compiled code calls `iced.Deferrals`, `__iced_k`,
|
|
106
|
+
`__iced_deferrals`, `iced.Rendezvous`; the runtime is inlined into every
|
|
107
|
+
compiled file by default or required from `iced-runtime` (`-I inline`,
|
|
108
|
+
`-I node`, `-I window`, `-I none`), and `iced.trace` / stack-trace options
|
|
109
|
+
exist for debugging the transformed code.
|
|
110
|
+
|
|
111
|
+
**MambaScript 2.0**: no runtime for async at all. The output is a native
|
|
112
|
+
`async function` with `await` and `Promise.all`; the only helpers MambaScript
|
|
113
|
+
ever emits are for classes, `in`, and object rest (`extends$`, `in$`,
|
|
114
|
+
`objectWithoutKeys$`), and only when used. Nothing to include, no flag.
|
|
115
|
+
|
|
116
|
+
```mamba
|
|
117
|
+
both = -> waitForAll [fetchUser(1), fetchUser(2)]
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
```js
|
|
121
|
+
both = async function () {
|
|
122
|
+
return await Promise.all([fetchUser(1), fetchUser(2)]);
|
|
123
|
+
};
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
**Why**: zero runtime dependencies is a locked decision for 2.0 (README,
|
|
127
|
+
"Why MambaScript"). The cost: Node 18 or newer and a browser with native
|
|
128
|
+
`async`/`await` (every current one).
|
|
129
|
+
|
|
130
|
+
### 1.4 Where `await` may appear
|
|
131
|
+
|
|
132
|
+
**Iced**: `await` is a statement block; it can sit anywhere in a function
|
|
133
|
+
body, inside loops, `if`, `try`; the whole function is transformed. At the
|
|
134
|
+
top level of a file it works too (the file becomes continuation-shaped).
|
|
135
|
+
|
|
136
|
+
**MambaScript 2.0**: `waitForIt` is an expression with unary precedence
|
|
137
|
+
(`(waitForIt p).name`, `waitForIt a + b` do what they say); it may appear in
|
|
138
|
+
loops, conditions, comprehensions, `consider` and `attempt` used as values
|
|
139
|
+
(those become awaited async IIFEs), and at the top level of a file, which
|
|
140
|
+
wraps the program in an immediately-invoked async function (or is a
|
|
141
|
+
top-level `await` in `--esm` output). The one refusal: a `blueprint`
|
|
142
|
+
constructor, because constructors cannot be async.
|
|
143
|
+
|
|
144
|
+
```mamba
|
|
145
|
+
fetchUser = (id) -> Promise.resolve {id}
|
|
146
|
+
user = waitForIt fetchUser 7
|
|
147
|
+
present user.id
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
(async function () {
|
|
152
|
+
var fetchUser, user;
|
|
153
|
+
var present = console.log;
|
|
154
|
+
fetchUser = function (id) {
|
|
155
|
+
return Promise.resolve({ id: id });
|
|
156
|
+
};
|
|
157
|
+
user = await fetchUser(7);
|
|
158
|
+
present(user.id);
|
|
159
|
+
}.call(this));
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Running it prints `7`.
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
## 2. Side by side
|
|
167
|
+
|
|
168
|
+
Every MambaScript program in this section was compiled with both parsers and
|
|
169
|
+
run; the printed output is quoted.
|
|
170
|
+
|
|
171
|
+
### 2.1 Serial requests
|
|
172
|
+
|
|
173
|
+
**Iced**: one `await` after another; each waits for its `defer` before the
|
|
174
|
+
next line runs.
|
|
175
|
+
|
|
176
|
+
```coffee
|
|
177
|
+
await fetchUser 1, defer a
|
|
178
|
+
await fetchUser a.id + 1, defer b
|
|
179
|
+
console.log a.name, b.name
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
**MambaScript 2.0**:
|
|
183
|
+
|
|
184
|
+
```mamba
|
|
185
|
+
fetchUser = (id) -> make Promise (resolve) -> setTimeout (-> resolve {id, name: "user#{id}"}), 5
|
|
186
|
+
|
|
187
|
+
main = ->
|
|
188
|
+
a = waitForIt fetchUser 1
|
|
189
|
+
b = waitForIt fetchUser a.id + 1
|
|
190
|
+
present a.name, b.name
|
|
191
|
+
|
|
192
|
+
main()
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Prints `user1 user2`.
|
|
196
|
+
|
|
197
|
+
### 2.2 Parallel requests: `defer` in a loop against `waitForAll`
|
|
198
|
+
|
|
199
|
+
**Iced**: the signature move. Several `defer`s inside one `await` block fire
|
|
200
|
+
their requests together; the block ends when all have called back. Each
|
|
201
|
+
`defer` names where its result lands (`results[i]`).
|
|
202
|
+
|
|
203
|
+
```coffee
|
|
204
|
+
results = []
|
|
205
|
+
await
|
|
206
|
+
for id in [1, 2, 3]
|
|
207
|
+
fetchUser id, defer results[id - 1]
|
|
208
|
+
console.log (u.name for u in results)
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
(The serial version is the same loop with the `await` inside it:
|
|
212
|
+
`for id in ids then await fetchUser id, defer results[id - 1]`.)
|
|
213
|
+
|
|
214
|
+
**MambaScript 2.0**: start every promise first (a comprehension does it in
|
|
215
|
+
one line), then `waitForAll` them; the results come back in order.
|
|
216
|
+
|
|
217
|
+
```mamba
|
|
218
|
+
fetchUser = (id) -> make Promise (resolve) -> setTimeout (-> resolve {id, name: "user#{id}"}), 5
|
|
219
|
+
|
|
220
|
+
main = ->
|
|
221
|
+
users = waitForAll (fetchUser id forEvery id in [1, 2, 3])
|
|
222
|
+
present (u.name forEvery u in users)
|
|
223
|
+
|
|
224
|
+
main()
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
```js
|
|
228
|
+
main = async function () {
|
|
229
|
+
var id, u, users;
|
|
230
|
+
users = await Promise.all(function (accum$) {
|
|
231
|
+
for (var cache$ = [1, 2, 3], i$ = 0, length$ = cache$.length; i$ < length$; ++i$) {
|
|
232
|
+
id = cache$[i$];
|
|
233
|
+
accum$.push(fetchUser(id));
|
|
234
|
+
}
|
|
235
|
+
return accum$;
|
|
236
|
+
}.call(this, []));
|
|
237
|
+
…
|
|
238
|
+
};
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Prints `[ 'user1', 'user2', 'user3' ]`. The serial version is 2.1's shape
|
|
242
|
+
inside a loop: `forEvery id in ids` then `users.push waitForIt fetchUser id`.
|
|
243
|
+
|
|
244
|
+
**Why**: Iced's "put the `await` around the loop or inside it" is exactly
|
|
245
|
+
"collect the promises or await each one"; `waitForAll` names the parallel
|
|
246
|
+
case so the two cannot be confused by indentation alone.
|
|
247
|
+
|
|
248
|
+
### 2.3 Error handling
|
|
249
|
+
|
|
250
|
+
**Iced**: errors travel as the first callback argument by Node convention;
|
|
251
|
+
you bind it with `defer err, data` and test it. `try`/`catch` around an
|
|
252
|
+
`await`: the 1.x line did not catch an error thrown after the `await`
|
|
253
|
+
boundary as far as I know (the continuation runs outside the `try`), and
|
|
254
|
+
later versions changed the rules; I am not certain of the details, so Iced
|
|
255
|
+
code in the wild checks `err` by hand.
|
|
256
|
+
|
|
257
|
+
```coffee
|
|
258
|
+
await fs.readFile path, 'utf8', defer err, text
|
|
259
|
+
if err
|
|
260
|
+
console.warn "could not read: #{err.message}"
|
|
261
|
+
else
|
|
262
|
+
console.log text
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
**MambaScript 2.0**: a rejected promise becomes an exception at the
|
|
266
|
+
`waitForIt`, so `attempt` / `onFail` / `regardless` work across it, and an
|
|
267
|
+
error thrown inside an async function rejects its promise for the caller.
|
|
268
|
+
|
|
269
|
+
```mamba
|
|
270
|
+
flaky = (ok) -> if ok then Promise.resolve 'fine' otherwise Promise.reject make Error 'server down'
|
|
271
|
+
|
|
272
|
+
main = ->
|
|
273
|
+
attempt
|
|
274
|
+
present waitForIt flaky yes
|
|
275
|
+
present waitForIt flaky no
|
|
276
|
+
onFail err
|
|
277
|
+
complain "caught: #{err.message}"
|
|
278
|
+
regardless
|
|
279
|
+
present 'finished'
|
|
280
|
+
|
|
281
|
+
main()
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Prints `fine`, `caught: server down`, `finished`.
|
|
285
|
+
|
|
286
|
+
### 2.4 A callback-style API (what `defer` wrapped for you)
|
|
287
|
+
|
|
288
|
+
**Iced**: `defer` turns any `(err, result)` callback into a slot.
|
|
289
|
+
|
|
290
|
+
```coffee
|
|
291
|
+
await readConfig 'app.json', defer err, text
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
**MambaScript 2.0**: wrap the call in a promise once, or use `util.promisify`
|
|
295
|
+
for Node-style callbacks; then `waitForIt` it.
|
|
296
|
+
|
|
297
|
+
```mamba
|
|
298
|
+
grab {promisify} from 'util'
|
|
299
|
+
|
|
300
|
+
readConfig = (path, callback) -> setTimeout (-> callback nothing, "contents of #{path}"), 5
|
|
301
|
+
|
|
302
|
+
main = ->
|
|
303
|
+
text = waitForIt make Promise (resolve, reject) ->
|
|
304
|
+
readConfig 'app.json', (err, data) -> if err then reject err otherwise resolve data
|
|
305
|
+
present text
|
|
306
|
+
readConfigAsync = promisify readConfig
|
|
307
|
+
present waitForIt readConfigAsync 'other.json'
|
|
308
|
+
|
|
309
|
+
main()
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
Prints `contents of app.json`, `contents of other.json`.
|
|
313
|
+
|
|
314
|
+
**Why**: this is the one place Iced is shorter. In 2026 nearly every API
|
|
315
|
+
(fetch, `fs/promises`, database drivers) already returns promises, so the
|
|
316
|
+
wrapper is rare and `promisify` covers the rest.
|
|
317
|
+
|
|
318
|
+
### 2.5 Waiting for a timer
|
|
319
|
+
|
|
320
|
+
**Iced**:
|
|
321
|
+
|
|
322
|
+
```coffee
|
|
323
|
+
await setTimeout defer(), 100
|
|
324
|
+
console.log 'later'
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
**MambaScript 2.0**: a `sleep` helper, written once.
|
|
328
|
+
|
|
329
|
+
```mamba
|
|
330
|
+
sleep = (ms) -> make Promise (resolve) -> setTimeout resolve, ms
|
|
331
|
+
|
|
332
|
+
main = ->
|
|
333
|
+
waitForIt sleep 10
|
|
334
|
+
present 'ten milliseconds later'
|
|
335
|
+
|
|
336
|
+
main()
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
Prints `ten milliseconds later`.
|
|
340
|
+
|
|
341
|
+
### 2.6 `iced.Rendezvous` (first to finish, or any order)
|
|
342
|
+
|
|
343
|
+
**Iced**: a `Rendezvous` collects deferrals with ids and `wait`s for them one
|
|
344
|
+
at a time in completion order, which is how "whichever comes first" and
|
|
345
|
+
"handle each as it lands" are written.
|
|
346
|
+
|
|
347
|
+
```coffee
|
|
348
|
+
rv = new iced.Rendezvous
|
|
349
|
+
slow 100, rv.id('slow').defer()
|
|
350
|
+
fast 50, rv.id('fast').defer()
|
|
351
|
+
await rv.wait defer which
|
|
352
|
+
console.log which # 'fast'
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
**MambaScript 2.0**: `Promise.race` for the first, `Promise.allSettled` for
|
|
356
|
+
every outcome without throwing, `Promise.any` for the first success; for
|
|
357
|
+
"handle each as it lands", attach a `.then` to each promise and `waitForAll`
|
|
358
|
+
the results.
|
|
359
|
+
|
|
360
|
+
```mamba
|
|
361
|
+
slow = make Promise (resolve) -> setTimeout (-> resolve 'slow'), 20
|
|
362
|
+
fast = make Promise (resolve) -> setTimeout (-> resolve 'fast'), 5
|
|
363
|
+
|
|
364
|
+
main = ->
|
|
365
|
+
winner = waitForIt Promise.race [slow, fast]
|
|
366
|
+
present winner
|
|
367
|
+
results = waitForIt Promise.allSettled [slow, Promise.reject(make Error 'nope')]
|
|
368
|
+
present (r.status forEvery r in results)
|
|
369
|
+
|
|
370
|
+
main()
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
Prints `fast`, then `[ 'fulfilled', 'rejected' ]`.
|
|
374
|
+
|
|
375
|
+
### 2.7 `autocb` and returning a value
|
|
376
|
+
|
|
377
|
+
**Iced**:
|
|
378
|
+
|
|
379
|
+
```coffee
|
|
380
|
+
finish = (autocb) ->
|
|
381
|
+
console.log 'working'
|
|
382
|
+
'result'
|
|
383
|
+
finish (r) -> console.log r
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
**MambaScript 2.0**: the callback parameter disappears; the caller awaits the
|
|
387
|
+
value.
|
|
388
|
+
|
|
389
|
+
```mamba
|
|
390
|
+
finishAsync = ->
|
|
391
|
+
present 'working'
|
|
392
|
+
'result'
|
|
393
|
+
|
|
394
|
+
main = -> present waitForIt finishAsync()
|
|
395
|
+
main()
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
Prints `working`, `result`. (A plain callback-taking function still works in
|
|
399
|
+
MambaScript, of course; it is just not what `waitForIt` is for.)
|
|
400
|
+
|
|
401
|
+
### 2.8 Top-level `await`
|
|
402
|
+
|
|
403
|
+
Both allow it (1.4): in MambaScript the file is wrapped in an async IIFE, or
|
|
404
|
+
becomes a top-level `await` in an ES module.
|
|
405
|
+
|
|
406
|
+
---
|
|
407
|
+
|
|
408
|
+
## 3. What has no equivalent
|
|
409
|
+
|
|
410
|
+
### 3.1 `await`, `defer`, `autocb`, `iced` as words
|
|
411
|
+
|
|
412
|
+
In MambaScript none is reserved. `defer = (x) -> x` compiles and runs;
|
|
413
|
+
`await p` is a compile error with a hint (MS1124, 1.1), so every Iced
|
|
414
|
+
`await` line is found at compile time; a parameter named `autocb` is an
|
|
415
|
+
ordinary parameter and is never called for you (a silent no-op: look for it
|
|
416
|
+
when porting); there is no `iced` object.
|
|
417
|
+
|
|
418
|
+
```mamba
|
|
419
|
+
defer = (x) -> x
|
|
420
|
+
present defer 3
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
Prints `3`.
|
|
424
|
+
|
|
425
|
+
### 3.2 The runtime flags
|
|
426
|
+
|
|
427
|
+
`-I inline|node|window|none`, `--runtime`, `iced.trace`: nothing to
|
|
428
|
+
configure (1.3).
|
|
429
|
+
|
|
430
|
+
### 3.3 Deferred slots that write into members and arrays
|
|
431
|
+
|
|
432
|
+
`defer @user`, `defer results[i]`, `defer err, data` are Iced's way of
|
|
433
|
+
naming where a result goes. In MambaScript the result is the value of the
|
|
434
|
+
`waitForIt` expression, so assign it wherever you like: `@user = waitForIt
|
|
435
|
+
load()`, `results[i] = waitForIt load i`, and a function that calls back with
|
|
436
|
+
`(err, data)` is wrapped as in 2.4.
|
|
437
|
+
|
|
438
|
+
```mamba
|
|
439
|
+
blueprint Profile
|
|
440
|
+
load: (id) ->
|
|
441
|
+
@user = waitForIt fetchUser id
|
|
442
|
+
@user
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
### 3.4 Function return values
|
|
446
|
+
|
|
447
|
+
An Iced function containing `await` returns `undefined` to its caller at the
|
|
448
|
+
first `await` (its result comes through a callback or `autocb`). A MambaScript
|
|
449
|
+
function containing `waitForIt` returns a promise of its last expression. Code
|
|
450
|
+
that relied on an Iced function returning nothing and signalling through
|
|
451
|
+
`autocb` becomes code that returns a value.
|
|
452
|
+
|
|
453
|
+
---
|
|
454
|
+
|
|
455
|
+
## 4. CoffeeScript 1 differences that carry over
|
|
456
|
+
|
|
457
|
+
Iced's base language is CoffeeScript 1.12, so every entry in
|
|
458
|
+
docs/vs-coffeescript.md applies, and a few of them read differently against
|
|
459
|
+
CoffeeScript 1. This section lists them all, briefly, with the CoffeeScript 1
|
|
460
|
+
angle; the headlines and codes are the ones the compiler prints.
|
|
461
|
+
|
|
462
|
+
### 4.1 Classes and `super`
|
|
463
|
+
|
|
464
|
+
**CoffeeScript 1 / Iced**: ES5 constructor functions with an `__extends`
|
|
465
|
+
helper; bare `super` forwards every argument; `super()` passes none;
|
|
466
|
+
`super.method()` is not supported.
|
|
467
|
+
|
|
468
|
+
**MambaScript 2.0**: the same ES5 output (`extends$`, `A.__super__`), so
|
|
469
|
+
porting Iced classes is close to mechanical, with two differences: `super()`
|
|
470
|
+
**forwards every argument** (it means the same as bare `super`, unlike
|
|
471
|
+
CoffeeScript 1 where it passes none); a splat after `super`
|
|
472
|
+
(`super args...`) compiles to `.apply(this, …)` as in Iced. `super.m()` is MS1117
|
|
473
|
+
"super followed by a property access is not supported" in both.
|
|
474
|
+
|
|
475
|
+
```mamba
|
|
476
|
+
blueprint Animal
|
|
477
|
+
constructor: (@name) ->
|
|
478
|
+
speak: (loud) -> if loud then @name.toUpperCase() otherwise @name
|
|
479
|
+
blueprint Dog inheritsFrom Animal
|
|
480
|
+
speak: (loud) -> super(loud) + ' woof'
|
|
481
|
+
shout: -> super
|
|
482
|
+
rex = make Dog 'rex'
|
|
483
|
+
present rex.speak yes
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
Prints `REX woof`.
|
|
487
|
+
|
|
488
|
+
### 4.2 `=>` and `arguments`
|
|
489
|
+
|
|
490
|
+
**CoffeeScript 1 / Iced**: `=>` is a `function` with `_this` captured;
|
|
491
|
+
`arguments` inside it is its own.
|
|
492
|
+
|
|
493
|
+
**MambaScript 2.0**: the same (`this$`). No change when porting (this is the
|
|
494
|
+
one spot where Iced and MambaScript agree against CoffeeScript 2).
|
|
495
|
+
|
|
496
|
+
### 4.3 Modules
|
|
497
|
+
|
|
498
|
+
**CoffeeScript 1 / Iced**: no `import`/`export`; `require` and
|
|
499
|
+
`module.exports` by hand (Iced itself is often used in Node and in the
|
|
500
|
+
browser with a bundler).
|
|
501
|
+
|
|
502
|
+
**MambaScript 2.0**: `require` and `module.exports` still work; `grab` /
|
|
503
|
+
`share` are the sentence forms and also give ES-module output with `--esm`.
|
|
504
|
+
|
|
505
|
+
```mamba
|
|
506
|
+
fs = require 'fs'
|
|
507
|
+
grab {join} from 'path'
|
|
508
|
+
share {join}
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
### 4.4 Operators and literals rejected with a hint
|
|
512
|
+
|
|
513
|
+
All present in CoffeeScript 1.12 and Iced, all rejected: `===` / `!==`
|
|
514
|
+
(MS1124 "=== is JavaScript; MambaScript spells it is"), `//` (MS1025 "// is
|
|
515
|
+
not an operator, and an empty regular expression is not allowed"), `%%`
|
|
516
|
+
(MS1125 "unexpected '%'"), `.5` (MS1125 "unexpected '.'"), splice assignment
|
|
517
|
+
`a[1..2] = x` (MS1105 "the left side of = is not something that can be
|
|
518
|
+
assigned to"), `while … when` (MS1119 "expected an indented block or then,
|
|
519
|
+
found 'when'"), a trailing comma continuing an implicit call (MS1101 "this
|
|
520
|
+
line is indented but nothing opened a block"), an indented operand after a
|
|
521
|
+
trailing operator (MS1107 "the right side of + is indented"), `function`,
|
|
522
|
+
`var`, `let`, `const` (MS1124). `**` compiles to `Math.pow` in both
|
|
523
|
+
CoffeeScript 1 and MambaScript.
|
|
524
|
+
|
|
525
|
+
```mamba
|
|
526
|
+
same = a is b
|
|
527
|
+
half = Math.floor 7 / 2
|
|
528
|
+
x = 0.5
|
|
529
|
+
list.splice 1, 2, 9
|
|
530
|
+
total = a +
|
|
531
|
+
b
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
### 4.5 Things that behave differently at run time
|
|
535
|
+
|
|
536
|
+
- A string broken across lines keeps its newline in MambaScript;
|
|
537
|
+
CoffeeScript 1 joins the lines with a space.
|
|
538
|
+
- `panic 'text'` throws an `Error`; CoffeeScript's `throw 'text'` (still
|
|
539
|
+
accepted in MambaScript) throws the string.
|
|
540
|
+
- `or=` needs a name assigned earlier; `?=` declares (CoffeeScript declares
|
|
541
|
+
for both).
|
|
542
|
+
- A line starting with `.` after an unparenthesised trailing callback attaches
|
|
543
|
+
to the callback body; wrap the callback (`.map((x) -> x * 2)`).
|
|
544
|
+
- `f a: 1 if c` guards the value, not the call (an open bug); write
|
|
545
|
+
`f {a: 1} if c`.
|
|
546
|
+
- The last expression of `finally` / `regardless` is not the value of the
|
|
547
|
+
`try` / `attempt` (CoffeeScript 1 agrees; MambaScript 0.5 did not).
|
|
548
|
+
- `bail` (`break`) inside `consider` (`switch`) leaves the switch in both;
|
|
549
|
+
MambaScript adds labels (`name: forEvery …`, `bail name`), which
|
|
550
|
+
CoffeeScript 1 lacks.
|
|
551
|
+
- Spreads go through `Array.from` (iterators spread; CoffeeScript 1 used
|
|
552
|
+
`[].slice.call`, which does not).
|
|
553
|
+
- `a?.b` repeats the chain instead of caching it, so a getter runs twice.
|
|
554
|
+
|
|
555
|
+
```mamba
|
|
556
|
+
s = 'one
|
|
557
|
+
two'
|
|
558
|
+
attempt
|
|
559
|
+
panic 'boom'
|
|
560
|
+
onFail e
|
|
561
|
+
present e areYouA Error
|
|
562
|
+
name = nothing
|
|
563
|
+
name or= 'anon'
|
|
564
|
+
rows: forEvery row in grid
|
|
565
|
+
forEvery cell in row
|
|
566
|
+
bail rows if cell is target
|
|
567
|
+
present 'searched'
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
### 4.6 New since CoffeeScript 1 that Iced does not have
|
|
571
|
+
|
|
572
|
+
Generators (`yield`, `yield from`), `for … from` over iterables, async
|
|
573
|
+
generators (`for waitForIt x from`), tagged templates (`tag"…"`), object
|
|
574
|
+
rest and spread (`{a..., b: 1}`, `{id, rest...} = o`), destructuring
|
|
575
|
+
defaults (`{age = 18} = p`), computed keys `{[k]: v}`, numeric separators
|
|
576
|
+
and BigInt, labeled loops, every regex flag, the type system (`x :: Int`,
|
|
577
|
+
`struct`, `implements`, generics), `grab`/`share` with ES-module output, and
|
|
578
|
+
the sentence keywords (`blueprint`, `make`, `consider`, `attempt`, `panic`,
|
|
579
|
+
`forEvery`, `bail`, `keepGoing`, `nothing`, `missing`, `exists`, `orElse`,
|
|
580
|
+
`has`, `present`, `complain`). Each is documented with its JavaScript in
|
|
581
|
+
docs/reference.md.
|
|
582
|
+
|
|
583
|
+
```mamba
|
|
584
|
+
numbers = ->
|
|
585
|
+
yield 1
|
|
586
|
+
yield from [2, 3]
|
|
587
|
+
forEvery n from numbers() when n > 1
|
|
588
|
+
present n
|
|
589
|
+
{id, safe...} = user
|
|
590
|
+
query = sql"""
|
|
591
|
+
SELECT * FROM users WHERE id = #{id}
|
|
592
|
+
"""
|
|
593
|
+
```
|
|
594
|
+
|
|
595
|
+
### 4.7 Reserved words
|
|
596
|
+
|
|
597
|
+
An Iced program may use `make`, `share`, `bail`, `has`, `exists`, `missing`,
|
|
598
|
+
`nothing`, `panic`, `attempt`, `consider`, `grab` and the other 2.0 words as
|
|
599
|
+
variable names (and, being Iced, uses `defer`, `await`, `autocb` and `iced`,
|
|
600
|
+
which stay free in MambaScript). Rename the first group: `missing = …` is
|
|
601
|
+
MS1106 "missing is a keyword and cannot be assigned to", `make = 1` is MS1118
|
|
602
|
+
"expected a class or constructor after make, found '='", `share = 1` is
|
|
603
|
+
MS1107 "expected a value or a {list of names} after share, found '='".
|
|
604
|
+
|
|
605
|
+
### 4.8 Tooling
|
|
606
|
+
|
|
607
|
+
`iced` (the CLI, same flags as `coffee` 1.x plus `-I`) against `mamba`
|
|
608
|
+
(run, compile, REPL, `init`) and `mambascript` (stdin/stdout, source maps,
|
|
609
|
+
AST); `require 'iced-coffee-script/register'` against `require 'mambascript'`
|
|
610
|
+
(automatic registration); `mambascript/jest` for tests; `--esm` and the
|
|
611
|
+
`node --import mambascript/esm` loader have no Iced counterpart. The
|
|
612
|
+
docs/vs-coffeescript.md tooling table has the full list.
|
|
613
|
+
|
|
614
|
+
---
|
|
615
|
+
|
|
616
|
+
## In one paragraph
|
|
617
|
+
|
|
618
|
+
Porting Iced: replace each `await f args, defer x` with `x = waitForIt f args`
|
|
619
|
+
(wrap a callback API in a promise first, or `promisify` it); a loop of
|
|
620
|
+
`defer`s inside one `await` becomes `waitForAll` over a comprehension; drop
|
|
621
|
+
`autocb` and return the value; check errors with `attempt`/`onFail` instead
|
|
622
|
+
of `err`; remove the runtime flags. Then apply the CoffeeScript 1 list:
|
|
623
|
+
rename the reserved words, fix `===`, `//`, `%%`, `.5`, splices and
|
|
624
|
+
`while … when`, remember that `super()` forwards everything and a splat after
|
|
625
|
+
`super` does not compile, that multi-line strings keep their newline, and that
|
|
626
|
+
`or=` wants an assigned name.
|