Skip to content

Commit b73cf9d

Browse files
committed
Binary operator rewrites for +=, -=, &&, ||, & and |
1 parent d7d314b commit b73cf9d

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

Transpiler.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,26 @@ export class LuaTranspiler {
225225
}
226226

227227
static transpileBinaryExpression(node: ts.BinaryExpression): string {
228-
return this.transpileExpression(node.left) + this.transpileOperator(node.operatorToken) + this.transpileExpression(node.right);
228+
// Transpile operands
229+
const lhs = this.transpileExpression(node.left);
230+
const rhs = this.transpileExpression(node.right);
231+
// Rewrite some non-existant binary operators
232+
switch (node.operatorToken.kind) {
233+
case ts.SyntaxKind.PlusEqualsToken:
234+
return `${lhs}=${lhs}+${rhs}`;
235+
case ts.SyntaxKind.MinusEqualsToken:
236+
return `${lhs}=${lhs}-${rhs}`;
237+
case ts.SyntaxKind.AmpersandAmpersandToken:
238+
return `${lhs} and ${rhs}`;
239+
case ts.SyntaxKind.BarBarToken:
240+
return `${lhs} or ${rhs}`;
241+
case ts.SyntaxKind.AmpersandToken:
242+
return `bit.band(${lhs},${rhs})`;
243+
case ts.SyntaxKind.BarToken:
244+
return `bit.bor(${lhs},${rhs})`;
245+
default:
246+
return lhs + this.transpileOperator(node.operatorToken) + rhs;
247+
}
229248
}
230249

231250
// Replace some missmatching operators

test/test2.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ function Activate() {
5858
++i;
5959
i+=1;
6060
i-=1;
61+
let a = 24 & 4;
6162

6263
let list = [1,2,3];
6364

0 commit comments

Comments
 (0)