Skip to content

Commit

Permalink
implement for let-statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich committed Jun 12, 2024
1 parent e0050fa commit 22005cd
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/generator/writers/writeFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ export function writeStatement(
}
return;
} else if (f.kind === "statement_let") {
// Underscore name case
if (f.name === "_") {
ctx.append(`${writeExpression(f.expression, ctx)};`);
return;
}

// Contract/struct case
const t = resolveTypeRef(ctx.ctx, f.type);
if (t.kind === "ref") {
Expand Down
15 changes: 14 additions & 1 deletion src/test/e2e-emulated/contracts/underscore-variable.tact
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
contract UnderscoreVariableTestContract {
something: Int;

init() {
// Nothing to do
self.something = 0;
}

receive() {
// Nothing to do
}

fun increaseSomething(): Int {
self.something += 1;
return 123;
}

get fun test1(): Int {
try {
nativeThrow(1);
Expand Down Expand Up @@ -39,4 +46,10 @@ contract UnderscoreVariableTestContract {
}
return x;
}

get fun test4(): Int {
let _: Int = self.increaseSomething();
let _: Int = self.increaseSomething();
return self.something;
}
}
1 change: 1 addition & 0 deletions src/test/e2e-emulated/underscore-variable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ describe("underscore-variable", () => {
expect(await contract.getTest1()).toEqual(0n);
expect(await contract.getTest2()).toEqual(12n);
expect(await contract.getTest3()).toEqual(6n);
expect(await contract.getTest4()).toEqual(2n);
});
});
27 changes: 27 additions & 0 deletions src/types/__snapshots__/resolveStatements.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,16 @@ Line 7, col 14:
"
`;
exports[`resolveStatements should fail statements for var-underscore-name-access3 1`] = `
"<unknown>:9:12: Unable to resolve id _
Line 9, col 12:
8 | let _: Int = someImpureFunction();
> 9 | return _;
^
10 | }
"
`;
exports[`resolveStatements should resolve statements for contract-receiver-bounced 1`] = `
[
[
Expand Down Expand Up @@ -1618,3 +1628,20 @@ exports[`resolveStatements should resolve statements for var-underscore-name-in-
],
]
`;
exports[`resolveStatements should resolve statements for var-underscore-name-in-let 1`] = `
[
[
"123",
"Int",
],
[
"someImpureFunction()",
"Int",
],
[
"123",
"Int",
],
]
`;
4 changes: 3 additions & 1 deletion src/types/resolveStatements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ function processStatements(
if (sctx.vars.has(s.name)) {
throwError(`Variable "${s.name}" already exists`, s.ref);
}
sctx = addVariable(s.name, variableType, sctx);
if (s.name !== "_") {
sctx = addVariable(s.name, variableType, sctx);
}
} else if (s.kind === "statement_assign") {
// Process lvalue
ctx = resolveLValueRef(s.path, sctx, ctx);
Expand Down
10 changes: 10 additions & 0 deletions src/types/stmts-failed/var-underscore-name-access3.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
primitive Int;

fun someImpureFunction(): Int {
return 123;
}

fun test(): Int {
let _: Int = someImpureFunction();
return _;
}
10 changes: 10 additions & 0 deletions src/types/stmts/var-underscore-name-in-let.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
primitive Int;

fun someImpureFunction(): Int {
return 123;
}

fun test(): Int {
let _: Int = someImpureFunction();
return 123;
}

0 comments on commit 22005cd

Please sign in to comment.