diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 0e6195df3..5ba9c4ef8 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -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"; diff --git a/test/translation/lua/continue.lua b/test/translation/lua/continue.lua index afb99a436..d622e235d 100644 --- a/test/translation/lua/continue.lua +++ b/test/translation/lua/continue.lua @@ -1,5 +1,5 @@ local i = 0 -while(i<10) do +while (i<10) do do if i<5 then goto __continue0 diff --git a/test/translation/lua/continueConcurrent.lua b/test/translation/lua/continueConcurrent.lua index 89196ef3b..251f95eea 100644 --- a/test/translation/lua/continueConcurrent.lua +++ b/test/translation/lua/continueConcurrent.lua @@ -1,5 +1,5 @@ local i = 0 -while(i<10) do +while (i<10) do do if i<5 then goto __continue0 diff --git a/test/translation/lua/continueNested.lua b/test/translation/lua/continueNested.lua index 0b4924745..73893c0e5 100644 --- a/test/translation/lua/continueNested.lua +++ b/test/translation/lua/continueNested.lua @@ -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 diff --git a/test/translation/lua/continueNestedConcurrent.lua b/test/translation/lua/continueNestedConcurrent.lua index 4f162668b..b9de6f526 100644 --- a/test/translation/lua/continueNestedConcurrent.lua +++ b/test/translation/lua/continueNestedConcurrent.lua @@ -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 diff --git a/test/translation/lua/for.lua b/test/translation/lua/for.lua index 86a2c18ec..f09fc0c47 100644 --- a/test/translation/lua/for.lua +++ b/test/translation/lua/for.lua @@ -1,5 +1,5 @@ local i = 1 -while(i<=100) do +while (i<=100) do do end ::__continue0:: diff --git a/test/unit/loops.spec.ts b/test/unit/loops.spec.ts index 4d6d6e985..3340ad8b7 100644 --- a/test/unit/loops.spec.ts +++ b/test/unit/loops.spec.ts @@ -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++")