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.
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
@@ -0,0 +1,107 @@
1
+ # MambaScript against TypeScript
2
+
3
+ Both compile to JavaScript and both add types. They make opposite bets:
4
+ TypeScript keeps JavaScript's syntax and adds a complete static type system
5
+ on top; MambaScript changes the syntax to read like sentences and adds a
6
+ small, gradual type system that warns. This page says where each wins, with
7
+ measured numbers for speed (`npm run bench:ts`, 2026-12-05).
8
+
9
+ ## Syntax
10
+
11
+ | | MambaScript | TypeScript |
12
+ |---|---|---|
13
+ | Blocks | indentation | braces |
14
+ | Statement end | newline | semicolon (optional) |
15
+ | Equality | `is` / `isnt`, always strict; no loose `==` | `===` and `==` |
16
+ | Logic | `and`, `or`, `not` (symbols also work) | `&&`, `\|\|`, `!` |
17
+ | Booleans, null | `yes`/`no`/`on`/`off`, `nothing`/`missing` (JavaScript words also work) | `true`/`false`, `null`/`undefined` |
18
+ | Classes | `blueprint`, `inheritsFrom`, `@field`, `make C` | `class`, `extends`, `this.field`, `new C()` |
19
+ | Branching | `if` / `unless` / `otherwise`, postfix `x if c`, `consider` / `when` without fallthrough | `if` / `else`, `switch` with `break` |
20
+ | Loops | `forEvery x in list`, `forEvery k, v of obj`, `forEvery x from iterable`, `until`, `loop`, `bail`, `keepGoing`, labels | `for`, `for…of`, `for…in`, `while`, `break`, `continue`, labels |
21
+ | Expressions | everything is an expression: `x = if c then 1 else 2`, `r = attempt … onFail …`, loops collect their values | statements and expressions are separate; ternary `?:` |
22
+ | Comprehensions, ranges, slices | `(x * 2 for x in xs when x > 1)`, `[1..10]`, `list[1..3]` | none (array methods) |
23
+ | Errors | `attempt` / `onFail` / `regardless`, `panic` | `try` / `catch` / `finally`, `throw` |
24
+ | Async | `waitForIt p` (the function becomes async by itself), `waitForAll` | `async` on the function, `await`, `Promise.all` |
25
+ | Existence | `x?`, `a ? b` / `orElse`, `a?.b`, `a ?= b` | `a ?? b`, `a?.b`, `a ??= b` |
26
+ | Modules | `grab x from 'm'`, `share {x}`; CommonJS or ES modules from the same source | `import` / `export`; module kind by configuration |
27
+ | Strings | `"#{x}"`, heredocs, heregex `///…///`, tagged `tag"…"` | template literals, tagged templates |
28
+ | Not in MambaScript | getters/setters syntax, `#private`, decorators, enums, namespaces as in TS, JSX | all present |
29
+
30
+ ## Types
31
+
32
+ | | MambaScript | TypeScript |
33
+ |---|---|---|
34
+ | Philosophy | gradual: annotate what you want checked, everything else is `Any`; mistakes warn by default (`--strict` makes them fail) | whole-program: everything has a type, inference fills the gaps, errors fail the build |
35
+ | Annotation | `x :: Int = 1`, `(a :: Int) :: String -> …` | `let x: number = 1`, `(a: number): string => …` |
36
+ | Structural types | `struct Point` with `x :: Int` lines, inline `{x :: Int, y :: Int}`, `implements` | interfaces, type literals, `implements` |
37
+ | Nullable | `T?` (nothing and missing) | `T \| null \| undefined`, `strictNullChecks` |
38
+ | Arrays and functions | `T[]`, `(A, B) -> R`, `A * B -> R`, curried `A -> B -> C` | `T[]`, `(a: A, b: B) => R` |
39
+ | Generics | `struct S<T>`, `blueprint C<A>`, `f<T> :: …`, instantiated at the call | full generics with constraints, inference, conditional and mapped types |
40
+ | Numbers | `Int`, `Float`, `Number` form a chain; `BigInt` separate | `number`, `bigint` |
41
+ | Unions, literals, narrowing, keyof, utility types | none (2.1 roadmap) | yes |
42
+ | Declaration files, DefinitelyTyped | none; untyped JavaScript flows in as `Any` | `.d.ts` for the whole ecosystem |
43
+ | Output | types leave no trace; the JavaScript is plain ES5-style with a few helpers | types erased; output target configurable (ES3 to ESNext) |
44
+
45
+ TypeScript's type system is the more powerful one by a wide margin, and it is
46
+ what you want when a large team needs the compiler to enforce contracts
47
+ across a big codebase. MambaScript's checker catches the mistakes a learner or
48
+ a small team makes (a string where a number was declared, a missing struct
49
+ member, the wrong argument type, a `BigInt` mixed with an `Int`) and gets out
50
+ of the way otherwise. Its messages are written for people (`x is declared as
51
+ Int, but this value is String`, a frame, a fix).
52
+
53
+ ## Speed
54
+
55
+ 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.
56
+
57
+ | | MambaScript | TypeScript | how |
58
+ |---|---|---|---|
59
+ | cold start, one small file (CLI) | 69 ms | 50 ms | node itself: 17 ms |
60
+ | parse only | 18098 KB/s | 16253 KB/s | MambaScript.parse vs ts.createSourceFile |
61
+ | transpile, no type check | 539 KB/s | 2692 KB/s | full compile (typeCheck off) vs ts.transpileModule |
62
+ | 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 |
63
+ | 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 |
64
+ | resident memory after the checked run | 629 MB | 687 MB | same process, TypeScript measured after MambaScript |
65
+
66
+ TypeScript is the faster compiler over a corpus: five times on transpile-only,
67
+ about 2.4x with type checking on. The parsers are equally fast. For one small
68
+ annotated program (the typed row, the same order service in both languages)
69
+ MambaScript's checked compile is about thirty times faster, because TypeScript
70
+ pays a fixed cost per program to create the checker. For one small file both
71
+ CLIs start in well under 100 ms. In practice a full build of a real
72
+ MambaScript application (the 15 files of the Cookout API) takes a fraction of a
73
+ second, so compile time is not where either language is chosen.
74
+
75
+ ## Tooling and ecosystem
76
+
77
+ TypeScript has first-class editor support in every editor (its language
78
+ service powers completion, refactoring and inline errors), DefinitelyTyped,
79
+ ts-node, every bundler and framework. MambaScript 2.0 has: `mamba` (run,
80
+ compile, REPL, project scaffold), a `require` hook and an ES-module loader
81
+ (`node --import mambascript/esm`), a jest transform (`mambascript/jest`),
82
+ `mamba --json` diagnostics for editors, a webpack loader, syntax highlighting
83
+ for Atom and VS Code, source maps, two interchangeable parsers (`--parser peg`
84
+ as the fallback and oracle), and zero runtime dependencies. A language
85
+ server, an LSP-based editor extension and `mamba lint` are on the 2.1 roadmap.
86
+
87
+ ## Output
88
+
89
+ MambaScript emits readable ES5-style JavaScript with `var` declarations
90
+ hoisted to the top of each function, a handful of helpers (`slice`,
91
+ `objectWithoutKeys$`), native `async`/`await`, generators, template literals
92
+ for tagged templates, and `import`/`export` when asked. TypeScript emits
93
+ whatever target you choose and keeps your code's shape. If you need to read
94
+ the output, MambaScript's is small and regular; if you need the newest syntax
95
+ in the output (`let`/`const`, native `?.`/`??`, arrow functions), TypeScript has
96
+ it today and MambaScript gets an output-target option in 2.1.
97
+
98
+ ## Choosing
99
+
100
+ Pick TypeScript when the type system is the point: a large codebase, many
101
+ contributors, typed libraries, editor-driven refactoring. Pick MambaScript
102
+ when readability is the point: code that a learner or a client can read after
103
+ an afternoon, scripts and services where `is`, `unless`, `forEvery`,
104
+ `waitForIt` and `attempt`/`onFail` say what happens, with types where they
105
+ help and warnings that do not stop you. Both run on the same Node and in the
106
+ same browsers; moving between them is a translation, not a rewrite
107
+ (`docs/reference.md` lists every construct against its JavaScript).
package/esm.js ADDED
@@ -0,0 +1,8 @@
1
+ // `node --import mambascript/esm app.mamba`: compile .mamba files on import.
2
+ // Uses the synchronous hooks API when the Node version has it, the async one otherwise.
3
+ const Module = require('module');
4
+ if (typeof Module.registerHooks === 'function') {
5
+ Module.registerHooks(require('./lib/esm-hooks'));
6
+ } else {
7
+ Module.register('./lib/esm-hooks.js', require('url').pathToFileURL(__filename));
8
+ }
package/jest.js ADDED
@@ -0,0 +1,33 @@
1
+ // Jest transform for .mamba files. In a project's package.json:
2
+ // "jest": { "moduleFileExtensions": ["mamba", "js"],
3
+ // "transform": { "\\.mamba$": "mambascript/jest" } }
4
+ const crypto = require('crypto');
5
+ const fs = require('fs');
6
+ const path = require('path');
7
+ const MambaScript = require('./lib/module');
8
+
9
+ // The compiler's own files are part of the cache key: a project that links a
10
+ // checkout (`"mambascript": "file:../mambascript"`) must not be served output
11
+ // compiled by the previous build.
12
+ const libDir = path.join(__dirname, 'lib');
13
+ const compilerStamp = fs.readdirSync(libDir).filter((f) => f.endsWith('.js')).map((f) => fs.statSync(path.join(libDir, f)).mtimeMs).join(',');
14
+
15
+ module.exports = {
16
+ process(src, filename, options) {
17
+ const esm = process.env.MAMBA_MODULES ? process.env.MAMBA_MODULES === 'esm' : !!(options && options.supportsStaticESM);
18
+ const code = MambaScript.cs2js(src, {
19
+ bare: true,
20
+ raw: true,
21
+ filename,
22
+ inputSource: filename,
23
+ typeCheck: process.env.MAMBA_TYPECHECK || 'warn',
24
+ parser: process.env.MAMBA_PARSER,
25
+ modules: esm ? 'esm' : 'cjs',
26
+ });
27
+ return { code };
28
+ },
29
+ getCacheKey(src, filename, options) {
30
+ const esm = process.env.MAMBA_MODULES || ((options && options.supportsStaticESM) ? 'esm' : 'cjs');
31
+ return crypto.createHash('md5').update(src).update(filename).update(MambaScript.VERSION).update(compilerStamp).update(process.env.MAMBA_PARSER || '').update(process.env.MAMBA_TYPECHECK || '').update(esm).digest('hex');
32
+ },
33
+ };
package/lib/black.js CHANGED
@@ -1,9 +1,8 @@
1
- // Generated by MambaScript 0.5.2
2
- var present = console.log;
3
- // Generated by MambaScript 0.5.2
4
- var black, cache, elements, mamba, mambascript_helpers, merge_elements, skeleton, skeleton, skeleton;
1
+ // Generated by MambaScript 2.0.0
2
+ var black, cache, elements, mamba, mambascript_helpers, merge_elements, skeleton, stdlib;
5
3
  black = module.exports;
