Skip to content

Commit 87507f7

Browse files
committed
Added splice array call & delete expression
1 parent eb65053 commit 87507f7

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ node_modules/
33
*.lua
44
!dist/*.js
55
!dist/lualib/*.lua
6+
7+
# OSX
8+
.DS_Store

dist/Transpiler.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,12 @@ var LuaTranspiler = /** @class */ (function () {
303303
return result;
304304
};
305305
LuaTranspiler.prototype.transpileReturn = function (node) {
306-
return "return " + this.transpileExpression(node.expression);
306+
if (node.expression) {
307+
return "return " + this.transpileExpression(node.expression);
308+
}
309+
else {
310+
return "return";
311+
}
307312
};
308313
LuaTranspiler.prototype.transpileExpression = function (node, brackets) {
309314
switch (node.kind) {
@@ -345,6 +350,8 @@ var LuaTranspiler = /** @class */ (function () {
345350
return this.transpileArrayLiteral(node);
346351
case ts.SyntaxKind.ObjectLiteralExpression:
347352
return this.transpileObjectLiteral(node);
353+
case ts.SyntaxKind.DeleteExpression:
354+
return this.transpileExpression(node.expression) + "=nil";
348355
case ts.SyntaxKind.FunctionExpression:
349356
case ts.SyntaxKind.ArrowFunction:
350357
return this.transpileArrowFunction(node);
@@ -530,6 +537,9 @@ var LuaTranspiler = /** @class */ (function () {
530537
var caller = this.transpileExpression(expression.expression);
531538
switch (expression.name.escapedText) {
532539
case "push":
540+
if (node.arguments.length > 1) {
541+
throw new TranspileError("Unsupported array function: " + expression.name.escapedText + " with more than one argument", node);
542+
}
533543
return "table.insert(" + caller + ", " + params + ")";
534544
case "forEach":
535545
return "TS_forEach(" + caller + ", " + params + ")";
@@ -543,6 +553,8 @@ var LuaTranspiler = /** @class */ (function () {
543553
return "TS_every(" + caller + ", " + params + ")";
544554
case "slice":
545555
return "TS_slice(" + caller + ", " + params + ")";
556+
case "splice":
557+
return "TS_splice(" + caller + ", " + params + ")";
546558
default:
547559
throw new TranspileError("Unsupported array function: " + expression.name.escapedText, node);
548560
}

dist/lualib/typescript.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ function TS_slice(list, startI, endI)
4040
return out
4141
end
4242

43+
function TS_splice(list, startI, deleteCount, ...)
44+
if not deleteCount then
45+
deleteCount = #list - startI
46+
else if deleteCount > #list - startI then
47+
deleteCount = #list - startI
48+
end
49+
local out = {}
50+
for i = startI, deleteCount do
51+
table.insert(out, table.remove(list, i))
52+
end
53+
for _, value in ipairs({...}) do
54+
table.insert(list, value)
55+
end
56+
return out
57+
end
58+
4359
function TS_some(list, func)
4460
return #TS_filter(list, func) > 0
4561
end

src/Transpiler.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ export class LuaTranspiler {
384384
return this.transpileArrayLiteral(<ts.ArrayLiteralExpression>node);
385385
case ts.SyntaxKind.ObjectLiteralExpression:
386386
return this.transpileObjectLiteral(<ts.ObjectLiteralExpression>node);
387+
case ts.SyntaxKind.DeleteExpression:
388+
return this.transpileExpression((<ts.DeleteExpression>node).expression) + "=nil";
387389
case ts.SyntaxKind.FunctionExpression:
388390
case ts.SyntaxKind.ArrowFunction:
389391
return this.transpileArrowFunction(<ts.ArrowFunction>node);
@@ -410,7 +412,7 @@ export class LuaTranspiler {
410412
// Transpile operands
411413
const lhs = this.transpileExpression(node.left, true);
412414
const rhs = this.transpileExpression(node.right, true);
413-
415+
414416
// Rewrite some non-existant binary operators
415417
let result = "";
416418
switch (node.operatorToken.kind) {
@@ -581,6 +583,9 @@ export class LuaTranspiler {
581583
const caller = this.transpileExpression(expression.expression);
582584
switch (expression.name.escapedText) {
583585
case "push":
586+
if (node.arguments.length > 1) {
587+
throw new TranspileError("Unsupported array function: " + expression.name.escapedText + " with more than one argument", node);
588+
}
584589
return `table.insert(${caller}, ${params})`;
585590
case "forEach":
586591
return `TS_forEach(${caller}, ${params})`;
@@ -594,6 +599,8 @@ export class LuaTranspiler {
594599
return `TS_every(${caller}, ${params})`;
595600
case "slice":
596601
return `TS_slice(${caller}, ${params})`
602+
case "splice":
603+
return `TS_splice(${caller}, ${params})`
597604
default:
598605
throw new TranspileError("Unsupported array function: " + expression.name.escapedText, node);
599606
}
@@ -616,7 +623,7 @@ export class LuaTranspiler {
616623

617624
transpilePropertyAccessExpression(node: ts.PropertyAccessExpression): string {
618625
const property = node.name.text;
619-
626+
620627
// Check for primitive types to override
621628
const type = this.checker.getTypeAtLocation(node.expression);
622629
switch (type.flags) {
@@ -824,7 +831,7 @@ export class LuaTranspiler {
824831
// Add static declarations
825832
for (const field of staticFields) {
826833
const fieldName = (<ts.Identifier>field.name).escapedText;
827-
let value = this.transpileExpression(field.initializer);
834+
let value = this.transpileExpression(field.initializer);
828835
result += this.indent + `${className}.${fieldName} = ${value}\n`;
829836
}
830837

0 commit comments

Comments
 (0)