|
|
|
|
@ -3,13 +3,20 @@ import { store } from './runtime-js';
|
|
|
|
|
|
|
|
|
|
let matchCounter = 0;
|
|
|
|
|
|
|
|
|
|
type CompileCtx = {
|
|
|
|
|
useStore: boolean;
|
|
|
|
|
bound: Set<string>;
|
|
|
|
|
topLevel: Set<string>;
|
|
|
|
|
};
|
|
|
|
|
const defaultCtx: CompileCtx = { useStore: true, bound: new Set(), topLevel: new Set() };
|
|
|
|
|
|
|
|
|
|
export const definitions: Map<string, AST> = new Map();
|
|
|
|
|
export const dependencies: Map<string, Set<string>> = new Map();
|
|
|
|
|
export const dependents: Map<string, Set<string>> = new Map();
|
|
|
|
|
export const astRegistry = new Map<number, AST>();
|
|
|
|
|
let astIdCounter = 0;
|
|
|
|
|
|
|
|
|
|
export function compile(ast: AST, useStore = true, bound = new Set<string>(), topLevel = new Set<string>()): string {
|
|
|
|
|
export function compile(ast: AST, ctx: CompileCtx = defaultCtx): string {
|
|
|
|
|
switch (ast.kind) {
|
|
|
|
|
case 'literal':
|
|
|
|
|
if (ast.value.kind === 'string')
|
|
|
|
|
@ -19,80 +26,78 @@ export function compile(ast: AST, useStore = true, bound = new Set<string>(), to
|
|
|
|
|
throw new Error(`Cannot compile literal`); // of kind ${ast.value.kind}`);
|
|
|
|
|
|
|
|
|
|
case 'variable': {
|
|
|
|
|
if (bound.has(ast.name)) {
|
|
|
|
|
if (ctx.bound.has(ast.name)) {
|
|
|
|
|
return sanitizeName(ast.name);
|
|
|
|
|
}
|
|
|
|
|
return sanitize(ast.name, useStore, topLevel);
|
|
|
|
|
return sanitize(ast.name, ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'lambda': {
|
|
|
|
|
const newBound = new Set([...bound, ...ast.params]);
|
|
|
|
|
const newBound = new Set([...ctx.bound, ...ast.params]);
|
|
|
|
|
const newCtx = { ...ctx, bound: newBound };
|
|
|
|
|
const params = ast.params.map(sanitizeName).join(') => (');
|
|
|
|
|
const id = astIdCounter++;
|
|
|
|
|
astRegistry.set(id, ast);
|
|
|
|
|
return `Object.assign((${params}) => ${compile(ast.body, useStore, newBound, topLevel)}, { _astId: (${id}) })`;
|
|
|
|
|
return `Object.assign((${params}) => ${compile(ast.body, newCtx)}, { _astId: (${id}) })`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'apply':
|
|
|
|
|
// Constructor
|
|
|
|
|
if (ast.func.kind === 'constructor') {
|
|
|
|
|
const ctorName = ast.func.name;
|
|
|
|
|
const arg = compile(ast.args[0], useStore, bound, topLevel);
|
|
|
|
|
const arg = compile(ast.args[0], ctx);
|
|
|
|
|
return `({ _tag: "${ctorName}", _0: ${arg} })`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const args = ast.args.map(a => compile(a, useStore, bound, topLevel)).join(')(');
|
|
|
|
|
return `${compile(ast.func, useStore, bound, topLevel)}(${args})`;
|
|
|
|
|
const args = ast.args.map(a => compile(a, ctx)).join(')(');
|
|
|
|
|
return `${compile(ast.func, ctx)}(${args})`;
|
|
|
|
|
|
|
|
|
|
case 'record': {
|
|
|
|
|
const parts = ast.entries.map(entry =>
|
|
|
|
|
entry.kind === 'spread'
|
|
|
|
|
? `...${compile(entry.expr, useStore, bound, topLevel)}`
|
|
|
|
|
: `${sanitizeName(entry.key)}: ${compile(entry.value, useStore, bound, topLevel)}`
|
|
|
|
|
? `...${compile(entry.expr, ctx)}`
|
|
|
|
|
: `${sanitizeName(entry.key)}: ${compile(entry.value, ctx)}`
|
|
|
|
|
)
|
|
|
|
|
return `({${parts.join(', ')}})`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'list': {
|
|
|
|
|
const elements = ast.elements.map(e =>
|
|
|
|
|
'spread' in e ? `...${compile(e.spread, useStore, bound, topLevel)}` : compile(e, useStore, bound, topLevel)
|
|
|
|
|
'spread' in e ? `...${compile(e.spread, ctx)}` : compile(e, ctx)
|
|
|
|
|
);
|
|
|
|
|
return `[${elements.join(', ')}]`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'record-access':
|
|
|
|
|
return `${compile(ast.record, useStore, bound, topLevel)}.${sanitizeName(ast.field)}`;
|
|
|
|
|
return `${compile(ast.record, ctx)}.${sanitizeName(ast.field)}`;
|
|
|
|
|
|
|
|
|
|
case 'record-update':
|
|
|
|
|
const updates = Object.entries(ast.updates)
|
|
|
|
|
.map(([k, v]) => `${sanitizeName(k)}: ${compile(v, useStore, bound, topLevel)}`);
|
|
|
|
|
return `({...${compile(ast.record, useStore, bound, topLevel)}, ${updates.join(', ')}})`;
|
|
|
|
|
.map(([k, v]) => `${sanitizeName(k)}: ${compile(v, ctx)}`);
|
|
|
|
|
return `({...${compile(ast.record, ctx)}, ${updates.join(', ')}})`;
|
|
|
|
|
|
|
|
|
|
case 'let':
|
|
|
|
|
const newBound = new Set([...bound, ast.name]);
|
|
|
|
|
return `(() => { let ${sanitizeName(ast.name)} = ${compile(ast.value, useStore, newBound, topLevel)};
|
|
|
|
|
return ${compile(ast.body, useStore, newBound, topLevel)}; })()`;
|
|
|
|
|
case 'let': {
|
|
|
|
|
const newBound = new Set([...ctx.bound, ast.name]);
|
|
|
|
|
const newCtx = { ...ctx, bound: newBound };
|
|
|
|
|
return `(() => { let ${sanitizeName(ast.name)} = ${compile(ast.value, newCtx)};
|
|
|
|
|
return ${compile(ast.body, newCtx)}; })()`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'match':
|
|
|
|
|
return compileMatch(ast, useStore, bound, topLevel);
|
|
|
|
|
return compileMatch(ast, ctx);
|
|
|
|
|
|
|
|
|
|
case 'constructor':
|
|
|
|
|
return `({ _tag: "${ast.name}" })`;
|
|
|
|
|
/*
|
|
|
|
|
return `((arg) => arg && typeof arg === 'object' && !arg._tag
|
|
|
|
|
? { _tag: "${ast.name}", ...arg }
|
|
|
|
|
: { _tag: "${ast.name}", _0: arg })`;
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
case 'rebind': {
|
|
|
|
|
const rootName = getRootName(ast.target);
|
|
|
|
|
const path = getPath(ast.target);
|
|
|
|
|
const value = compile(ast.value, useStore, bound, topLevel);
|
|
|
|
|
const value = compile(ast.value, ctx);
|
|
|
|
|
|
|
|
|
|
if (!rootName) throw new Error('Rebind target must be a variable');
|
|
|
|
|
|
|
|
|
|
if (bound.has(rootName)) {
|
|
|
|
|
const target = compile(ast.target, useStore, bound, topLevel);
|
|
|
|
|
if (ctx.bound.has(rootName)) {
|
|
|
|
|
const target = compile(ast.target, ctx);
|
|
|
|
|
return `(() => { ${target} = ${value}; return { _tag: "NoOp" }; })()`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -109,7 +114,7 @@ export function compile(ast: AST, useStore = true, bound = new Set<string>(), to
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sanitize(name: string, useStore = true, topLevel: Set<string>): string {
|
|
|
|
|
function sanitize(name: string, { useStore, topLevel }: CompileCtx): string {
|
|
|
|
|
if (!useStore && topLevel.has(name)) return sanitizeName(name);
|
|
|
|
|
return `store[${JSON.stringify(name)}]`
|
|
|
|
|
}
|
|
|
|
|
@ -128,8 +133,8 @@ function sanitizeName(name: string): string {
|
|
|
|
|
return name.replace(/-/g, '_');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function compileMatch(ast: AST & { kind: 'match'}, useStore = true, bound = new Set<string>(), topLevel = new Set<string>()): string {
|
|
|
|
|
const expr = compile(ast.expr, useStore, bound, topLevel);
|
|
|
|
|
function compileMatch(ast: AST & { kind: 'match'}, ctx: CompileCtx): string {
|
|
|
|
|
const expr = compile(ast.expr, ctx);
|
|
|
|
|
const tmpVar = `_m${matchCounter++}`;
|
|
|
|
|
|
|
|
|
|
let code = `((${tmpVar}) => { `;
|
|
|
|
|
@ -137,12 +142,13 @@ function compileMatch(ast: AST & { kind: 'match'}, useStore = true, bound = new
|
|
|
|
|
for (const c of ast.cases) {
|
|
|
|
|
const { condition, bindings } = compilePattern(c.pattern, tmpVar);
|
|
|
|
|
const patternBound = patternVars(c.pattern);
|
|
|
|
|
const newBound = new Set([...bound, ...patternBound]);
|
|
|
|
|
const newBound = new Set([...ctx.bound, ...patternBound]);
|
|
|
|
|
const newCtx = { ...ctx, bound: newBound };
|
|
|
|
|
code += `if (${condition}) { `;
|
|
|
|
|
if (bindings.length > 0) {
|
|
|
|
|
code += `const ${bindings.join(', ')}; `;
|
|
|
|
|
}
|
|
|
|
|
code += `return ${compile(c.result, useStore, newBound, topLevel)}; }`;
|
|
|
|
|
code += `return ${compile(c.result, newCtx)}; }`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
code += `console.error("No match for:", ${tmpVar}); throw new Error("No match"); })(${expr})`;
|
|
|
|
|
@ -238,7 +244,8 @@ export function compileAndRun(defs: Definition[]) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const def of defs) {
|
|
|
|
|
const compiled = `const ${sanitizeName(def.name)} = ${compile(def.body, false, new Set(), topLevel)};`;
|
|
|
|
|
const ctx: CompileCtx = { useStore: false, topLevel, bound: new Set() };
|
|
|
|
|
const compiled = `const ${sanitizeName(def.name)} = ${compile(def.body, ctx)};`;
|
|
|
|
|
compiledDefs.push(compiled);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|