-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreclassify-v2.ts
More file actions
151 lines (133 loc) · 5 KB
/
reclassify-v2.ts
File metadata and controls
151 lines (133 loc) · 5 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
// Reclassify NOT_APPLICABLE fixture entries using updated normalize + equalTagged.
import { readFileSync, writeFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import { Compile, Run, Env } from "../src/expr.js";
import { mockEnv } from "../tests/go-parity/mock-env.js";
const here = dirname(fileURLToPath(import.meta.url));
interface Tagged { k: string; v?: any }
interface Row { expr: string; expected?: Tagged | null; error?: boolean; bucket: string; reason?: string; errorContains?: string }
function normalize(v: any): any {
if (v === null || v === undefined) return { k: "nil" };
if (typeof v === "boolean") return { k: "bool", v };
if (typeof v === "bigint") return { k: "int", v: v.toString() };
if (typeof v === "number") {
return Number.isInteger(v) ? { k: "int-or-float", v } : { k: "float", v };
}
if (typeof v === "string") return { k: "string", v };
if (v && typeof v === "object" && typeof v.ms === "number" && "Year" in v && "Month" in v && "Format" in v) {
return { k: "time", v: v.ms };
}
if (v && typeof v === "object" && typeof v.value === "bigint" && "Nanoseconds" in v) {
return { k: "duration", v: v.value.toString() };
}
if (v && typeof v === "object" && typeof v.name === "string" && typeof v.String === "function" && !("Year" in v) && !("value" in v)) {
return { k: "string", v: v.name };
}
if (Array.isArray(v)) return { k: "array", v: v.map(normalize) };
if (v instanceof Map) {
const m: Record<string, any> = {};
for (const [k, val] of v) m[String(k)] = normalize(val);
return { k: "map", v: m };
}
if (typeof v === "object") {
const m: Record<string, any> = {};
for (const [k, val] of Object.entries(v)) m[k] = normalize(val);
return { k: "map", v: m };
}
return { k: "other", v: String(v) };
}
function builtinEnv(): Record<string, any> {
return {
ArrayOfString: ["foo", "bar", "baz"],
ArrayOfInt: [1n, 2n, 3n],
ArrayOfInt32: [1n, 2n, 3n, 4n, 5n],
ArrayOfFloat: [1.5, 2.5, 3.5],
ArrayOfAny: [1n, "2", true],
ArrayOfFoo: [
{ Value: "a", Bar: { Baz: "baz" } },
{ Value: "b", Bar: { Baz: "baz" } },
{ Value: "c", Bar: { Baz: "baz" } },
],
EmptyIntArray: [],
EmptyFloatArray: [],
NestedIntArrays: [[1n, 2n], [3n, 4n]],
NestedAnyArrays: [[1n, 2n], [3n, 4n]],
NestedInt32Array: [[1n, 2n, 3n], [4n, 5n, 6n]],
};
}
type EnvFactory = () => Record<string, any>;
function tryReclassify(filePath: string, envFactory: EnvFactory, label: string): number {
const rows: Row[] = JSON.parse(readFileSync(filePath, "utf8"));
let promoted = 0;
let failed = 0;
for (const row of rows) {
if (row.bucket !== "NOT_APPLICABLE") continue;
// Skip known FORCED_DIVERGENCE entries
if (row.reason?.includes("pointer") || row.reason?.includes("Go typed-nil") ||
row.reason?.includes("Go pointer") || row.reason?.includes("no JS analog")) {
continue;
}
const env = envFactory();
const isChecker = label === "checker";
try {
if (isChecker) {
// Checker: just need Compile to throw
Compile(row.expr, Env(env));
// If it didn't throw, it's not a checker error
continue;
}
const program = Compile(row.expr, Env(env));
const out = Run(program, env);
const normalized = normalize(out);
row.expected = normalized;
row.bucket = "PASS";
delete row.reason;
promoted++;
console.log(` ✓ [${label}] ${row.expr} → ${JSON.stringify(normalized)}`);
} catch (e: any) {
if (isChecker) {
// Compile threw — that's what we want for checker entries
row.bucket = "PASS_WITH_ADAPTER";
delete row.reason;
promoted++;
console.log(` ✓ [${label}] ${row.expr} → throws (expected)`);
} else {
// Runtime error — check if the row expects an error
if (row.error) {
row.bucket = "PASS";
delete row.reason;
promoted++;
console.log(` ✓ [${label}] ${row.expr} → throws (expected)`);
} else {
failed++;
console.log(` ✗ [${label}] ${row.expr} → ${e.message}`);
}
}
}
}
if (promoted > 0) {
writeFileSync(filePath, JSON.stringify(rows, null, 2) + "\n");
}
console.log(`\n${label}: promoted=${promoted}, failed=${failed}\n`);
return promoted;
}
console.log("=== Reclassifying expr_mock.json ===");
const exprPromoted = tryReclassify(
join(here, "../parity/fixtures/expr_mock.json"),
mockEnv,
"expr"
);
console.log("=== Reclassifying builtin_mock.json ===");
const builtinPromoted = tryReclassify(
join(here, "../parity/fixtures/builtin_mock.json"),
builtinEnv,
"builtin"
);
console.log("=== Reclassifying checker_mock.json ===");
const checkerPromoted = tryReclassify(
join(here, "../parity/fixtures/checker_mock.json"),
mockEnv,
"checker"
);
console.log(`\n=== TOTAL: ${exprPromoted + builtinPromoted + checkerPromoted} entries promoted ===`);