X Tutup
Skip to content

Commit bab8df3

Browse files
lollekoPerryvw
authored andcommitted
Moved transpile switch to Lua 5.2 (#259)
* Moved transpile switch to Lua 5.2 Closes #194 * Fixed typo
1 parent 3054adb commit bab8df3

File tree

3 files changed

+82
-69
lines changed

3 files changed

+82
-69
lines changed

src/Transpiler.ts

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -615,75 +615,7 @@ export abstract class LuaTranspiler {
615615
}
616616

617617
public transpileSwitch(node: ts.SwitchStatement): string {
618-
const expression = this.transpileExpression(node.expression, true);
619-
const clauses = node.caseBlock.clauses;
620-
621-
let result = this.indent + "-------Switch statement start-------\n";
622-
623-
const switchVarName = "____switch" + this.genVarCounter;
624-
this.genVarCounter++;
625-
626-
result += this.indent + `local ${switchVarName} = ${expression}\n`;
627-
628-
let hasDefaultClause = false;
629-
630-
// If statement to go to right entry label
631-
clauses.forEach((clause, index) => {
632-
if (ts.isCaseClause(clause)) {
633-
result += this.indent +
634-
`if ${this.transpileExpression(clause.expression, true)} == ${switchVarName} then\n`;
635-
636-
this.pushIndent();
637-
result += this.indent + `goto ${switchVarName}_case_${index}\n`;
638-
this.popIndent();
639-
640-
result += this.indent + "end\n";
641-
} else if (ts.isDefaultClause(clause)) {
642-
hasDefaultClause = true;
643-
}
644-
});
645-
646-
result += "\n";
647-
648-
// If no case condition is matched jump to end or default immediately
649-
if (hasDefaultClause) {
650-
result += this.indent + `goto ${switchVarName}_default\n`;
651-
} else {
652-
result += this.indent + `goto ${switchVarName}_end\n`;
653-
}
654-
655-
result += "\n";
656-
657-
const transpileClauseBody = (clause: ts.CaseOrDefaultClause) => {
658-
this.transpilingSwitch++;
659-
result += this.indent + "do\n";
660-
this.pushIndent();
661-
result += this.transpileBlock(ts.createBlock(clause.statements));
662-
this.popIndent();
663-
result += this.indent + "end\n";
664-
this.transpilingSwitch--;
665-
};
666-
667-
clauses.forEach((clause, index) => {
668-
if (ts.isCaseClause(clause)) {
669-
result += this.indent + `::${switchVarName}_case_${index}::\n`;
670-
671-
transpileClauseBody(clause);
672-
673-
if (tsHelper.containsStatement(clause.statements, ts.SyntaxKind.BreakStatement)) {
674-
result += this.indent + `goto ${switchVarName}_end\n`;
675-
}
676-
} else if (ts.isDefaultClause(clause)) {
677-
result += this.indent + `::${switchVarName}_default::\n`;
678-
679-
transpileClauseBody(clause);
680-
}
681-
});
682-
683-
result += this.indent + `::${switchVarName}_end::\n`;
684-
result += this.indent + "-------Switch statement end-------\n";
685-
686-
return result;
618+
throw TSTLErrors.UnsupportedForTarget("Switch statements", this.options.luaTarget, node);
687619
}
688620

689621
public transpileTry(node: ts.TryStatement): string {

src/targets/Transpiler.52.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,79 @@ export class LuaTranspiler52 extends LuaTranspiler51 {
5959
}
6060
}
6161

62+
/** @override */
63+
public transpileSwitch(node: ts.SwitchStatement): string {
64+
const expression = this.transpileExpression(node.expression, true);
65+
const clauses = node.caseBlock.clauses;
66+
67+
let result = this.indent + "-------Switch statement start-------\n";
68+
69+
const switchVarName = "____switch" + this.genVarCounter;
70+
this.genVarCounter++;
71+
72+
result += this.indent + `local ${switchVarName} = ${expression}\n`;
73+
74+
let hasDefaultClause = false;
75+
76+
// If statement to go to right entry label
77+
clauses.forEach((clause, index) => {
78+
if (ts.isCaseClause(clause)) {
79+
result += this.indent +
80+
`if ${this.transpileExpression(clause.expression, true)} == ${switchVarName} then\n`;
81+
82+
this.pushIndent();
83+
result += this.indent + `goto ${switchVarName}_case_${index}\n`;
84+
this.popIndent();
85+
86+
result += this.indent + "end\n";
87+
} else if (ts.isDefaultClause(clause)) {
88+
hasDefaultClause = true;
89+
}
90+
});
91+
92+
result += "\n";
93+
94+
// If no case condition is matched jump to end or default immediately
95+
if (hasDefaultClause) {
96+
result += this.indent + `goto ${switchVarName}_default\n`;
97+
} else {
98+
result += this.indent + `goto ${switchVarName}_end\n`;
99+
}
100+
101+
result += "\n";
102+
103+
const transpileClauseBody = (clause: ts.CaseOrDefaultClause) => {
104+
this.transpilingSwitch++;
105+
result += this.indent + "do\n";
106+
this.pushIndent();
107+
result += this.transpileBlock(ts.createBlock(clause.statements));
108+
this.popIndent();
109+
result += this.indent + "end\n";
110+
this.transpilingSwitch--;
111+
};
112+
113+
clauses.forEach((clause, index) => {
114+
if (ts.isCaseClause(clause)) {
115+
result += this.indent + `::${switchVarName}_case_${index}::\n`;
116+
117+
transpileClauseBody(clause);
118+
119+
if (tsHelper.containsStatement(clause.statements, ts.SyntaxKind.BreakStatement)) {
120+
result += this.indent + `goto ${switchVarName}_end\n`;
121+
}
122+
} else if (ts.isDefaultClause(clause)) {
123+
result += this.indent + `::${switchVarName}_default::\n`;
124+
125+
transpileClauseBody(clause);
126+
}
127+
});
128+
129+
result += this.indent + `::${switchVarName}_end::\n`;
130+
result += this.indent + "-------Switch statement end-------\n";
131+
132+
return result;
133+
}
134+
62135
/** @override */
63136
public transpileDestructingAssignmentValue(node: ts.Expression): string {
64137
return `table.unpack(${this.transpileExpression(node)})`;

test/unit/conditionals.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Expect, Test, TestCase } from "alsatian";
2+
import { TranspileError } from "../../src/Errors";
3+
import { LuaTarget } from "../../src/Transpiler";
24
import * as util from "../src/util";
35

46
export class LuaConditionalsTests {
@@ -326,4 +328,10 @@ export class LuaConditionalsTests {
326328

327329
Expect(result).toBe(5);
328330
}
331+
332+
@Test("switch not allowed in 5.1")
333+
public switchThrow51(): void {
334+
Expect( () => util.transpileString(`switch ("abc") {}`, {luaTarget: LuaTarget.Lua51}))
335+
.toThrowError(TranspileError, "Switch statements is/are not supported for target Lua 5.1.");
336+
}
329337
}

0 commit comments

Comments
 (0)
X Tutup