Skip to content

Commit 638a0d1

Browse files
committed
Fixed switches
1 parent 8f692fd commit 638a0d1

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/Transpiler.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -383,19 +383,17 @@ export class LuaTranspiler {
383383
let jumpTableName = "____switch" + this.genVarCounter;
384384
this.genVarCounter++;
385385

386-
result += this.indent + `local ${jumpTableName} = {\n`;
387-
388-
this.pushIndent();
386+
result += this.indent + `local ${jumpTableName} = {}\n`;
389387

390388
// If statement to go to right entry label
391389
clauses.forEach((clause, index) => {
392390
if (ts.isCaseClause(clause)) {
393391
result += this.indent + `-- case:\n`;
394-
result += this.indent + `[${this.transpileExpression(clause.expression, true)}] = function(self)\n`;
392+
result += this.indent + `${jumpTableName}[${this.transpileExpression(clause.expression, true)}] = function()\n`;
395393
}
396394
if (ts.isDefaultClause(clause)) {
397395
result += this.indent + `-- default:\n`;
398-
result += this.indent + `["____default${this.genVarCounter}"] = function(self)\n`;
396+
result += this.indent + `${jumpTableName}["____default${this.genVarCounter}"] = function()\n`;
399397
}
400398
this.pushIndent();
401399

@@ -418,9 +416,9 @@ export class LuaTranspiler {
418416

419417
if (i !== index && nextClause) {
420418
if (ts.isCaseClause(nextClause)) {
421-
result += this.indent + `self[${this.transpileExpression(nextClause.expression, true)}]()\n`;
419+
result += this.indent + `${jumpTableName}[${this.transpileExpression(nextClause.expression, true)}]()\n`;
422420
} else {
423-
result += this.indent + `self["____default${this.genVarCounter}"]()\n`;
421+
result += this.indent + `${jumpTableName}["____default${this.genVarCounter}"]()\n`;
424422
}
425423
}
426424
} else {
@@ -429,12 +427,10 @@ export class LuaTranspiler {
429427

430428
this.popIndent();
431429

432-
result += this.indent + `end,\n`;
430+
result += this.indent + `end\n`;
433431
});
434-
this.popIndent();
435-
result += this.indent + "}\n";
436-
result += this.indent + `if ${jumpTableName}[${expression}] then ${jumpTableName}[${expression}](${jumpTableName})\n`;
437-
result += this.indent + `elseif ${jumpTableName}["____default${this.genVarCounter}"] then ${jumpTableName}["____default${this.genVarCounter}"](${jumpTableName}) end\n`;
432+
result += this.indent + `if ${jumpTableName}[${expression}] then ${jumpTableName}[${expression}]()\n`;
433+
result += this.indent + `elseif ${jumpTableName}["____default${this.genVarCounter}"] then ${jumpTableName}["____default${this.genVarCounter}"]() end\n`;
438434
result += this.indent + "--------Switch statement end--------\n";
439435

440436
//Increment counter for next switch statement

0 commit comments

Comments
 (0)