diff options
author | Li Jin <dragon-fly@qq.com> | 2024-03-19 09:12:47 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2024-03-19 09:12:47 +0800 |
commit | 8279fa0231a5f4f2d34c46863462ab7228b9a338 (patch) | |
tree | 87a341859cc1e594549f05a881f4cefc89ae8df1 /src | |
parent | afc86619d710a578dc3f2bfc2ee1d62913f9abe8 (diff) | |
download | yuescript-8279fa0231a5f4f2d34c46863462ab7228b9a338.tar.gz yuescript-8279fa0231a5f4f2d34c46863462ab7228b9a338.tar.bz2 yuescript-8279fa0231a5f4f2d34c46863462ab7228b9a338.zip |
fix anonymous function args order.
Diffstat (limited to 'src')
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index d416dbd..de41b45 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -3653,7 +3653,7 @@ private: | |||
3653 | } | 3653 | } |
3654 | } | 3654 | } |
3655 | 3655 | ||
3656 | std::optional<std::pair<std::string, str_list>> upValueFuncFrom(Exp_t* exp, str_list* ensureArgList = nullptr) { | 3656 | std::optional<std::pair<std::string, str_list>> upValueFuncFrom(Exp_t* exp, str_list* ensureArgListInTheEnd = nullptr) { |
3657 | if (_funcLevel <= 1) return std::nullopt; | 3657 | if (_funcLevel <= 1) return std::nullopt; |
3658 | auto result = exp->traverse([&](ast_node* node) { | 3658 | auto result = exp->traverse([&](ast_node* node) { |
3659 | switch (node->get_id()) { | 3659 | switch (node->get_id()) { |
@@ -3725,25 +3725,33 @@ private: | |||
3725 | } | 3725 | } |
3726 | if (!upVarsAssignedOrCaptured) { | 3726 | if (!upVarsAssignedOrCaptured) { |
3727 | auto x = exp; | 3727 | auto x = exp; |
3728 | if (usedVar) { | 3728 | if (ensureArgListInTheEnd) { |
3729 | args.push_back("..."s); | ||
3730 | } | ||
3731 | if (ensureArgList) { | ||
3732 | std::unordered_set<std::string> vars; | 3729 | std::unordered_set<std::string> vars; |
3733 | for (const auto& arg : args) { | 3730 | for (const auto& arg : args) { |
3734 | vars.insert(arg); | 3731 | vars.insert(arg); |
3735 | } | 3732 | } |
3736 | for (const auto& arg : *ensureArgList) { | 3733 | for (const auto& arg : *ensureArgListInTheEnd) { |
3737 | vars.erase(arg); | 3734 | vars.erase(arg); |
3738 | } | 3735 | } |
3739 | str_list finalArgs; | 3736 | str_list finalArgs; |
3740 | for (const auto& arg : vars) { | 3737 | for (const auto& arg : vars) { |
3741 | finalArgs.push_back(arg); | 3738 | finalArgs.push_back(arg); |
3742 | } | 3739 | } |
3743 | for (const auto& arg : *ensureArgList) { | 3740 | finalArgs.sort(); |
3741 | for (const auto& arg : *ensureArgListInTheEnd) { | ||
3744 | finalArgs.push_back(arg); | 3742 | finalArgs.push_back(arg); |
3745 | } | 3743 | } |
3744 | if (usedVar) { | ||
3745 | if (finalArgs.back() != "..."sv) { | ||
3746 | finalArgs.push_back("..."s); | ||
3747 | } | ||
3748 | } | ||
3746 | args = std::move(finalArgs); | 3749 | args = std::move(finalArgs); |
3750 | } else { | ||
3751 | args.sort(); | ||
3752 | if (usedVar) { | ||
3753 | args.push_back("..."s); | ||
3754 | } | ||
3747 | } | 3755 | } |
3748 | auto funLit = toAst<FunLit_t>("("s + join(args, ","sv) + ")-> nil"s, x); | 3756 | auto funLit = toAst<FunLit_t>("("s + join(args, ","sv) + ")-> nil"s, x); |
3749 | funLit->body->content.set(stmt.get()); | 3757 | funLit->body->content.set(stmt.get()); |
@@ -8070,7 +8078,7 @@ private: | |||
8070 | auto chainValue = static_cast<ChainValue_t*>(value->item.get()); | 8078 | auto chainValue = static_cast<ChainValue_t*>(value->item.get()); |
8071 | if (auto callable = ast_cast<Callable_t>(chainValue->items.front()); callable && chainValue->items.size() == 1) { | 8079 | if (auto callable = ast_cast<Callable_t>(chainValue->items.front()); callable && chainValue->items.size() == 1) { |
8072 | if (auto self = callable->item.as<SelfItem_t>()) { | 8080 | if (auto self = callable->item.as<SelfItem_t>()) { |
8073 | if (auto selfVar = self->name.as<Self_t>()) { | 8081 | if (self->name.as<Self_t>()) { |
8074 | classTextName = "\"self\""; | 8082 | classTextName = "\"self\""; |
8075 | } | 8083 | } |
8076 | } else if (auto var = callable->item.as<Variable_t>()) { | 8084 | } else if (auto var = callable->item.as<Variable_t>()) { |