Refactor withCapture

This commit is contained in:
Tony Garnock-Jones 2021-03-18 22:41:27 +01:00
parent d372977023
commit c8b752a73b
2 changed files with 15 additions and 13 deletions

View File

@ -103,19 +103,22 @@ export class FunctionContext {
... items); ... items);
} }
withCapture(fieldName: string | undefined, withCapture<R>(
sourceExpr: string, fieldName: string | undefined, sourceExpr: string, ks: (sourceExpr: string) => R): R
ks: (sourceExpr: string) => Item[],
withCheck = true): Item
{ {
if (fieldName !== void 0) this.captures.push({ fieldName, sourceExpr }); if (fieldName !== void 0) this.captures.push({ fieldName, sourceExpr });
const result = withCheck const result = ks(sourceExpr);
? seq(`if (${sourceExpr} !== void 0) `, this.block(() => ks(sourceExpr)))
: block(... ks(sourceExpr));
if (fieldName !== void 0) this.captures.pop(); if (fieldName !== void 0) this.captures.pop();
return result; return result;
} }
convertCapture(
fieldName: string | undefined, sourceExpr: string, ks: (sourceExpr: string) => Item[]): Item
{
return this.withCapture(fieldName, sourceExpr, sourceExpr =>
seq(`if (${sourceExpr} !== void 0) `, this.block(() => ks(sourceExpr))));
}
buildCapturedCompound(dest: string): Item { buildCapturedCompound(dest: string): Item {
return seq(`${dest} = `, braces( return seq(`${dest} = `, braces(
... variantInitFor(this.variantName), ... variantInitFor(this.variantName),

View File

@ -47,8 +47,7 @@ function converterForAlternative(
} else if (ctx.variantName !== void 0) { } else if (ctx.variantName !== void 0) {
return [ctx.withCapture('value', return [ctx.withCapture('value',
simpleValue, simpleValue,
() => [ctx.buildCapturedCompound(dest)], () => ctx.buildCapturedCompound(dest))];
false)];
} else { } else {
return [`${dest} = ${simpleValue}`]; return [`${dest} = ${simpleValue}`];
} }
@ -102,7 +101,7 @@ function converterForArray(ctx: FunctionContext,
... converterFor(ctx, arrayType, v, vv => [`${r}.push(${vv})`, `continue`]), ... converterFor(ctx, arrayType, v, vv => [`${r}.push(${vv})`, `continue`]),
seq(`${r} = void 0`), seq(`${r} = void 0`),
seq(`break`)])), seq(`break`)])),
ctx.withCapture(M.nameFor(arrayType), r, k)]; ctx.convertCapture(M.nameFor(arrayType), r, k)];
}; };
return (checkArray return (checkArray
? seq(`if (_.Array.isArray(${src})) `, ctx.block(postCheck)) ? seq(`if (_.Array.isArray(${src})) `, ctx.block(postCheck))
@ -122,7 +121,7 @@ function converterFor(
if (M.isSimplePattern(p)) { if (M.isSimplePattern(p)) {
const dest = ctx.gentemp(typeFor(ctx.mod, p), ` | undefined`); const dest = ctx.gentemp(typeFor(ctx.mod, p), ` | undefined`);
return [... converterForSimple(ctx, p, src, dest), return [... converterForSimple(ctx, p, src, dest),
ctx.withCapture(maybeName, dest, ks)]; ctx.convertCapture(maybeName, dest, ks)];
} else { } else {
switch (p.label) { switch (p.label) {
case M.$setof: { case M.$setof: {
@ -137,7 +136,7 @@ function converterFor(
[`${r}.add(${vv})`, `continue`]), [`${r}.add(${vv})`, `continue`]),
seq(`${r} = void 0`), seq(`${r} = void 0`),
seq(`break`)])), seq(`break`)])),
ctx.withCapture(maybeName, r, ks)]))]; ctx.convertCapture(maybeName, r, ks)]))];
} }
case M.$dictof: { case M.$dictof: {
const keyPattern = p[0]; const keyPattern = p[0];
@ -154,7 +153,7 @@ function converterFor(
[`${r}.set(${kk}, ${vv})`, `continue`])), [`${r}.set(${kk}, ${vv})`, `continue`])),
seq(`${r} = void 0`), seq(`${r} = void 0`),
seq(`break`)])), seq(`break`)])),
ctx.withCapture(maybeName, r, ks)]))]; ctx.convertCapture(maybeName, r, ks)]))];
} }
default: { default: {
const arrayType = M.simpleArray(p); const arrayType = M.simpleArray(p);