Skip to content

Commit 1273d73

Browse files
committed
Added tests for conditionals && Proposal for new Switch statement
1 parent 2ff8dd6 commit 1273d73

File tree

3 files changed

+100
-33
lines changed

3 files changed

+100
-33
lines changed

dist/Transpiler.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ var LuaTranspiler = /** @class */ (function () {
221221
};
222222
LuaTranspiler.prototype.transpileBreak = function () {
223223
if (this.transpilingSwitch) {
224-
return this.indent + ("goto switchDone" + this.genVarCounter + "\n");
224+
return '';
225225
}
226226
else {
227227
return this.indent + "break\n";
@@ -312,32 +312,52 @@ var LuaTranspiler = /** @class */ (function () {
312312
var expression = this.transpileExpression(node.expression, true);
313313
var clauses = node.caseBlock.clauses;
314314
var result = this.indent + "-------Switch statement start-------\n";
315+
var jumpTableName = "____switch" + ++this.genVarCounter;
316+
result += this.indent + ("local " + jumpTableName + " = {\n");
317+
this.pushIndent();
315318
// If statement to go to right entry label
316319
clauses.forEach(function (clause, index) {
317-
if (index !== clauses.length - 1
318-
&& clause.statements.length !== 0
319-
&& !TSHelper_1.TSHelper.containsStatement(clause.statements, ts.SyntaxKind.BreakStatement)) {
320-
throw new TranspileError("Missing break, fall through is not allowed.", clause);
321-
}
322320
if (ts.isCaseClause(clause)) {
323-
var keyword = index == 0 ? "if" : "elseif";
324-
var condition = _this.transpileExpression(clause.expression, true);
325-
result += _this.indent + (keyword + " " + expression + "==" + condition + " then\n");
321+
result += _this.indent + "-- case:\n";
322+
result += _this.indent + ("[" + _this.transpileExpression(clause.expression, true) + "] = function(self)\n");
326323
}
327-
else {
328-
// Default
329-
result += _this.indent + "else\n";
324+
if (ts.isDefaultClause(clause)) {
325+
result += _this.indent + "-- default:\n";
326+
result += _this.indent + ("[\"____default" + _this.genVarCounter + "\"] = function(self)\n");
330327
}
331328
_this.pushIndent();
332329
_this.transpilingSwitch = true;
333330
clause.statements.forEach(function (statement) {
334331
result += _this.transpileNode(statement);
335332
});
336333
_this.transpilingSwitch = false;
334+
var i = index + 1;
335+
if (i < clauses.length && !TSHelper_1.TSHelper.containsStatement(clause.statements, ts.SyntaxKind.BreakStatement)) {
336+
var nextClause = clauses[i];
337+
while (i < clauses.length
338+
&& ts.isCaseClause(nextClause)
339+
&& nextClause.statements.length === 0) {
340+
nextClause = clauses[++i];
341+
}
342+
if (i !== index && nextClause) {
343+
if (ts.isCaseClause(nextClause)) {
344+
result += _this.indent + ("self[" + _this.transpileExpression(nextClause.expression, true) + "]()\n");
345+
}
346+
else {
347+
result += _this.indent + ("self[\"____default" + _this.genVarCounter + "\"]()\n");
348+
}
349+
}
350+
}
351+
else {
352+
result += _this.indent + "-- break;\n";
353+
}
337354
_this.popIndent();
355+
result += _this.indent + "end,\n";
338356
});
339-
result += this.indent + "end\n";
340-
result += this.indent + ("::switchDone" + this.genVarCounter + "::\n");
357+
this.popIndent();
358+
result += this.indent + "}\n";
359+
result += this.indent + ("if " + jumpTableName + "[" + expression + "] then " + jumpTableName + "[" + expression + "](" + jumpTableName + ")\n");
360+
result += this.indent + ("elseif " + jumpTableName + "[\"____default" + this.genVarCounter + "\"] then " + jumpTableName + "[\"____default" + this.genVarCounter + "\"](" + jumpTableName + ") end\n");
341361
result += this.indent + "--------Switch statement end--------\n";
342362
//Increment counter for next switch statement
343363
this.genVarCounter += clauses.length;

src/Transpiler.ts

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export class LuaTranspiler {
232232

233233
transpileBreak(): string {
234234
if (this.transpilingSwitch) {
235-
return this.indent + `goto switchDone${this.genVarCounter}\n`;
235+
return '';
236236
} else {
237237
return this.indent + "break\n";
238238
}
@@ -347,22 +347,22 @@ export class LuaTranspiler {
347347

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

350+
let jumpTableName = "____switch" + ++this.genVarCounter
351+
352+
result += this.indent + `local ${jumpTableName} = {\n`;
353+
354+
this.pushIndent();
355+
350356
// If statement to go to right entry label
351357
clauses.forEach((clause, index) => {
352-
if (index !== clauses.length - 1
353-
&& clause.statements.length !== 0
354-
&& !tsEx.containsStatement(clause.statements, ts.SyntaxKind.BreakStatement)) {
355-
throw new TranspileError("Missing break, fall through is not allowed.", clause);
356-
}
357358
if (ts.isCaseClause(clause)) {
358-
let keyword = index == 0 ? "if" : "elseif";
359-
let condition = this.transpileExpression(clause.expression, true);
360-
result += this.indent + `${keyword} ${expression}==${condition} then\n`;
361-
} else {
362-
// Default
363-
result += this.indent + `else\n`;
359+
result += this.indent + `-- case:\n`;
360+
result += this.indent + `[${this.transpileExpression(clause.expression, true)}] = function(self)\n`;
361+
}
362+
if (ts.isDefaultClause(clause)) {
363+
result += this.indent + `-- default:\n`;
364+
result += this.indent + `["____default${this.genVarCounter}"] = function(self)\n`;
364365
}
365-
366366
this.pushIndent();
367367

368368
this.transpilingSwitch = true;
@@ -371,10 +371,35 @@ export class LuaTranspiler {
371371
});
372372
this.transpilingSwitch = false;
373373

374+
let i = index + 1;
375+
if (i < clauses.length && !tsEx.containsStatement(clause.statements, ts.SyntaxKind.BreakStatement)) {
376+
let nextClause = clauses[i];
377+
while(i < clauses.length
378+
&& ts.isCaseClause(nextClause)
379+
&& nextClause.statements.length === 0
380+
) {
381+
nextClause = clauses[++i];
382+
}
383+
384+
if (i !== index && nextClause) {
385+
if (ts.isCaseClause(nextClause)) {
386+
result += this.indent + `self[${this.transpileExpression(nextClause.expression, true)}]()\n`;
387+
} else {
388+
result += this.indent + `self["____default${this.genVarCounter}"]()\n`;
389+
}
390+
}
391+
} else {
392+
result += this.indent + `-- break;\n`;
393+
}
394+
374395
this.popIndent();
396+
397+
result += this.indent + `end,\n`;
375398
});
376-
result += this.indent + "end\n";
377-
result += this.indent + `::switchDone${this.genVarCounter}::\n`;
399+
this.popIndent();
400+
result += this.indent + "}\n";
401+
result += this.indent + `if ${jumpTableName}[${expression}] then ${jumpTableName}[${expression}](${jumpTableName})\n`;
402+
result += this.indent + `elseif ${jumpTableName}["____default${this.genVarCounter}"] then ${jumpTableName}["____default${this.genVarCounter}"](${jumpTableName}) end\n`;
378403
result += this.indent + "--------Switch statement end--------\n";
379404

380405
//Increment counter for next switch statement

test/integration/lua/conditionals.spec.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,16 @@ export class LuaConditionalsTests {
168168
Expect(result).toBe(expected);
169169
}
170170

171-
@TestCase(0, 0)
171+
@TestCase(0, 1)
172+
@TestCase(0, 1)
173+
@TestCase(2, 4)
174+
@TestCase(3, 4)
175+
@TestCase(4, 4)
176+
@TestCase(5, -2)
172177
@Test("switchfallthrough")
173178
public switchfallthrough(inp: number, expected: number) {
174-
// Transpile & Assert
175-
Expect(() => util.transpileString(
179+
/// Transpile
180+
let lua = util.transpileString(
176181
`let result = -1;
177182
178183
switch (${inp}) {
@@ -181,10 +186,27 @@ export class LuaConditionalsTests {
181186
case 1:
182187
result = 1;
183188
break;
189+
case 2:
190+
result = 2;
191+
case 3:
192+
case 4:
193+
result = 4;
194+
break;
195+
case 5:
196+
result = 5;
197+
default:
198+
result = -2;
199+
break;
184200
}
185201
return result;`
186202
, util.dummyTypes.Number
187-
)).toThrowError(Error, "Missing break, fall through is not allowed.")
203+
);
204+
205+
// Execute
206+
let result = util.executeLua(lua);
207+
208+
// Assert
209+
Expect(result).toBe(expected);
188210
}
189211

190212

0 commit comments

Comments
 (0)