Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten block after replacement causes nesting #266

Merged
merged 2 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions src/common/Spine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type IR, isOp, op } from "../IR";
import { type IR, isOp, op, isOfKind, block } from "../IR";
import { type CompilationContext } from "./compile";
import { getChild, getChildFragments, type PathFragment } from "./fragments";
import { replaceAtIndex } from "./arrays";
Expand Down Expand Up @@ -70,26 +70,29 @@ export class Spine<N extends IR.Node = IR.Node> {
if (this.parent === null || this.pathFragment === null) {
return new Spine(newNode, null, null);
}
if (newNode.kind === "Block" && this.parent.node.kind === "Block") {
throw new Error(
`Programming error: attempt to insert a Block into a Block`,
);
}
const parentNode = this.parent.node;
const parent =
canonizeAndReturnRoot &&
isOp()(parentNode) &&
isOfKind("Op", "Block")(parentNode) &&
typeof this.pathFragment === "object"
? this.parent.replacedWith(
{
...op(
parentNode.op,
...replaceAtIndex(
parentNode.args,
this.pathFragment.index,
newNode,
),
),
...(isOp()(parentNode)
? op(
parentNode.op,
...replaceAtIndex(
parentNode.args,
this.pathFragment.index,
newNode,
),
)
: block(
replaceAtIndex(
parentNode.children,
this.pathFragment.index,
newNode,
),
)),
targetType: parentNode.targetType,
},
true,
Expand Down Expand Up @@ -144,8 +147,8 @@ export class Spine<N extends IR.Node = IR.Node> {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let curr = this as Spine;
// recurse on children
if (isOp()(this.node)) {
// Create canonical Op instead of just replacing the chidren
if (isOfKind("Op", "Block")(this.node)) {
// Create canonical Op / block instead of just replacing the chidren
const newChildren: IR.Node[] = [];
let someChildrenIsNew = false;
for (const child of this.getChildSpines()) {
Expand All @@ -155,7 +158,9 @@ export class Spine<N extends IR.Node = IR.Node> {
}
if (someChildrenIsNew)
curr = curr.replacedWith({
...op(this.node.op, ...newChildren),
...(isOp()(this.node)
? op(this.node.op, ...newChildren)
: block(newChildren)),
targetType: this.node.targetType,
});
} else {
Expand Down
24 changes: 24 additions & 0 deletions src/plugins/loops.test.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ for_c_like ($i <- 0) ($i < 10) ($i <- (1 + $i)) (
);
```

## For range to while, inside a block

```polygolf
for $i 0 10 {
print_int $x;
};
for $j 1 11 {
print_int $y;
};
```

```polygolf loops.forRangeToWhile
$i <- 0;
while ($i < 10) {
print_int $x;
$i <- (1 + $i);
};
$j <- 1;
while ($j < 11) {
print_int $y;
$j <- (1 + $j);
};
```

## For each

```polygolf
Expand Down
Loading