From e96308912e2e04535836a2c282b0a7300d2d81fd Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 11 Apr 2025 17:33:12 +0800 Subject: Fixing issue #206. --- spec/outputs/5.1/attrib.lua | 18 +++--- spec/outputs/5.1/try_catch.lua | 6 +- spec/outputs/assign.lua | 6 +- spec/outputs/attrib.lua | 18 +++--- spec/outputs/do.lua | 6 +- spec/outputs/syntax.lua | 8 ++- spec/outputs/try_catch.lua | 6 +- spec/outputs/unicode/assign.lua | 6 +- spec/outputs/unicode/attrib.lua | 18 +++--- spec/outputs/unicode/do.lua | 6 +- spec/outputs/unicode/macro.lua | 113 ++++++++++++++++++++++++------------- spec/outputs/unicode/syntax.lua | 8 ++- spec/outputs/unicode/try_catch.lua | 10 ++-- src/yuescript/yue_ast.h | 1 + src/yuescript/yue_compiler.cpp | 12 +++- 15 files changed, 150 insertions(+), 92 deletions(-) diff --git a/spec/outputs/5.1/attrib.lua b/spec/outputs/5.1/attrib.lua index a156e84..bda24bc 100644 --- a/spec/outputs/5.1/attrib.lua +++ b/spec/outputs/5.1/attrib.lua @@ -136,9 +136,11 @@ do end local b if not false then - if x then - b = 1 - end + b = ((function() + if x then + return 1 + end + end)()) end local _close_0 if (function() @@ -164,10 +166,12 @@ do end)(pcall(function() local c if true then - local _exp_0 = x - if "abc" == _exp_0 then - c = 998 - end + c = ((function() + local _exp_0 = x + if "abc" == _exp_0 then + return 998 + end + end)()) end local d if (function() diff --git a/spec/outputs/5.1/try_catch.lua b/spec/outputs/5.1/try_catch.lua index d4c80c1..efd92c6 100644 --- a/spec/outputs/5.1/try_catch.lua +++ b/spec/outputs/5.1/try_catch.lua @@ -8,10 +8,10 @@ local _anon_func_2 = function(tb) return tb.func() end local _anon_func_3 = function(tb) - return tb.func() + return (tb.func()) end local _anon_func_4 = function(tb) - return tb:func(1, 2, 3) + return (tb:func(1, 2, 3)) end local _anon_func_5 = function(tb) return tb.func(1) @@ -64,7 +64,7 @@ f = function() print("OK") end if xpcall(function() - return func(1) + return (func(1)) end, function(err) return print(err) end) then diff --git a/spec/outputs/assign.lua b/spec/outputs/assign.lua index f889865..162c5a8 100644 --- a/spec/outputs/assign.lua +++ b/spec/outputs/assign.lua @@ -36,10 +36,10 @@ local x do local f = getHandler() if f then - do + x = ((function() f() - x = 123 - end + return 123 + end)()) end end local _anon_func_0 = function(print) diff --git a/spec/outputs/attrib.lua b/spec/outputs/attrib.lua index e48963c..bb9916c 100644 --- a/spec/outputs/attrib.lua +++ b/spec/outputs/attrib.lua @@ -56,17 +56,21 @@ do end local b if not false then - if x then - b = 1 - end + b = ((function() + if x then + return 1 + end + end)()) end local _close_0 = b local c if true then - local _exp_0 = x - if "abc" == _exp_0 then - c = 998 - end + c = ((function() + local _exp_0 = x + if "abc" == _exp_0 then + return 998 + end + end)()) end local d if (function() diff --git a/spec/outputs/do.lua b/spec/outputs/do.lua index 6473e03..96d1022 100644 --- a/spec/outputs/do.lua +++ b/spec/outputs/do.lua @@ -32,10 +32,10 @@ local t = { } return function(y, k) if y == nil then - do + y = ((function() x = 10 + 2 - y = x - end + return x + end)()) end if k == nil then do diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index 5fd1821..040a325 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -239,9 +239,11 @@ x = 0 local _list_0 = values for _index_0 = 1, #_list_0 do local v = _list_0[_index_0] - if ntype(v) == "fndef" then - _ = x + 1 - end + _ = ((function() + if ntype(v) == "fndef" then + return x + 1 + end + end)()) end hello = { something = world, diff --git a/spec/outputs/try_catch.lua b/spec/outputs/try_catch.lua index d4c80c1..efd92c6 100644 --- a/spec/outputs/try_catch.lua +++ b/spec/outputs/try_catch.lua @@ -8,10 +8,10 @@ local _anon_func_2 = function(tb) return tb.func() end local _anon_func_3 = function(tb) - return tb.func() + return (tb.func()) end local _anon_func_4 = function(tb) - return tb:func(1, 2, 3) + return (tb:func(1, 2, 3)) end local _anon_func_5 = function(tb) return tb.func(1) @@ -64,7 +64,7 @@ f = function() print("OK") end if xpcall(function() - return func(1) + return (func(1)) end, function(err) return print(err) end) then diff --git a/spec/outputs/unicode/assign.lua b/spec/outputs/unicode/assign.lua index bf43953..d4ad56a 100644 --- a/spec/outputs/unicode/assign.lua +++ b/spec/outputs/unicode/assign.lua @@ -36,10 +36,10 @@ local _u53d8_u91cfx do local _u51fd_u6570 = _u83b7_u53d6_u5904_u7406_u51fd_u6570() if _u51fd_u6570 then - do + _u53d8_u91cfx = ((function() _u51fd_u6570() - _u53d8_u91cfx = 123 - end + return 123 + end)()) end end local _anon_func_0 = function(_u6253_u5370) diff --git a/spec/outputs/unicode/attrib.lua b/spec/outputs/unicode/attrib.lua index 1c48de4..5e5bb99 100644 --- a/spec/outputs/unicode/attrib.lua +++ b/spec/outputs/unicode/attrib.lua @@ -48,17 +48,21 @@ do end local _u5173_u95ed_u53d8_u91cfb if not false then - if _u6761_u4ef6x then - _u5173_u95ed_u53d8_u91cfb = 1 - end + _u5173_u95ed_u53d8_u91cfb = ((function() + if _u6761_u4ef6x then + return 1 + end + end)()) end local _close_0 = _u5173_u95ed_u53d8_u91cfb local _u5e38_u91cfc if true then - local _exp_0 = _u6761_u4ef6x - if "abc" == _exp_0 then - _u5e38_u91cfc = 998 - end + _u5e38_u91cfc = ((function() + local _exp_0 = _u6761_u4ef6x + if "abc" == _exp_0 then + return 998 + end + end)()) end local _u5173_u95ed_u53d8_u91cfd if (function() diff --git a/spec/outputs/unicode/do.lua b/spec/outputs/unicode/do.lua index f9c3079..7bf1da3 100644 --- a/spec/outputs/unicode/do.lua +++ b/spec/outputs/unicode/do.lua @@ -32,10 +32,10 @@ local _u53d8_u91cft = { } return function(_u53c2_u6570y, _u53c2_u6570k) if _u53c2_u6570y == nil then - do + _u53c2_u6570y = ((function() _u53d8_u91cfx = 10 + 2 - _u53c2_u6570y = _u53d8_u91cfx - end + return _u53d8_u91cfx + end)()) end if _u53c2_u6570k == nil then do diff --git a/spec/outputs/unicode/macro.lua b/spec/outputs/unicode/macro.lua index 099080f..b14f571 100644 --- a/spec/outputs/unicode/macro.lua +++ b/spec/outputs/unicode/macro.lua @@ -215,52 +215,85 @@ do -- 这有个注释 end local _ = require('下划线') -local _call_0 = (_({ - 1, - 2, - 3, - 4, - -2, - 3 -})) -_call_0 = _call_0["链"](_call_0) -_call_0 = _call_0["映射"](_call_0, function(self) - return self * 2 -end) -_call_0 = _call_0["过滤"](_call_0, function(self) - return self > 3 -end) -local _u7ed3_u679ca = _call_0["取值"](_call_0) -do - local _call_1 = (_({ - 1, - 2, - 3, - 4, - -2, - 3 - })) - _call_1 = _call_1["链"](_call_1) - _call_1 = _call_1["映射"](_call_1, function(self) - return self * 2 - end) - _call_1 = _call_1["过滤"](_call_1, function(self) +local _anon_func_0 = function(_) + do + local _call_0 = (_({ + 1, + 2, + 3, + 4, + -2, + 3 + })) + return _call_0["链"](_call_0) + end +end +local _call_0 = ((function() + local _call_0 = ((function() + local _call_0 = (_anon_func_0(_)) + return _call_0["映射"](_call_0, function(self) + return self * 2 + end) + end)()) + return _call_0["过滤"](_call_0, function(self) return self > 3 end) +end)()) +local _u7ed3_u679ca = _call_0["取值"](_call_0) +local _anon_func_1 = function(_) + do + local _call_1 = (_({ + 1, + 2, + 3, + 4, + -2, + 3 + })) + return _call_1["链"](_call_1) + end +end +do + local _call_1 = ((function() + local _call_1 = ((function() + local _call_1 = (_anon_func_1(_)) + return _call_1["映射"](_call_1, function(self) + return self * 2 + end) + end)()) + return _call_1["过滤"](_call_1, function(self) + return self > 3 + end) + end)()) _call_1["每一个"](_call_1, function(self) return _u6253_u5370(self) end) end -local _call_1 = _u539f_u70b9["变换"]["根节点"]["游戏对象"] -_call_1 = _call_1["父节点"](_call_1) -_call_1 = _call_1["后代"](_call_1) -_call_1 = _call_1["选择启用"](_call_1) -_call_1 = _call_1["选择可见"](_call_1) -_call_1 = _call_1["标签等于"](_call_1, "fx") -_call_1 = _call_1["其中"](_call_1, function(x) - local _call_2 = x["名称"] - return _call_2["结尾为"](_call_2, "(克隆)") -end) +local _anon_func_2 = function(_u539f_u70b9) + do + local _call_1 = _u539f_u70b9["变换"]["根节点"]["游戏对象"] + return _call_1["父节点"](_call_1) + end +end +local _call_1 = ((function() + local _call_1 = ((function() + local _call_1 = ((function() + local _call_1 = ((function() + local _call_1 = ((function() + local _call_1 = (_anon_func_2(_u539f_u70b9)) + return _call_1["后代"](_call_1) + end)()) + return _call_1["选择启用"](_call_1) + end)()) + return _call_1["选择可见"](_call_1) + end)()) + return _call_1["标签等于"](_call_1, "fx") + end)()) + return _call_1["其中"](_call_1, function(x) + local _call_2 = x["名称"] + return _call_2["结尾为"](_call_2, "(克隆)") + end) +end)()) _u7ed3_u679c = _call_1["摧毁"](_call_1) do do diff --git a/spec/outputs/unicode/syntax.lua b/spec/outputs/unicode/syntax.lua index ea97bb9..1984f40 100644 --- a/spec/outputs/unicode/syntax.lua +++ b/spec/outputs/unicode/syntax.lua @@ -258,9 +258,11 @@ _u53d8_u91cfx = 0 local _list_0 = _u503c for _index_0 = 1, #_list_0 do local _u53d8_u91cfv = _list_0[_index_0] - if ntype(_u53d8_u91cfv) == "函数定义" then - _ = _u53d8_u91cfx + 1 - end + _ = ((function() + if ntype(_u53d8_u91cfv) == "函数定义" then + return _u53d8_u91cfx + 1 + end + end)()) end _u4f60_u597d = { ["某物"] = _u4e16_u754c, diff --git a/spec/outputs/unicode/try_catch.lua b/spec/outputs/unicode/try_catch.lua index 22f29f9..f8c7849 100644 --- a/spec/outputs/unicode/try_catch.lua +++ b/spec/outputs/unicode/try_catch.lua @@ -32,11 +32,13 @@ pcall(function() return _u8868["函数"]() end) pcall(function() - return _u8868["函数"]() + return (_u8868["函数"]()) end) pcall(function() - local _call_0 = _u8868 - return _call_0["函数"](_call_0, 1, 2, 3) + return ((function() + local _call_0 = _u8868 + return _call_0["函数"](_call_0, 1, 2, 3) + end)()) end) pcall(function() return _u8868["函数"](1) @@ -52,7 +54,7 @@ end)) then _u6253_u5370("好的") end if xpcall(function() - return _u51fd_u6570(1) + return (_u51fd_u6570(1)) end, function(_u9519_u8bef) return _u6253_u5370(_u9519_u8bef) end) then diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index e670126..5e70645 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -620,6 +620,7 @@ AST_END(Slice) AST_NODE(Parens) ast_ptr expr; + bool extra = false; AST_MEMBER(Parens, &expr) AST_END(Parens) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index a2a1864..68ce9b5 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -75,7 +75,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.27.3"sv; +const std::string_view version = "0.27.4"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -1080,8 +1080,8 @@ private: if (unary->ops.empty()) { Value_t* value = static_cast(unary->expos.back()); if (auto chain = ast_cast(value->item); chain && chain->items.size() == 1) { - if (auto exp = chain->get_by_path()) { - if (auto insideValue = singleValueFrom(exp)) { + if (auto parens = chain->get_by_path(); parens && parens->extra) { + if (auto insideValue = singleValueFrom(parens->expr)) { return insideValue; } } @@ -3262,6 +3262,7 @@ private: } else if (destruct.items.size() == 1 && !singleValueFrom(*j)) { auto p = destruct.value.get(); auto parens = p->new_ptr(); + parens->extra = true; if (auto tableBlock = ast_cast(p)) { auto tableLit = p->new_ptr(); tableLit->values.dup(tableBlock->values); @@ -4750,6 +4751,7 @@ private: newSimpleValue->value.set(funLit); auto newExpInParens = newExp(newSimpleValue, x); auto newParens = x->new_ptr(); + newParens->extra = true; newParens->expr.set(newExpInParens); auto newCallable = x->new_ptr(); newCallable->item.set(newParens); @@ -5570,6 +5572,7 @@ private: auto x = chainList.front(); if (ast_is(chainList.back())) { auto parens = x->new_ptr(); + parens->extra = true; { auto chainValue = x->new_ptr(); for (auto item : chainList) { @@ -6163,6 +6166,7 @@ private: ++next; if (next != chainList.end()) { auto paren = x->new_ptr(); + paren->extra = true; paren->expr.set(newExp(chainValue, x)); auto ncallable = x->new_ptr(); ncallable->item.set(paren); @@ -6215,6 +6219,7 @@ private: simpleValue->value.set(funLit); auto exp = newExp(simpleValue, x); auto paren = x->new_ptr(); + paren->extra = true; paren->expr.set(exp); auto callable = x->new_ptr(); callable->item.set(paren); @@ -6631,6 +6636,7 @@ private: exp.set(info.node); if (!exp->opValues.empty() || (chainList.size() > 2 || (chainList.size() == 2 && !ast_is(chainList.back())))) { auto paren = x->new_ptr(); + paren->extra = true; paren->expr.set(exp); auto callable = x->new_ptr(); callable->item.set(paren); -- cgit v1.2.3-55-g6feb