Skip to content

Commit d7d314b

Browse files
committed
ForOf and ForIn loops
1 parent 9eb5609 commit d7d314b

2 files changed

Lines changed: 51 additions & 7 deletions

File tree

Transpiler.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export class LuaTranspiler {
4444
case ts.SyntaxKind.ReturnStatement:
4545
return indent + this.transpileReturn(<ts.ReturnStatement>node) + "\n";
4646
case ts.SyntaxKind.ForStatement:
47-
return this.transpileFor(<ts.ForStatement>node, indent) + "\n";
47+
return this.transpileFor(<ts.ForStatement>node, indent);
48+
case ts.SyntaxKind.ForOfStatement:
49+
return this.transpileForOf(<ts.ForOfStatement>node, indent);
50+
case ts.SyntaxKind.ForInStatement:
51+
return this.transpileForIn(<ts.ForInStatement>node, indent);
4852
case ts.SyntaxKind.EndOfFileToken:
4953
return "";
5054
default:
@@ -142,14 +146,47 @@ export class LuaTranspiler {
142146
let result = indent + `for ${identifier.escapedText}=${start},${end},${step} do\n`;
143147

144148
// Add body
145-
const block = tsEx.getFirstChildOfType<ts.Block>(node, ts.isBlock);
146-
result += this.transpileBlock(block, indent + " ");
149+
result += this.transpileBlock(node.statement, indent + " ");
150+
151+
return result + indent + "end\n";
152+
}
153+
154+
static transpileForOf(node: ts.ForOfStatement, indent: string): string {
155+
// Get variable identifier
156+
const variable = <ts.VariableDeclaration>(<ts.VariableDeclarationList>node.initializer).declarations[0];
157+
const identifier = <ts.Identifier>variable.name;
158+
159+
// Transpile expression
160+
const expression = this.transpileExpression(node.expression);
161+
162+
// Make header
163+
let result = indent + `for _, ${identifier.escapedText} in pairs(${expression}) do\n`;
164+
165+
// For body
166+
result += this.transpileBlock(node.statement, indent + " ");
167+
168+
return result + indent + "end\n";
169+
}
170+
171+
static transpileForIn(node: ts.ForInStatement, indent: string): string {
172+
// Get variable identifier
173+
const variable = <ts.VariableDeclaration>(<ts.VariableDeclarationList>node.initializer).declarations[0];
174+
const identifier = <ts.Identifier>variable.name;
175+
176+
// Transpile expression
177+
const expression = this.transpileExpression(node.expression);
178+
179+
// Make header
180+
let result = indent + `for ${identifier.escapedText}, _ in pairs(${expression}) do\n`;
181+
182+
// For body
183+
result += this.transpileBlock(node.statement, indent + " ");
147184

148185
return result + indent + "end\n";
149186
}
150187

151188
static transpileReturn(node: ts.ReturnStatement): string {
152-
return "return " + this.transpileExpression(tsEx.getChildren(node)[0]);
189+
return "return " + this.transpileExpression(node.expression);
153190
}
154191

155192
static transpileExpression(node: ts.Node): string {

test/test2.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class TestClass {
3737
}
3838
}
3939

40-
function Activate(a) {
40+
function Activate() {
4141
let test = function() { return ""; }
4242
for (let i = 0; i< 10; i++) {
4343
print(i);
@@ -59,11 +59,18 @@ function Activate(a) {
5959
i+=1;
6060
i-=1;
6161

62-
/*for (let i of [1,2,3]) {
62+
let list = [1,2,3];
6363

64+
for (let i of list) {
65+
print(i);
66+
}
67+
68+
for (let i of [1,2,3]) {
69+
print(i);
70+
print(i+1);
6471
}
6572

6673
for (let i in [1,2,3]) {
6774

68-
}*/
75+
}
6976
}

0 commit comments

Comments
 (0)