Skip to content

Commit 7ae1e79

Browse files
committed
Using ipairs and proper indexing for array types
1 parent 61a3c52 commit 7ae1e79

3 files changed

Lines changed: 48 additions & 15 deletions

File tree

Compiler.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ import {LuaTranspiler} from "./Transpiler";
55
import {TSHelper as tsEx} from "./TSHelper";
66

77
function compile(fileNames: string[], options: ts.CompilerOptions): void {
8-
fileNames.forEach(fileName => {
9-
const sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES2017);
10-
11-
// Print AST for debugging
12-
printAST(sourceFile, 0);
13-
14-
// Transpile AST
15-
console.log(LuaTranspiler.transpileSourceFile(sourceFile));
8+
let program = ts.createProgram(fileNames, options);
9+
let checker = program.getTypeChecker();
10+
11+
program.getSourceFiles().forEach(sourceFile => {
12+
if (!sourceFile.isDeclarationFile) {
13+
// Print AST for debugging
14+
//printAST(sourceFile, 0);
15+
16+
// Transpile AST
17+
let lua = LuaTranspiler.transpileSourceFile(sourceFile, checker);
18+
console.log(lua);
19+
}
1620
});
1721

1822
process.exit();

Transpiler.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ export class TranspileError extends Error {
1313

1414
export class LuaTranspiler {
1515
// Transpile a source file
16-
static transpileSourceFile(node: ts.SourceFile): string {
17-
let transpiler = new LuaTranspiler();
16+
static transpileSourceFile(node: ts.SourceFile, checker: ts.TypeChecker): string {
17+
let transpiler = new LuaTranspiler(checker);
1818
return transpiler.transpileBlock(node);
1919
}
2020

2121
indent: string;
22+
checker: ts.TypeChecker;
2223

23-
constructor() {
24+
constructor(checker: ts.TypeChecker) {
2425
this.indent = "";
26+
this.checker = checker;
2527
}
2628

2729
pushIndent(): void {
@@ -138,8 +140,12 @@ export class LuaTranspiler {
138140
// Transpile expression
139141
const expression = this.transpileExpression(node.expression);
140142

143+
// Use ipairs for array types, pairs otherwise
144+
const isArray = this.checker.getTypeAtLocation(node.expression).symbol.escapedName == "Array";
145+
const pairs = isArray ? "ipairs" : "pairs";
146+
141147
// Make header
142-
let result = this.indent + `for _, ${identifier.escapedText} in pairs(${expression}) do\n`;
148+
let result = this.indent + `for _, ${identifier.escapedText} in ${pairs}(${expression}) do\n`;
143149

144150
// For body
145151
this.pushIndent();
@@ -157,8 +163,12 @@ export class LuaTranspiler {
157163
// Transpile expression
158164
const expression = this.transpileExpression(node.expression);
159165

166+
// Use ipairs for array types, pairs otherwise
167+
const isArray = this.checker.getTypeAtLocation(node.expression).symbol.escapedName == "Array";
168+
const pairs = isArray ? "ipairs" : "pairs";
169+
160170
// Make header
161-
let result = this.indent + `for ${identifier.escapedText}, _ in pairs(${expression}) do\n`;
171+
let result = this.indent + `for ${identifier.escapedText}, _ in ${pairs}(${expression}) do\n`;
162172

163173
// For body
164174
this.pushIndent();
@@ -329,7 +339,13 @@ export class LuaTranspiler {
329339
const element = this.transpileExpression(node.expression);
330340
const index = this.transpileExpression(node.argumentExpression);
331341

332-
return `${element}[${index}]`;
342+
const isArray = this.checker.getTypeAtLocation(node.expression).symbol.escapedName == "Array";
343+
344+
if (isArray) {
345+
return `${element}[${index}+1]`;
346+
} else {
347+
return `${element}[${index}]`;
348+
}
333349
}
334350

335351
// Transpile a variable statement
@@ -503,6 +519,6 @@ export class LuaTranspiler {
503519
this.pushIndent();
504520
result += this.transpileBlock(node.body);
505521
this.popIndent();
506-
return result + this.indent + "end\n";
522+
return result + this.indent + "end ";
507523
}
508524
}

test/test2.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,23 @@ function Activate() {
6161
let a = 24 & 4;
6262

6363
let list = [1,2,3];
64+
let obj = {a: 3};
6465
for (let i = 0; i < list.length; i++) {
6566
print(list[i]);
6667
}
6768

69+
for (var b of [1,2,3]) {
70+
71+
}
72+
73+
for (var c of list) {
74+
75+
}
76+
77+
for (var d in obj) {
78+
79+
}
80+
6881
if (i == 3 && i < 3) {
6982
print(4);
7083
} else {

0 commit comments

Comments
 (0)