@skillstech/thunderlang 1.0.4 → 1.2.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 (81) hide show
  1. package/CHANGELOG.md +221 -0
  2. package/README.md +4 -1
  3. package/dist/core.cjs +2568 -462
  4. package/dist/index.cjs +7562 -5084
  5. package/dist/learn.cjs +16994 -8339
  6. package/intent-graph.schema.json +11 -0
  7. package/package.json +6 -2
  8. package/src/cli-home.mjs +242 -0
  9. package/src/cli.mjs +241 -89
  10. package/src/codegen.mjs +145 -55
  11. package/src/conformance.mjs +15 -6
  12. package/src/core/check.mjs +725 -0
  13. package/src/core/cli.mjs +184 -0
  14. package/src/core/diagnostics.mjs +82 -0
  15. package/src/core/engine-core.mjs +572 -0
  16. package/src/core/engine-values.mjs +1054 -0
  17. package/src/core/engine-worker.mjs +15 -0
  18. package/src/core/engine.mjs +63 -0
  19. package/src/core/format.mjs +20 -0
  20. package/src/core/from-python.mjs +645 -0
  21. package/src/core/index.mjs +39 -0
  22. package/src/core/lexer.mjs +115 -0
  23. package/src/core/parser.mjs +742 -0
  24. package/src/core/project.mjs +177 -0
  25. package/src/core/round-trip.mjs +150 -0
  26. package/src/core/runtime-python.mjs +801 -0
  27. package/src/core/runtime-typescript.mjs +119 -0
  28. package/src/core/target-python.mjs +487 -0
  29. package/src/core/target-typescript.mjs +466 -0
  30. package/src/core/typescript-run.mjs +114 -0
  31. package/src/core.d.ts +5 -2
  32. package/src/core.mjs +5 -2
  33. package/src/data-schema.mjs +8 -1
  34. package/src/emit.mjs +1 -1
  35. package/src/evidence.mjs +1 -1
  36. package/src/expr.mjs +81 -4
  37. package/src/form-words.mjs +2 -0
  38. package/src/index.d.ts +8 -1
  39. package/src/index.mjs +5 -1
  40. package/src/intent-schema.mjs +1 -0
  41. package/src/learn-lessons.mjs +245 -1
  42. package/src/learn.d.mts +8 -0
  43. package/src/learn.mjs +40 -4
  44. package/src/mcp.mjs +23 -0
  45. package/src/meaning.mjs +45 -6
  46. package/src/model.d.mts +7 -3
  47. package/src/model.mjs +19 -6
  48. package/src/money.d.mts +18 -0
  49. package/src/money.mjs +124 -0
  50. package/src/plain-contract.mjs +18 -2
  51. package/src/read-level.mjs +146 -0
  52. package/src/read.d.mts +11 -0
  53. package/src/read.mjs +40 -6
  54. package/src/review-comment.d.mts +16 -0
  55. package/src/review-comment.mjs +145 -0
  56. package/src/say-less.mjs +1432 -0
  57. package/src/system.mjs +153 -44
  58. package/src/target-cs.mjs +39 -34
  59. package/src/target-elixir.mjs +40 -30
  60. package/src/target-go.mjs +41 -36
  61. package/src/target-java.mjs +34 -27
  62. package/src/target-kotlin.mjs +43 -35
  63. package/src/target-py.mjs +57 -55
  64. package/src/target-result.d.mts +48 -0
  65. package/src/target-result.mjs +112 -0
  66. package/src/target-rust.mjs +42 -38
  67. package/src/target-scala.mjs +45 -35
  68. package/src/target-ts.mjs +37 -45
  69. package/src/target-types.mjs +41 -5
  70. package/src/target-util.mjs +55 -2
  71. package/src/term.mjs +89 -0
  72. package/src/trace-cli.mjs +58 -0
  73. package/src/trace-fs.mjs +76 -0
  74. package/src/trace.mjs +261 -0
  75. package/src/translate.d.mts +3 -0
  76. package/src/translate.mjs +62 -32
  77. package/src/try-cli.mjs +225 -0
  78. package/src/why-cli.mjs +41 -0
  79. package/src/why.d.mts +38 -0
  80. package/src/why.mjs +613 -0
  81. package/src/write-steps.mjs +22 -5
