X Tutup
Skip to content
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
17 changes: 13 additions & 4 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,15 +542,24 @@ export abstract class LuaTranspiler {
public transpileFor(node: ts.ForStatement): string {
// Add header
let result = "";
for (const variableDeclaration of (node.initializer as ts.VariableDeclarationList).declarations) {
result += this.indent + this.transpileVariableDeclaration(variableDeclaration) + "\n";

if (node.initializer) {
for (const variableDeclaration of (node.initializer as ts.VariableDeclarationList).declarations) {
result += this.indent + this.transpileVariableDeclaration(variableDeclaration) + "\n";
}
}
result += this.indent + `while(${this.transpileExpression(node.condition)}) do\n`;

const conditionText = node.condition ? this.transpileExpression(node.condition) : "true";
result += this.indent + `while (${conditionText}) do\n`;

// Add body
this.pushIndent();
result += this.transpileLoopBody(node);
result += this.indent + this.transpileExpression(node.incrementor) + "\n";

if (node.incrementor) {
result += this.indent + this.transpileExpression(node.incrementor) + "\n";
}

this.popIndent();

result += this.indent + "end\n";
Expand Down
2 changes: 1 addition & 1 deletion test/translation/lua/continue.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local i = 0
while(i<10) do
while (i<10) do
do
if i<5 then
goto __continue0
Expand Down
2 changes: 1 addition & 1 deletion test/translation/lua/continueConcurrent.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local i = 0
while(i<10) do
while (i<10) do
do
if i<5 then
goto __continue0
Expand Down
4 changes: 2 additions & 2 deletions test/translation/lua/continueNested.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local i = 0
while(i<5) do
while (i<5) do
do
if (i%2)==0 then
goto __continue0
end
local j = 0
while(j<2) do
while (j<2) do
do
if j==1 then
goto __continue1
Expand Down
4 changes: 2 additions & 2 deletions test/translation/lua/continueNestedConcurrent.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local i = 0
while(i<5) do
while (i<5) do
do
if (i%2)==0 then
goto __continue0
end
local j = 0
while(j<2) do
while (j<2) do
do
if j==1 then
goto __continue1
Expand Down
2 changes: 1 addition & 1 deletion test/translation/lua/for.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local i = 1
while(i<=100) do
while (i<=100) do
do
end
::__continue0::
Expand Down
70 changes: 70 additions & 0 deletions test/unit/loops.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,76 @@ export class LuaLoopTests {
Expect(result).toBe(JSON.stringify(expected));
}

@TestCase([0, 1, 2, 3], [1, 2, 3, 4])
@Test("forNoDeclarations")
public forNoDeclarations(inp: number[], expected: number[]): void {
// Transpile
const lua = util.transpileString(
`let arrTest = ${JSON.stringify(inp)};
let i = 0;
for (; i < arrTest.length; ++i) {
arrTest[i] = arrTest[i] + 1;
}
return JSONStringify(arrTest);`
);

// Execute
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(JSON.stringify(expected));
}

@TestCase([0, 1, 2, 3], [1, 2, 3, 4])
@Test("forNoCondition")
public forNoCondition(inp: number[], expected: number[]): void {
// Transpile
const lua = util.transpileString(
`let arrTest = ${JSON.stringify(inp)};
let i = 0;
for (;; ++i) {
if (i >= arrTest.length) {
break;
}

arrTest[i] = arrTest[i] + 1;
}
return JSONStringify(arrTest);`
);

// Execute
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(JSON.stringify(expected));
}

@TestCase([0, 1, 2, 3], [1, 2, 3, 4])
@Test("forNoPostExpression")
public forNoPostExpression(inp: number[], expected: number[]): void {
// Transpile
const lua = util.transpileString(
`let arrTest = ${JSON.stringify(inp)};
let i = 0;
for (;;) {
if (i >= arrTest.length) {
break;
}

arrTest[i] = arrTest[i] + 1;

i++;
}
return JSONStringify(arrTest);`
);

// Execute
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(JSON.stringify(expected));
}

@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; i < arrTest.length; i++")
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; i <= arrTest.length - 1; i++")
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; arrTest.length > i; i++")
Expand Down
X Tutup