6
- mamba = require('../lib/light-module');
4
+ mamba = require('./light-module');
5
+ stdlib = require('./underscore');
7
6
  black.version = '0.1.3';
8
7
  black.doctypes = {
9
8
  'default': '<!DOCTYPE html>',
@@ -17,9 +16,9 @@ black.doctypes = {
17
16
  mobile: '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">',
18
17
  ce: '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ce-html-1.0-transitional.dtd">'
19
18
  };
20
- mambascript_helpers = 'var present = console.log;'.replace(/\n/g, '');
19
+ mambascript_helpers = 'var present = console.log, complain = console.warn;'.replace(/\n/g, '');
21
20
  elements = {
22
- regular: 'a abbr address article aside audio b bdi bdo blockquote body button\n canvas caption cite code colgroup datalist dd del details dfn div dl dt em\n fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 head header hgroup\n html i iframe ins kbd label legend li main map mark menu meter nav noscript object\n ol optgroup option output p pre progress q rp rt ruby s samp script section\n select small span strong style sub summary sup table tbody td textarea tfoot\n th thead time title tr u ul video',
21
+ regular: 'a abbr address article aside audio b bdi bdo blockquote body button\n canvas caption cite code colgroup datalist dd del details dfn dialog div dl dt em\n fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 head header hgroup\n html i iframe ins kbd label legend li main map mark menu meter nav noscript object\n ol optgroup option output p picture pre progress q rp rt ruby s samp script search section\n select slot small span strong style sub summary sup table tbody td template textarea tfoot\n th thead time title tr u ul video\n svg g defs use path circle rect line polyline polygon',
23
22
  'void': 'area base br col command embed hr img input keygen link meta param\n source track wbr',
24
23
  obsolete: 'applet acronym bgsound dir frameset noframes isindex listing\n nextid noembed plaintext rb strike xmp big blink center font marquee multicol\n nobr spacer tt',
25
24
  obsolete_void: 'basefont frame'
@@ -30,8 +29,10 @@ merge_elements = function () {
30
29
  result = [];
31
30
  for (var i$ = 0, length$ = args.length; i$ < length$; ++i$) {
32
31
  a = args[i$];
33
- for (var cache$ = elements[a].split(' '), i$1 = 0, length$1 = cache$.length; i$1 < length$1; ++i$1) {
32
+ for (var cache$ = elements[a].split(/\s+/), i$1 = 0, length$1 = cache$.length; i$1 < length$1; ++i$1) {
34
33
  element = cache$[i$1];
34
+ if (!element)
35
+ continue;
35
36
  if (!in$(element, result))
36
37
  result.push(element);
37
38
  }
@@ -83,7 +84,7 @@ skeleton = function (data) {
83
84
  classes = [];
84
85
  for (var cache$ = str.split('.'), i$ = 0, length$ = cache$.length; i$ < length$; ++i$) {
85
86
  i = cache$[i$];
86
- if (in$('#', i)) {
87
+ if (i.indexOf('#') !== -1) {
87
88
  id = i.replace('#', '');
88
89
  } else if (!(i === '')) {
89
90
  classes.push(i);
@@ -243,7 +244,7 @@ skeleton = function (data) {
243
244
  skeleton = String(skeleton).replace(/function\s*\(.*\)\s*\{/, '').replace(/return null;\s*\}$/, '');
244
245
  skeleton = mambascript_helpers + skeleton;
245
246
  black.compile = function (template, options) {
246
- var code, hardcoded_locals, k, t, tag_functions, tags_used, v;
247
+ var code, compiled, hardcoded_locals, k, t, tag_functions, tags_used, v;
247
248
  if (null == options)
248
249
  options = {};
249
250
  if (typeof template === 'function') {
@@ -284,7 +285,10 @@ black.compile = function (template, options) {
284
285
  if (options.locals)
285
286
  code += '}';
286
287
  code += "return __bm.buffer.join('');";
287
- return new Function('data', code);
288
+ compiled = new Function('data', '_', code);
289
+ return function (data) {
290
+ return compiled.call(this, data, stdlib);
291
+ };
288
292
  };
289
293
  cache = {};
290
294
  black.render = function (template, data, options) {
@@ -344,7 +348,7 @@ if (!('undefined' !== typeof window && null != window))
344
348
  }
345
349
  return function () {
346
350
  try {
347
- return tpl.apply(null, [].slice.call(arguments));
351
+ return tpl.apply(null, Array.from(arguments));
348
352
  } catch (e$1) {
349
353
  e = e$1;
350
354
  throw new TemplateError('Error rendering ' + data.filename + ': ' + e.message);
@@ -373,4 +377,4 @@ function extends$(child, parent) {
373
377
  child.prototype = new ctor;
374
378
  child.__super__ = parent.prototype;
375
379
  return child;
376
- }
380
+ }
package/lib/browser.js CHANGED
@@ -1,6 +1,4 @@
1
- // Generated by MambaScript 0.5.2
2
- var present = console.log;
3
- // Generated by MambaScript 0.5.2
1
+ // Generated by MambaScript 2.0.0
4
2
  var MambaScript, runScripts;
5
3
  MambaScript = require('./module');
6
4
  MambaScript['eval'] = function (code, options) {
@@ -76,4 +74,4 @@ if ('undefined' !== typeof addEventListener && null != addEventListener) {
76
74
  } else if ('undefined' !== typeof attachEvent && null != attachEvent) {
77
75
  attachEvent('onload', runScripts);
78
76
  }
79
- module.exports = MambaScript;
77
+ module.exports = MambaScript;