Skip to content

Commit 955824a

Browse files
committed
added compiled version
1 parent 90060b3 commit 955824a

File tree

5 files changed

+895
-0
lines changed

5 files changed

+895
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.js
22
node_modules/
33
*.lua
4+
!dist/*.js

dist/Compiler.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
var ts = require("typescript");
4+
var Transpiler_1 = require("./Transpiler");
5+
var TSHelper_1 = require("./TSHelper");
6+
function compile(fileNames, options) {
7+
// Verify target
8+
if (options.target != "lua") {
9+
console.error("Wrong compilation target! Add \"target\": \"lua\" to your tsconfig.json!");
10+
process.exit();
11+
}
12+
var program = ts.createProgram(fileNames, options);
13+
var checker = program.getTypeChecker();
14+
// Get all diagnostics, ignore unsupported extension
15+
var diagnostics = ts.getPreEmitDiagnostics(program).filter(function (diag) { return diag.code != 6054; });
16+
diagnostics.forEach(function (diagnostic) {
17+
if (diagnostic.file) {
18+
var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character;
19+
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
20+
console.log(diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + message);
21+
}
22+
else {
23+
console.log("" + ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'));
24+
}
25+
});
26+
// If there are errors dont emit
27+
if (diagnostics.filter(function (diag) { return diag.category == ts.DiagnosticCategory.Error; }).length > 0) {
28+
console.log("Stopping compilation process because of errors.");
29+
process.exit();
30+
}
31+
program.getSourceFiles().forEach(function (sourceFile) {
32+
if (!sourceFile.isDeclarationFile) {
33+
// Print AST for debugging
34+
//printAST(sourceFile, 0);
35+
try {
36+
// Transpile AST
37+
var lua = Transpiler_1.LuaTranspiler.transpileSourceFile(sourceFile, checker);
38+
var outPath = sourceFile.fileName.substring(0, sourceFile.fileName.lastIndexOf(".")) + ".lua";
39+
//console.log(outPath);
40+
// Write output
41+
ts.sys.writeFile(outPath, lua);
42+
}
43+
catch (exception) {
44+
if (exception.node) {
45+
var pos = ts.getLineAndCharacterOfPosition(sourceFile, exception.node.pos);
46+
// Graciously handle transpilation errors
47+
console.error("Encountered error parsing file: " + exception.message);
48+
console.error(sourceFile.fileName + " line: " + (1 + pos.line) + " column: " + pos.character);
49+
console.error(exception.stack);
50+
}
51+
else {
52+
throw exception;
53+
}
54+
}
55+
}
56+
});
57+
process.exit();
58+
}
59+
function printAST(node, indent) {
60+
var indentStr = "";
61+
for (var i = 0; i < indent; i++)
62+
indentStr += " ";
63+
console.log(indentStr + TSHelper_1.TSHelper.enumName(node.kind, ts.SyntaxKind));
64+
node.forEachChild(function (child) { return printAST(child, indent + 1); });
65+
}
66+
// Try to find tsconfig.json
67+
var filename = process.argv[2].split("\\").join("/");
68+
var filepath = filename.substring(0, filename.lastIndexOf("/"));
69+
var configPath = ts.findConfigFile(filepath, ts.sys.fileExists);
70+
if (configPath) {
71+
configPath = configPath.split("\\").join("/");
72+
var projectRoot = configPath.substring(0, configPath.lastIndexOf("/"));
73+
// Find all files
74+
var files = ts.sys.readDirectory(projectRoot, [".ts"]);
75+
// Read config
76+
var configFile = ts.readConfigFile(configPath, ts.sys.readFile);
77+
if (configFile.error) {
78+
console.error("Error occured:");
79+
console.error(configFile.error);
80+
}
81+
else {
82+
compile(files, configFile.config.compilerOptions);
83+
}
84+
}
85+
else {
86+
console.error("Could not find tsconfig.json, place one in your project root!");
87+
}

dist/ForHelper.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
var ts = require("typescript");
4+
var TSHelper_1 = require("./TSHelper");
5+
var Transpiler_1 = require("./Transpiler");
6+
var ForHelper = /** @class */ (function () {
7+
function ForHelper() {
8+
}
9+
// Get the ending value of a numeric for loop
10+
ForHelper.GetForEnd = function (condition, transpiler) {
11+
if (ts.isBinaryExpression(condition)) {
12+
if (ts.isIdentifier(condition.left)) {
13+
// Account for lua 1 indexing
14+
switch (condition.operatorToken.kind) {
15+
case ts.SyntaxKind.LessThanEqualsToken:
16+
case ts.SyntaxKind.GreaterThanEqualsToken:
17+
return transpiler.transpileExpression(condition.right);
18+
case ts.SyntaxKind.LessThanToken:
19+
return transpiler.transpileExpression(condition.right) + "-1";
20+
case ts.SyntaxKind.GreaterThanToken:
21+
return transpiler.transpileExpression(condition.right) + "+1";
22+
default:
23+
throw new Transpiler_1.TranspileError("Unsupported for-loop condition operator: " + TSHelper_1.TSHelper.enumName(condition.operatorToken, ts.SyntaxKind), condition);
24+
}
25+
}
26+
else {
27+
// Account for lua 1 indexing
28+
switch (condition.operatorToken.kind) {
29+
case ts.SyntaxKind.LessThanEqualsToken:
30+
case ts.SyntaxKind.GreaterThanEqualsToken:
31+
return transpiler.transpileExpression(condition.right);
32+
case ts.SyntaxKind.LessThanToken:
33+
return transpiler.transpileExpression(condition.right) + "+1";
34+
case ts.SyntaxKind.GreaterThanToken:
35+
return transpiler.transpileExpression(condition.right) + "-1";
36+
default:
37+
throw new Transpiler_1.TranspileError("Unsupported for-loop condition operator: " + TSHelper_1.TSHelper.enumName(condition.operatorToken, ts.SyntaxKind), condition);
38+
}
39+
}
40+
}
41+
else {
42+
throw new Transpiler_1.TranspileError("Unsupported for-loop condition type: " + TSHelper_1.TSHelper.enumName(condition.kind, ts.SyntaxKind), condition);
43+
}
44+
};
45+
// Get increment step for numeric for loop
46+
ForHelper.GetForStep = function (incrementor, transpiler) {
47+
switch (incrementor.kind) {
48+
case ts.SyntaxKind.PostfixUnaryExpression:
49+
case ts.SyntaxKind.PrefixUnaryExpression:
50+
switch (incrementor.operator) {
51+
case ts.SyntaxKind.PlusPlusToken:
52+
return "1";
53+
case ts.SyntaxKind.MinusMinusToken:
54+
return "-1";
55+
default:
56+
throw new Transpiler_1.TranspileError("Unsupported for-loop increment step: " + TSHelper_1.TSHelper.enumName(incrementor.kind, ts.SyntaxKind), incrementor);
57+
}
58+
case ts.SyntaxKind.BinaryExpression:
59+
var value = ts.isIdentifier(incrementor.left) ?
60+
transpiler.transpileExpression(incrementor.right) :
61+
transpiler.transpileExpression(incrementor.left);
62+
switch (incrementor.operatorToken.kind) {
63+
case ts.SyntaxKind.PlusEqualsToken:
64+
return value;
65+
case ts.SyntaxKind.MinusEqualsToken:
66+
return "-" + value;
67+
default:
68+
throw new Transpiler_1.TranspileError("Unsupported for-loop increment step: " + TSHelper_1.TSHelper.enumName(incrementor.kind, ts.SyntaxKind), incrementor);
69+
}
70+
default:
71+
throw new Transpiler_1.TranspileError("Unsupported for-loop increment step: " + TSHelper_1.TSHelper.enumName(incrementor.kind, ts.SyntaxKind), incrementor);
72+
}
73+
};
74+
return ForHelper;
75+
}());
76+
exports.ForHelper = ForHelper;

