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 11, 2024
1 parent 87b6f55 commit 2794628
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 8 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
1 change: 1 addition & 0 deletions src/test/feature-underscore-variable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ describe("feature-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);
});
});
15 changes: 14 additions & 1 deletion src/test/features/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;
}
}
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 @@ -570,6 +570,16 @@ Line 7, col 14:
"
`;
exports[`resolveStatements should fail statements for case-57 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 case-0 1`] = `
[
[
Expand Down Expand Up @@ -1581,6 +1591,23 @@ exports[`resolveStatements should resolve statements for case-23 1`] = `
`;
exports[`resolveStatements should resolve statements for case-24 1`] = `
[
[
"123",
"Int",
],
[
"someImpureFunction()",
"Int",
],
[
"123",
"Int",
],
]
`;
exports[`resolveStatements should resolve statements for case-25 1`] = `
[
[
"emptyMap()",
Expand Down
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/case-57.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 _;
}
12 changes: 6 additions & 6 deletions src/types/stmts/case-24.tact
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
primitive Int;

fun someImpureFunction(): Int {
return 123;
}

fun test(): Int {
let m: map<Int, Int> = emptyMap();
let x: Int = 0;
foreach (_, v in m) {
x += v;
}
return x;
let _: Int = someImpureFunction();
return 123;
}
10 changes: 10 additions & 0 deletions src/types/stmts/case-25.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
primitive Int;

fun test(): Int {
let m: map<Int, Int> = emptyMap();
let x: Int = 0;
foreach (_, v in m) {
x += v;
}
return x;
}

0 comments on commit 2794628

Please sign in to comment.