-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathshape-codegen.ts
More file actions
315 lines (296 loc) · 9.37 KB
/
Copy pathshape-codegen.ts
File metadata and controls
315 lines (296 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Gate: TS shape codegen — validateShape, emitTs+parseAst golden AST, no-shape byte identity.
import { execFileSync } from 'node:child_process';
import { createHash } from 'node:crypto';
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { emitParser, tsTarget } from '../src/emit.ts';
import { emitTs } from '../src/target-ts.ts';
import { calcShape } from '../src/shape-calc.ts';
import { validateShape, validateShapeOrThrow } from '../src/shape-validate.ts';
import calcGrammar from './fixtures/calc.ts';
type Ast = Record<string, unknown>;
function stripSpans(v: unknown): unknown {
if (v === null || typeof v !== 'object') return v;
if (Array.isArray(v)) return v.map(stripSpans);
const o = v as Record<string, unknown>;
const out: Record<string, unknown> = {};
for (const [k, val] of Object.entries(o)) {
if (k === 'off' || k === 'end') continue;
out[k] = stripSpans(val);
}
return out;
}
function deepEq(a: unknown, b: unknown): boolean {
return JSON.stringify(a) === JSON.stringify(b);
}
function emitAndLoad(shape?: typeof calcShape): Promise<{
parseAst: (src: string) => Ast | null;
parseWith: (src: string, b: unknown) => Ast | null;
cstBuilder: unknown;
}> {
const src = emitTs(calcGrammar, shape ? { shape } : undefined);
const dir = '/tmp/shape-codegen';
mkdirSync(dir, { recursive: true });
const file = `${dir}/calc-parser.ts`;
writeFileSync(file, src);
return import(file);
}
// ── Handwritten golden expectations (no parseAst backfill) ───────────────────
const GOLDEN: { src: string; expect: Ast }[] = [
{
src: 'let x = 1;',
expect: {
type: 'Program',
body: [{ type: 'LetStatement', id: 'x', init: 1 }],
},
},
{
src: '1 + 2;',
expect: {
type: 'Program',
body: [{
type: 'ExpressionStatement',
expression: { type: 'BinaryExpression', left: 1, operator: '+', right: 2 },
}],
},
},
{
src: '1 + 2 * 3;',
expect: {
type: 'Program',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'BinaryExpression', left: 1, operator: '+',
right: { type: 'BinaryExpression', left: 2, operator: '*', right: 3 },
},
}],
},
},
{
src: '-a;',
expect: {
type: 'Program',
body: [{
type: 'ExpressionStatement',
expression: { type: 'UnaryExpression', operator: '-', argument: 'a' },
}],
},
},
{
src: '(1);',
expect: {
type: 'Program',
body: [{ type: 'ExpressionStatement', expression: 1 }],
},
},
{
src: 'let a = 1; let b = 2; a + b;',
expect: {
type: 'Program',
body: [
{ type: 'LetStatement', id: 'a', init: 1 },
{ type: 'LetStatement', id: 'b', init: 2 },
{
type: 'ExpressionStatement',
expression: { type: 'BinaryExpression', left: 'a', operator: '+', right: 'b' },
},
],
},
},
{
src: '1 - 2 - 3;',
expect: {
type: 'Program',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'BinaryExpression',
left: { type: 'BinaryExpression', left: 1, operator: '-', right: 2 },
operator: '-',
right: 3,
},
}],
},
},
{
src: '-(a * b);',
expect: {
type: 'Program',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'UnaryExpression', operator: '-',
argument: { type: 'BinaryExpression', left: 'a', operator: '*', right: 'b' },
},
}],
},
},
{
src: 'foo; bar; baz;',
expect: {
type: 'Program',
body: [
{ type: 'ExpressionStatement', expression: 'foo' },
{ type: 'ExpressionStatement', expression: 'bar' },
{ type: 'ExpressionStatement', expression: 'baz' },
],
},
},
];
// ── Validator negatives ───────────────────────────────────────────────────────
const NEGATIVES: { label: string; spec: typeof calcShape; wantCode: string }[] = [
{
label: 'field-oob',
spec: {
...calcShape,
rules: {
...calcShape.rules,
Program: {
kind: 'node',
type: 'Program',
fields: [{ name: 'body', bind: { at: 5 }, typeHint: 'Statement' }],
},
},
},
wantCode: 'field-oob',
},
{
label: 'star-needs-list',
spec: {
...calcShape,
rules: {
...calcShape.rules,
Program: {
kind: 'node',
type: 'Program',
fields: [{ name: 'body', bind: { at: 0 }, typeHint: 'Statement' }],
},
},
},
wantCode: 'star-needs-list',
},
{
label: 'unmapped',
spec: {
...calcShape,
rules: { Expr: calcShape.rules.Expr!, Stmt: calcShape.rules.Stmt! },
},
wantCode: 'unmapped',
},
];
function cstPrefix(src: string): string {
const marker = '// ─── Shape AST';
const i = src.indexOf(marker);
return i < 0 ? src : src.slice(0, i);
}
async function main(): Promise<void> {
let pass = 0;
let fail = 0;
// validateShape on calc
const vr = validateShape({ default: calcGrammar }, calcShape);
if (!vr.ok) {
console.error('validateShape calc failed:', vr.errors);
fail++;
} else {
pass++;
console.log(`✓ validateShape calc ok (${vr.ir.rules.length} rules)`);
}
// negatives
for (const neg of NEGATIVES) {
const r = validateShape({ default: calcGrammar }, neg.spec);
const hit = r.errors.some((e) => e.code === neg.wantCode);
if (r.ok || !hit) {
console.error(`✗ negative ${neg.label}: want code=${neg.wantCode}, got ok=${r.ok} errors=${JSON.stringify(r.errors)}`);
fail++;
} else {
pass++;
console.log(`✓ negative ${neg.label} → code=${neg.wantCode}`);
}
}
// validateShapeOrThrow hard error
try {
validateShapeOrThrow(calcGrammar, NEGATIVES[0]!.spec);
console.error('✗ validateShapeOrThrow should throw');
fail++;
} catch (e) {
pass++;
console.log('✓ validateShapeOrThrow throws on field-oob');
}
// no-shape: emitTs byte-identical to emitParser (full file); SHA256 recorded for master proof
const noShape = emitTs(calcGrammar);
const master = emitParser(calcGrammar, tsTarget);
const hNo = createHash('sha256').update(noShape).digest('hex');
const hMa = createHash('sha256').update(master).digest('hex');
if (noShape !== master || hNo !== hMa) {
console.error(`✗ no-shape emitTs !== emitParser (sha ${hNo.slice(0,12)} vs ${hMa.slice(0,12)})`);
fail++;
} else {
pass++;
console.log(`✓ no-shape emitted full-file sha256=${hNo.slice(0,12)} ≡ emitParser`);
}
// Generated types: list fields must be T[]; opText fields must be string
const withShapeSrc = emitTs(calcGrammar, { shape: calcShape });
if (!/export interface Program \{[^}]*body: Statement\[\];/.test(withShapeSrc)) {
console.error('✗ Program.body must be typed Statement[]');
fail++;
} else { pass++; console.log('✓ Program.body: Statement[]'); }
if (!/operator: string;/.test(withShapeSrc)) {
console.error('✗ opText fields must be typed string');
fail++;
} else { pass++; console.log('✓ opText fields: string'); }
// golden parseAst
const mod = await emitAndLoad(calcShape);
for (const g of GOLDEN) {
const got = stripSpans(mod.parseAst(g.src));
if (!deepEq(got, g.expect)) {
console.error(`✗ golden ${JSON.stringify(g.src)}`);
console.error(' got: ', JSON.stringify(got));
console.error(' want: ', JSON.stringify(g.expect));
fail++;
} else {
pass++;
}
}
console.log(`✓ golden parseAst ${GOLDEN.length}/${GOLDEN.length}`);
// operator spot-check
const bin = mod.parseAst('2 / 3;') as Ast;
const stmt = (bin?.body as Ast[])?.[0] as Ast;
const expr = stmt?.expression as Ast;
if (expr?.type !== 'BinaryExpression' || expr.operator !== '/') {
console.error(`✗ BinaryExpression.operator got ${JSON.stringify(expr)}`);
fail++;
} else {
pass++;
console.log('✓ BinaryExpression.operator=/');
}
const un = mod.parseAst('--x;') as Ast;
const ustmt = (un?.body as Ast[])?.[0] as Ast;
const uexpr = ustmt?.expression as Ast;
if (uexpr?.type !== 'UnaryExpression' || uexpr.operator !== '-') {
console.error(`✗ UnaryExpression.operator got ${JSON.stringify(uexpr)}`);
fail++;
} else {
pass++;
console.log('✓ UnaryExpression.operator=-');
}
// performance smoke: parseAst vs parseWith on ≥1MB calc input
const chunk = 'let x = 1 + 2 * 3;\n';
const big = chunk.repeat(Math.ceil((1024 * 1024) / chunk.length));
const t0 = performance.now();
mod.parseAst(big);
const astMs = performance.now() - t0;
if (typeof mod.parseWith === 'function') {
const t1 = performance.now();
mod.parseWith(big, mod.cstBuilder);
const withMs = performance.now() - t1;
console.log(`✓ perf smoke ${(big.length / 1024 / 1024).toFixed(2)}MB parseAst=${astMs.toFixed(1)}ms parseWith=${withMs.toFixed(1)}ms`);
pass++;
} else {
console.log(`✓ perf smoke ${(big.length / 1024 / 1024).toFixed(2)}MB parseAst=${astMs.toFixed(1)}ms (no parseWith)`);
pass++;
}
const total = pass + fail;
console.log(`shape-codegen: ${pass}/${total} checks passed`);
if (fail) process.exit(1);
}
main().catch((e) => { console.error(e); process.exit(1); });