-
-
Save SquidDev/2fb2ea604ff3f79ea09be018b9038c93 to your computer and use it in GitHub Desktop.
luafmt (https://github.com/trixnz/lua-fmt/) patch for binary operators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- a/src/printer.ts | |
+++ b/src/printer.ts | |
@@ -101,6 +101,17 @@ function isLastStatement(path: FastPath) { | |
return body && body[body.length - 1] === node; | |
} | |
+const binOps = { | |
+ ["~"]: "__bxor", | |
+ ["|"]: "__bor", | |
+ ["<<"]: "__shl", | |
+ [">>"]: "__shr", | |
+} | |
+ | |
+const unOps = { | |
+ ["~"]: "__bnot", | |
+} | |
+ | |
function printNodeNoParens(path: FastPath, options: Options, print: PrintFn) { | |
const value = path.getValue(); | |
@@ -111,6 +122,19 @@ function printNodeNoParens(path: FastPath, options: Options, print: PrintFn) { | |
const parts: Doc[] = []; | |
const node = value as luaparse.Node; | |
+ if(node.type == "BinaryExpression" && binOps[node.operator]) { | |
+ Object.assign(node, { | |
+ type: "CallExpression", loc: node.loc, range: node.range, inParens: false, | |
+ base: { type: "Identifier", loc: node.loc, range: node.range, inParens: false, name: binOps[node.operator] }, | |
+ arguments: [node.left, node.right], | |
+ }); | |
+ } else if (node.type == "UnaryExpression" && unOps[node.operator]) { | |
+ Object.assign(node, { | |
+ type: "CallExpression", loc: node.loc, range: node.range, inParens: false, | |
+ base: { type: "Identifier", loc: node.loc, range: node.range, inParens: false, name: binOps[node.operator] }, | |
+ arguments: [node.argument], | |
+ }); | |
+ } | |
switch (node.type) { | |
case 'Chunk': |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment