-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathmath.ts
More file actions
135 lines (121 loc) · 4.82 KB
/
math.ts
File metadata and controls
135 lines (121 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as ts from "typescript";
import { LuaTarget } from "../../CompilerOptions";
import * as lua from "../../LuaAST";
import { TransformationContext } from "../context";
import { unsupportedProperty } from "../utils/diagnostics";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import { transformArguments } from "../visitors/call";
export function transformMathProperty(
context: TransformationContext,
node: ts.PropertyAccessExpression
): lua.Expression | undefined {
const name = node.name.text;
switch (name) {
case "PI":
const property = lua.createStringLiteral("pi", node.name);
const math = lua.createIdentifier("math", node.expression);
return lua.createTableIndexExpression(math, property, node);
case "E":
case "LN10":
case "LN2":
case "LOG10E":
case "LOG2E":
case "SQRT1_2":
case "SQRT2":
return lua.createNumericLiteral(Math[name], node);
default:
context.diagnostics.push(unsupportedProperty(node.name, "Math", name));
}
}
export function transformMathCall(
context: TransformationContext,
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
): lua.Expression | undefined {
const signature = context.checker.getResolvedSignature(node);
const params = transformArguments(context, node.arguments, signature);
const math = lua.createIdentifier("math");
const expressionName = calledMethod.name.text;
switch (expressionName) {
// Lua 5.3+: math.atan(y, x)
// Otherwise: math.atan2(y, x)
case "atan2": {
if (context.luaTarget === LuaTarget.Universal) {
return transformLuaLibFunction(context, LuaLibFeature.MathAtan2, node, ...params);
}
const useAtan2 =
context.luaTarget === LuaTarget.Lua50 ||
context.luaTarget === LuaTarget.Lua51 ||
context.luaTarget === LuaTarget.Lua52 ||
context.luaTarget === LuaTarget.LuaJIT ||
context.luaTarget === LuaTarget.Luau;
const method = lua.createStringLiteral(useAtan2 ? "atan2" : "atan");
return lua.createCallExpression(lua.createTableIndexExpression(math, method), params, node);
}
// (math.log(x) / Math.LNe)
case "log10":
case "log2": {
const log1 = lua.createTableIndexExpression(math, lua.createStringLiteral("log"));
const logCall1 = lua.createCallExpression(log1, params);
const e = lua.createNumericLiteral(expressionName === "log10" ? Math.LN10 : Math.LN2);
return lua.createBinaryExpression(logCall1, e, lua.SyntaxKind.DivisionOperator, node);
}
// math.log(1 + x)
case "log1p": {
const log = lua.createStringLiteral("log");
const one = lua.createNumericLiteral(1);
const add = lua.createBinaryExpression(
one,
params[0] ?? lua.createNilLiteral(),
lua.SyntaxKind.AdditionOperator
);
return lua.createCallExpression(lua.createTableIndexExpression(math, log), [add], node);
}
case "pow": {
// Translate to base ^ power
return lua.createBinaryExpression(
params[0] ?? lua.createNilLiteral(),
params[1] ?? lua.createNilLiteral(),
lua.SyntaxKind.PowerOperator,
node
);
}
// math.floor(x + 0.5)
case "round": {
const floor = lua.createStringLiteral("floor");
const half = lua.createNumericLiteral(0.5);
const add = lua.createBinaryExpression(
params[0] ?? lua.createNilLiteral(),
half,
lua.SyntaxKind.AdditionOperator
);
return lua.createCallExpression(lua.createTableIndexExpression(math, floor), [add], node);
}
case "sign": {
return transformLuaLibFunction(context, LuaLibFeature.MathSign, node, ...params);
}
case "trunc": {
return transformLuaLibFunction(context, LuaLibFeature.MathTrunc, node, ...params);
}
case "abs":
case "acos":
case "asin":
case "atan":
case "ceil":
case "cos":
case "exp":
case "floor":
case "log":
case "max":
case "min":
case "random":
case "sin":
case "sqrt":
case "tan": {
const method = lua.createStringLiteral(expressionName);
return lua.createCallExpression(lua.createTableIndexExpression(math, method), params, node);
}
default:
context.diagnostics.push(unsupportedProperty(calledMethod.name, "Math", expressionName));
}
}