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.
Files changed (82) hide show
  1. package/CHANGELOG.md +326 -0
  2. package/README.md +509 -267
  3. package/bin/mamba +1 -258
  4. package/decision.js +1 -246
  5. package/docs/coverage.md +70 -0
  6. package/docs/features.md +3348 -0
  7. package/docs/perf.md +94 -0
  8. package/docs/reference.md +1295 -0
  9. package/docs/vs-coffeescript.md +1593 -0
  10. package/docs/vs-iced-coffeescript.md +626 -0
  11. package/docs/vs-typescript.md +107 -0
  12. package/esm.js +8 -0
  13. package/jest.js +33 -0
  14. package/lib/black.js +17 -13
  15. package/lib/browser.js +2 -4
  16. package/lib/cli.js +207 -136
  17. package/lib/compiler.js +1162 -93
  18. package/lib/decision.js +10 -10
  19. package/lib/diagnostics.js +219 -0
  20. package/lib/esm-hooks.js +60 -0
  21. package/lib/fast-parser.js +3469 -0
  22. package/lib/functional-helpers.js +26 -9
  23. package/lib/helpers.js +67 -50
  24. package/lib/js-nodes.js +151 -12
  25. package/lib/lexer.js +1499 -0
  26. package/lib/light-module.js +200 -63
  27. package/lib/mamba.js +372 -0
  28. package/lib/module.js +23 -172
  29. package/lib/nodes.js +84 -21
  30. package/lib/optimiser.js +29 -9
  31. package/lib/optparse.js +32 -15
  32. package/lib/parser.js +9433 -4840
  33. package/lib/preprocessor.js +3 -5
  34. package/lib/register.js +49 -56
  35. package/lib/repl.js +28 -9
  36. package/lib/reporter.js +106 -69
  37. package/lib/run.js +3 -5
  38. package/lib/string-scanner.js +94 -0
  39. package/lib/syntax-tables.js +480 -0
  40. package/lib/type-checker.js +258 -44
  41. package/lib/type-helpers.js +28 -10
  42. package/lib/type-resolver.js +256 -34
  43. package/lib/type-scope.js +10 -6
  44. package/lib/type-walker.js +664 -85
  45. package/lib/types.js +17 -7
  46. package/lib/underscore.js +170 -76
  47. package/lib/unicode-classes.js +122 -0
  48. package/mambaview.js +1 -0
  49. package/package.json +49 -46
  50. package/stdlib.js +1 -0
  51. package/vendor/README.md +40 -0
  52. package/vendor/escodegen/LICENSE.BSD +19 -0
  53. package/vendor/escodegen/escodegen.js +2289 -0
  54. package/vendor/estraverse/LICENSE.BSD +19 -0
  55. package/vendor/estraverse/estraverse.js +706 -0
  56. package/vendor/mambacodegen/LICENSE +24 -0
  57. package/vendor/mambacodegen/lib/mscodegen.js +620 -0
  58. package/vendor/patches/escodegen.diff +130 -0
  59. package/vendor/patches/estraverse.diff +51 -0
  60. package/vendor/patches/pegjs.diff +70 -0
  61. package/vendor/patches/source-map.diff +160 -0
  62. package/vendor/source-map/LICENSE +28 -0
  63. package/vendor/source-map/lib/source-map/array-set.js +92 -0
  64. package/vendor/source-map/lib/source-map/base64-vlq.js +140 -0
  65. package/vendor/source-map/lib/source-map/base64.js +38 -0
  66. package/vendor/source-map/lib/source-map/binary-search.js +77 -0
  67. package/vendor/source-map/lib/source-map/source-map-consumer.js +404 -0
  68. package/vendor/source-map/lib/source-map/source-map-generator.js +229 -0
  69. package/vendor/source-map/lib/source-map/source-node.js +201 -0
  70. package/vendor/source-map/lib/source-map/util.js +57 -0
  71. package/vendor/source-map/lib/source-map.js +8 -0
  72. package/bin/fix +0 -17
  73. package/bin/updateImports +0 -46
  74. package/build.js +0 -25
  75. package/lib/module-esm.js +0 -901
  76. package/lib/pegjs-kofu-plugin.js +0 -35
  77. package/lib/pegjs-mamba-plugin.js +0 -35
  78. package/lib/processor-esm.js +0 -507
  79. package/mamba +0 -259
  80. package/msast.js +0 -1
  81. package/utils/gulp-mamba.js +0 -29
  82. /package/{lib/esm.js → vendor/patches/mambacodegen.diff} +0 -0
