Skip to content

Commit 3f97c79

Browse files
committed
Translation for Math library
1 parent 54839d7 commit 3f97c79

File tree

3 files changed

+90
-6
lines changed

3 files changed

+90
-6
lines changed

dist/Transpiler.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -537,17 +537,21 @@ var LuaTranspiler = /** @class */ (function () {
537537
if (TSHelper_1.TSHelper.isArrayType(type))
538538
return this.transpileArrayCallExpression(node);
539539
}
540-
// Include context parameter if present
541540
var expType = this.checker.getTypeAtLocation(node.expression.expression);
541+
if (expType.symbol && expType.symbol.escapedName == "Math") {
542+
var params_1 = this.transpileArguments(node.arguments);
543+
return this.transpileMathExpression(node.expression.name) + ("(" + params_1 + ")");
544+
}
545+
// Include context parameter if present
542546
var callPath_1 = (expType && expType.symbol) ? expType.symbol.name + "." + node.expression.name.escapedText : this.transpileExpression(node.expression);
543-
var params_1 = this.transpileArguments(node.arguments, node.expression.expression);
544-
return callPath_1 + "(" + params_1 + ")";
547+
var params_2 = this.transpileArguments(node.arguments, node.expression.expression);
548+
return callPath_1 + "(" + params_2 + ")";
545549
}
546550
// Handle super calls properly
547551
if (node.expression.kind == ts.SyntaxKind.SuperKeyword) {
548552
var callPath_2 = this.transpileExpression(node.expression);
549-
var params_2 = this.transpileArguments(node.arguments, ts.createNode(ts.SyntaxKind.ThisKeyword));
550-
return "self.__base.constructor(" + params_2 + ")";
553+
var params_3 = this.transpileArguments(node.arguments, ts.createNode(ts.SyntaxKind.ThisKeyword));
554+
return "self.__base.constructor(" + params_3 + ")";
551555
}
552556
var callPath = this.transpileExpression(node.expression);
553557
var params = this.transpileArguments(node.arguments);
@@ -638,9 +642,42 @@ var LuaTranspiler = /** @class */ (function () {
638642
if (TSHelper_1.TSHelper.isCompileMembersOnlyEnum(type, this.checker)) {
639643
return property;
640644
}
645+
// Catch math expressions
646+
if (ts.isIdentifier(node.expression) && node.expression.escapedText == "Math") {
647+
return this.transpileMathExpression(node.name);
648+
}
641649
var path = this.transpileExpression(node.expression);
642650
return path + "." + property;
643651
};
652+
// Transpile a Math._ property
653+
LuaTranspiler.prototype.transpileMathExpression = function (identifier) {
654+
var translation = {
655+
abs: "abs",
656+
acos: "acos",
657+
asin: "asin",
658+
atan: "atan",
659+
ceil: "ceil",
660+
cos: "cos",
661+
exp: "exp",
662+
floor: "floor",
663+
log: "log",
664+
max: "max",
665+
min: "min",
666+
PI: "pi",
667+
pow: "pow",
668+
random: "random",
669+
round: "round",
670+
sin: "sin",
671+
sqrt: "sqrt",
672+
tan: "tan"
673+
};
674+
if (translation[identifier.escapedText]) {
675+
return "math." + translation[identifier.escapedText];
676+
}
677+
else {
678+
throw new TranspileError("Unsupported math property " + identifier.escapedText + ".", identifier);
679+
}
680+
};
644681
// Transpile access of string properties, only supported properties are allowed
645682
LuaTranspiler.prototype.transpileStringProperty = function (node) {
646683
var property = node.name;

dist/lualib/typescript.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ function TS_ITE(condition, v1f, v2f)
77
end
88
end
99

10+
function TS_forEach(list, func)
11+
for i, v in ipairs(list) do
12+
func(v, i-1, list)
13+
end
14+
end
15+
1016
function TS_map(list, func)
1117
local out = {}
1218
for _, v in ipairs(list) do

src/Transpiler.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,14 @@ export class LuaTranspiler {
588588
return this.transpileArrayCallExpression(node);
589589
}
590590

591-
// Include context parameter if present
592591
const expType = this.checker.getTypeAtLocation(node.expression.expression);
592+
593+
if (expType.symbol && expType.symbol.escapedName == "Math") {
594+
const params = this.transpileArguments(node.arguments);
595+
return this.transpileMathExpression(node.expression.name) + `(${params})`;
596+
}
597+
598+
// Include context parameter if present
593599
let callPath = (expType && expType.symbol) ? `${expType.symbol.name}.${node.expression.name.escapedText}` : this.transpileExpression(node.expression);
594600
let params = this.transpileArguments(node.arguments, node.expression.expression);
595601

@@ -699,10 +705,45 @@ export class LuaTranspiler {
699705
return property;
700706
}
701707

708+
// Catch math expressions
709+
if (ts.isIdentifier(node.expression) && node.expression.escapedText == "Math") {
710+
return this.transpileMathExpression(node.name);
711+
}
712+
702713
let path = this.transpileExpression(node.expression);
703714
return `${path}.${property}`;
704715
}
705716

717+
// Transpile a Math._ property
718+
transpileMathExpression(identifier: ts.Identifier): string {
719+
const translation = {
720+
abs: "abs",
721+
acos: "acos",
722+
asin: "asin",
723+
atan: "atan",
724+
ceil: "ceil",
725+
cos: "cos",
726+
exp: "exp",
727+
floor: "floor",
728+
log: "log",
729+
max: "max",
730+
min: "min",
731+
PI: "pi",
732+
pow: "pow",
733+
random: "random",
734+
round: "round",
735+
sin: "sin",
736+
sqrt: "sqrt",
737+
tan: "tan"
738+
};
739+
740+
if (translation[<string>identifier.escapedText]) {
741+
return `math.${translation[<string>identifier.escapedText]}`;
742+
} else {
743+
throw new TranspileError(`Unsupported math property ${identifier.escapedText}.`, identifier);
744+
}
745+
}
746+
706747
// Transpile access of string properties, only supported properties are allowed
707748
transpileStringProperty(node: ts.PropertyAccessExpression): string {
708749
const property = node.name;

0 commit comments

Comments
 (0)