From 55eae79ba496f992ca54f7a70951b021047a52eb Mon Sep 17 00:00:00 2001 From: doctorgester Date: Thu, 27 Dec 2018 03:06:45 +0300 Subject: [PATCH 1/3] Fixed for loops without initializer, conidition or post increment --- src/Transpiler.ts | 17 +++++++--- test/unit/loops.spec.ts | 70 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 40f54c517..075c9501a 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/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++") From 3dc0f538bf7becf5357fc509a73e57a579141d59 Mon Sep 17 00:00:00 2001 From: Kirill Artemov Date: Thu, 27 Dec 2018 12:32:27 +0300 Subject: [PATCH 2/3] Getting away from code police --- src/Transpiler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 075c9501a..0c7e76cca 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -550,7 +550,7 @@ export abstract class LuaTranspiler { } const conditionText = node.condition ? this.transpileExpression(node.condition) : "true"; - result += this.indent + `while(${conditionText}) do\n`; + result += this.indent + `while (${conditionText}) do\n`; // Add body this.pushIndent(); From 2d19d27da69bb4869809bcea65ab3bb10508a922 Mon Sep 17 00:00:00 2001 From: doctorgester Date: Fri, 28 Dec 2018 03:13:40 +0300 Subject: [PATCH 3/3] Fixed tests --- test/translation/lua/continue.lua | 2 +- test/translation/lua/continueConcurrent.lua | 2 +- test/translation/lua/continueNested.lua | 4 ++-- test/translation/lua/continueNestedConcurrent.lua | 4 ++-- test/translation/lua/for.lua | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) 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::