package/docs/perf.md ADDED
@@ -0,0 +1,94 @@
1
+ # Compiler performance notes
2
+
3
+ Measured with `npm run profile` (stage split + CPU profile over `src/` and
4
+ `examples/`, 55 files, 410 KB) and `npm run bench`. Numbers are from one
5
+ desktop (node 24); ratios are what matter.
6
+
7
+ ## Where the time goes (2026-12-05)
8
+
9
+ | | before | after | what changed |
10
+ |---|---|---|---|
11
+ | full compile, whole corpus | 183 KB/s | 584 KB/s | |
12
+ | parse | 1% | 3% | nothing: the hand-written parser was already 15 MB/s |
13
+ | optimise | 49% | 28% | `envEnrichments` cached per node |
14
+ | type walk | 12% | 14% | `@` no longer deep-copies the member list; inherited members shallow-copied |
15
+ | compile (CS AST to JS AST) | 33% | 38% | scope extension returns the same array when nothing is new; `declarationsNeededRecursive` memoised for `generateSymbols` |
16
+ | codegen (escodegen + source maps) | 5% | 17% | nothing yet |
17
+
18
+ The three structural fixes were all the same shape: a per-node scan of the
19
+ whole subtree repeated at every level of a tree walk (quadratic in depth), or a
20
+ deep copy where a shared, never-mutated structure was fine.
21
+
22
+ - `src/helpers.mamba` `possibilitiesOf`: the names a subtree can declare depend
23
+ only on the subtree, so they are computed once (WeakMap keyed by node). Nodes
24
+ are never mutated structurally once built; the optimiser replaces nodes with
25
+ fresh objects.
26
+ - `src/type-walker.mamba` `walkThis`: `this.typeAnnotation.properties` is a
27
+ shallow copy of the scope's member list; the entries are shared because the
28
+ checker clones an annotation before mutating it everywhere else.
29
+ - `src/functional-helpers.mamba` `extendScope`: `union` that returns `listA`
30
+ itself when `listB` adds nothing. Only for lists that are never mutated in
31
+ place (`inScope` in both walks, `declaredSymbols`); `usedSymbols` is pushed to
32
+ and keeps the copying `union`.
33
+ - `src/compiler.mamba` `declarationsNeededRecursiveMemo`: cached per JS node,
34
+ used only by `generateSymbols`, which asks on finished subtrees (generated
35
+ symbols already renamed). The compile walk keeps the uncached function
36
+ because its GenSyms are still unnamed.
37
+
38
+ ## Rules for the next round
39
+
40
+ - Profile first: `npm run profile` (or `node scripts/profile.js --compiler
41
+ lib.scratch` for a scratch build). Change one thing per commit.
42
+ - Output must not change: the build's compat step runs `scripts/compat.js
43
+ --golden lib`, byte-comparing generated JavaScript and type warnings of the
44
+ committed compiler and the new one over every sister project; `npm run build`
45
+ also requires `src/` to compile identically (fixpoint) and runs the sister
46
+ suites. An intended output change is listed in `scripts/golden-allow.txt`.
47
+ - `scripts/perf-gate.js` keeps the floor (`MIN_FULL_KBPS`) under what CI
48
+ runners reach.
49
+
50
+ ## Remaining levers
51
+
52
+ Roughly in order of the current profile: garbage collection (9%, fewer
53
+ short-lived arrays in the walks), `SourceNode` building in `MS.js` when no
54
+ source map is wanted (6%), the optimiser walk's `in$` scans over
55
+ `listMembers` and `ancestry.unshift/shift` (6%), `Nodes.instanceof` (6%, now
56
+ mostly the switch-style rule matching in the optimiser), `possibilitiesOf` and
57
+ `concat` (6%).
58
+
59
+ ## Against TypeScript (2026-12-05)
60
+
61
+ `npm run bench:ts` (`scripts/bench-ts.js`) installs `typescript@5` into
62
+ `lib.bench-ts/` (never a dependency) and compiles the same programs with both:
63
+ every MambaScript file of the corpus, and the JavaScript it produces as
64
+ TypeScript's input (JavaScript is valid TypeScript, so both see a program of
65
+ the same size and shape). Best of five rounds after a warm-up.
66
+
67
+ Corpus: 126 programs, 728 KB of MambaScript; the JavaScript it compiles to (887 KB) is TypeScript's input. Best of 5 rounds after a warm-up; node v24.13.0, typescript 5.9.3, MambaScript 2.0.0, 2026-12-06.
68
+
69
+ | | MambaScript | TypeScript | how |
70
+ |---|---|---|---|
71
+ | cold start, one small file (CLI) | 69 ms | 50 ms | node itself: 17 ms |
72
+ | parse only | 18098 KB/s | 16253 KB/s | MambaScript.parse vs ts.createSourceFile |
73
+ | transpile, no type check | 539 KB/s | 2692 KB/s | full compile (typeCheck off) vs ts.transpileModule |
74
+ | compile with type checking | 467 KB/s (1560 ms) | 1110 KB/s (799 ms + 0 ms lib.d.ts) | typeCheck warn vs createProgram + getPreEmitDiagnostics + emit, skipLibCheck |
75
+ | typed program, checked compile (same annotated program in both languages) | 3.87 ms | 114.80 ms (+ lib.d.ts once) | scripts/bench/typed.mamba vs typed.ts, 20 compiles per round |
76
+ | resident memory after the checked run | 629 MB | 687 MB | same process, TypeScript measured after MambaScript |
77
+
78
+ Reading the table: the two parsers are equally fast. TypeScript's
79
+ `transpileModule` is five times faster than a MambaScript compile with the
80
+ checker off, because MambaScript's back end (optimiser, scope analysis, the
81
+ CoffeeScript-style lowering to ES5 and escodegen) does more work per
82
+ kilobyte than TypeScript's type-stripping emit. With type checking on the gap
83
+ is 2.4x; TypeScript's checker is far more thorough, and its standard library
84
+ load (`lib.d.ts`) is skipped here, which is why its first checked compile in a
85
+ real project costs a few hundred milliseconds more than the table shows. Cold
86
+ start favours `tsc` by 20 ms. The typed-against-typed row is the one small annotated program
87
+ (`scripts/bench/typed.mamba`, `scripts/bench/typed.ts`: the same order service, annotation for
88
+ annotation) compiled with checking twenty times per round: there TypeScript's fixed cost per program
89
+ (creating the program and the checker) dominates, so MambaScript is faster by about thirty times
90
+ for one file, while over a whole corpus TypeScript wins as the table shows. The honest summary: TypeScript is the faster
91
+ compiler; MambaScript is fast enough that a full build of a real application
92
+ (the 15 files of `../mamba-cookout`) takes a fraction of a second, and its
93
+ parser is not the bottleneck. The next lever is the back end
94
+ (`docs/perf.md`, "Where the time goes").