Skip to content

Commit

Permalink
feat: rework destructuring to support mapping of fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich committed Oct 8, 2024
1 parent a0c737b commit 6c3ba88
Show file tree
Hide file tree
Showing 13 changed files with 456 additions and 141 deletions.
16 changes: 15 additions & 1 deletion src/generator/writers/writeFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,22 @@ export function writeStatement(
return;
}
case "statement_destruct": {
const t = getExpType(ctx.ctx, f.expression);
if (t.kind !== "ref") {
throwInternalCompilerError(
`invalid destruct expression kind: ${t.kind}`,
f.expression.loc,
);
}
const ty = getType(ctx.ctx, t.name);
const ids = ty.fields.map((field) => {
const id = f.identifiers.find(
(item) => item.from.text === field.name,
);
return id ? funcIdOf(id.name) : "_";
});
ctx.append(
`var (${f.identifiers.map((n) => funcIdOf(n)).join(", ")}) = ${writeCastedExpression(f.expression, getExpType(ctx.ctx, f.expression), ctx)};`,
`var (${ids.join(", ")}) = ${writeCastedExpression(f.expression, t, ctx)};`,
);
return;
}
Expand Down
Loading

0 comments on commit 6c3ba88

Please sign in to comment.