dist/TSHelper.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
var ts = require("typescript");
4+
var TSHelper = /** @class */ (function () {
5+
function TSHelper() {
6+
}
7+
// Get all children of a node, required until microsoft fixes node.getChildren()
8+
TSHelper.getChildren = function (node) {
9+
var children = [];
10+
node.forEachChild(function (child) {
11+
children.push(child);
12+
});
13+
return children;
14+
};
15+
// Get children filtered by function and cast to predefined type
16+
TSHelper.getChildrenOfType = function (node, typeFilter) {
17+
return this.getChildren(node).filter(typeFilter);
18+
};
19+
TSHelper.getFirstChildOfType = function (node, typeFilter) {
20+
return this.getChildrenOfType(node, typeFilter)[0];
21+
};
22+
// Reverse lookup of enum key by value
23+
TSHelper.enumName = function (needle, haystack) {
24+
for (var name in haystack) {
25+
if (haystack[name] == needle) {
26+
return name;
27+
}
28+
}
29+
return "unknown";
30+
};
31+
TSHelper.isValueType = function (node) {
32+
return ts.isIdentifier(node) || ts.isLiteralExpression(node) || ts.isArrayLiteralExpression(node) || ts.isObjectLiteralExpression(node);
33+
};
34+
TSHelper.isArrayType = function (type) {
35+
return type.flags == ts.TypeFlags.Object && type.symbol.escapedName == "Array";
36+
};
37+
TSHelper.isCompileMembersOnlyEnum = function (type) {
38+
return type.symbol
39+
&& ((type.symbol.flags & ts.SymbolFlags.Enum) != 0)
40+
&& type.symbol.getDocumentationComment()[0] != undefined
41+
&& (type.symbol.getDocumentationComment()[0].text.trim() == "CompileMembersOnly");
42+
};
43+
return TSHelper;
44+
}());
45+
exports.TSHelper = TSHelper;

0 commit comments

Comments
 (0)