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
3 changes: 3 additions & 0 deletions dist/TSHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ var TSHelper = /** @class */ (function () {
}
return "unknown";
};
TSHelper.containsStatement = function (statements, kind) {
return statements.some(function (statement) { return statement.kind === kind; });
};
TSHelper.isFileModule = function (sourceFile) {
if (sourceFile) {
// Vanilla ts flags files as external module if they have an import or
Expand Down
49 changes: 35 additions & 14 deletions dist/Transpiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ var LuaTranspiler = /** @class */ (function () {
};
LuaTranspiler.prototype.transpileBreak = function () {
if (this.transpilingSwitch) {
return this.indent + ("goto switchDone" + this.genVarCounter + "\n");
return '';
}
else {
return this.indent + "break\n";
Expand Down Expand Up @@ -312,33 +312,54 @@ var LuaTranspiler = /** @class */ (function () {
var expression = this.transpileExpression(node.expression, true);
var clauses = node.caseBlock.clauses;
var result = this.indent + "-------Switch statement start-------\n";
var jumpTableName = "____switch" + this.genVarCounter;
this.genVarCounter++;
result += this.indent + ("local " + jumpTableName + " = {\n");
this.pushIndent();
// If statement to go to right entry label
clauses.forEach(function (clause, index) {
if (ts.isCaseClause(clause)) {
var keyword = index == 0 ? "if" : "elseif";
var condition = _this.transpileExpression(clause.expression, true);
result += _this.indent + (keyword + " " + expression + "==" + condition + " then\n");
result += _this.indent + "-- case:\n";
result += _this.indent + ("[" + _this.transpileExpression(clause.expression, true) + "] = function(self)\n");
}
else {
// Default
result += _this.indent + "else\n";
if (ts.isDefaultClause(clause)) {
result += _this.indent + "-- default:\n";
result += _this.indent + ("[\"____default" + _this.genVarCounter + "\"] = function(self)\n");
}
_this.pushIndent();
// Labels for fallthrough
result += _this.indent + ("::switchCase" + (_this.genVarCounter + index) + "::\n");
_this.transpilingSwitch = true;
clause.statements.forEach(function (statement) {
result += _this.transpileNode(statement);
});
_this.transpilingSwitch = false;
// If this goto is reached, fall through to the next case
if (index < clauses.length - 1) {
result += _this.indent + ("goto switchCase" + (_this.genVarCounter + index + 1) + "\n");
var i = index + 1;
if (i < clauses.length && !TSHelper_1.TSHelper.containsStatement(clause.statements, ts.SyntaxKind.BreakStatement)) {
var nextClause = clauses[i];
while (i < clauses.length
&& ts.isCaseClause(nextClause)
&& nextClause.statements.length === 0) {
i++;
nextClause = clauses[i];
}
if (i !== index && nextClause) {
if (ts.isCaseClause(nextClause)) {
result += _this.indent + ("self[" + _this.transpileExpression(nextClause.expression, true) + "]()\n");
}
else {
result += _this.indent + ("self[\"____default" + _this.genVarCounter + "\"]()\n");
}
}
}
else {
result += _this.indent + "-- break;\n";
}
_this.popIndent();
result += _this.indent + "end,\n";
});
result += this.indent + "end\n";
result += this.indent + ("::switchDone" + this.genVarCounter + "::\n");
this.popIndent();
result += this.indent + "}\n";
result += this.indent + ("if " + jumpTableName + "[" + expression + "] then " + jumpTableName + "[" + expression + "](" + jumpTableName + ")\n");
result += this.indent + ("elseif " + jumpTableName + "[\"____default" + this.genVarCounter + "\"] then " + jumpTableName + "[\"____default" + this.genVarCounter + "\"](" + jumpTableName + ") end\n");
result += this.indent + "--------Switch statement end--------\n";
//Increment counter for next switch statement
this.genVarCounter += clauses.length;
Expand Down
6 changes: 3 additions & 3 deletions src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function compile(fileNames: string[], options: ts.CompilerOptions, projectRoot:
}

program.getSourceFiles().forEach(sourceFile => {
if (!sourceFile.isDeclarationFile) {
if (!sourceFile.isDeclarationFile) {
// Print AST for debugging
//printAST(sourceFile, 0);

Expand All @@ -43,7 +43,7 @@ function compile(fileNames: string[], options: ts.CompilerOptions, projectRoot:
const addHeader = options.noHeader === true ? false : true;
let lua = LuaTranspiler.transpileSourceFile(sourceFile, checker, addHeader);
let outPath = sourceFile.fileName.substring(0, sourceFile.fileName.lastIndexOf(".")) + ".lua";

if (options.outDir) {
var extension = options.outDir;
if (extension[extension.length - 1] != "/") extension = extension + "/";
Expand Down Expand Up @@ -83,7 +83,7 @@ function printAST(node: ts.Node, indent: number) {
const filename = process.argv[2].split("\\").join("/");
const filepath = filename.substring(0, filename.lastIndexOf("/"));
let configPath = ts.findConfigFile(filepath, ts.sys.fileExists);

if (configPath) {
configPath = configPath.split("\\").join("/");
const projectRoot = configPath.substring(0, configPath.lastIndexOf("/"));
Expand Down
4 changes: 4 additions & 0 deletions src/TSHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export class TSHelper {
return "unknown";
}

static containsStatement(statements: ts.NodeArray<ts.Statement>, kind: ts.SyntaxKind): boolean {
return statements.some(statement => statement.kind === kind);
}

static isFileModule(sourceFile: ts.SourceFile) {
if (sourceFile) {
// Vanilla ts flags files as external module if they have an import or
Expand Down
56 changes: 40 additions & 16 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class LuaTranspiler {

transpileBreak(): string {
if (this.transpilingSwitch) {
return this.indent + `goto switchDone${this.genVarCounter}\n`;
return '';
} else {
return this.indent + "break\n";
}
Expand Down Expand Up @@ -347,37 +347,61 @@ export class LuaTranspiler {

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

let jumpTableName = "____switch" + this.genVarCounter;
this.genVarCounter++;

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

this.pushIndent();

// If statement to go to right entry label
clauses.forEach((clause, index) => {
if (ts.isCaseClause(clause)) {
let keyword = index == 0 ? "if" : "elseif";
let condition = this.transpileExpression(clause.expression, true);
result += this.indent + `${keyword} ${expression}==${condition} then\n`;
} else {
// Default
result += this.indent + `else\n`;
result += this.indent + `-- case:\n`;
result += this.indent + `[${this.transpileExpression(clause.expression, true)}] = function(self)\n`;
}
if (ts.isDefaultClause(clause)) {
result += this.indent + `-- default:\n`;
result += this.indent + `["____default${this.genVarCounter}"] = function(self)\n`;
}

this.pushIndent();

// Labels for fallthrough
result += this.indent + `::switchCase${this.genVarCounter + index}::\n`;

this.transpilingSwitch = true;
clause.statements.forEach(statement => {
result += this.transpileNode(statement);
});
this.transpilingSwitch = false;

// If this goto is reached, fall through to the next case
if (index < clauses.length - 1) {
result += this.indent + `goto switchCase${this.genVarCounter + index + 1}\n`;
let i = index + 1;
if (i < clauses.length && !tsEx.containsStatement(clause.statements, ts.SyntaxKind.BreakStatement)) {
let nextClause = clauses[i];
while(i < clauses.length
&& ts.isCaseClause(nextClause)
&& nextClause.statements.length === 0
) {
i++;
nextClause = clauses[i];
}

if (i !== index && nextClause) {
if (ts.isCaseClause(nextClause)) {
result += this.indent + `self[${this.transpileExpression(nextClause.expression, true)}]()\n`;
} else {
result += this.indent + `self["____default${this.genVarCounter}"]()\n`;
}
}
} else {
result += this.indent + `-- break;\n`;
}

this.popIndent();

result += this.indent + `end,\n`;
});
result += this.indent + "end\n";
result += this.indent + `::switchDone${this.genVarCounter}::\n`;
this.popIndent();
result += this.indent + "}\n";
result += this.indent + `if ${jumpTableName}[${expression}] then ${jumpTableName}[${expression}](${jumpTableName})\n`;
result += this.indent + `elseif ${jumpTableName}["____default${this.genVarCounter}"] then ${jumpTableName}["____default${this.genVarCounter}"](${jumpTableName}) end\n`;
result += this.indent + "--------Switch statement end--------\n";

//Increment counter for next switch statement
Expand Down
Loading