Skip to content

Commit

Permalink
fix:smart additon in case of binary expression contains string literal,
Browse files Browse the repository at this point in the history
close #4
  • Loading branch information
xiangnanscu committed Oct 9, 2024
1 parent 258cbcc commit 4502061
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ js2lua(`let a = 1`, { importStatementHoisting: true });
* [operator](#operator)
* [optionalNullish](#optionalNullish)
* [others](#others)
* [smartAddition](#smartAddition)
* [spread](#spread)
* [stringTemplate](#stringTemplate)
* [switch](#switch)
Expand Down Expand Up @@ -1005,6 +1006,29 @@ local constraints = (function()
return __tmp
end)()

```
## smartAddition
### js
```js
const s1 = b + c
const s2 = b + 'c'
const s3 = 'c' + b
const s4 = a + b + 'c'
const s7 = 'a' + b + c
const s5 = a + b + 'c' + d + f
const s6 = a + b + c + d + f

```
### lua
```lua
local s1 = b + c
local s2 = b .. "c"
local s3 = "c" .. b
local s4 = a .. b .. "c"
local s7 = "a" .. b .. c
local s5 = a .. b .. "c" .. d .. f
local s6 = a + b + c + d + f

```
## spread
### js
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xiangnanscu/js2lua",
"version": "0.43.0",
"version": "0.44.0",
"type": "module",
"description": "Writing LuaJIT with the expressiveness of JavaScript.",
"main": "src/js2lua.mjs",
Expand Down
33 changes: 32 additions & 1 deletion src/js2lua.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,37 @@ function ast2lua(ast, opts = {}) {
// tohex = <function 12>
case "BinaryExpression": {
const op = logicMap[ast.operator] || ast.operator;
// first handle addition operator
const isAdditionNode = (node) => node.type === "BinaryExpression" && node.operator === "+";
const containsString = (node) => {
if (node.type === "StringLiteral") return true;
if (isAdditionNode(node)) {
return containsString(node.left) || containsString(node.right);
}
return false;
};
const processAddition = (node) => {
if (!isAdditionNode(node)) {
return _ast2lua(node);
}
if (node.stringConcat) {
return `${_ast2lua(node.left)} .. ${_ast2lua(node.right)}`;
}
if (containsString(node.left) || containsString(node.right)) {
walkAst(node, (e) => {
if (isAdditionNode(e)) {
e.stringConcat = true;
}
});
return `${_ast2lua(node.left)} .. ${_ast2lua(node.right)}`;
} else {
return `${_ast2lua(node.left)} + ${_ast2lua(node.right)}`;
}
};
// then handle other operators
if (ast.operator === "+") {
return processAddition(ast);
}
const left = _ast2lua(ast.left);
const right = _ast2lua(ast.right);
if (ast.operator == "instanceof") {
Expand All @@ -599,7 +630,7 @@ function ast2lua(ast, opts = {}) {
} else if (ast.operator == "**") {
return `math.pow(${left}, ${right})`;
} else {
return `${left} ${op} ${_ast2lua(ast.right)}`;
return `${left} ${op} ${right}`;
}
}
case "UnaryExpression": {
Expand Down
7 changes: 7 additions & 0 deletions test/smartAddition.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const s1 = b + c
const s2 = b + 'c'
const s3 = 'c' + b
const s4 = a + b + 'c'
const s7 = 'a' + b + c
const s5 = a + b + 'c' + d + f
const s6 = a + b + c + d + f

0 comments on commit 4502061

Please sign in to comment.