Skip to content

Commit d2418e5

Browse files
committed
String array function overrides
1 parent afb2a57 commit d2418e5

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

Transpiler.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,26 +367,66 @@ export class LuaTranspiler {
367367
}
368368

369369
transpileCallExpression(node: ts.CallExpression): string {
370+
// Check for calls on primitives to override
371+
if (ts.isPropertyAccessExpression(node.expression)) {
372+
const type = this.checker.getTypeAtLocation(node.expression.expression);
373+
switch (type.flags) {
374+
case ts.TypeFlags.String:
375+
case ts.TypeFlags.StringLiteral:
376+
return this.transpileStringCallExpression(node);
377+
case ts.TypeFlags.Object:
378+
if (tsEx.isArrayType(type))
379+
return this.transpileArrayCallExpression(node);
380+
}
381+
}
382+
370383
let callPath = this.transpileExpression(node.expression);
371384
// Remove last . with :
372385
if (callPath.indexOf(".") > -1) {
373386
callPath = callPath.substr(0, callPath.lastIndexOf(".")) + ":" + callPath.substr(callPath.lastIndexOf(".") + 1);
374387
}
375388

389+
const params = this.transpileArguments(node.arguments);
390+
return `${callPath}(${params})`;
391+
}
392+
393+
transpileStringCallExpression(node: ts.CallExpression): string {
394+
const expression = <ts.PropertyAccessExpression>node.expression;
395+
const params = this.transpileArguments(node.arguments);
396+
const caller = this.transpileExpression(expression.expression);
397+
switch (expression.name.escapedText) {
398+
case "replace":
399+
return `${caller}:sub(${params})`;
400+
default:
401+
throw new TranspileError("Unsupported string function: " + expression.name.escapedText, node);
402+
}
403+
}
404+
405+
transpileArrayCallExpression(node: ts.CallExpression): string {
406+
const expression = <ts.PropertyAccessExpression>node.expression;
407+
const params = this.transpileArguments(node.arguments);
408+
const caller = this.transpileExpression(expression.expression);
409+
switch (expression.name.escapedText) {
410+
case "push":
411+
return `table.insert(${caller}, ${params})`;
412+
default:
413+
throw new TranspileError("Unsupported array function: " + expression.name.escapedText, node);
414+
}
415+
}
416+
417+
transpileArguments(params: ts.NodeArray<ts.Expression>): string {
376418
const parameters = [];
377-
node.arguments.forEach(param => {
419+
params.forEach(param => {
378420
parameters.push(this.transpileExpression(param));
379421
});
380-
381-
return `${callPath}(${parameters.join(",")})`;
422+
return parameters.join(",");
382423
}
383424

384425
transpilePropertyAccessExpression(node: ts.PropertyAccessExpression): string {
385426
const property = node.name.text;
386427

387428
// Check for primitive types to override
388429
const type = this.checker.getTypeAtLocation(node.expression);
389-
console.log(type.flags);
390430
switch (type.flags) {
391431
case ts.TypeFlags.String:
392432
case ts.TypeFlags.StringLiteral:

test/test3.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ let f = c.d.length;
77
let g = c.e.length;
88
let h = "abc".length;
99
b.push(3);
10+
a.replace("a", "b");
1011
//let d = a.indexOf("b");

0 commit comments

Comments
 (0)