From 1bdbef28e72d0ef3618452891b28358b9d0dd379 Mon Sep 17 00:00:00 2001 From: Lolleko Date: Sat, 20 Oct 2018 16:11:49 +0200 Subject: [PATCH 1/2] Moved transpile switch to Lua 5.2 Closes #194 --- src/Transpiler.ts | 70 +------------------------------- src/targets/Transpiler.52.ts | 73 ++++++++++++++++++++++++++++++++++ test/unit/conditionals.spec.ts | 8 ++++ 3 files changed, 82 insertions(+), 69 deletions(-) diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 6bcae227e..c6ed2d6ef 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -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 { diff --git a/src/targets/Transpiler.52.ts b/src/targets/Transpiler.52.ts index 31a6f4dcb..0fcba461f 100644 --- a/src/targets/Transpiler.52.ts +++ b/src/targets/Transpiler.52.ts @@ -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)})`; diff --git a/test/unit/conditionals.spec.ts b/test/unit/conditionals.spec.ts index 3e56620ca..afd0a65e8 100644 --- a/test/unit/conditionals.spec.ts +++ b/test/unit/conditionals.spec.ts @@ -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 { @@ -326,4 +328,10 @@ export class LuaConditionalsTests { Expect(result).toBe(5); } + + @Test("switch not allowed in 5.1") + public switchThrow50(): void { + Expect( () => util.transpileString(`switch ("abc") {}`, {luaTarget: LuaTarget.Lua51})) + .toThrowError(TranspileError, "Switch statements is/are not supported for target Lua 5.1."); + } } From b760a8f9dca617c9356d30ecd09538c4fedfc1c5 Mon Sep 17 00:00:00 2001 From: Lolleko Date: Sat, 20 Oct 2018 16:39:15 +0200 Subject: [PATCH 2/2] Fixed typo --- test/unit/conditionals.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/conditionals.spec.ts b/test/unit/conditionals.spec.ts index afd0a65e8..e48fd962f 100644 --- a/test/unit/conditionals.spec.ts +++ b/test/unit/conditionals.spec.ts @@ -330,7 +330,7 @@ export class LuaConditionalsTests { } @Test("switch not allowed in 5.1") - public switchThrow50(): void { + public switchThrow51(): void { Expect( () => util.transpileString(`switch ("abc") {}`, {luaTarget: LuaTarget.Lua51})) .toThrowError(TranspileError, "Switch statements is/are not supported for target Lua 5.1."); }