mambascript 0.5.5 → 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,1593 @@
|
|
|
1
|
+
# MambaScript against CoffeeScript 2
|
|
2
|
+
|
|
3
|
+
MambaScript grew out of CoffeeScript Redux and TypedCoffeeScript, so it has
|
|
4
|
+
CoffeeScript's indentation, its `->` and `=>`, its comprehensions, ranges,
|
|
5
|
+
heregexes, `is`/`isnt`/`and`/`or`/`not`/`yes`/`no`/`on`/`off`. It is not a
|
|
6
|
+
CoffeeScript superset and does not try to be: it adds sentence keywords, a
|
|
7
|
+
gradual type checker and stricter line rules, and it drops a handful of
|
|
8
|
+
CoffeeScript forms on purpose. This page lists every difference we know of,
|
|
9
|
+
in both directions, for people moving between the two.
|
|
10
|
+
|
|
11
|
+
How this page was made (2026-12-05): every `mamba` block was compiled with
|
|
12
|
+
MambaScript 2.0.0 on Node 24 (`mamba -c -p --strict` with the default parser
|
|
13
|
+
and `mambascript --js --bare --strict --parser peg` with the fallback), and
|
|
14
|
+
the error headlines and codes are quoted from the compiler. The `coffee`
|
|
15
|
+
blocks are not compiled here (there is no CoffeeScript in this repository):
|
|
16
|
+
they describe CoffeeScript 2.7 from its language reference and changelog, and
|
|
17
|
+
where a CoffeeScript detail is uncertain the entry says so instead of guessing.
|
|
18
|
+
The last section covers MambaScript 0.5 to 2.0, since 0.5 was closer to
|
|
19
|
+
CoffeeScript than 2.0 is.
|
|
20
|
+
|
|
21
|
+
Contents: [the words](#1-the-words) · [rejected with a hint](#2-forms-that-are-rejected-with-a-hint)
|
|
22
|
+
· [looks the same, behaves differently](#3-looks-the-same-behaves-differently) ·
|
|
23
|
+
[types](#4-the-type-system) · [error messages](#5-error-messages) ·
|
|
24
|
+
[compiled output](#6-the-compiled-output) · [tooling](#7-tooling) ·
|
|
25
|
+
[0.5 to 2.0](#8-mambascript-05-to-20)
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## 1. The words
|
|
30
|
+
|
|
31
|
+
### 1.1 What is shared
|
|
32
|
+
|
|
33
|
+
**CoffeeScript 2**: `is`, `isnt`, `and`, `or`, `not`, `yes`, `no`, `on`, `off`,
|
|
34
|
+
`unless`, `until`, `loop`, `then`, `when`, `by`, `do`, `of`, `in`, `@`, `::`,
|
|
35
|
+
`?`, `?.`, `?=`, splats `args...`, ranges `[1..5]`, slices `list[1...3]`,
|
|
36
|
+
heredocs `"""`, heregexes `///…///`, `#` and `###` comments, embedded
|
|
37
|
+
JavaScript in backticks, chained comparisons `1 < x < 10`, postfix `if` /
|
|
38
|
+
`unless` / `for` / `while`, everything-is-an-expression, implicit returns,
|
|
39
|
+
implicit calls `f a, b`, implicit objects `f a: 1`.
|
|
40
|
+
|
|
41
|
+
```coffee
|
|
42
|
+
greet = (name) -> "hi #{name}" unless name is 'nobody'
|
|
43
|
+
evens = (n for n in [1..10] when n % 2 is 0)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
**MambaScript 2.0**: the same program compiles unchanged.
|
|
47
|
+
|
|
48
|
+
```mamba
|
|
49
|
+
greet = (name) -> "hi #{name}" unless name is 'nobody'
|
|
50
|
+
evens = (n for n in [1..10] when n % 2 is 0)
|
|
51
|
+
ok = 1 < x < 10 and x not in [3, 4]
|
|
52
|
+
name = user?.profile?.name ? 'anon'
|
|
53
|
+
options ?= {}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`==` is also accepted and compiles to `===`, exactly as CoffeeScript does
|
|
57
|
+
(`a == b` becomes `a === b`; there is no loose equality in either language).
|
|
58
|
+
|
|
59
|
+
**Why**: the resemblance is historical. Everything in this list means the
|
|
60
|
+
same thing in both languages; the rest of this page is what differs.
|
|
61
|
+
|
|
62
|
+
### 1.2 The sentence words have JavaScript-word synonyms, in one direction
|
|
63
|
+
|
|
64
|
+
**CoffeeScript 2**: `class`, `extends`, `new`, `instanceof`, `delete`, `for`,
|
|
65
|
+
`switch`, `else`, `try`, `catch`, `finally`, `throw`, `break`, `continue`,
|
|
66
|
+
`in` (membership), `await`, `import`, `export`.
|
|
67
|
+
|
|
68
|
+
```coffee
|
|
69
|
+
class Dog extends Animal
|
|
70
|
+
speak: -> super() + ' woof'
|
|
71
|
+
rex = new Dog 'Rex'
|
|
72
|
+
throw new Error 'no' unless rex instanceof Animal
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
**MambaScript 2.0**: each construct has a sentence word, and most of the
|
|
76
|
+
JavaScript words still work as synonyms (`class`, `extends`, `new`,
|
|
77
|
+
`instanceof`, `delete`, `for`, `switch`, `else`, `try`, `catch`, `finally`,
|
|
78
|
+
`throw`, `break`, `continue`). The exceptions are the module and async words:
|
|
79
|
+
`await`, `async`, `import` and `export` are **not** synonyms (1.9, 2.4).
|
|
80
|
+
|
|
81
|
+
```mamba
|
|
82
|
+
class Dog extends Animal
|
|
83
|
+
speak: -> super() + ' woof'
|
|
84
|
+
rex = new Dog 'Rex'
|
|
85
|
+
throw new Error 'no' unless rex instanceof Animal
|
|
86
|
+
|
|
87
|
+
blueprint Cat inheritsFrom Animal
|
|
88
|
+
speak: -> super() + ' meow'
|
|
89
|
+
tom = make Cat 'Tom'
|
|
90
|
+
panic 'no' unless tom areYouA Animal
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Both halves compile to the same shape of JavaScript (`Dog = function (super$) { extends$(Dog, super$); … }(Animal)`, `rex = new Dog('Rex')`).
|
|
94
|
+
|
|
95
|
+
**Why**: the sentence words are the house style (the README's keyword table
|
|
96
|
+
is the canonical list); the synonyms exist so CoffeeScript habits do not
|
|
97
|
+
block anyone. The cost is two spellings for one thing. The style guide and the
|
|
98
|
+
agent skill always write the sentence form.
|
|
99
|
+
|
|
100
|
+
### 1.3 `blueprint` / `inheritsFrom` / `make` (classes)
|
|
101
|
+
|
|
102
|
+
**CoffeeScript 2**: `class A extends B`, `new A`; compiles to a native ES2015
|
|
103
|
+
`class` with `extends` and `super`.
|
|
104
|
+
|
|
105
|
+
```coffee
|
|
106
|
+
class Hero extends Human
|
|
107
|
+
constructor: (name, @power) ->
|
|
108
|
+
super name
|
|
109
|
+
greet: -> super() + '!'
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
**MambaScript 2.0**: `blueprint A inheritsFrom B`, `make A`; compiles to an
|
|
113
|
+
ES5 constructor function with an `extends$` helper and `A.__super__`.
|
|
114
|
+
|
|
115
|
+
```mamba
|
|
116
|
+
blueprint Hero inheritsFrom Human
|
|
117
|
+
constructor: (name, @power) ->
|
|
118
|
+
super name
|
|
119
|
+
greet: -> super() + '!'
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
Hero = function (super$) {
|
|
124
|
+
extends$(Hero, super$);
|
|
125
|
+
function Hero(name, param$) {
|
|
126
|
+
this.power = param$;
|
|
127
|
+
Hero.__super__.constructor.call(this, name);
|
|
128
|
+
}
|
|
129
|
+
Hero.prototype.greet = function () {
|
|
130
|
+
return Hero.__super__.greet.apply(this, arguments) + '!';
|
|
131
|
+
};
|
|
132
|
+
return Hero;
|
|
133
|
+
}(Human);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
**Why**: a `blueprint` is a plan for an object, which is what a learner needs
|
|
137
|
+
to hear; `make` says what `new` does. The ES5 output is a real cost: a
|
|
138
|
+
blueprint cannot extend a native ES2015 class that requires `new` (todos.md
|
|
139
|
+
lists native `class` output as the fix), and `super` has different rules
|
|
140
|
+
(2.11).
|
|
141
|
+
|
|
142
|
+
### 1.4 `areYouA`, `thanos`, `has`, `of`, `in`
|
|
143
|
+
|
|
144
|
+
**CoffeeScript 2**: `x instanceof C`, `delete o.k`, `'k' of o` (key
|
|
145
|
+
membership), `x in list` (array membership).
|
|
146
|
+
|
|
147
|
+
```coffee
|
|
148
|
+
if dog instanceof Animal and 'name' of dog and dog.name in names
|
|
149
|
+
delete dog.tmp
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
**MambaScript 2.0**: `areYouA`, `thanos`, `has` as the sentence forms; `of`
|
|
153
|
+
and `in` keep CoffeeScript's meanings (`of` is JavaScript's `in`; `in` is
|
|
154
|
+
array membership, a chain of `===` for a literal list or an `in$` helper for
|
|
155
|
+
a variable).
|
|
156
|
+
|
|
157
|
+
```mamba
|
|
158
|
+
if dog areYouA Animal and dog has 'name' and dog.name in names
|
|
159
|
+
thanos dog.tmp
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
```js
|
|
163
|
+
if (dog instanceof Animal && 'name' in dog && in$(dog.name, names)) {
|
|
164
|
+
delete dog.tmp;
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
**Why**: `areYouA` and `thanos` are memorable (one snaps a property out of
|
|
169
|
+
existence); `has` reads with the object first, which is how people say it.
|
|
170
|
+
The cost is that `'k' of o` and `o has 'k'` are two spellings of one thing.
|
|
171
|
+
|
|
172
|
+
### 1.5 `consider` / `when` / `otherwise` (switch)
|
|
173
|
+
|
|
174
|
+
**CoffeeScript 2**: `switch x` with `when a, b then …` and `else`; no
|
|
175
|
+
fallthrough, no `break` needed; a `switch` with no subject tests each `when`
|
|
176
|
+
as a boolean; usable as a value.
|
|
177
|
+
|
|
178
|
+
```coffee
|
|
179
|
+
kind = switch n
|
|
180
|
+
when 1 then 'one'
|
|
181
|
+
when 2, 3 then 'few'
|
|
182
|
+
else 'many'
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
**MambaScript 2.0**: `consider` / `when` / `otherwise`; `switch` and `else`
|
|
186
|
+
remain synonyms; the same no-fallthrough, no-subject and value forms.
|
|
187
|
+
|
|
188
|
+
```mamba
|
|
189
|
+
kind = consider n
|
|
190
|
+
when 1 then 'one'
|
|
191
|
+
when 2, 3 then 'few'
|
|
192
|
+
otherwise 'many'
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
```js
|
|
196
|
+
kind = function () {
|
|
197
|
+
switch (n) {
|
|
198
|
+
case 1:
|
|
199
|
+
return 'one';
|
|
200
|
+
case 2:
|
|
201
|
+
case 3:
|
|
202
|
+
return 'few';
|
|
203
|
+
default:
|
|
204
|
+
return 'many';
|
|
205
|
+
}
|
|
206
|
+
}.call(this);
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
`case` is not a word: `case 1 then …` inside the block is MS1120 "expected
|
|
210
|
+
when or otherwise inside the consider block, found 'case'". A `consider` with
|
|
211
|
+
no branches as a value (`x = switch`) is MS1120 "expected when branches".
|
|
212
|
+
|
|
213
|
+
**Why**: `consider` says "consider the cases"; `otherwise` is the word people
|
|
214
|
+
say for the default branch, and it is the same word as the `else` of an
|
|
215
|
+
`if`/`then`. The cost: `otherwise` is one more reserved word.
|
|
216
|
+
|
|
217
|
+
### 1.6 `forEvery`, `keepGoing`, `bail`, labeled loops
|
|
218
|
+
|
|
219
|
+
**CoffeeScript 2**: `for x in list`, `for k, v of obj`, `for own k of obj`,
|
|
220
|
+
`for x from iterable`, `continue`, `break`; no labels.
|
|
221
|
+
|
|
222
|
+
```coffee
|
|
223
|
+
for row, r in grid
|
|
224
|
+
for cell, c in row
|
|
225
|
+
continue if cell is 0
|
|
226
|
+
break if cell is target # leaves the inner loop only
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
**MambaScript 2.0**: `forEvery` is the sentence form of `for` (all three
|
|
230
|
+
loop kinds, `own`, `by`, `when`); `keepGoing` / `bail`; and a loop may be
|
|
231
|
+
labeled with `name:` on its own line so `bail name` / `keepGoing name` leave
|
|
232
|
+
or advance an outer loop.
|
|
233
|
+
|
|
234
|
+
```mamba
|
|
235
|
+
rows: forEvery row, r in grid
|
|
236
|
+
forEvery cell, c in row
|
|
237
|
+
keepGoing if cell is 0
|
|
238
|
+
bail rows if cell is target # leaves both loops
|
|
239
|
+
present 'searched'
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
```js
|
|
243
|
+
rows:
|
|
244
|
+
for (var i$ = 0, length$ = grid.length; i$ < length$; ++i$) {
|
|
245
|
+
row = grid[i$];
|
|
246
|
+
r = i$;
|
|
247
|
+
for (var i$1 = 0, length$1 = row.length; i$1 < length$1; ++i$1) {
|
|
248
|
+
cell = row[i$1];
|
|
249
|
+
c = i$1;
|
|
250
|
+
if (cell === 0)
|
|
251
|
+
continue;
|
|
252
|
+
if (cell === target)
|
|
253
|
+
break rows;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
A `name: forEvery …` line is read exactly as CoffeeScript reads it, as an
|
|
259
|
+
object with one member. In statement position that object would be thrown
|
|
260
|
+
away, so it becomes a label; as a value (the last line of a function or of a
|
|
261
|
+
file) it stays an object, and `bail rows` then says MS1131 "no loop is
|
|
262
|
+
labeled rows here … rows: … here is the last line of its block, so its value
|
|
263
|
+
is used". Put a line after the loop (the `present` above, or `return`).
|
|
264
|
+
|
|
265
|
+
**Why**: `forEvery` reads aloud; `bail` and `keepGoing` are the words a
|
|
266
|
+
person uses. Labels came in 2.0 because `bail` inside `consider` (3.3) and
|
|
267
|
+
nested loops needed them, and the implicit-object reading was kept so no
|
|
268
|
+
CoffeeScript-shaped program changed meaning.
|
|
269
|
+
|
|
270
|
+
### 1.7 `attempt` / `onFail` / `regardless` / `panic`
|
|
271
|
+
|
|
272
|
+
**CoffeeScript 2**: `try` / `catch e` / `finally`; `throw x` throws `x` as
|
|
273
|
+
it is, so `throw 'text'` throws a string with no stack.
|
|
274
|
+
|
|
275
|
+
```coffee
|
|
276
|
+
try
|
|
277
|
+
risky()
|
|
278
|
+
catch err
|
|
279
|
+
console.warn err.message
|
|
280
|
+
finally
|
|
281
|
+
cleanup()
|
|
282
|
+
throw 'no port' unless config.port
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
**MambaScript 2.0**: `attempt` / `onFail err` / `regardless`; `panic`. A
|
|
286
|
+
string (or interpolated string) after `panic` becomes `new Error(...)`;
|
|
287
|
+
anything else is thrown as it is. `throw 'x'` is still accepted and still
|
|
288
|
+
throws the bare string.
|
|
289
|
+
|
|
290
|
+
```mamba
|
|
291
|
+
attempt
|
|
292
|
+
risky()
|
|
293
|
+
onFail err
|
|
294
|
+
complain err.message
|
|
295
|
+
regardless
|
|
296
|
+
cleanup()
|
|
297
|
+
panic 'no port' unless config.port
|
|
298
|
+
panic make RangeError 'too big' if size > 10
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
```js
|
|
302
|
+
try {
|
|
303
|
+
risky();
|
|
304
|
+
} catch (e$) {
|
|
305
|
+
err = e$;
|
|
306
|
+
complain(err.message);
|
|
307
|
+
} finally {
|
|
308
|
+
cleanup();
|
|
309
|
+
}
|
|
310
|
+
if (!config.port)
|
|
311
|
+
throw new Error('no port');
|
|
312
|
+
if (size > 10)
|
|
313
|
+
throw new RangeError('too big');
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
**Why**: a thrown string has no stack trace and cannot be told apart from a
|
|
317
|
+
value; `panic 'text'` is what people mean and it always produces an `Error`.
|
|
318
|
+
The cost: `panic err` and `panic 'text'` behave differently, and `throw` and
|
|
319
|
+
`panic` are not quite synonyms.
|
|
320
|
+
|
|
321
|
+
### 1.8 `waitForIt` / `waitForAll` (async)
|
|
322
|
+
|
|
323
|
+
**CoffeeScript 2**: `await` is a keyword; a function that contains `await` is
|
|
324
|
+
compiled as `async` automatically (no `async` keyword), `for await` walks
|
|
325
|
+
async iterables. There is no `Promise.all` shorthand.
|
|
326
|
+
|
|
327
|
+
```coffee
|
|
328
|
+
load = ->
|
|
329
|
+
res = await fetch url
|
|
330
|
+
await res.json()
|
|
331
|
+
both = -> await Promise.all [load(), load()]
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
**MambaScript 2.0**: the same implicit-async design, with `waitForIt` and
|
|
335
|
+
`waitForAll` (= `await Promise.all`). `await` is **not** a keyword (2.4).
|
|
336
|
+
|
|
337
|
+
```mamba
|
|
338
|
+
load = ->
|
|
339
|
+
res = waitForIt fetch url
|
|
340
|
+
waitForIt res.json()
|
|
341
|
+
both = -> waitForAll [load(), load()]
|
|
342
|
+
lines = (stream) ->
|
|
343
|
+
for waitForIt chunk from stream
|
|
344
|
+
yield chunk.toString()
|
|
345
|
+
return
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
```js
|
|
349
|
+
load = async function () {
|
|
350
|
+
var res;
|
|
351
|
+
res = await fetch(url);
|
|
352
|
+
return await res.json();
|
|
353
|
+
};
|
|
354
|
+
both = async function () {
|
|
355
|
+
return await Promise.all([load(), load()]);
|
|
356
|
+
};
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
A top-level `waitForIt` wraps the whole program in an async function (or is
|
|
360
|
+
a top-level `await` in `--esm` output); `waitForIt` in a `blueprint`
|
|
361
|
+
constructor is a compile error, because constructors cannot be async.
|
|
362
|
+
|
|
363
|
+
**Why**: "wait for it" is what `await` does; `waitForAll` is the shape every
|
|
364
|
+
parallel fetch takes. The cost: `await` and `async` look like they should
|
|
365
|
+
work and do not (2.4).
|
|
366
|
+
|
|
367
|
+
### 1.9 `grab` / `share` (modules)
|
|
368
|
+
|
|
369
|
+
**CoffeeScript 2**: `import` / `export` are passed through to the JavaScript
|
|
370
|
+
unchanged (CoffeeScript does not resolve or rewrite them), so the output is
|
|
371
|
+
an ES module whatever the rest of the file does; CommonJS is written by hand
|
|
372
|
+
with `require` and `module.exports`.
|
|
373
|
+
|
|
374
|
+
```coffee
|
|
375
|
+
import express from 'express'
|
|
376
|
+
import {join, resolve as abs} from 'path'
|
|
377
|
+
export default router
|
|
378
|
+
export {helper}
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
**MambaScript 2.0**: `grab` / `share` are the only module syntax; the same
|
|
382
|
+
source produces CommonJS (the default) or ES modules (`--esm`, or a
|
|
383
|
+
`"type": "module"` package). `import` and `export` are rejected (2.4).
|
|
384
|
+
|
|
385
|
+
```mamba
|
|
386
|
+
grab express from 'express'
|
|
387
|
+
grab {join, resolve: abs} from 'path'
|
|
388
|
+
grab * as fs from 'fs/promises'
|
|
389
|
+
grab './boot'
|
|
390
|
+
router = express.Router()
|
|
391
|
+
share {router, PORT: 3000}
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
CommonJS (default):
|
|
395
|
+
|
|
396
|
+
```js
|
|
397
|
+
express = require('express');
|
|
398
|
+
cache$ = require('path');
|
|
399
|
+
join = cache$.join;
|
|
400
|
+
abs = cache$.resolve;
|
|
401
|
+
require('./boot');
|
|
402
|
+
router = express.Router();
|
|
403
|
+
module.exports.router = router;
|
|
404
|
+
module.exports.PORT = 3e3;
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
`--esm`:
|
|
408
|
+
|
|
409
|
+
```js
|
|
410
|
+
import express from 'express';
|
|
411
|
+
import { join, resolve as abs } from 'path';
|
|
412
|
+
import './boot';
|
|
413
|
+
var PORT$, router;
|
|
414
|
+
router = express.Router();
|
|
415
|
+
PORT$ = 3e3;
|
|
416
|
+
export { router, PORT$ as PORT };
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
`share X` once per file is the default export; `share {a, b}` are named
|
|
420
|
+
exports; `share {a} from 'm'` and `share * from 'm'` re-export;
|
|
421
|
+
`waitForIt import('m')` is a dynamic import in both modes. `require` and
|
|
422
|
+
`module.exports` still work as plain JavaScript.
|
|
423
|
+
|
|
424
|
+
**Why**: one syntax for both module systems, decided at build time, keeps a
|
|
425
|
+
0.5 project on CommonJS and a new project on ES modules without touching the
|
|
426
|
+
source. The cost: `grab` destructures one level only (MS3001), and the ESM
|
|
427
|
+
form has its own rules (one default share, MS3003; top level only, MS3002).
|
|
428
|
+
|
|
429
|
+
### 1.10 `nothing` / `missing`, `exists`, `orElse`, `aint`
|
|
430
|
+
|
|
431
|
+
**CoffeeScript 2**: `null`, `undefined`, `x?`, `a ? b`, `isnt`.
|
|
432
|
+
|
|
433
|
+
```coffee
|
|
434
|
+
a = null
|
|
435
|
+
name = input ? 'anon' if input?
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
**MambaScript 2.0**: `nothing` (`null`), `missing` (`void 0`), `x exists`
|
|
439
|
+
(= `x?`), `a orElse b` (= `a ? b`), `aint` (= `isnt`). The CoffeeScript
|
|
440
|
+
spellings all still work.
|
|
441
|
+
|
|
442
|
+
```mamba
|
|
443
|
+
a = nothing
|
|
444
|
+
b = missing
|
|
445
|
+
name = input orElse 'anon' if input exists
|
|
446
|
+
different = mood aint 'good'
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
```js
|
|
450
|
+
a = null;
|
|
451
|
+
b = void 0;
|
|
452
|
+
if ('undefined' !== typeof input && null != input)
|
|
453
|
+
name = 'undefined' !== typeof input && null != input ? input : 'anon';
|
|
454
|
+
different = mood !== 'good';
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
The 2.0 words are reserved: `missing = (x for x in xs)` is MS1106
|
|
458
|
+
"missing is a keyword and cannot be assigned to"; `make = 1` is MS1118
|
|
459
|
+
"expected a class or constructor after make, found '='"; `share = 1` is
|
|
460
|
+
MS1107 "expected a value or a {list of names} after share, found '='". In
|
|
461
|
+
CoffeeScript all of these are ordinary variable names.
|
|
462
|
+
|
|
463
|
+
**Why**: the words say what the values are. The cost is the reserved-word
|
|
464
|
+
list: a CoffeeScript program that names a variable `make`, `share`, `bail`,
|
|
465
|
+
`missing`, `has` or `exists` must rename it.
|
|
466
|
+
|
|
467
|
+
### 1.11 `present` / `complain`
|
|
468
|
+
|
|
469
|
+
**CoffeeScript 2**: `console.log`, `console.warn`.
|
|
470
|
+
|
|
471
|
+
**MambaScript 2.0**: `present` and `complain` are built-in values, declared
|
|
472
|
+
with a real `var` at the top of any program that uses them without defining
|
|
473
|
+
them (`var present = console.log, complain = console.warn;`). They are
|
|
474
|
+
ordinary values (`list.forEach present` works). `_` (the standard library,
|
|
475
|
+
Underscore ported to MambaScript) is injected the same way as
|
|
476
|
+
`var _ = require('mambascript/stdlib')`.
|
|
477
|
+
|
|
478
|
+
```mamba
|
|
479
|
+
present 'hello', 42
|
|
480
|
+
complain 'careful now'
|
|
481
|
+
[1, 2].forEach present
|
|
482
|
+
present _.map [1, 2], (n) -> n * 2
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
**Why**: `console.log` is the first thing a learner types and the hardest
|
|
486
|
+
thing to explain; `present` is a verb. The cost: a program that defines its
|
|
487
|
+
own `present` keeps it and gets no injection, which is the right behaviour
|
|
488
|
+
but can surprise.
|
|
489
|
+
|
|
490
|
+
### 1.12 `struct`, `::` types, `implements`, generics (no CoffeeScript equivalent)
|
|
491
|
+
|
|
492
|
+
**CoffeeScript 2**: no type system; `::` is prototype access only.
|
|
493
|
+
|
|
494
|
+
**MambaScript 2.0**: `x :: T` (with spaces) declares a type; `a::b` without
|
|
495
|
+
spaces is still prototype access. `struct` declares a named structural type
|
|
496
|
+
(no output), `blueprint X implements S` checks a class against it, `<T>` is
|
|
497
|
+
a generic parameter. See section 4.
|
|
498
|
+
|
|
499
|
+
```mamba
|
|
500
|
+
struct Point
|
|
501
|
+
x :: Int
|
|
502
|
+
y :: Int
|
|
503
|
+
add :: (Int, Int) -> Int = (a, b) -> a + b
|
|
504
|
+
p :: Point = {x: 1, y: 2}
|
|
505
|
+
A::late = -> 1
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
### 1.13 Generators: `yield`, `yield from`, `for … from`
|
|
509
|
+
|
|
510
|
+
**CoffeeScript 2**: a function containing `yield` is a generator; `yield from`
|
|
511
|
+
delegates; `for x from iterable` walks any iterable (added in 2.0).
|
|
512
|
+
|
|
513
|
+
```coffee
|
|
514
|
+
numbers = ->
|
|
515
|
+
yield 1
|
|
516
|
+
yield from [2, 3]
|
|
517
|
+
for n from numbers()
|
|
518
|
+
console.log n
|
|
519
|
+
```
|
|
520
|
+
|
|
521
|
+
**MambaScript 2.0**: identical, with `forEvery … from` as the sentence form,
|
|
522
|
+
and the same diagnostics the rest of the language has (MS4001 `yield` outside
|
|
523
|
+
a function, MS4002 `yield from` needs an iterable, MS4003 a `from` loop takes
|
|
524
|
+
one variable and no `own` or `by`).
|
|
525
|
+
|
|
526
|
+
```mamba
|
|
527
|
+
numbers = ->
|
|
528
|
+
yield 1
|
|
529
|
+
yield from [2, 3]
|
|
530
|
+
forEvery n from numbers()
|
|
531
|
+
present n
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
```js
|
|
535
|
+
numbers = function* () {
|
|
536
|
+
yield 1;
|
|
537
|
+
return yield* [2, 3];
|
|
538
|
+
};
|
|
539
|
+
for (n of numbers()) {
|
|
540
|
+
present(n);
|
|
541
|
+
}
|
|
542
|
+
```
|
|
543
|
+
|
|
544
|
+
See 3.17 for a generator whose last statement is a loop.
|
|
545
|
+
|
|
546
|
+
---
|
|
547
|
+
|
|
548
|
+
## 2. Forms that are rejected with a hint
|
|
549
|
+
|
|
550
|
+
Each of these is valid CoffeeScript 2 and a compile error in MambaScript. The
|
|
551
|
+
headline and code are what `mamba` prints (the fallback `--parser peg` reports
|
|
552
|
+
the same position with a generic `unexpected "…"` message and asks you to
|
|
553
|
+
rerun without it).
|
|
554
|
+
|
|
555
|
+
### 2.1 `===` and `!==`
|
|
556
|
+
|
|
557
|
+
**CoffeeScript 2**: accepted as synonyms of `is` / `isnt`.
|
|
558
|
+
|
|
559
|
+
```coffee
|
|
560
|
+
same = a === b
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
**MambaScript 2.0**: MS1124 "=== is JavaScript; MambaScript spells it is";
|
|
564
|
+
MS1124 "!== is JavaScript; MambaScript spells it isnt".
|
|
565
|
+
|
|
566
|
+
```mamba
|
|
567
|
+
same = a is b
|
|
568
|
+
different = a isnt b
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
**Why**: one spelling for strict equality, and it is the one you can read
|
|
572
|
+
aloud. `==` is accepted as a courtesy and means `===` (1.1).
|
|
573
|
+
|
|
574
|
+
### 2.2 `//` floor division and `%%` modulo
|
|
575
|
+
|
|
576
|
+
**CoffeeScript 2**: `7 // 2` is `Math.floor(7 / 2)`; `-7 %% 3` is a true
|
|
577
|
+
modulo (2) through a `modulo` helper.
|
|
578
|
+
|
|
579
|
+
```coffee
|
|
580
|
+
half = 7 // 2
|
|
581
|
+
wrapped = -7 %% 3
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
**MambaScript 2.0**: MS1025 "// is not an operator, and an empty regular
|
|
585
|
+
expression is not allowed" (the fix it prints is `Math.floor 7 / 2`); `%%`
|
|
586
|
+
is MS1125 "unexpected '%'" with no special hint. `%` is JavaScript's
|
|
587
|
+
remainder (`7 % -3` is `1`).
|
|
588
|
+
|
|
589
|
+
```mamba
|
|
590
|
+
half = Math.floor 7 / 2
|
|
591
|
+
modulo = (a, b) -> ((a % b) + b) % b
|
|
592
|
+
wrapped = modulo -7, 3
|
|
593
|
+
```
|
|
594
|
+
|
|
595
|
+
**Why**: `//` and `%%` are invisible to a reader who knows JavaScript and
|
|
596
|
+
silently differ from `/` and `%`; writing `Math.floor` says what happens.
|
|
597
|
+
The cost is a helper for true modulo.
|
|
598
|
+
|
|
599
|
+
### 2.3 `.5`, holes in arrays, splice assignment, `while … when`
|
|
600
|
+
|
|
601
|
+
**CoffeeScript 2**: `.5` is a number; `[a, , b] = arr` skips an element
|
|
602
|
+
(elisions, added in a later 2.x release); `a[1..2] = [9]` splices; `while c
|
|
603
|
+
when guard` filters a `while` loop.
|
|
604
|
+
|
|
605
|
+
```coffee
|
|
606
|
+
x = .5
|
|
607
|
+
[first, , third] = list
|
|
608
|
+
list[1..2] = [9]
|
|
609
|
+
while x > 0 when x isnt 5
|
|
610
|
+
x -= 1
|
|
611
|
+
```
|
|
612
|
+
|
|
613
|
+
**MambaScript 2.0**: `.5` is MS1125 "unexpected '.'"; the hole is MS1125
|
|
614
|
+
"unexpected ','"; the splice is MS1105 "the left side of = is not something
|
|
615
|
+
that can be assigned to"; the guard is MS1119 "expected an indented block or
|
|
616
|
+
then, found 'when'".
|
|
617
|
+
|
|
618
|
+
```mamba
|
|
619
|
+
x = 0.5
|
|
620
|
+
[first, _skip, third] = list
|
|
621
|
+
list.splice 1, 2, 9
|
|
622
|
+
while x > 0
|
|
623
|
+
x -= 1
|
|
624
|
+
keepGoing if x is 5
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
**Why**: a digit before the point and a name in every slot are readable; a
|
|
628
|
+
slice is a copy, so a slice on the left of `=` was a trap; `when` on a
|
|
629
|
+
`while` is a filter nobody expects. The cost: a line more in each case.
|
|
630
|
+
|
|
631
|
+
### 2.4 `import`, `export`, `function`, `var`, `let`, `const`, `await`, `async`
|
|
632
|
+
|
|
633
|
+
**CoffeeScript 2**: `import` / `export` pass through; `function`, `var`,
|
|
634
|
+
`let`, `const` are reserved-word errors; `await` is the async keyword.
|
|
635
|
+
|
|
636
|
+
```coffee
|
|
637
|
+
import fs from 'fs'
|
|
638
|
+
export {x}
|
|
639
|
+
data = await fs.promises.readFile path
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
**MambaScript 2.0**: `import express from 'express'` is MS1124 "import is
|
|
643
|
+
JavaScript; MambaScript spells it differently" (the Fix line shows `grab`);
|
|
644
|
+
`export x` is MS1124 "export is JavaScript; MambaScript spells it
|
|
645
|
+
differently" (fix: `share {x}`); `function hi() {}`, `var x = 1`,
|
|
646
|
+
`let x = 1`, `const x = 1` are each MS1124 "… is JavaScript; MambaScript
|
|
647
|
+
spells it differently". `await` and `async` are not keywords, but written the
|
|
648
|
+
JavaScript way they are MS1124 with a hint: `x = await p` reports "await is
|
|
649
|
+
JavaScript; MambaScript spells it waitForIt" and `f = async -> 1` reports
|
|
650
|
+
"async is JavaScript; MambaScript needs no async keyword". Used alone as
|
|
651
|
+
names (`x = await`, `async = 1`) they stay ordinary identifiers. Write
|
|
652
|
+
`waitForIt`.
|
|
653
|
+
|
|
654
|
+
```mamba
|
|
655
|
+
grab fs from 'fs'
|
|
656
|
+
share {x}
|
|
657
|
+
read = (path) -> waitForIt fs.promises.readFile path
|
|
658
|
+
```
|
|
659
|
+
|
|
660
|
+
**Why**: the hint-bearing words are JavaScript spellings of things MambaScript
|
|
661
|
+
spells differently, and the error says which. `await` escaping as a call is
|
|
662
|
+
an honest gap: the parser treats it as an identifier so that 0.5 programs
|
|
663
|
+
which used the name keep compiling; a diagnostic for a bare `await` /
|
|
664
|
+
`async` before a function is a reasonable future addition.
|
|
665
|
+
|
|
666
|
+
### 2.5 Trailing-comma continuation of an implicit call
|
|
667
|
+
|
|
668
|
+
**CoffeeScript 2**: a trailing comma lets an implicit call continue on the
|
|
669
|
+
next, indented line.
|
|
670
|
+
|
|
671
|
+
```coffee
|
|
672
|
+
f 1,
|
|
673
|
+
2
|
|
674
|
+
```
|
|
675
|
+
|
|
676
|
+
**MambaScript 2.0**: MS1101 "this line is indented but nothing opened a
|
|
677
|
+
block". Use parentheses or one line.
|
|
678
|
+
|
|
679
|
+
```mamba
|
|
680
|
+
f(
|
|
681
|
+
1
|
|
682
|
+
2
|
|
683
|
+
)
|
|
684
|
+
f 1, 2
|
|
685
|
+
```
|
|
686
|
+
|
|
687
|
+
**Why**: indentation opens blocks, and only blocks; a comma that silently
|
|
688
|
+
opens one made indentation errors unreportable. The cost: a pair of
|
|
689
|
+
parentheses on long argument lists.
|
|
690
|
+
|
|
691
|
+
### 2.6 Backslash line continuation
|
|
692
|
+
|
|
693
|
+
**CoffeeScript 2**: a backslash at the end of a line continues the statement
|
|
694
|
+
on the next line, which may be indented (I believe both forms are accepted;
|
|
695
|
+
the trailing-operator form is the idiomatic one).
|
|
696
|
+
|
|
697
|
+
```coffee
|
|
698
|
+
total = a + \
|
|
699
|
+
b
|
|
700
|
+
```
|
|
701
|
+
|
|
702
|
+
**MambaScript 2.0**: a backslash is allowed only at the very end of a line
|
|
703
|
+
(`x = 1 \ 2` is MS1024 "a backslash is only allowed at the end of a line"),
|
|
704
|
+
and the continuation must sit at the same indentation: the indented form is
|
|
705
|
+
MS1107 "the right side of + is missing". Prefer a trailing operator with the
|
|
706
|
+
next line at the same indentation.
|
|
707
|
+
|
|
708
|
+
```mamba
|
|
709
|
+
total = a + \
|
|
710
|
+
b
|
|
711
|
+
total = a +
|
|
712
|
+
b
|
|
713
|
+
```
|
|
714
|
+
|
|
715
|
+
**Why**: the continuation line is part of the same statement, and the same
|
|
716
|
+
indentation says so; an indented continuation looks like a block. The
|
|
717
|
+
headline for the indented backslash case ("the right side of + is missing")
|
|
718
|
+
is accurate but could name the backslash; that is a known rough edge.
|
|
719
|
+
|
|
720
|
+
### 2.7 An indented operand after a trailing operator
|
|
721
|
+
|
|
722
|
+
**CoffeeScript 2**: `a = b +` followed by an indented `c` is `a = b + c`.
|
|
723
|
+
|
|
724
|
+
```coffee
|
|
725
|
+
a = b +
|
|
726
|
+
c
|
|
727
|
+
```
|
|
728
|
+
|
|
729
|
+
**MambaScript 2.0**: MS1107 "the right side of + is indented". The operand
|
|
730
|
+
goes on the next line at the same indentation (2.6), or on the same line.
|
|
731
|
+
|
|
732
|
+
```mamba
|
|
733
|
+
a = b +
|
|
734
|
+
c
|
|
735
|
+
```
|
|
736
|
+
|
|
737
|
+
**Why**: as 2.6; indentation means one thing.
|
|
738
|
+
|
|
739
|
+
### 2.8 Rest and spread with the dots in front
|
|
740
|
+
|
|
741
|
+
**CoffeeScript 2**: both `args...` and `...args` are accepted, in parameters,
|
|
742
|
+
calls, arrays and objects.
|
|
743
|
+
|
|
744
|
+
```coffee
|
|
745
|
+
f = (...args) -> args
|
|
746
|
+
o = {...defaults, debug: yes}
|
|
747
|
+
```
|
|
748
|
+
|
|
749
|
+
**MambaScript 2.0**: the dots go after the expression. `(...args) ->` is
|
|
750
|
+
MS1105 "expected a variable, property or destructuring pattern, found
|
|
751
|
+
'...'"; `{...a, b: 1}` is MS1128 "write the spread as name... (the dots go
|
|
752
|
+
after the expression)".
|
|
753
|
+
|
|
754
|
+
```mamba
|
|
755
|
+
f = (args...) -> args
|
|
756
|
+
o = {defaults..., debug: yes}
|
|
757
|
+
merged = {a..., b..., version: 2}
|
|
758
|
+
{id, rest...} = user
|
|
759
|
+
```
|
|
760
|
+
|
|
761
|
+
```js
|
|
762
|
+
o = Object.assign({}, defaults, { debug: true });
|
|
763
|
+
rest = objectWithoutKeys$(cache$, ['id']);
|
|
764
|
+
```
|
|
765
|
+
|
|
766
|
+
**Why**: one spelling, the CoffeeScript one, everywhere. The cost: a habit
|
|
767
|
+
from JavaScript to unlearn, which is why the error names the fix.
|
|
768
|
+
|
|
769
|
+
### 2.9 `case`, and `switch` without branches
|
|
770
|
+
|
|
771
|
+
See 1.5: `case` is MS1120; `x = switch` with nothing after it is MS1120
|
|
772
|
+
"expected when branches". `switch` used as a value with `when` branches is
|
|
773
|
+
fine in both languages.
|
|
774
|
+
|
|
775
|
+
### 2.10 JSX
|
|
776
|
+
|
|
777
|
+
**CoffeeScript 2**: JSX is supported natively (`<div className="a">hi</div>`
|
|
778
|
+
in a `.coffee` file).
|
|
779
|
+
|
|
780
|
+
**MambaScript 2.0**: not supported; the lexer reads the `/` in `</div>` as
|
|
781
|
+
the start of a regular expression and reports MS1016 "unterminated regular
|
|
782
|
+
expression: the closing / must be on the same line". Elements are built with
|
|
783
|
+
Decision (`require 'mambascript/decision'`), which gives every tag as a
|
|
784
|
+
function.
|
|
785
|
+
|
|
786
|
+
```mamba
|
|
787
|
+
Decision = require 'mambascript/decision'
|
|
788
|
+
$ = Decision(React).$
|
|
789
|
+
Counter = (props) ->
|
|
790
|
+
$ 'main', {className: 'counter'}, ->
|
|
791
|
+
$ 'h1', "#{props.count}"
|
|
792
|
+
```
|
|
793
|
+
|
|
794
|
+
**Why**: MambaScript has had a tag-function element builder since before JSX
|
|
795
|
+
support reached CoffeeScript, and it keeps the file one language. The cost:
|
|
796
|
+
no JSX, so designers' snippets must be translated.
|
|
797
|
+
|
|
798
|
+
### 2.11 `super.method()`, `super()` with no arguments, bare `super`
|
|
799
|
+
|
|
800
|
+
**CoffeeScript 2**: `super()` passes no arguments; `super arguments...`
|
|
801
|
+
forwards them; bare `super` (no parentheses) was removed in 2.0; `super.method()`
|
|
802
|
+
calls a different parent method (supported since 2.0). A derived-class
|
|
803
|
+
constructor must call `super` before touching `this` (a JavaScript rule).
|
|
804
|
+
|
|
805
|
+
```coffee
|
|
806
|
+
class B extends A
|
|
807
|
+
constructor: (a, @b) ->
|
|
808
|
+
super a
|
|
809
|
+
m: -> super.helper() + super() + 1
|
|
810
|
+
```
|
|
811
|
+
|
|
812
|
+
**MambaScript 2.0**: `super.m()` is MS1117 "super followed by a property
|
|
813
|
+
access is not supported" (the fix it prints: `Parent::method.call this,
|
|
814
|
+
args`). Bare `super` and `super()` **both forward every argument**
|
|
815
|
+
(`.apply(this, arguments)`); only `super(x)` passes exactly `x`. A derived
|
|
816
|
+
constructor does not have to call `super` (ES5 constructors have no such
|
|
817
|
+
rule).
|
|
818
|
+
|
|
819
|
+
```mamba
|
|
820
|
+
blueprint B inheritsFrom A
|
|
821
|
+
constructor: (a, @b) ->
|
|
822
|
+
super a
|
|
823
|
+
m: -> A::helper.call(this) + super() + 1
|
|
824
|
+
n: -> super
|
|
825
|
+
```
|
|
826
|
+
|
|
827
|
+
```js
|
|
828
|
+
B.prototype.m = function () {
|
|
829
|
+
return A.prototype.helper.call(this) + B.__super__.m.apply(this, arguments) + 1;
|
|
830
|
+
};
|
|
831
|
+
B.prototype.n = function () {
|
|
832
|
+
return B.__super__.n.apply(this, arguments);
|
|
833
|
+
};
|
|
834
|
+
```
|
|
835
|
+
|
|
836
|
+
**Why**: the ES5 class output gives MambaScript the CoffeeScript 1 rules for
|
|
837
|
+
`super`, and that is where `super()` forwarding arguments comes from. It is a
|
|
838
|
+
real divergence from both CoffeeScript 2 and JavaScript, and the one to
|
|
839
|
+
remember when porting: `super()` means "the parent's version with everything
|
|
840
|
+
I was given"; to pass a subset, name the arguments (`super a, b`). A splat in
|
|
841
|
+
a `super` call works as in CoffeeScript (`super args...` and `super 'x', args...`
|
|
842
|
+
compile to `.apply(this, …)` in constructors, methods and statics). Native
|
|
843
|
+
`class` output is on the roadmap.
|
|
844
|
+
|
|
845
|
+
### 2.12 `new.target`, getters and setters, `#private`, class fields, decorators
|
|
846
|
+
|
|
847
|
+
**CoffeeScript 2**: no getter/setter syntax (use `Object.defineProperty`);
|
|
848
|
+
`#` is a comment so there are no `#private` fields; `x = 1` in a class body
|
|
849
|
+
is a local variable of the body, `x: 1` is a prototype property, `@x = 1` is
|
|
850
|
+
a static; no decorators. `new.target`: I am not sure whether CoffeeScript
|
|
851
|
+
accepts it.
|
|
852
|
+
|
|
853
|
+
```coffee
|
|
854
|
+
class A
|
|
855
|
+
count: 0 # prototype property
|
|
856
|
+
@total = 0 # static
|
|
857
|
+
secret = 1 # local to the class body
|
|
858
|
+
get name: -> 1 # NOT a getter: a call get({name: ...})
|
|
859
|
+
```
|
|
860
|
+
|
|
861
|
+
**MambaScript 2.0**: the same, and `new.target` is MS1118 "expected a class
|
|
862
|
+
or constructor after make, found '.'". Two silent traps are shared with
|
|
863
|
+
CoffeeScript and worth knowing: `get name: -> …` compiles to a call
|
|
864
|
+
`get({name: …})`, and `#secret = 1` is a comment. Decorators written as
|
|
865
|
+
`@dec` on a line compile to `this.dec;`.
|
|
866
|
+
|
|
867
|
+
```mamba
|
|
868
|
+
blueprint A
|
|
869
|
+
count: 0
|
|
870
|
+
@total: 0
|
|
871
|
+
secret = 1
|
|
872
|
+
constructor: -> @_name = 'a'
|
|
873
|
+
Object.defineProperty A.prototype, 'name', get: -> @_name
|
|
874
|
+
```
|
|
875
|
+
|
|
876
|
+
**Why**: none of these exist in either language; the difference is only that
|
|
877
|
+
MambaScript's `make` parser catches `new.target`. Use a `_private` property
|
|
878
|
+
and a method, or `Object.defineProperty`, in both.
|
|
879
|
+
|
|
880
|
+
### 2.13 `**` and BigInt
|
|
881
|
+
|
|
882
|
+
**CoffeeScript 2**: `a ** b` compiles to the native `**` operator, which
|
|
883
|
+
works on BigInt operands; numeric separators and BigInt literals are
|
|
884
|
+
accepted (2.5 and later).
|
|
885
|
+
|
|
886
|
+
```coffee
|
|
887
|
+
big = 2n ** 64n
|
|
888
|
+
```
|
|
889
|
+
|
|
890
|
+
**MambaScript 2.0**: `a ** b` compiles to `Math.pow(a, b)`, so `-2 ** 2` is
|
|
891
|
+
`Math.pow(-2, 2)` (4) and a BigInt operand throws at run time. The type
|
|
892
|
+
checker reports BigInt mixed with Int (MS2007). `1_000_000` and `10n`
|
|
893
|
+
literals are fine.
|
|
894
|
+
|
|
895
|
+
```mamba
|
|
896
|
+
x = 2 ** 10
|
|
897
|
+
n = 1_000_000
|
|
898
|
+
big = 10n
|
|
899
|
+
```
|
|
900
|
+
|
|
901
|
+
```js
|
|
902
|
+
x = Math.pow(2, 10);
|
|
903
|
+
n = 1e6;
|
|
904
|
+
big = 10n;
|
|
905
|
+
```
|
|
906
|
+
|
|
907
|
+
**Why**: the printer is ES5-era; native `**` arrives with the ES2020 output
|
|
908
|
+
target in the roadmap. Until then, multiply BigInts or use a backtick
|
|
909
|
+
literal.
|
|
910
|
+
|
|
911
|
+
---
|
|
912
|
+
|
|
913
|
+
## 3. Looks the same, behaves differently
|
|
914
|
+
|
|
915
|
+
### 3.1 A string that spans lines keeps the newline
|
|
916
|
+
|
|
917
|
+
**CoffeeScript 2**: a single- or double-quoted string broken across lines is
|
|
918
|
+
joined with a single space (`'one\ntwo'` in the source becomes `"one two"`).
|
|
919
|
+
|
|
920
|
+
```coffee
|
|
921
|
+
s = 'one
|
|
922
|
+
two' # "one two"
|
|
923
|
+
```
|
|
924
|
+
|
|
925
|
+
**MambaScript 2.0**: the line break is kept as written, including the
|
|
926
|
+
indentation of the continuation line. Heredocs `"""` strip the common
|
|
927
|
+
indentation, as in CoffeeScript.
|
|
928
|
+
|
|
929
|
+
```mamba
|
|
930
|
+
s = 'one
|
|
931
|
+
two'
|
|
932
|
+
t = "a
|
|
933
|
+
b"
|
|
934
|
+
present JSON.stringify s
|
|
935
|
+
```
|
|
936
|
+
|
|
937
|
+
```js
|
|
938
|
+
s = 'one\ntwo';
|
|
939
|
+
t = 'a\n b';
|
|
940
|
+
```
|
|
941
|
+
|
|
942
|
+
Running it prints `"one\ntwo"`.
|
|
943
|
+
|
|
944
|
+
**Why**: what you see is what you get; the space-joining rule was a surprise
|
|
945
|
+
in CoffeeScript classrooms. The cost: a long message must use a heredoc or
|
|
946
|
+
`+`.
|
|
947
|
+
|
|
948
|
+
### 3.2 `panic 'x'` throws an `Error`
|
|
949
|
+
|
|
950
|
+
See 1.7. `throw 'x'` (the synonym) still throws the string, as CoffeeScript
|
|
951
|
+
does; `panic` is the one that wraps.
|
|
952
|
+
|
|
953
|
+
### 3.3 `bail` inside `consider` leaves the consider
|
|
954
|
+
|
|
955
|
+
**CoffeeScript 2**: `break` inside a `switch` that sits in a loop leaves the
|
|
956
|
+
`switch`, not the loop (JavaScript's rule, and CoffeeScript has no labels).
|
|
957
|
+
|
|
958
|
+
```coffee
|
|
959
|
+
for n in [1, 2, 3]
|
|
960
|
+
switch n
|
|
961
|
+
when 2 then break # leaves the switch
|
|
962
|
+
console.log n # 1 2 3
|
|
963
|
+
```
|
|
964
|
+
|
|
965
|
+
**MambaScript 2.0**: the same JavaScript rule, so `bail` in a `when` leaves
|
|
966
|
+
the `consider`; label the loop to leave it.
|
|
967
|
+
|
|
968
|
+
```mamba
|
|
969
|
+
forEvery n in [1, 2, 3]
|
|
970
|
+
consider n
|
|
971
|
+
when 2 then bail
|
|
972
|
+
present n
|
|
973
|
+
scan: forEvery n in [1, 2, 3]
|
|
974
|
+
consider n
|
|
975
|
+
when 2 then bail scan
|
|
976
|
+
present n
|
|
977
|
+
present 'done'
|
|
978
|
+
```
|
|
979
|
+
|
|
980
|
+
Running it prints `1 2 3` then `1`.
|
|
981
|
+
|
|
982
|
+
**Why**: the output is a real `switch`, and MambaScript does not rewrite
|
|
983
|
+
`break` behind your back. Labels are the honest fix, and the compiler can
|
|
984
|
+
tell you when a label is not in scope (MS1131).
|
|
985
|
+
|
|
986
|
+
### 3.4 A line starting with `.` continues the innermost expression
|
|
987
|
+
|
|
988
|
+
**CoffeeScript 2**: a leading-dot line closes any trailing callback and
|
|
989
|
+
chains on the call: `list\n .map (x) -> x * 2\n .filter (x) -> x > 2` is
|
|
990
|
+
`list.map(…).filter(…)`.
|
|
991
|
+
|
|
992
|
+
```coffee
|
|
993
|
+
out = list
|
|
994
|
+
.map (x) -> x * 2
|
|
995
|
+
.filter (x) -> x > 2
|
|
996
|
+
```
|
|
997
|
+
|
|
998
|
+
**MambaScript 2.0**: the `.filter` attaches to the innermost expression,
|
|
999
|
+
which is the callback's body `x * 2`, and the program compiles silently to
|
|
1000
|
+
the wrong thing:
|
|
1001
|
+
|
|
1002
|
+
```js
|
|
1003
|
+
out = list.map(function (x) {
|
|
1004
|
+
return x * 2..filter(function (x) {
|
|
1005
|
+
return x > 2;
|
|
1006
|
+
});
|
|
1007
|
+
});
|
|
1008
|
+
```
|
|
1009
|
+
|
|
1010
|
+
(the fallback parser rejects it instead: `unexpected "("`). Wrap each
|
|
1011
|
+
callback in parentheses to chain past it:
|
|
1012
|
+
|
|
1013
|
+
```mamba
|
|
1014
|
+
out = list
|
|
1015
|
+
.map((x) -> x * 2)
|
|
1016
|
+
.filter((x) -> x > 2)
|
|
1017
|
+
```
|
|
1018
|
+
|
|
1019
|
+
```js
|
|
1020
|
+
out = list.map(function (x) {
|
|
1021
|
+
return x * 2;
|
|
1022
|
+
}).filter(function (x) {
|
|
1023
|
+
return x > 2;
|
|
1024
|
+
});
|
|
1025
|
+
```
|
|
1026
|
+
|
|
1027
|
+
**Why**: this one is not a design choice; it is the first item on the
|
|
1028
|
+
roadmap in todos.md ("make both parsers chain like CoffeeScript"). Until
|
|
1029
|
+
then the recipe is the parentheses.
|
|
1030
|
+
|
|
1031
|
+
### 3.5 `arguments` inside `=>`
|
|
1032
|
+
|
|
1033
|
+
**CoffeeScript 2**: `=>` compiles to a native arrow function, so `arguments`
|
|
1034
|
+
inside it belongs to the enclosing function.
|
|
1035
|
+
|
|
1036
|
+
```coffee
|
|
1037
|
+
outer = ->
|
|
1038
|
+
inner = => arguments[0] # the outer function's first argument
|
|
1039
|
+
inner 'ignored'
|
|
1040
|
+
```
|
|
1041
|
+
|
|
1042
|
+
**MambaScript 2.0**: `=>` compiles to a plain `function` that captures `this`
|
|
1043
|
+
(as `this$`) when `@` is used, so `arguments` is the bound function's own.
|
|
1044
|
+
Use a rest parameter when you mean the arguments.
|
|
1045
|
+
|
|
1046
|
+
```mamba
|
|
1047
|
+
count = =>
|
|
1048
|
+
arguments.length
|
|
1049
|
+
present count 1, 2, 3
|
|
1050
|
+
outer = (first) ->
|
|
1051
|
+
inner = => first
|
|
1052
|
+
inner 'ignored'
|
|
1053
|
+
```
|
|
1054
|
+
|
|
1055
|
+
Running it prints `3`.
|
|
1056
|
+
|
|
1057
|
+
**Why**: the ES5 output again. The behaviour is consistent (every `->` and
|
|
1058
|
+
`=>` has its own `arguments`) and the rest parameter form reads better
|
|
1059
|
+
anyway.
|
|
1060
|
+
|
|
1061
|
+
### 3.6 Default values apply to `null` too
|
|
1062
|
+
|
|
1063
|
+
**CoffeeScript 2**: a parameter default `(a = 1) ->` applies when the
|
|
1064
|
+
argument is `null` or `undefined` (a `== null` test), unlike JavaScript,
|
|
1065
|
+
where only `undefined` triggers it.
|
|
1066
|
+
|
|
1067
|
+
**MambaScript 2.0**: the same CoffeeScript rule, for parameters and for
|
|
1068
|
+
destructuring defaults (`{age = 18} = person`), so a `null` in the data is
|
|
1069
|
+
replaced too. Same as CoffeeScript, different from JavaScript.
|
|
1070
|
+
|
|
1071
|
+
```mamba
|
|
1072
|
+
f = (a = 1) -> a
|
|
1073
|
+
present f(null), f(undefined), f(0)
|
|
1074
|
+
{name, age = 18} = person
|
|
1075
|
+
```
|
|
1076
|
+
|
|
1077
|
+
Running the first two lines prints `1 1 0`.
|
|
1078
|
+
|
|
1079
|
+
### 3.7 `by 1` pins the direction; `[1...n]` with `n = 0` visits 1
|
|
1080
|
+
|
|
1081
|
+
**CoffeeScript 2**: a range with a variable end decides its direction at run
|
|
1082
|
+
time, so `for i in [1...n]` with `n = 0` counts *down* from 1 and visits 1
|
|
1083
|
+
once; writing `by 1` compiles a plain ascending loop that runs zero times.
|
|
1084
|
+
|
|
1085
|
+
**MambaScript 2.0**: the same: without `by` the loop follows its bounds at
|
|
1086
|
+
run time; a written `by 1` pins the count upwards (this was fixed on
|
|
1087
|
+
2026-12-05, see tdd/AUTHORING.md). Same trap, same cure.
|
|
1088
|
+
|
|
1089
|
+
```mamba
|
|
1090
|
+
n = 0
|
|
1091
|
+
present (i for i in [1...n])
|
|
1092
|
+
present (i for i in [1...n] by 1)
|
|
1093
|
+
```
|
|
1094
|
+
|
|
1095
|
+
Running it prints `[ 1 ]` then `[]`.
|
|
1096
|
+
|
|
1097
|
+
```js
|
|
1098
|
+
for (var i$ = 1; 1 <= n ? i$ < n : i$ > n; 1 <= n ? ++i$ : --i$) { ... }
|
|
1099
|
+
for (var i$1 = 1; i$1 < n; ++i$1) { ... }
|
|
1100
|
+
```
|
|
1101
|
+
|
|
1102
|
+
### 3.8 Spreads go through `Array.from`
|
|
1103
|
+
|
|
1104
|
+
**CoffeeScript 2**: `f xs...`, `[a..., b]` compile to the native `...`
|
|
1105
|
+
operator, which walks any iterable.
|
|
1106
|
+
|
|
1107
|
+
**MambaScript 2.0**: `f xs...` compiles to `f.apply(null, Array.from(xs))`
|
|
1108
|
+
and `[s...]` to `Array.from(s)`, so any iterable or array-like spreads
|
|
1109
|
+
(strings by code point, Sets, `Map.values()`, generators, `arguments`); a
|
|
1110
|
+
sparse array's holes become `undefined`.
|
|
1111
|
+
|
|
1112
|
+
```mamba
|
|
1113
|
+
parts = ['héllo'...]
|
|
1114
|
+
biggest = Math.max set...
|
|
1115
|
+
keys = [m.keys()...]
|
|
1116
|
+
```
|
|
1117
|
+
|
|
1118
|
+
Running the first line prints `[ 'h', 'é', 'l', 'l', 'o' ]`.
|
|
1119
|
+
|
|
1120
|
+
### 3.9 Heregexes strip whitespace and comments; `{nothing...}` adds nothing
|
|
1121
|
+
|
|
1122
|
+
Both the same as CoffeeScript (`/// ^ a b # letters \n \s+ c $ ///i` is
|
|
1123
|
+
`/^ab\s+c$/i`; spreading `nothing` or `missing` into an object adds no
|
|
1124
|
+
keys, as `{...null}` does in JavaScript).
|
|
1125
|
+
|
|
1126
|
+
```mamba
|
|
1127
|
+
r = ///
|
|
1128
|
+
^ a b # letters
|
|
1129
|
+
\s+ c $
|
|
1130
|
+
///i
|
|
1131
|
+
o = {nothing..., a: 1}
|
|
1132
|
+
```
|
|
1133
|
+
|
|
1134
|
+
```js
|
|
1135
|
+
r = /^ab\s+c$/i;
|
|
1136
|
+
o = Object.assign({}, null, { a: 1 });
|
|
1137
|
+
```
|
|
1138
|
+
|
|
1139
|
+
### 3.10 A postfix loop after `if … then a else b` loops over `b` only
|
|
1140
|
+
|
|
1141
|
+
**CoffeeScript 2**: I believe the same parse (the postfix `for` binds to the
|
|
1142
|
+
`else` expression), but I have not checked it.
|
|
1143
|
+
|
|
1144
|
+
**MambaScript 2.0**: `(if c then a else b for x in xs)` is
|
|
1145
|
+
`c ? a : (b for x in xs)`. Parenthesise the `if` to loop over the whole
|
|
1146
|
+
thing.
|
|
1147
|
+
|
|
1148
|
+
```mamba
|
|
1149
|
+
xs = [1, 2]
|
|
1150
|
+
a = (if yes then 'y' else 'n' for x in xs)
|
|
1151
|
+
b = ((if yes then 'y' else 'n') for x in xs)
|
|
1152
|
+
present a, b
|
|
1153
|
+
```
|
|
1154
|
+
|
|
1155
|
+
Running it prints `y [ 'y', 'y' ]`.
|
|
1156
|
+
|
|
1157
|
+
### 3.11 Postfix stacking order
|
|
1158
|
+
|
|
1159
|
+
Both read a stack of postfix clauses left to right with the last one
|
|
1160
|
+
outermost: `present n for n in xs if ok` is `if (ok) for (…) present(n)`,
|
|
1161
|
+
and `present n if ok for n in xs` is `for (…) if (ok) present(n)`. Same in
|
|
1162
|
+
CoffeeScript.
|
|
1163
|
+
|
|
1164
|
+
```mamba
|
|
1165
|
+
present n for n in [1, 2, 3] if ok
|
|
1166
|
+
present n if ok for n in [1, 2, 3]
|
|
1167
|
+
```
|
|
1168
|
+
|
|
1169
|
+
### 3.12 An implicit object argument followed by a postfix `if`
|
|
1170
|
+
|
|
1171
|
+
**CoffeeScript 2**: `f a: 1 if c` is `if (c) f({a: 1})`: the postfix
|
|
1172
|
+
condition guards the statement.
|
|
1173
|
+
|
|
1174
|
+
```coffee
|
|
1175
|
+
res.status(403).json msg: 'nope' unless ok
|
|
1176
|
+
```
|
|
1177
|
+
|
|
1178
|
+
**MambaScript 2.0**: the postfix binds to the last value of the implicit
|
|
1179
|
+
object, so `f a: 1 if c` compiles to `f({a: c ? 1 : void 0})` and the call
|
|
1180
|
+
always happens. This is an open grammar bug (todos.md, found in
|
|
1181
|
+
mamba-cookout); until it is fixed, write braces or a block.
|
|
1182
|
+
|
|
1183
|
+
```mamba
|
|
1184
|
+
f {a: 1} if c
|
|
1185
|
+
unless ok
|
|
1186
|
+
res.status(403).json msg: 'nope'
|
|
1187
|
+
```
|
|
1188
|
+
|
|
1189
|
+
```js
|
|
1190
|
+
if (c)
|
|
1191
|
+
f({ a: 1 });
|
|
1192
|
+
```
|
|
1193
|
+
|
|
1194
|
+
### 3.13 `x ?= v` declares; `x or= v` needs an assigned name
|
|
1195
|
+
|
|
1196
|
+
**CoffeeScript 2**: `name or= 'anon'` declares `name` with a `var` and
|
|
1197
|
+
compiles to `name || (name = 'anon')` even when nothing assigned it before.
|
|
1198
|
+
|
|
1199
|
+
**MambaScript 2.0**: `x ?= v` declares `x` (it compiles to an
|
|
1200
|
+
`if (null != x) … else x = v`); `x or= v` and `x and= v` on a name that was
|
|
1201
|
+
never assigned earlier in the file is a compile error without a code:
|
|
1202
|
+
"the variable "name" can't be assigned with or= because it has not been
|
|
1203
|
+
assigned yet; assign it first (name = …), or use ?= which declares it."
|
|
1204
|
+
|
|
1205
|
+
```mamba
|
|
1206
|
+
opts ?= {}
|
|
1207
|
+
name = nothing
|
|
1208
|
+
name or= 'anon'
|
|
1209
|
+
```
|
|
1210
|
+
|
|
1211
|
+
```js
|
|
1212
|
+
if (null != opts)
|
|
1213
|
+
opts;
|
|
1214
|
+
else
|
|
1215
|
+
opts = {};
|
|
1216
|
+
name = null;
|
|
1217
|
+
name || (name = 'anon');
|
|
1218
|
+
```
|
|
1219
|
+
|
|
1220
|
+
**Why**: `or=` reads "keep it, or set it", which only makes sense for a
|
|
1221
|
+
name that has something to keep. The cost: a CoffeeScript idiom that must
|
|
1222
|
+
become `?=` or an explicit first assignment.
|
|
1223
|
+
|
|
1224
|
+
### 3.14 Everything is an expression, including `attempt`
|
|
1225
|
+
|
|
1226
|
+
Both: `if`, `consider`/`switch`, loops and `try`/`attempt` have values. In
|
|
1227
|
+
both the value of a `try` comes from its body or its `catch`, never from the
|
|
1228
|
+
`finally` (`regardless` runs, its last expression is not the value; this was
|
|
1229
|
+
a 0.5 bug fixed in 2.0). One difference of form: `attempt` needs an indented
|
|
1230
|
+
block (`attempt then x` is MS1107 "expected an indented block after attempt,
|
|
1231
|
+
found 'then'"); I am not sure whether CoffeeScript accepts a one-line `try`.
|
|
1232
|
+
|
|
1233
|
+
```mamba
|
|
1234
|
+
config = attempt
|
|
1235
|
+
JSON.parse text
|
|
1236
|
+
onFail e
|
|
1237
|
+
{}
|
|
1238
|
+
```
|
|
1239
|
+
|
|
1240
|
+
### 3.15 Variables are function-scoped and hoisted in both; closures in a loop share the variable
|
|
1241
|
+
|
|
1242
|
+
Same as CoffeeScript (`var` output, no block scope, no temporal dead zone):
|
|
1243
|
+
every closure created in a loop sees the final value; `do (x) ->` captures
|
|
1244
|
+
per iteration. Note that a postfix `for` after `-> …` or `do (i) -> …`
|
|
1245
|
+
becomes part of the function body, in both languages, so write the capture
|
|
1246
|
+
as a block loop.
|
|
1247
|
+
|
|
1248
|
+
```mamba
|
|
1249
|
+
fns = []
|
|
1250
|
+
forEvery i in [1, 2, 3]
|
|
1251
|
+
fns.push do (i) -> -> i
|
|
1252
|
+
present (fn() for fn in fns)
|
|
1253
|
+
```
|
|
1254
|
+
|
|
1255
|
+
Running it prints `[ 1, 2, 3 ]`; without the `do (i)` it prints `[ 3, 3, 3 ]`.
|
|
1256
|
+
|
|
1257
|
+
### 3.16 `yield` binds looser than every operator except `=`
|
|
1258
|
+
|
|
1259
|
+
Same as CoffeeScript: `yield a + b` yields the sum; `(yield x) + 1` uses the
|
|
1260
|
+
received value as an operand.
|
|
1261
|
+
|
|
1262
|
+
```mamba
|
|
1263
|
+
g = ->
|
|
1264
|
+
a = yield 1 + 2
|
|
1265
|
+
b = (yield 3) + 1
|
|
1266
|
+
yield from [4]
|
|
1267
|
+
```
|
|
1268
|
+
|
|
1269
|
+
```js
|
|
1270
|
+
g = function* () {
|
|
1271
|
+
var a, b;
|
|
1272
|
+
a = yield 1 + 2;
|
|
1273
|
+
b = (yield 3) + 1;
|
|
1274
|
+
return yield* [4];
|
|
1275
|
+
};
|
|
1276
|
+
```
|
|
1277
|
+
|
|
1278
|
+
### 3.17 A loop in tail position of a generator still collects
|
|
1279
|
+
|
|
1280
|
+
**CoffeeScript 2**: a function that ends in a loop returns the collected
|
|
1281
|
+
array; I have not checked what CoffeeScript does when that loop contains
|
|
1282
|
+
`yield`.
|
|
1283
|
+
|
|
1284
|
+
**MambaScript 2.0**: the tail loop is still collected, through `yield*` of an
|
|
1285
|
+
inner generator, so the iterator's final `value` is an array of
|
|
1286
|
+
`undefined`s; end the generator with an explicit `return` when the loop's
|
|
1287
|
+
values are not wanted.
|
|
1288
|
+
|
|
1289
|
+
```mamba
|
|
1290
|
+
g = ->
|
|
1291
|
+
yield 0
|
|
1292
|
+
forEvery n in [1, 2]
|
|
1293
|
+
yield n
|
|
1294
|
+
h = ->
|
|
1295
|
+
yield 0
|
|
1296
|
+
forEvery n in [1, 2]
|
|
1297
|
+
yield n
|
|
1298
|
+
return
|
|
1299
|
+
present Array.from(g()), Array.from(h())
|
|
1300
|
+
```
|
|
1301
|
+
|
|
1302
|
+
Both print `[ 0, 1, 2 ]`; only `g`'s iterator ends with `{ value: [ undefined, undefined ], done: true }`.
|
|
1303
|
+
|
|
1304
|
+
### 3.18 `@` in a plain `->`
|
|
1305
|
+
|
|
1306
|
+
Same meaning as CoffeeScript (`this` of whoever calls the function); the type
|
|
1307
|
+
checker types it `Any` inside a plain callback and as the instance inside a
|
|
1308
|
+
method or a `=>` callback.
|
|
1309
|
+
|
|
1310
|
+
```mamba
|
|
1311
|
+
blueprint A
|
|
1312
|
+
constructor: -> @n = 1
|
|
1313
|
+
m: -> [1].map (x) => @n + x
|
|
1314
|
+
```
|
|
1315
|
+
|
|
1316
|
+
### 3.19 `a?.b` compiles to a test, and evaluates the object chain twice
|
|
1317
|
+
|
|
1318
|
+
**CoffeeScript 2**: `user?.profile?.name` compiles to a conditional with a
|
|
1319
|
+
cached reference (`(ref = user.profile) != null ? ref.name : void 0`); it
|
|
1320
|
+
does not emit JavaScript's native `?.` (as far as I know).
|
|
1321
|
+
|
|
1322
|
+
**MambaScript 2.0**: the same syntax and the same null test, but the chain is
|
|
1323
|
+
repeated rather than cached: `null != user.profile ? user.profile.name :
|
|
1324
|
+
void 0`. A getter on `profile` therefore runs twice. Native `?.` output is on
|
|
1325
|
+
the roadmap with the ES2020 target.
|
|
1326
|
+
|
|
1327
|
+
```mamba
|
|
1328
|
+
name = user?.profile?.name
|
|
1329
|
+
hit = fn?()
|
|
1330
|
+
```
|
|
1331
|
+
|
|
1332
|
+
```js
|
|
1333
|
+
name = 'undefined' !== typeof user && null != user && null != user.profile ? user.profile.name : void 0;
|
|
1334
|
+
hit = 'function' === typeof fn ? fn() : void 0;
|
|
1335
|
+
```
|
|
1336
|
+
|
|
1337
|
+
### 3.20 `a ? b` is the existential default, not a ternary
|
|
1338
|
+
|
|
1339
|
+
Same as CoffeeScript: `a ? b` is `a` unless it is `null`/`undefined`, then
|
|
1340
|
+
`b`; a ternary is `if c then a else b`. MambaScript adds `orElse` as the
|
|
1341
|
+
word form (1.10). A `?` directly after a name with no space (`a?`) is the
|
|
1342
|
+
existence test.
|
|
1343
|
+
|
|
1344
|
+
### 3.21 Bound methods
|
|
1345
|
+
|
|
1346
|
+
Both keep `m: =>` bound when detached; CoffeeScript 2 binds in the
|
|
1347
|
+
constructor (`this.m = this.m.bind(this)`), MambaScript wraps in the
|
|
1348
|
+
constructor with an `instance$` capture. Same behaviour.
|
|
1349
|
+
|
|
1350
|
+
```mamba
|
|
1351
|
+
blueprint Button
|
|
1352
|
+
constructor: (@label) ->
|
|
1353
|
+
click: => @label
|
|
1354
|
+
```
|
|
1355
|
+
|
|
1356
|
+
---
|
|
1357
|
+
|
|
1358
|
+
## 4. The type system
|
|
1359
|
+
|
|
1360
|
+
CoffeeScript has none. MambaScript's is gradual: annotate what you want
|
|
1361
|
+
checked, everything else is `Any` and never complained about. Mistakes
|
|
1362
|
+
*warn* by default (the compile continues, exit 0); `--strict`,
|
|
1363
|
+
`typeCheck: 'error'` or `MAMBA_TYPECHECK=error` make them fail; `--no-typecheck`
|
|
1364
|
+
skips the checker.
|
|
1365
|
+
|
|
1366
|
+
```mamba
|
|
1367
|
+
struct Point
|
|
1368
|
+
x :: Int
|
|
1369
|
+
y :: Int
|
|
1370
|
+
distance :: (Point, Point) -> Number = (a, b) -> Math.hypot a.x - b.x, a.y - b.y
|
|
1371
|
+
origin :: Point = {x: 0, y: 0}
|
|
1372
|
+
blueprint Pixel implements Point
|
|
1373
|
+
constructor: (@x :: Int, @y :: Int) ->
|
|
1374
|
+
box<T> :: T -> {value :: T}
|
|
1375
|
+
box = (v) -> {value: v}
|
|
1376
|
+
```
|
|
1377
|
+
|
|
1378
|
+
The annotations leave no trace in the output (`struct` produces nothing).
|
|
1379
|
+
What it says when a type is wrong, verbatim from `mamba -c -p --strict`:
|
|
1380
|
+
|
|
1381
|
+
```
|
|
1382
|
+
app.mamba:1:1 TypeError age is declared as Int, but this value is String
|
|
1383
|
+
|
|
1384
|
+
> 1 | age :: Int = 'thirty'
|
|
1385
|
+
| ^^^^^^^^^^^^^^^^^^^^^
|
|
1386
|
+
|
|
1387
|
+
age :: Int means every value assigned to age must be Int. The value on this line is String.
|
|
1388
|
+
|
|
1389
|
+
Fix: make the value Int, or change the annotation to age :: String if String is what you meant.
|
|
1390
|
+
|
|
1391
|
+
See https://mambascript.org/docs/reference/errors#ms2001
|
|
1392
|
+
```
|
|
1393
|
+
|
|
1394
|
+
Other headlines: MS2001 "expected Int, but this value is String" (a function
|
|
1395
|
+
returning the wrong type), MS2009 "Point has no member z" (a member a
|
|
1396
|
+
struct or known blueprint does not declare), MS2008 "Ghost implements Named
|
|
1397
|
+
but does not define name", MS2002 "x is declared again with a different
|
|
1398
|
+
type", MS2007 for BigInt mixed with Int.
|
|
1399
|
+
|
|
1400
|
+
Built-in types: `Any`, `String`, `Int`, `Float`, `Number`, `Boolean`, `Null`,
|
|
1401
|
+
`Undefined`, `Void`, `BigInt`; `T?` nullable, `T[]` arrays (nested),
|
|
1402
|
+
`(A, B) -> R` and `A * B -> R` functions, `{x :: Int}` structural literals,
|
|
1403
|
+
generics on structs, blueprints and functions. No unions, literal types or
|
|
1404
|
+
narrowing (docs/vs-typescript.md has the honest comparison).
|
|
1405
|
+
|
|
1406
|
+
**Why**: the checker exists to catch what a learner or a small team gets
|
|
1407
|
+
wrong (a string where a number was declared, a missing member, the wrong
|
|
1408
|
+
argument) and to say so in a sentence with a fix. The cost: `::` with spaces
|
|
1409
|
+
is a type and `::` without spaces is the prototype, which a CoffeeScript eye
|
|
1410
|
+
must learn.
|
|
1411
|
+
|
|
1412
|
+
---
|
|
1413
|
+
|
|
1414
|
+
## 5. Error messages
|
|
1415
|
+
|
|
1416
|
+
**CoffeeScript 2**: `file.coffee:LINE:COL: error: unexpected X` (or
|
|
1417
|
+
`reserved word 'var'`, `unmatched )`), the offending line and a caret.
|
|
1418
|
+
|
|
1419
|
+
**MambaScript 2.0**: every diagnostic, from both parsers and the type
|
|
1420
|
+
checker, has the same shape: `file:line:column Kind headline`, a code frame
|
|
1421
|
+
with the whole span underlined (and a second frame for the opener when a
|
|
1422
|
+
bracket is unclosed), a paragraph on why, a `Fix:` built from the program's
|
|
1423
|
+
own names, and a `See` link to a stable code (MS1001 to MS4003; the catalogue
|
|
1424
|
+
is in docs/reference.md). `mamba --json` prints the same as a JSON array for
|
|
1425
|
+
editors (`code`, `file`, `line`, `column`, `endColumn`, `headline`, `why`,
|
|
1426
|
+
`fix`, `see`); `NODE_DISABLE_COLORS=1` turns colour off. Verbatim:
|
|
1427
|
+
|
|
1428
|
+
```
|
|
1429
|
+
strict_eq.mamba:1:8 SyntaxError === is JavaScript; MambaScript spells it is
|
|
1430
|
+
|
|
1431
|
+
> 1 | ok = a === b
|
|
1432
|
+
| ^^^
|
|
1433
|
+
```
|
|
1434
|
+
|
|
1435
|
+
The fallback `--parser peg` gives the position with a generic
|
|
1436
|
+
`unexpected "="` headline and the code MS1900.
|
|
1437
|
+
|
|
1438
|
+
**Why**: an error should teach. Every `Fix:` in the catalogue is itself
|
|
1439
|
+
compiled by the test suite (tdd/lang/error-messages.spec.mamba), so the
|
|
1440
|
+
advice is never stale.
|
|
1441
|
+
|
|
1442
|
+
---
|
|
1443
|
+
|
|
1444
|
+
## 6. The compiled output
|
|
1445
|
+
|
|
1446
|
+
| | CoffeeScript 2.7 | MambaScript 2.0 |
|
|
1447
|
+
|---|---|---|
|
|
1448
|
+
| Header | `// Generated by CoffeeScript 2.7.0` (`--no-header` drops it) | `// Generated by MambaScript 2.0.0` |
|
|
1449
|
+
| Wrapper | `(function() { … }).call(this);` unless `-b` | `mamba -c` is bare by default; `mambascript --js` wraps in `void function () { … }.call(this);` unless `--bare` |
|
|
1450
|
+
| Variables | `var` hoisted to the top of each function | the same |
|
|
1451
|
+
| `'use strict'` | not emitted | not emitted |
|
|
1452
|
+
| Classes | native `class … extends`, `super` | ES5 constructor functions, `extends$` helper, `A.__super__` (1.3, 2.11) |
|
|
1453
|
+
| `=>` | native arrow function | `function` with a `this$` capture (3.5) |
|
|
1454
|
+
| Spread, rest | native `...` | `Array.from`, `[].slice.call(arguments, …)`, `objectWithoutKeys$` (3.8, 2.8) |
|
|
1455
|
+
| Object spread | native `{...a}` | `Object.assign({}, a, …)` |
|
|
1456
|
+
| String interpolation | template literal `` `a ${b}` `` | concatenation `'a ' + b` |
|
|
1457
|
+
| Tagged templates | native | native (`tag`…${x}…``) |
|
|
1458
|
+
| `**` | native | `Math.pow` (2.13) |
|
|
1459
|
+
| `a?.b`, `a ? b` | conditionals with cached refs | conditionals, no caching (3.19) |
|
|
1460
|
+
| `x in list` | `indexOf.call(list, x) >= 0` | `x === a \|\| x === b` for a literal list, `in$` helper otherwise |
|
|
1461
|
+
| `for x in list` | indexed `for` with `len` | indexed `for` with `cache$`, `i$`, `length$` |
|
|
1462
|
+
| Async, generators | native `async function`, `function*`, `for await` | the same |
|
|
1463
|
+
| Default parameters | native | `if (null == a) a = 1` (3.6) |
|
|
1464
|
+
| Numbers | as written | escodegen's shortest form: `3000` prints as `3e3`, `1_000_000` as `1e6` |
|
|
1465
|
+
| Helpers | `indexOf`, `hasProp`, `slice`, `splice`, `modulo` | `extends$`, `isOwn$`, `in$`, `objectWithoutKeys$`, `_module_`; `var present = console.log, complain = console.warn, _ = require('mambascript/stdlib')` when used |
|
|
1466
|
+
| Source maps | `-m` / `-M` | `mambascript --source-map-file`, and the require hook maps stack traces |
|
|
1467
|
+
|
|
1468
|
+
A full example, from the program in 1.3 plus a `make`:
|
|
1469
|
+
|
|
1470
|
+
```js
|
|
1471
|
+
// Generated by MambaScript 2.0.0
|
|
1472
|
+
var art, Hero;
|
|
1473
|
+
Hero = function (super$) {
|
|
1474
|
+
extends$(Hero, super$);
|
|
1475
|
+
function Hero(name, param$) {
|
|
1476
|
+
this.power = param$;
|
|
1477
|
+
Hero.__super__.constructor.call(this, name);
|
|
1478
|
+
}
|
|
1479
|
+
Hero.prototype.greet = function () {
|
|
1480
|
+
return Hero.__super__.greet.apply(this, arguments) + '!';
|
|
1481
|
+
};
|
|
1482
|
+
return Hero;
|
|
1483
|
+
}(Human);
|
|
1484
|
+
art = new Hero('Art', 'code');
|
|
1485
|
+
function isOwn$(o, p) {
|
|
1486
|
+
return {}.hasOwnProperty.call(o, p);
|
|
1487
|
+
}
|
|
1488
|
+
function extends$(child, parent) {
|
|
1489
|
+
for (var key in parent)
|
|
1490
|
+
if (isOwn$(parent, key))
|
|
1491
|
+
child[key] = parent[key];
|
|
1492
|
+
function ctor() {
|
|
1493
|
+
this.constructor = child;
|
|
1494
|
+
}
|
|
1495
|
+
ctor.prototype = parent.prototype;
|
|
1496
|
+
child.prototype = new ctor;
|
|
1497
|
+
child.__super__ = parent.prototype;
|
|
1498
|
+
return child;
|
|
1499
|
+
}
|
|
1500
|
+
```
|
|
1501
|
+
|
|
1502
|
+
(The helpers are emitted once per file, after the code that uses them, as
|
|
1503
|
+
function declarations, so hoisting makes them available.)
|
|
1504
|
+
|
|
1505
|
+
**Why**: the printer is the vendored escodegen 0.0.28 with in-house patches
|
|
1506
|
+
for `async`, generators, `for … of`, template literals and modules, and the
|
|
1507
|
+
output runs unchanged on Node 18 and in every browser. The cost is the list
|
|
1508
|
+
above (native classes, arrows, `**`, `?.`, `??`), all gathered under the
|
|
1509
|
+
"ES2020 output target" item of the roadmap.
|
|
1510
|
+
|
|
1511
|
+
---
|
|
1512
|
+
|
|
1513
|
+
## 7. Tooling
|
|
1514
|
+
|
|
1515
|
+
| Task | CoffeeScript 2 | MambaScript 2.0 |
|
|
1516
|
+
|---|---|---|
|
|
1517
|
+
| Run a file | `coffee app.coffee` | `mamba app.mamba` |
|
|
1518
|
+
| Compile | `coffee -c src -o lib` | `mamba -c src/*.mamba -o lib` |
|
|
1519
|
+
| Print JavaScript | `coffee -p file` | `mamba -p file` (`-p` implies `-c`) |
|
|
1520
|
+
| Bare output | `coffee -b` | `mamba -c` is bare; `mambascript --js --bare` |
|
|
1521
|
+
| Join files | `coffee -j out.js -c a b` | `mamba -j out.js -c a b` |
|
|
1522
|
+
| Watch | `coffee -w` | none yet (roadmap) |
|
|
1523
|
+
| Evaluate a string | `coffee -e 'code'` | `mambascript --cli 'code' --js` |
|
|
1524
|
+
| stdin to stdout | `coffee -s -p` | `mambascript --js < in.mamba` |
|
|
1525
|
+
| Source maps | `coffee -m` / `-M` | `mambascript --js --source-map-file out.js.map -i in.mamba -o out.js` |
|
|
1526
|
+
| Tokens, AST | `coffee -t` / `-n` | `mambascript --tokens` / `--parse` / `--compile` |
|
|
1527
|
+
| Literate | `.litcoffee`, `-l` | `-l, --literate` on `mambascript` |
|
|
1528
|
+
| Transpile with Babel | `coffee -t` (reads `.babelrc`) | none; the output is already ES5 plus native async/generators |
|
|
1529
|
+
| REPL | `coffee` / `coffee -i` | `mamba` (with `present`, `complain`, `stdlib` predefined; `_` is the last result) |
|
|
1530
|
+
| Modules | `import`/`export` only, passed through | `grab`/`share`; `--esm` or `--cjs` choose the output; `"type": "module"` in `package.json` is honoured |
|
|
1531
|
+
| ES-module loader | none (compile first) | `node --import mambascript/esm app.mamba` |
|
|
1532
|
+
| `require` hook | `require 'coffeescript/register'` | `require 'mambascript'` (registering is automatic; `register()` still works), or `node -r mambascript index.js` |
|
|
1533
|
+
| Jest | a community transform | `"transform": {"\\.mamba$": "mambascript/jest"}`, written by `mamba init`; `MAMBA_TYPECHECK` and `MAMBA_PARSER` env vars |
|
|
1534
|
+
| Scaffold | none (Cakefiles for tasks) | `mamba init my-app` |
|
|
1535
|
+
| Type checking | none | default warn; `--strict`, `--no-typecheck`, `--json` for editors |
|
|
1536
|
+
| Dependencies | none at run time | none at run time; one dev dependency (jest) |
|
|
1537
|
+
| Template engine, element builder | none | MambaView (HTML), Decision (React/Preact) shipped in the package |
|
|
1538
|
+
| Fallback parser | one parser | `--parser peg` / `MAMBA_PARSER=peg` for one more release |
|
|
1539
|
+
|
|
1540
|
+
**Why**: `mamba` is meant to be the one command a learner needs; `mambascript`
|
|
1541
|
+
keeps the fine-grained flags for build scripts. Folding the two together is on
|
|
1542
|
+
the roadmap.
|
|
1543
|
+
|
|
1544
|
+
---
|
|
1545
|
+
|
|
1546
|
+
## 8. MambaScript 0.5 to 2.0
|
|
1547
|
+
|
|
1548
|
+
0.5 was a typed CoffeeScript with `blueprint`, `inheritsFrom`, `forEvery`,
|
|
1549
|
+
`also`, `thanos`, `areYouA`, `present` and a type walker that never spoke.
|
|
1550
|
+
2.0 changed these things (CHANGELOG.md, "Breaking", and the README's
|
|
1551
|
+
"Migrating from 0.5.x"):
|
|
1552
|
+
|
|
1553
|
+
| | 0.5.x | 2.0 |
|
|
1554
|
+
|---|---|---|
|
|
1555
|
+
| New reserved words | | `waitForIt waitForAll panic attempt onFail regardless otherwise keepGoing bail consider exists orElse aint nothing missing has grab share make`, plus `yield` and `as` in their positions; `from` only after `grab` and in `for … from`. Rename any variable that used one (1.10). |
|
|
1556
|
+
| Built-ins | `present` injected | `present`, `complain`, `_` declared with a real `var` when used; a program that assigns them keeps its own value |
|
|
1557
|
+
| Type checking | warnings silently swallowed (`has_errors` always false) | warnings print to stderr; `--strict` / `typeCheck: 'error'` fail the build; `--no-typecheck` / `MAMBA_TYPECHECK=off` silence; `MambaScript.parse` throws `TypeError` objects, not strings |
|
|
1558
|
+
| CoffeeScript forms | `===`, `//`, splice assignment, `while … when`, holes, `.5`, trailing-comma continuation, indented operand after an operator accepted (or silently wrong) | all rejected with hints (section 2) |
|
|
1559
|
+
| `panic 'text'` | threw the string | throws `new Error('text')` |
|
|
1560
|
+
| `bail` in `consider` | left the consider (same) | the same, and loops can be labeled |
|
|
1561
|
+
| Leading-dot lines | continued the innermost expression | the same (3.4), documented and on the roadmap |
|
|
1562
|
+
| Multi-line strings | kept the line break | the same |
|
|
1563
|
+
| Ranges | always built an array; a variable range always counted up | counted directly; `[a..b]` follows its bounds at run time; `by 1` pins the direction; a wrong-sign step gives no iterations (3.7) |
|
|
1564
|
+
| Spreads | `[].slice.call` | `Array.from`: iterators spread, holes fill, strings walk code points |
|
|
1565
|
+
| `attempt` value | last expression of `regardless` was returned | the value comes from the body or `onFail` |
|
|
1566
|
+
| `arguments` in a collected loop | the collecting helper's | the enclosing function's |
|
|
1567
|
+
| Modules | CommonJS only | `grab`/`share` to CommonJS or ES modules, loader, `--esm` |
|
|
1568
|
+
| New syntax | | destructuring defaults, object rest/spread, computed keys, labeled loops, generators, `for … from`, tagged templates, `1_000`, `10n`, every regex flag, typed `@` parameters, `make(X).method()` |
|
|
1569
|
+
| Errors | PEG `unexpected "x"` | codes, frames, why, fix (section 5) |
|
|
1570
|
+
| Parser | PEG.js (about 100 KB/s) | hand-written (about 15 MB/s); `--parser peg` for one release |
|
|
1571
|
+
| Dependencies | coffeescript, lodash, escodegen, esmangle, nopt, … | none; escodegen, estraverse, source-map vendored and patched |
|
|
1572
|
+
| Removed | `--minify`, `--nodejs`, `--self`, `.coffee` through the coffeescript package | gone; `.coffee` files compile through MambaScript itself |
|
|
1573
|
+
| `register()` | required | automatic on `require 'mambascript'`; calling it is a no-op |
|
|
1574
|
+
|
|
1575
|
+
Unchanged for existing projects: `require('mambascript').register()`,
|
|
1576
|
+
`{MambaView} = require 'mambascript'`, `Decision = require 'mambascript/decision'`,
|
|
1577
|
+
`Decision(React).$`, and CommonJS output byte-identical to 0.5 for programs
|
|
1578
|
+
that use none of the new syntax.
|
|
1579
|
+
|
|
1580
|
+
---
|
|
1581
|
+
|
|
1582
|
+
## In one paragraph
|
|
1583
|
+
|
|
1584
|
+
If you write CoffeeScript 2 today: keep your indentation, comprehensions,
|
|
1585
|
+
`is`/`and`/`or`/`?`; rename variables that collide with the 2.0 words; replace
|
|
1586
|
+
`===`, `//`, `%%`, `.5`, holes, splice assignment, `while … when`, trailing
|
|
1587
|
+
commas and indented operands with the forms above; move the dots after the
|
|
1588
|
+
spread; write `grab`/`share` instead of `import`/`export` and `waitForIt`
|
|
1589
|
+
instead of `await`; remember that `super()` forwards every argument (and a splat after `super`
|
|
1590
|
+
does not compile), that a
|
|
1591
|
+
multi-line string keeps its newline, that `panic 'x'` makes an `Error`, that
|
|
1592
|
+
a leading `.` after a bare callback needs the callback wrapped, and that
|
|
1593
|
+
`or=` wants a name that was assigned. Then add `::` types where they help.
|