Skip to content

Commit b57d824

Browse files
committed
Added list.slice() and destructing list types
1 parent d456030 commit b57d824

5 files changed

Lines changed: 116 additions & 44 deletions

File tree

dist/Transpiler.js

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var LuaTranspiler = /** @class */ (function () {
2727
function LuaTranspiler(checker) {
2828
this.indent = "";
2929
this.checker = checker;
30-
this.switchCounter = 0;
30+
this.genVarCounter = 0;
3131
this.transpilingSwitch = false;
3232
}
3333
// Transpile a source file
@@ -153,7 +153,7 @@ var LuaTranspiler = /** @class */ (function () {
153153
};
154154
LuaTranspiler.prototype.transpileBreak = function () {
155155
if (this.transpilingSwitch) {
156-
return this.indent + ("goto switchDone" + this.switchCounter + "\n");
156+
return this.indent + ("goto switchDone" + this.genVarCounter + "\n");
157157
}
158158
else {
159159
return this.indent + "break\n";
@@ -257,23 +257,23 @@ var LuaTranspiler = /** @class */ (function () {
257257
}
258258
_this.pushIndent();
259259
// Labels for fallthrough
260-
result += _this.indent + ("::switchCase" + (_this.switchCounter + index) + "::\n");
260+
result += _this.indent + ("::switchCase" + (_this.genVarCounter + index) + "::\n");
261261
_this.transpilingSwitch = true;
262262
clause.statements.forEach(function (statement) {
263263
result += _this.transpileNode(statement);
264264
});
265265
_this.transpilingSwitch = false;
266266
// If this goto is reached, fall through to the next case
267267
if (index < clauses.length - 1) {
268-
result += _this.indent + ("goto switchCase" + (_this.switchCounter + index + 1) + "\n");
268+
result += _this.indent + ("goto switchCase" + (_this.genVarCounter + index + 1) + "\n");
269269
}
270270
_this.popIndent();
271271
});
272272
result += this.indent + "end\n";
273-
result += this.indent + ("::switchDone" + this.switchCounter + "::\n");
273+
result += this.indent + ("::switchDone" + this.genVarCounter + "::\n");
274274
result += this.indent + "--------Switch statement end--------\n";
275275
//Increment counter for next switch statement
276-
this.switchCounter += clauses.length;
276+
this.genVarCounter += clauses.length;
277277
return result;
278278
};
279279
LuaTranspiler.prototype.transpileReturn = function (node) {
@@ -497,6 +497,8 @@ var LuaTranspiler = /** @class */ (function () {
497497
return "TS_some(" + caller + ", " + params + ")";
498498
case "every":
499499
return "TS_every(" + caller + ", " + params + ")";
500+
case "slice":
501+
return "TS_slice(" + caller + ", " + params + ")";
500502
default:
501503
throw new TranspileError("Unsupported array function: " + expression.name.escapedText, node);
502504
}
@@ -573,14 +575,36 @@ var LuaTranspiler = /** @class */ (function () {
573575
return result;
574576
};
575577
LuaTranspiler.prototype.transpileVariableDeclaration = function (node) {
576-
// Find variable identifier
577-
var identifier = node.name;
578-
if (node.initializer) {
578+
var _this = this;
579+
if (ts.isIdentifier(node.name)) {
580+
// Find variable identifier
581+
var identifier = node.name;
582+
if (node.initializer) {
583+
var value = this.transpileExpression(node.initializer);
584+
return "local " + identifier.escapedText + " = " + value;
585+
}
586+
else {
587+
return "local " + identifier.escapedText + " = nil";
588+
}
589+
}
590+
else if (ts.isArrayBindingPattern(node.name)) {
591+
// Destructuring type
579592
var value = this.transpileExpression(node.initializer);
580-
return "local " + identifier.escapedText + " = " + value;
593+
var parentName_1 = "__destr" + this.genVarCounter;
594+
this.genVarCounter++;
595+
var result_1 = "local " + parentName_1 + " = " + value + "\n";
596+
node.name.elements.forEach(function (elem, index) {
597+
if (!elem.dotDotDotToken) {
598+
result_1 += _this.indent + ("local " + elem.name.escapedText + " = " + parentName_1 + "[" + (index + 1) + "]\n");
599+
}
600+
else {
601+
result_1 += _this.indent + ("local " + elem.name.escapedText + " = TS_slice(" + parentName_1 + ", " + index + ")\n");
602+
}
603+
});
604+
return result_1;
581605
}
582606
else {
583-
return "local " + identifier.escapedText + " = nil";
607+
throw new TranspileError("Unsupported variable declaration type " + TSHelper_1.TSHelper.enumName(node.name.kind, ts.SyntaxKind), node);
584608
}
585609
};
586610
LuaTranspiler.prototype.transpileFunctionDeclaration = function (node) {

dist/lualib/typescript.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ function TS_filter(list, func)
3131
return out
3232
end
3333

34+
function TS_slice(list, startI, endI)
35+
endI = endI or (#list-1)
36+
local out = {}
37+
for i = startI + 1, endI + 1 do
38+
table.insert(out, list[i])
39+
end
40+
return out
41+
end
42+
43+
function TS_some(list, func)
44+
return #TS_filter(list, func) > 0
45+
end
46+
47+
function TS_every(list, func)
48+
return #list == #TS_filter(list, func)
49+
end
50+
3451
-- Set data structure implementation
3552
Set = Set or {}
3653
Set.__index = Set

src/Transpiler.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ export class LuaTranspiler {
2020

2121
indent: string;
2222
checker: ts.TypeChecker;
23-
switchCounter: number;
23+
genVarCounter: number;
2424
transpilingSwitch: boolean;
2525

2626
constructor(checker: ts.TypeChecker) {
2727
this.indent = "";
2828
this.checker = checker;
29-
this.switchCounter = 0;
29+
this.genVarCounter = 0;
3030
this.transpilingSwitch = false;
3131
}
3232

@@ -154,7 +154,7 @@ export class LuaTranspiler {
154154

155155
transpileBreak(): string {
156156
if (this.transpilingSwitch) {
157-
return this.indent + `goto switchDone${this.switchCounter}\n`;
157+
return this.indent + `goto switchDone${this.genVarCounter}\n`;
158158
} else {
159159
return this.indent + "break\n";
160160
}
@@ -283,7 +283,7 @@ export class LuaTranspiler {
283283
this.pushIndent();
284284

285285
// Labels for fallthrough
286-
result += this.indent + `::switchCase${this.switchCounter+index}::\n`;
286+
result += this.indent + `::switchCase${this.genVarCounter+index}::\n`;
287287

288288
this.transpilingSwitch = true;
289289
clause.statements.forEach(statement => {
@@ -293,17 +293,17 @@ export class LuaTranspiler {
293293

294294
// If this goto is reached, fall through to the next case
295295
if (index < clauses.length - 1) {
296-
result += this.indent + `goto switchCase${this.switchCounter + index + 1}\n`;
296+
result += this.indent + `goto switchCase${this.genVarCounter + index + 1}\n`;
297297
}
298298

299299
this.popIndent();
300300
});
301301
result += this.indent + "end\n";
302-
result += this.indent + `::switchDone${this.switchCounter}::\n`;
302+
result += this.indent + `::switchDone${this.genVarCounter}::\n`;
303303
result += this.indent + "--------Switch statement end--------\n";
304304

305305
//Increment counter for next switch statement
306-
this.switchCounter += clauses.length;
306+
this.genVarCounter += clauses.length;
307307
return result;
308308
}
309309

@@ -543,6 +543,8 @@ export class LuaTranspiler {
543543
return `TS_some(${caller}, ${params})`;
544544
case "every":
545545
return `TS_every(${caller}, ${params})`;
546+
case "slice":
547+
return `TS_slice(${caller}, ${params})`
546548
default:
547549
throw new TranspileError("Unsupported array function: " + expression.name.escapedText, node);
548550
}
@@ -633,13 +635,31 @@ export class LuaTranspiler {
633635
}
634636

635637
transpileVariableDeclaration(node: ts.VariableDeclaration): string {
636-
// Find variable identifier
637-
const identifier = <ts.Identifier>node.name;
638-
if (node.initializer) {
638+
if (ts.isIdentifier(node.name)) {
639+
// Find variable identifier
640+
const identifier = node.name;
641+
if (node.initializer) {
642+
const value = this.transpileExpression(node.initializer);
643+
return `local ${identifier.escapedText} = ${value}`;
644+
} else {
645+
return `local ${identifier.escapedText} = nil`;
646+
}
647+
} else if (ts.isArrayBindingPattern(node.name)) {
648+
// Destructuring type
639649
const value = this.transpileExpression(node.initializer);
640-
return `local ${identifier.escapedText} = ${value}`;
650+
let parentName = `__destr${this.genVarCounter}`;
651+
this.genVarCounter++;
652+
let result = `local ${parentName} = ${value}\n`;
653+
node.name.elements.forEach((elem: ts.BindingElement, index: number) => {
654+
if (!elem.dotDotDotToken) {
655+
result += this.indent + `local ${(<ts.Identifier>elem.name).escapedText} = ${parentName}[${index + 1}]\n`;
656+
} else {
657+
result += this.indent + `local ${(<ts.Identifier>elem.name).escapedText} = TS_slice(${parentName}, ${index})\n`;
658+
}
659+
});
660+
return result;
641661
} else {
642-
return `local ${identifier.escapedText} = nil`;
662+
throw new TranspileError("Unsupported variable declaration type " + tsEx.enumName(node.name.kind, ts.SyntaxKind), node);
643663
}
644664
}
645665

test/src/test2.lua

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
local globalString = "glob"
1+
myGlobalVar={a=3,b="hello"}
2+
local globalString = "3"
23
local input = {1,2}
34
local objTest = {a=3,["B"]=true,[input[0+1]]=5}
5+
local __destr0 = input
6+
local p = __destr0[1]
7+
local q = __destr0[2]
8+
local rest = TS_slice(__destr0, 2)
9+
410
TestClass = TestClass or {}
511
TestClass.__index = TestClass
612
function TestClass.new(construct, ...)
@@ -63,43 +69,43 @@ function Activate()
6369
end
6470
-------Switch statement start-------
6571
if a==1 then
66-
::switchCase0::
72+
::switchCase1::
6773
2+2
68-
goto switchCase1
74+
goto switchCase2
6975
elseif a==3 then
70-
::switchCase1::
76+
::switchCase2::
7177
return 5
72-
goto switchCase2
78+
goto switchCase3
7379
elseif a==4 then
74-
::switchCase2::
80+
::switchCase3::
7581
1+1
76-
goto switchDone0
77-
goto switchCase3
82+
goto switchDone1
83+
goto switchCase4
7884
else
79-
::switchCase3::
85+
::switchCase4::
8086
local b = 3
8187
end
82-
::switchDone0::
88+
::switchDone1::
8389
--------Switch statement end--------
8490
-------Switch statement start-------
8591
if a==1 then
86-
::switchCase4::
92+
::switchCase5::
8793
2+2
88-
goto switchCase5
94+
goto switchCase6
8995
elseif a==3 then
90-
::switchCase5::
96+
::switchCase6::
9197
return 5
92-
goto switchCase6
98+
goto switchCase7
9399
elseif a==4 then
94-
::switchCase6::
100+
::switchCase7::
95101
1+1
96-
goto switchDone4
97-
goto switchCase7
102+
goto switchDone5
103+
goto switchCase8
98104
else
99-
::switchCase7::
105+
::switchCase8::
100106
local b = 3
101107
end
102-
::switchDone4::
108+
::switchDone5::
103109
--------Switch statement end--------
104110
end
105111
local a = TestClass.new(true,3)

test/src/test2.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
declare function print(msg: any): void;
22

3-
var globalString = "glob";
3+
declare var myGlobalVar: any;
4+
myGlobalVar = {a: 3, b: "hello"};
5+
6+
const globalString = "3";
47

58
let input = [1,2];
69

7-
let objTest = {a: 3, "B": true, [input[0]]: 5}
10+
let objTest = {a: 3, "B": true, [input[0]]: 5};
11+
12+
let [p, q, ...rest] = input;
813

914
declare interface unit {
1015
GetParent(): unit;

0 commit comments

Comments
 (0)