X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 1 addition & 69 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,75 +615,7 @@ export abstract class LuaTranspiler {
}

public transpileSwitch(node: ts.SwitchStatement): string {
const expression = this.transpileExpression(node.expression, true);
const clauses = node.caseBlock.clauses;

let result = this.indent + "-------Switch statement start-------\n";

const switchVarName = "____switch" + this.genVarCounter;
this.genVarCounter++;

result += this.indent + `local ${switchVarName} = ${expression}\n`;

let hasDefaultClause = false;

// If statement to go to right entry label
clauses.forEach((clause, index) => {
if (ts.isCaseClause(clause)) {
result += this.indent +
`if ${this.transpileExpression(clause.expression, true)} == ${switchVarName} then\n`;

this.pushIndent();
result += this.indent + `goto ${switchVarName}_case_${index}\n`;
this.popIndent();

result += this.indent + "end\n";
} else if (ts.isDefaultClause(clause)) {
hasDefaultClause = true;
}
});

result += "\n";

// If no case condition is matched jump to end or default immediately
if (hasDefaultClause) {
result += this.indent + `goto ${switchVarName}_default\n`;
} else {
result += this.indent + `goto ${switchVarName}_end\n`;
}

result += "\n";

const transpileClauseBody = (clause: ts.CaseOrDefaultClause) => {
this.transpilingSwitch++;
result += this.indent + "do\n";
this.pushIndent();
result += this.transpileBlock(ts.createBlock(clause.statements));
this.popIndent();
result += this.indent + "end\n";
this.transpilingSwitch--;
};

clauses.forEach((clause, index) => {
if (ts.isCaseClause(clause)) {
result += this.indent + `::${switchVarName}_case_${index}::\n`;

transpileClauseBody(clause);

if (tsHelper.containsStatement(clause.statements, ts.SyntaxKind.BreakStatement)) {
result += this.indent + `goto ${switchVarName}_end\n`;
}
} else if (ts.isDefaultClause(clause)) {
result += this.indent + `::${switchVarName}_default::\n`;

transpileClauseBody(clause);
}
});

result += this.indent + `::${switchVarName}_end::\n`;
result += this.indent + "-------Switch statement end-------\n";

return result;
throw TSTLErrors.UnsupportedForTarget("Switch statements", this.options.luaTarget, node);
}

public transpileTry(node: ts.TryStatement): string {
Expand Down
73 changes: 73 additions & 0 deletions src/targets/Transpiler.52.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,79 @@ export class LuaTranspiler52 extends LuaTranspiler51 {
}
}

/** @override */
public transpileSwitch(node: ts.SwitchStatement): string {
const expression = this.transpileExpression(node.expression, true);
const clauses = node.caseBlock.clauses;

let result = this.indent + "-------Switch statement start-------\n";

const switchVarName = "____switch" + this.genVarCounter;
this.genVarCounter++;

result += this.indent + `local ${switchVarName} = ${expression}\n`;

let hasDefaultClause = false;

// If statement to go to right entry label
clauses.forEach((clause, index) => {
if (ts.isCaseClause(clause)) {
result += this.indent +
`if ${this.transpileExpression(clause.expression, true)} == ${switchVarName} then\n`;

this.pushIndent();
result += this.indent + `goto ${switchVarName}_case_${index}\n`;
this.popIndent();

result += this.indent + "end\n";
} else if (ts.isDefaultClause(clause)) {
hasDefaultClause = true;
}
});

result += "\n";

// If no case condition is matched jump to end or default immediately
if (hasDefaultClause) {
result += this.indent + `goto ${switchVarName}_default\n`;
} else {
result += this.indent + `goto ${switchVarName}_end\n`;
}

result += "\n";

const transpileClauseBody = (clause: ts.CaseOrDefaultClause) => {
this.transpilingSwitch++;
result += this.indent + "do\n";
this.pushIndent();
result += this.transpileBlock(ts.createBlock(clause.statements));
this.popIndent();
result += this.indent + "end\n";
this.transpilingSwitch--;
};

clauses.forEach((clause, index) => {
if (ts.isCaseClause(clause)) {
result += this.indent + `::${switchVarName}_case_${index}::\n`;

transpileClauseBody(clause);

if (tsHelper.containsStatement(clause.statements, ts.SyntaxKind.BreakStatement)) {
result += this.indent + `goto ${switchVarName}_end\n`;
}
} else if (ts.isDefaultClause(clause)) {
result += this.indent + `::${switchVarName}_default::\n`;

transpileClauseBody(clause);
}
});

result += this.indent + `::${switchVarName}_end::\n`;
result += this.indent + "-------Switch statement end-------\n";

return result;
}

/** @override */
public transpileDestructingAssignmentValue(node: ts.Expression): string {
return `table.unpack(${this.transpileExpression(node)})`;
Expand Down
8 changes: 8 additions & 0 deletions test/unit/conditionals.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Expect, Test, TestCase } from "alsatian";
import { TranspileError } from "../../src/Errors";
import { LuaTarget } from "../../src/Transpiler";
import * as util from "../src/util";

export class LuaConditionalsTests {
Expand Down Expand Up @@ -326,4 +328,10 @@ export class LuaConditionalsTests {

Expect(result).toBe(5);
}

@Test("switch not allowed in 5.1")
public switchThrow51(): void {
Expect( () => util.transpileString(`switch ("abc") {}`, {luaTarget: LuaTarget.Lua51}))
.toThrowError(TranspileError, "Switch statements is/are not supported for target Lua 5.1.");
}
}
X Tutup