We're checking types!!!!

This commit is contained in:
Dustin Swan 2026-03-26 18:32:40 -06:00
parent f3c3a76671
commit f272ffaca2
No known key found for this signature in database
GPG key ID: 30D46587E2100467
6 changed files with 304 additions and 24 deletions

View file

@ -1,5 +1,6 @@
import type { AST, Pattern, Definition } from './ast';
import { store } from './runtime-js';
import { typecheck } from './typechecker';
let matchCounter = 0;
@ -221,11 +222,14 @@ function compilePattern(pattern: Pattern, expr: string): { condition: string, bi
}
export function compileAndRun(defs: Definition[]) {
typecheck(defs);
const compiledDefs: string[] = [];
const topLevel = new Set(defs.map(d => d.name));
const topLevel = new Set(defs.filter(d => d.body).map(d => d.name));
for (const def of defs) {
if (!def.body) continue; // type declaration only
definitions.set(def.name, def);
const free = freeVars(def.body);
const deps = new Set([...free].filter(v => topLevel.has(v)));
@ -244,6 +248,7 @@ export function compileAndRun(defs: Definition[]) {
}
for (const def of defs) {
if (!def.body) continue;
const ctx: CompileCtx = { useStore: false, topLevel, bound: new Set() };
const compiled = `const ${sanitizeName(def.name)} = ${compile(def.body, ctx)};`;
compiledDefs.push(compiled);
@ -257,8 +262,9 @@ export function compileAndRun(defs: Definition[]) {
}
}
const defsWithBody = defs.filter(d => d.body);
const lastName = defs[defs.length - 1].name;
const defNames = defs.map(d => sanitizeName(d.name)).join(', ');
const defNames = defsWithBody.map(d => sanitizeName(d.name)).join(', ');
const code = `${compiledDefs.join('\n')}
return { ${defNames}, __result: ${sanitizeName(lastName)} };`;
@ -396,7 +402,7 @@ export function recompile(name: string, newAst: AST) {
collectDependents(name);
for (const defName of toRecompile) {
const ast = definitions.get(defName)!.body;
const ast = definitions.get(defName)!.body!;
const compiled = compile(ast);
const fn = new Function('store', `return ${compiled}`);