-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathstring.ts
More file actions
212 lines (191 loc) · 9.33 KB
/
string.ts
File metadata and controls
212 lines (191 loc) · 9.33 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import * as ts from "typescript";
import { LuaTarget } from "../../CompilerOptions";
import * as lua from "../../LuaAST";
import { TransformationContext } from "../context";
import { unsupportedProperty } from "../utils/diagnostics";
import { addToNumericExpression, createNaN, getNumberLiteralValue, wrapInTable } from "../utils/lua-ast";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import { transformArguments, transformCallAndArguments } from "../visitors/call";
function createStringCall(methodName: string, tsOriginal: ts.Node, ...params: lua.Expression[]): lua.CallExpression {
const stringIdentifier = lua.createIdentifier("string");
return lua.createCallExpression(
lua.createTableIndexExpression(stringIdentifier, lua.createStringLiteral(methodName)),
params,
tsOriginal
);
}
export function transformStringPrototypeCall(
context: TransformationContext,
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
): lua.Expression | undefined {
const signature = context.checker.getResolvedSignature(node);
const [caller, params] = transformCallAndArguments(context, calledMethod.expression, node.arguments, signature);
const expressionName = calledMethod.name.text;
switch (expressionName) {
case "replace":
return transformLuaLibFunction(context, LuaLibFeature.StringReplace, node, caller, ...params);
case "replaceAll":
return transformLuaLibFunction(context, LuaLibFeature.StringReplaceAll, node, caller, ...params);
case "concat":
return lua.createCallExpression(
lua.createTableIndexExpression(lua.createIdentifier("table"), lua.createStringLiteral("concat")),
[wrapInTable(caller, ...params)],
node
);
case "indexOf": {
const stringExpression = createStringCall(
"find",
node,
caller,
params[0] ?? lua.createNilLiteral(),
params[1]
? // string.find handles negative indexes by making it relative to string end, but for indexOf it's the same as 0
lua.createCallExpression(
lua.createTableIndexExpression(lua.createIdentifier("math"), lua.createStringLiteral("max")),
[addToNumericExpression(params[1], 1), lua.createNumericLiteral(1)]
)
: lua.createNilLiteral(),
lua.createBooleanLiteral(true)
);
return lua.createBinaryExpression(
lua.createBinaryExpression(stringExpression, lua.createNumericLiteral(0), lua.SyntaxKind.OrOperator),
lua.createNumericLiteral(1),
lua.SyntaxKind.SubtractionOperator,
node
);
}
case "substr":
return transformLuaLibFunction(context, LuaLibFeature.StringSubstr, node, caller, ...params);
case "substring":
return transformLuaLibFunction(context, LuaLibFeature.StringSubstring, node, caller, ...params);
case "slice": {
const literalArg1 = getNumberLiteralValue(params[0]);
if (params[0] && literalArg1 !== undefined) {
let stringSubArgs: lua.Expression[] | undefined = [
addToNumericExpression(params[0], literalArg1 < 0 ? 0 : 1),
];
if (params[1]) {
const literalArg2 = getNumberLiteralValue(params[1]);
if (literalArg2 !== undefined) {
stringSubArgs.push(addToNumericExpression(params[1], literalArg2 < 0 ? -1 : 0));
} else {
stringSubArgs = undefined;
}
}
// Inline string.sub call if we know that both parameters are pure and aren't negative
if (stringSubArgs) {
return createStringCall("sub", node, caller, ...stringSubArgs);
}
}
return transformLuaLibFunction(context, LuaLibFeature.StringSlice, node, caller, ...params);
}
case "toLowerCase":
return createStringCall("lower", node, caller);
case "toUpperCase":
return createStringCall("upper", node, caller);
case "trim":
return transformLuaLibFunction(context, LuaLibFeature.StringTrim, node, caller);
case "trimEnd":
case "trimRight":
return transformLuaLibFunction(context, LuaLibFeature.StringTrimEnd, node, caller);
case "trimStart":
case "trimLeft":
return transformLuaLibFunction(context, LuaLibFeature.StringTrimStart, node, caller);
case "split":
return transformLuaLibFunction(context, LuaLibFeature.StringSplit, node, caller, ...params);
case "charAt": {
const literalValue = getNumberLiteralValue(params[0]);
// Inline string.sub call if we know that parameter is pure and isn't negative
if (literalValue !== undefined && literalValue >= 0) {
const firstParamPlusOne = addToNumericExpression(params[0] ?? lua.createNilLiteral(), 1);
return createStringCall("sub", node, caller, firstParamPlusOne, firstParamPlusOne);
}
return transformLuaLibFunction(context, LuaLibFeature.StringCharAt, node, caller, ...params);
}
case "charCodeAt": {
const literalValue = getNumberLiteralValue(params[0]);
// Inline string.sub call if we know that parameter is pure and isn't negative
if (literalValue !== undefined && literalValue >= 0) {
return lua.createBinaryExpression(
createStringCall(
"byte",
node,
caller,
addToNumericExpression(params[0] ?? lua.createNilLiteral(), 1)
),
createNaN(),
lua.SyntaxKind.OrOperator
);
}
return transformLuaLibFunction(context, LuaLibFeature.StringCharCodeAt, node, caller, ...params);
}
case "startsWith":
return transformLuaLibFunction(context, LuaLibFeature.StringStartsWith, node, caller, ...params);
case "endsWith":
return transformLuaLibFunction(context, LuaLibFeature.StringEndsWith, node, caller, ...params);
case "includes":
return transformLuaLibFunction(context, LuaLibFeature.StringIncludes, node, caller, ...params);
case "repeat":
const math = lua.createIdentifier("math");
const floor = lua.createStringLiteral("floor");
const parameter = lua.createCallExpression(lua.createTableIndexExpression(math, floor), [
params[0] ?? lua.createNilLiteral(),
]);
return createStringCall("rep", node, caller, parameter);
case "padStart":
return transformLuaLibFunction(context, LuaLibFeature.StringPadStart, node, caller, ...params);
case "padEnd":
return transformLuaLibFunction(context, LuaLibFeature.StringPadEnd, node, caller, ...params);
case "toString":
return; // will be handled by transformObjectPrototypeCall
default:
context.diagnostics.push(unsupportedProperty(calledMethod.name, "string", expressionName));
}
}
export function transformStringConstructorMethodCall(
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 expressionName = calledMethod.name.text;
switch (expressionName) {
case "fromCharCode":
return lua.createCallExpression(
lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("char")),
params,
node
);
default:
context.diagnostics.push(unsupportedProperty(calledMethod.name, "String", expressionName));
}
}
export function transformStringProperty(
context: TransformationContext,
node: ts.PropertyAccessExpression
): lua.Expression | undefined {
switch (node.name.text) {
case "length":
const expression = context.transformExpression(node.expression);
if (context.luaTarget === LuaTarget.Lua50) {
const stringLen = lua.createTableIndexExpression(
lua.createIdentifier("string"),
lua.createStringLiteral("len")
);
return lua.createCallExpression(stringLen, [expression], node);
} else {
return lua.createUnaryExpression(expression, lua.SyntaxKind.LengthOperator, node);
}
default:
context.diagnostics.push(unsupportedProperty(node.name, "string", node.name.text));
}
}
export function transformStringConstructorCall(
originalNode: ts.CallExpression,
...args: lua.Expression[]
): lua.Expression | undefined {
const tostring = lua.createIdentifier("tostring", originalNode.expression);
return lua.createCallExpression(tostring, args, originalNode);
}