Skip to content

Commit

Permalink
Allow assignment to undefined to reset a variable value
Browse files Browse the repository at this point in the history
  • Loading branch information
quisar committed Dec 28, 2023
1 parent 5bc1a7e commit 811541a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,18 @@ class Compiler {
}
}
} else if (ts.isIdentifier(e)) {
if (e.text == "undefined") {
if (dest) {
this.#emit(methods.setReg, "nil", this.ref(dest, 1, "w"));
} else {
return {
refs: [],
name: "",
reg: "nil",
};
}
return dest;
}
if (e.text == "self" && !this.currentScope.scope.has(e.text)) {
const v = this.variable(e);
this.#emit(methods.getSelf, v);
Expand Down
8 changes: 8 additions & 0 deletions tests/__snapshots__/compile.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,11 @@ l10:
l11:
.ret "
`;

exports[`assign_to_undefined.ts 1`] = `
"foo:
.name "foo"
.pname p1, v
set_reg nil, p1
.ret "
`
3 changes: 3 additions & 0 deletions tests/assign_to_undefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo(v:Value) {
v = undefined;
}

0 comments on commit 811541a

Please sign in to comment.