Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Aug 19, 2024
1 parent 129dde2 commit 74d38b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
18 changes: 18 additions & 0 deletions src/compiler/compiler-babel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,24 @@ describe("lambda expressions", () => {
`"const Main$x = Main$f(() => 0, () => 1);"`,
);
});

test("do not let GEN values be shadowed", () => {
const out = compileSrc(`
type Box<a> { Box(a) }
let x = fn Box(a) {
fn Box(_) {
a
}
}
`);
expect(out).toMatchInlineSnapshot(`
"const Main$Box = _0 => ({
$: 0,
_0
});
const Main$x = GEN__0 => GEN__1 => GEN__0._0;"
`);
});
});

describe("if expressions", () => {
Expand Down
17 changes: 8 additions & 9 deletions src/compiler/compiler-babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class Compiler {
private frames: Frame[] = [];
private bindingsJsName = new WeakMap<Binding, t.Expression>();

private nextId = 0;
genFreshId(): string {
return `GEN__${this.nextId++}`;
}

constructor(private ns: string) {}

private getCurrentFrame(): Frame {
Expand Down Expand Up @@ -72,8 +77,7 @@ class Compiler {
}

private makeFreshIdent(): t.Identifier {
const curFrame = this.getCurrentFrame();
const name = this.makeJsLetPathName(curFrame.genFreshId());
const name = this.makeJsLetPathName(this.genFreshId());
return { type: "Identifier", name };
}

Expand Down Expand Up @@ -177,7 +181,7 @@ class Compiler {
src.pattern.name,
);
} else {
jsPatternName = this.getCurrentFrame().genFreshId();
jsPatternName = this.genFreshId();
}
this.frames.push(
new Frame({
Expand Down Expand Up @@ -229,7 +233,7 @@ class Compiler {
return ident;
}

const freshId = this.getCurrentFrame().genFreshId();
const freshId = this.genFreshId();
const ident: t.Identifier = {
type: "Identifier",
name: freshId,
Expand Down Expand Up @@ -758,18 +762,13 @@ class Frame {
) {}

private usedVars = new Map<string, number>();
private nextId = 0;

registerLocal(name: string): string {
const timesUsed = this.usedVars.get(name) ?? 0;
this.usedVars.set(name, timesUsed + 1);

return timesUsed === 0 ? name : `${name}$${timesUsed}`;
}

genFreshId(): string {
return `GEN__${this.nextId++}`;
}
}

function* reversed<T>(xs: T[]): Generator<T> {
Expand Down

0 comments on commit 74d38b8

Please sign in to comment.