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
125 changes: 93 additions & 32 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3123,11 +3123,7 @@ export class LuaTransformer {
const ownerType = this.checker.getTypeAtLocation(node.expression.expression);

if (ownerType.symbol && ownerType.symbol.escapedName === "Math") {
return tstl.createCallExpression(
this.transformMathExpression(node.expression.name),
this.transformArguments(node.arguments),
node
);
return this.transformMathCallExpression(node);
}

if (ownerType.symbol && ownerType.symbol.escapedName === "StringConstructor") {
Expand Down Expand Up @@ -3320,34 +3316,99 @@ export class LuaTransformer {
}

// Transpile a Math._ property
public transformMathExpression(identifier: ts.Identifier): tstl.TableIndexExpression {
const translation = {
PI: "pi",
abs: "abs",
acos: "acos",
asin: "asin",
atan: "atan",
ceil: "ceil",
cos: "cos",
exp: "exp",
floor: "floor",
log: "log",
max: "max",
min: "min",
pow: "pow",
random: "random",
round: "round",
sin: "sin",
sqrt: "sqrt",
tan: "tan",
};
public transformMathExpression(identifier: ts.Identifier): tstl.Expression {
const name = identifier.escapedText as string;
switch (name) {
case "PI":
const property = tstl.createStringLiteral("pi");
const math = tstl.createIdentifier("math");
return tstl.createTableIndexExpression(math, property, identifier);
Comment thread
Perryvw marked this conversation as resolved.

case "E":
case "LN10":
case "LN2":
case "LOG10E":
case "LOG2E":
case "SQRT1_2":
case "SQRT2":
return tstl.createNumericLiteral(Math[name], identifier);

if (translation[identifier.escapedText as string]) {
const property = tstl.createStringLiteral(translation[identifier.escapedText as string]);
const math = tstl.createIdentifier("math");
return tstl.createTableIndexExpression(math, property, identifier);
} else {
throw TSTLErrors.UnsupportedProperty("math", identifier.escapedText as string, identifier);
default:
throw TSTLErrors.UnsupportedProperty("math", name, identifier);
}
}

// Transpile a Math._ property
public transformMathCallExpression(node: ts.CallExpression): tstl.Expression {
const expression = node.expression as ts.PropertyAccessExpression;
const params = this.transformArguments(node.arguments);
const expressionName = expression.name.escapedText as string;
switch (expressionName) {
// math.tan(x / y)
case "atan2":
{
const math = tstl.createIdentifier("math");
const atan = tstl.createStringLiteral("atan");
const div = tstl.createBinaryExpression(params[0], params[1], tstl.SyntaxKind.DivisionOperator);
return tstl.createCallExpression(tstl.createTableIndexExpression(math, atan), [div], node);
}

// (math.log(x) / Math.LNe)
case "log10":
case "log2":
{
const math = tstl.createIdentifier("math");
const log1 = tstl.createTableIndexExpression(math, tstl.createStringLiteral("log"));
const logCall1 = tstl.createCallExpression(log1, params);
const e = tstl.createNumericLiteral(expressionName === "log10" ? Math.LN10 : Math.LN2);
const div = tstl.createBinaryExpression(logCall1, e, tstl.SyntaxKind.DivisionOperator);
return tstl.createParenthesizedExpression(div, node);
}

// math.log(1 + x)
case "log1p":
{
const math = tstl.createIdentifier("math");
const log = tstl.createStringLiteral("log");
const one = tstl.createNumericLiteral(1);
const add = tstl.createBinaryExpression(one, params[0], tstl.SyntaxKind.AdditionOperator);
return tstl.createCallExpression(tstl.createTableIndexExpression(math, log), [add], node);
}

// math.floor(x + 0.5)
case "round":
{
const math = tstl.createIdentifier("math");
const floor = tstl.createStringLiteral("floor");
const half = tstl.createNumericLiteral(0.5);
const add = tstl.createBinaryExpression(params[0], half, tstl.SyntaxKind.AdditionOperator);
return tstl.createCallExpression(tstl.createTableIndexExpression(math, floor), [add], node);
}

case "abs":
case "acos":
case "asin":
case "atan":
case "ceil":
case "cos":
case "exp":
case "floor":
case "log":
case "max":
case "min":
case "pow":
case "random":
case "sin":
case "sqrt":
case "tan":
{
const math = tstl.createIdentifier("math");
const method = tstl.createStringLiteral(expressionName);
return tstl.createCallExpression(tstl.createTableIndexExpression(math, method), params, node);
}

default:
throw TSTLErrors.UnsupportedProperty("math", name, expression);
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/unit/math.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export class MathTests {
@TestCase("Math.cos()", "math.cos();")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test for constants (probably would be nice to actually util.transpileAndExecute those)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are tests for Math.PI and Math.E. Should I add a test for every one?

Also, I'm not sure we should do execute tests since there could be inconsistencies with precision.

@TestCase("Math.sin()", "math.sin();")
@TestCase("Math.min()", "math.min();")
@TestCase("Math.atan2(2, 3)", "math.atan(2 / 3);")
@TestCase("Math.log2(3)", `(math.log(3) / ${Math.LN2});`)
@TestCase("Math.log10(3)", `(math.log(3) / ${Math.LN10});`)
@TestCase("Math.log1p(3)", "math.log(1 + 3);")
@TestCase("Math.round(3.3)", "math.floor(3.3 + 0.5);")
@TestCase("Math.PI", "math.pi;")
@Test("Math")
public math(inp: string, expected: string): void {
Expand All @@ -18,6 +23,20 @@ export class MathTests {
Expect(lua).toBe(expected);
}

@TestCase("E")
@TestCase("LN10")
@TestCase("LN2")
@TestCase("LOG10E")
@TestCase("LOG2E")
@TestCase("SQRT1_2")
@TestCase("SQRT2")
@Test("Math constant")
public mathConstant(constant: string): void {
const epsilon = 0.000001;
const code = `return Math.abs(Math.${constant} - ${Math[constant]}) <= ${epsilon}`;
Expect(util.transpileAndExecute(code)).toBe(true);
}

@TestCase("++x", "x=4;y=6")
@TestCase("x++", "x=4;y=6")
@TestCase("--x", "x=2;y=6")
Expand Down