@@ -0,0 +1,39 @@
1
+ // Thunder Core: the part of Thunder a person can write and the compiler turns into a running program.
2
+ // Public API (pure; no file system): parse, check, and generate Python from Thunder Core source.
3
+ //
4
+ // compileCore(source, { file }) -> { ok, diagnostics, python, program, info }
5
+ //
6
+ // The design and the grammar are in docs/core.md.
7
+
8
+ import { parseCore } from './parser.mjs';
9
+ import { checkCore } from './check.mjs';
10
+ import { toPython } from './target-python.mjs';
11
+ import { formatDiagnostic } from './diagnostics.mjs';
12
+
13
+ export { parseCore } from './parser.mjs';
14
+ export { checkCore } from './check.mjs';
15
+ export { toPython } from './target-python.mjs';
16
+ export { toTypeScript, pythonOnlyUses } from './target-typescript.mjs';
17
+ export { PYTHON_RUNTIME, RUNTIME_VERSION } from './runtime-python.mjs';
18
+ export { CODES, formatDiagnostic } from './diagnostics.mjs';
19
+ export { formatCore } from './format.mjs';
20
+
21
+ export const CORE_VERSION = '0.1.0';
22
+
23
+ /**
24
+ * Compile one Thunder Core file. `imported` is a list of earlier results whose records, functions, and kept
25
+ * values this file may use (test files see the project's src files); `srcModules` are the Python modules to
26
+ * import them from.
27
+ */
28
+ export function compileCore(source, { file = 'main.thunder', imported = [], srcModules = [] } = {}) {
29
+ const seed = imported.length ? { known: imported.flatMap((r) => [...r.program.names.known].filter(([, kind]) => kind !== 'var')), fields: imported.flatMap((r) => [...r.program.names.fields]), records: imported.flatMap((r) => [...r.program.names.records]) } : null;
30
+ const program = parseCore(source, { seed });
31
+ const { diagnostics, info } = checkCore(program, { imported: imported.map((r) => r.info) });
32
+ const ok = diagnostics.length === 0;
33
+ const python = ok ? toPython(program, info, { source: file, imports: srcModules }) : null;
34
+ return { ok, diagnostics, python, program, info, file };
35
+ }
36
+
37
+ export function diagnosticsText(result) {
38
+ return result.diagnostics.map((d) => formatDiagnostic(d, result.file)).join('\n');
39
+ }
@@ -0,0 +1,115 @@
1
+ // Thunder Core lexer: source text -> lines of tokens, with indentation.
2
+ //
3
+ // A Thunder Core program is a list of lines. A line that ends with ":" opens a block; the lines under it are
4
+ // indented further (spaces only). "#" starts a comment that runs to the end of the line, outside text.
5
+ //
6
+ // Tokens:
7
+ // word letters, digits, underscores; `yes/no` is one word { t: 'word', text, lower }
8
+ // poss the possessive 's (customer's balance) { t: 'poss' }
9
+ // num 12, 0.10, 1_000 { t: 'num', text, whole }
10
+ // money $200, $0.10 { t: 'money', text }
11
+ // percent 20%, 12.5% { t: 'percent', text }
12
+ // str "Hello, {name}!" (parts: strings and { tokens } holes) { t: 'str', parts }
13
+ // sym + - * / ( ) [ ] , : . = == != <> < <= > >= { t: 'sym', text }
14
+
15
+ import { diag, CoreError } from './diagnostics.mjs';
16
+
17
+ const SYMS = ['==', '!=', '<>', '<=', '>=', '+', '-', '*', '/', '(', ')', '[', ']', ',', ':', '.', '=', '<', '>'];
18
+ const WORD_START = /[\p{L}_]/u;
19
+ const WORD_CHAR = /[\p{L}\p{N}_]/u;
20
+
21
+ /** Tokenize one line's text (no newline). Throws CoreError with a TC1xx diagnostic. */
22
+ export function tokenizeLine(text, line, colOffset = 0) {
23
+ const toks = [];
24
+ let i = 0;
25
+ const at = (k = 0) => text[i + k];
26
+ while (i < text.length) {
27
+ const c = at();
28
+ const col = i + colOffset + 1;
29
+ if (c === ' ' || c === '\t' || c === '\r') { i++; continue; }
30
+ if (c === '#') break; // comment
31
+ if (c === '"' || c === '“') { const [tok, end] = readString(text, i, line, colOffset); toks.push({ ...tok, col }); i = end; continue; }
32
+ if (c === '$' && /[0-9]/.test(at(1) ?? '')) {
33
+ const m = text.slice(i + 1).match(/^[0-9][0-9_]*(\.[0-9]+)?/);
34
+ toks.push({ t: 'money', text: m[0].replace(/_/g, ''), col, line }); i += 1 + m[0].length; continue;
35
+ }
36
+ if (/[0-9]/.test(c)) {
37
+ const m = text.slice(i).match(/^[0-9][0-9_]*(\.[0-9]+)?/);
38
+ const num = m[0].replace(/_/g, '');
39
+ i += m[0].length;
40
+ if (at() === '%') { toks.push({ t: 'percent', text: num, col, line }); i++; continue; }
41
+ toks.push({ t: 'num', text: num, whole: !num.includes('.'), col, line }); continue;
42
+ }
43
+ if (WORD_START.test(c)) {
44
+ let j = i;
45
+ while (j < text.length && WORD_CHAR.test(text[j])) j++;
46
+ let word = text.slice(i, j);
47
+ if (word.toLowerCase() === 'yes' && text.slice(j, j + 3).toLowerCase() === '/no') { word = text.slice(i, j + 3); j += 3; }
48
+ toks.push({ t: 'word', text: word, lower: word.toLowerCase(), col, line });
49
+ i = j;
50
+ if ((text[i] === "'" || text[i] === '’') && (text[i + 1] === 's' || text[i + 1] === 'S') && !WORD_CHAR.test(text[i + 2] ?? ' ')) {
51
+ toks.push({ t: 'poss', col: i + colOffset + 1, line }); i += 2;
52
+ }
53
+ continue;
54
+ }
55
+ const sym = SYMS.find((s) => text.startsWith(s, i));
56
+ if (sym) { toks.push({ t: 'sym', text: sym === '<>' ? '!=' : sym, col, line }); i += sym.length; continue; }
57
+ throw new CoreError(diag('TC105', line, `I can't read the character "${c}" (column ${col}).`, 'Text goes inside double quotes: "like this".'));
58
+ }
59
+ return toks;
60
+ }
61
+
62
+ function readString(text, start, line, colOffset) {
63
+ const close = text[start] === '“' ? '”' : '"';
64
+ const parts = [];
65
+ let buf = '';
66
+ let i = start + 1;
67
+ while (i < text.length) {
68
+ const c = text[i];
69
+ if (c === '\\') {
70
+ const n = text[i + 1];
71
+ buf += n === 'n' ? '\n' : n === 't' ? '\t' : n === 'r' ? '\r' : n ?? '';
72
+ i += 2; continue;
73
+ }
74
+ if (c === close || (close === '”' && c === '"')) {
75
+ if (buf || !parts.length) parts.push(buf);
76
+ return [{ t: 'str', parts, line }, i + 1];
77
+ }
78
+ if (c === '{' && text[i + 1] === '{') { buf += '{'; i += 2; continue; }
79
+ if (c === '}' && text[i + 1] === '}') { buf += '}'; i += 2; continue; }
80
+ if (c === '{') {
81
+ const end = text.indexOf('}', i);
82
+ if (end < 0) throw new CoreError(diag('TC107', line, 'A "{" inside text has no closing "}".', 'Write {{ for a brace that is just text.'));
83
+ if (buf) parts.push(buf);
84
+ buf = '';
85
+ const inner = text.slice(i + 1, end);
86
+ parts.push({ tokens: tokenizeLine(inner, line, colOffset + i + 1), source: inner.trim() });
87
+ i = end + 1; continue;
88
+ }
89
+ buf += c; i++;
90
+ }
91
+ throw new CoreError(diag('TC106', line, 'This text is missing its closing quote (").'));
92
+ }
93
+
94
+ /**
95
+ * Split source into logical lines: { line, indent, tokens, raw }. Blank and comment-only lines are dropped.
96
+ * Collects diagnostics instead of stopping at the first one.
97
+ */
98
+ export function lex(source) {
99
+ const lines = [];
100
+ const diagnostics = [];
101
+ source.replace(/\r\n?/g, '\n').split('\n').forEach((raw, idx) => {
102
+ const line = idx + 1;
103
+ const m = raw.match(/^([ \t]*)/);
104
+ if (m[1].includes('\t')) { diagnostics.push(diag('TC101', line, 'This line is indented with a tab. Thunder uses spaces (two per level).')); return; }
105
+ const body = raw.slice(m[1].length);
106
+ if (!body.trim() || body.trimStart().startsWith('#')) return;
107
+ try {
108
+ const tokens = tokenizeLine(body, line, m[1].length);
109
+ if (tokens.length) lines.push({ line, indent: m[1].length, tokens, raw: body.replace(/\s+$/, '') });
110
+ } catch (e) {
111
+ if (e instanceof CoreError) diagnostics.push(e.diagnostic); else throw e;
112
+ }
113
+ });
114
+ return { lines, diagnostics };
115
+ }