X Tutup
Skip to content

Commit e8b55ae

Browse files
DoctorGesterPerryvw
authored andcommitted
Fixed for loops without initializer, condition or post increment (#314)
* Fixed for loops without initializer, conidition or post increment * Getting away from code police * Fixed tests
1 parent b4366cd commit e8b55ae

File tree

7 files changed

+90
-11
lines changed

7 files changed

+90
-11
lines changed

src/Transpiler.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,15 +542,24 @@ export abstract class LuaTranspiler {
542542
public transpileFor(node: ts.ForStatement): string {
543543
// Add header
544544
let result = "";
545-
for (const variableDeclaration of (node.initializer as ts.VariableDeclarationList).declarations) {
546-
result += this.indent + this.transpileVariableDeclaration(variableDeclaration) + "\n";
545+
546+
if (node.initializer) {
547+
for (const variableDeclaration of (node.initializer as ts.VariableDeclarationList).declarations) {
548+
result += this.indent + this.transpileVariableDeclaration(variableDeclaration) + "\n";
549+
}
547550
}
548-
result += this.indent + `while(${this.transpileExpression(node.condition)}) do\n`;
551+
552+
const conditionText = node.condition ? this.transpileExpression(node.condition) : "true";
553+
result += this.indent + `while (${conditionText}) do\n`;
549554

550555
// Add body
551556
this.pushIndent();
552557
result += this.transpileLoopBody(node);
553-
result += this.indent + this.transpileExpression(node.incrementor) + "\n";
558+
559+
if (node.incrementor) {
560+
result += this.indent + this.transpileExpression(node.incrementor) + "\n";
561+
}
562+
554563
this.popIndent();
555564

556565
result += this.indent + "end\n";

test/translation/lua/continue.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local i = 0
2-
while(i<10) do
2+
while (i<10) do
33
do
44
if i<5 then
55
goto __continue0

test/translation/lua/continueConcurrent.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local i = 0
2-
while(i<10) do
2+
while (i<10) do
33
do
44
if i<5 then
55
goto __continue0

test/translation/lua/continueNested.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
local i = 0
2-
while(i<5) do
2+
while (i<5) do
33
do
44
if (i%2)==0 then
55
goto __continue0
66
end
77
local j = 0
8-
while(j<2) do
8+
while (j<2) do
99
do
1010
if j==1 then
1111
goto __continue1

test/translation/lua/continueNestedConcurrent.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
local i = 0
2-
while(i<5) do
2+
while (i<5) do
33
do
44
if (i%2)==0 then
55
goto __continue0
66
end
77
local j = 0
8-
while(j<2) do
8+
while (j<2) do
99
do
1010
if j==1 then
1111
goto __continue1

test/translation/lua/for.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local i = 1
2-
while(i<=100) do
2+
while (i<=100) do
33
do
44
end
55
::__continue0::

test/unit/loops.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,76 @@ export class LuaLoopTests {
183183
Expect(result).toBe(JSON.stringify(expected));
184184
}
185185

186+
@TestCase([0, 1, 2, 3], [1, 2, 3, 4])
187+
@Test("forNoDeclarations")
188+
public forNoDeclarations(inp: number[], expected: number[]): void {
189+
// Transpile
190+
const lua = util.transpileString(
191+
`let arrTest = ${JSON.stringify(inp)};
192+
let i = 0;
193+
for (; i < arrTest.length; ++i) {
194+
arrTest[i] = arrTest[i] + 1;
195+
}
196+
return JSONStringify(arrTest);`
197+
);
198+
199+
// Execute
200+
const result = util.executeLua(lua);
201+
202+
// Assert
203+
Expect(result).toBe(JSON.stringify(expected));
204+
}
205+
206+
@TestCase([0, 1, 2, 3], [1, 2, 3, 4])
207+
@Test("forNoCondition")
208+
public forNoCondition(inp: number[], expected: number[]): void {
209+
// Transpile
210+
const lua = util.transpileString(
211+
`let arrTest = ${JSON.stringify(inp)};
212+
let i = 0;
213+
for (;; ++i) {
214+
if (i >= arrTest.length) {
215+
break;
216+
}
217+
218+
arrTest[i] = arrTest[i] + 1;
219+
}
220+
return JSONStringify(arrTest);`
221+
);
222+
223+
// Execute
224+
const result = util.executeLua(lua);
225+
226+
// Assert
227+
Expect(result).toBe(JSON.stringify(expected));
228+
}
229+
230+
@TestCase([0, 1, 2, 3], [1, 2, 3, 4])
231+
@Test("forNoPostExpression")
232+
public forNoPostExpression(inp: number[], expected: number[]): void {
233+
// Transpile
234+
const lua = util.transpileString(
235+
`let arrTest = ${JSON.stringify(inp)};
236+
let i = 0;
237+
for (;;) {
238+
if (i >= arrTest.length) {
239+
break;
240+
}
241+
242+
arrTest[i] = arrTest[i] + 1;
243+
244+
i++;
245+
}
246+
return JSONStringify(arrTest);`
247+
);
248+
249+
// Execute
250+
const result = util.executeLua(lua);
251+
252+
// Assert
253+
Expect(result).toBe(JSON.stringify(expected));
254+
}
255+
186256
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; i < arrTest.length; i++")
187257
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; i <= arrTest.length - 1; i++")
188258
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; arrTest.length > i; i++")

0 commit comments

Comments
 (0)
X Tutup