diff options
author | Li Jin <dragon-fly@qq.com> | 2024-05-16 17:25:37 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2024-05-16 17:25:37 +0800 |
commit | fe317e2bdd9cb60b3c7cd347e21ce65cf90396e7 (patch) | |
tree | ed0b010b08f91fdb2bc9a4df18e8d638d47b1c7a /src | |
parent | c857b3b83c7485693bfcc2b65e9e6e95107396f4 (diff) | |
download | yuescript-fe317e2bdd9cb60b3c7cd347e21ce65cf90396e7.tar.gz yuescript-fe317e2bdd9cb60b3c7cd347e21ce65cf90396e7.tar.bz2 yuescript-fe317e2bdd9cb60b3c7cd347e21ce65cf90396e7.zip |
fix ambiguous issue in try-catch syntax.v0.23.4
Diffstat (limited to 'src')
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 69 |
1 files changed, 57 insertions, 12 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 9a632fb..c205031 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -75,7 +75,7 @@ static std::unordered_set<std::string> Metamethods = { | |||
75 | "close"s // Lua 5.4 | 75 | "close"s // Lua 5.4 |
76 | }; | 76 | }; |
77 | 77 | ||
78 | const std::string_view version = "0.23.3"sv; | 78 | const std::string_view version = "0.23.4"sv; |
79 | const std::string_view extension = "yue"sv; | 79 | const std::string_view extension = "yue"sv; |
80 | 80 | ||
81 | class CompileError : public std::logic_error { | 81 | class CompileError : public std::logic_error { |
@@ -4663,8 +4663,10 @@ private: | |||
4663 | return 503; | 4663 | return 503; |
4664 | } else if (target.value() == "5.4"sv) { | 4664 | } else if (target.value() == "5.4"sv) { |
4665 | return 504; | 4665 | return 504; |
4666 | } else if (target.value() == "5.5"sv) { | ||
4667 | return 505; | ||
4666 | } else { | 4668 | } else { |
4667 | throw CompileError("get invalid Lua target \""s + target.value() + "\", should be 5.1, 5.2, 5.3 or 5.4"s, x); | 4669 | throw CompileError("get invalid Lua target \""s + target.value() + "\", should be from 5.1 to 5.5"s, x); |
4668 | } | 4670 | } |
4669 | } | 4671 | } |
4670 | #ifndef YUE_NO_MACRO | 4672 | #ifndef YUE_NO_MACRO |
@@ -9112,7 +9114,9 @@ private: | |||
9112 | body->content.set(tryNode->catchBlock->block); | 9114 | body->content.set(tryNode->catchBlock->block); |
9113 | funLit->body.set(body); | 9115 | funLit->body.set(body); |
9114 | } | 9116 | } |
9115 | if (auto tryBlock = tryNode->func.as<Block_t>()) { | 9117 | ast_sel<false, Block_t, Exp_t> tryFunc; |
9118 | tryFunc.set(tryNode->func); | ||
9119 | if (auto tryBlock = tryFunc.as<Block_t>()) { | ||
9116 | BLOCK_START | 9120 | BLOCK_START |
9117 | BREAK_IF(tryBlock->statements.size() != 1); | 9121 | BREAK_IF(tryBlock->statements.size() != 1); |
9118 | auto stmt = static_cast<Statement_t*>(tryBlock->statements.front()); | 9122 | auto stmt = static_cast<Statement_t*>(tryBlock->statements.front()); |
@@ -9124,11 +9128,43 @@ private: | |||
9124 | auto chainValue = value->item.as<ChainValue_t>(); | 9128 | auto chainValue = value->item.as<ChainValue_t>(); |
9125 | BREAK_IF(!chainValue); | 9129 | BREAK_IF(!chainValue); |
9126 | BREAK_IF(!isChainValueCall(chainValue)); | 9130 | BREAK_IF(!isChainValueCall(chainValue)); |
9127 | tryNode->func.set(expListAssign->expList->exprs.front()); | 9131 | auto tmpChain = chainValue->new_ptr<ChainValue_t>(); |
9132 | tmpChain->items.dup(chainValue->items); | ||
9133 | tmpChain->items.pop_back(); | ||
9134 | auto var = singleVariableFrom(tmpChain, AccessType::None); | ||
9135 | BREAK_IF(var.empty()); | ||
9136 | tryFunc.set(expListAssign->expList->exprs.front()); | ||
9128 | BLOCK_END | 9137 | BLOCK_END |
9138 | } else { | ||
9139 | auto tryExp = tryFunc.as<Exp_t>(); | ||
9140 | bool needWrap = singleVariableFrom(tryExp, AccessType::None).empty(); | ||
9141 | BLOCK_START | ||
9142 | auto value = singleValueFrom(tryExp); | ||
9143 | BREAK_IF(!value); | ||
9144 | auto chainValue = value->item.as<ChainValue_t>(); | ||
9145 | BREAK_IF(!chainValue); | ||
9146 | BREAK_IF(!isChainValueCall(chainValue)); | ||
9147 | auto tmpChain = chainValue->new_ptr<ChainValue_t>(); | ||
9148 | tmpChain->items.dup(chainValue->items); | ||
9149 | tmpChain->items.pop_back(); | ||
9150 | auto var = singleVariableFrom(tmpChain, AccessType::None); | ||
9151 | BREAK_IF(var.empty()); | ||
9152 | needWrap = false; | ||
9153 | BLOCK_END | ||
9154 | if (needWrap) { | ||
9155 | auto expList = x->new_ptr<ExpList_t>(); | ||
9156 | expList->exprs.push_back(tryFunc); | ||
9157 | auto expListAssign = x->new_ptr<ExpListAssign_t>(); | ||
9158 | expListAssign->expList.set(expList); | ||
9159 | auto stmt = x->new_ptr<Statement_t>(); | ||
9160 | stmt->content.set(expListAssign); | ||
9161 | auto block = x->new_ptr<Block_t>(); | ||
9162 | block->statements.push_back(stmt); | ||
9163 | tryFunc.set(block); | ||
9164 | } | ||
9129 | } | 9165 | } |
9130 | if (auto tryBlock = tryNode->func.as<Block_t>()) { | 9166 | if (auto tryBlock = tryFunc.as<Block_t>()) { |
9131 | { | 9167 | if (getLuaTarget(tryBlock) >= 502 || !errHandler) { |
9132 | if (auto result = upValueFuncFrom(tryBlock)) { | 9168 | if (auto result = upValueFuncFrom(tryBlock)) { |
9133 | auto [funcName, args] = std::move(*result); | 9169 | auto [funcName, args] = std::move(*result); |
9134 | if (errHandler) { | 9170 | if (errHandler) { |
@@ -9150,6 +9186,7 @@ private: | |||
9150 | transformChainValue(pcall, out, ExpUsage::Closure); | 9186 | transformChainValue(pcall, out, ExpUsage::Closure); |
9151 | } | 9187 | } |
9152 | if (usage == ExpUsage::Common) { | 9188 | if (usage == ExpUsage::Common) { |
9189 | out.back().insert(0, indent()); | ||
9153 | out.back().append(nlr(x)); | 9190 | out.back().append(nlr(x)); |
9154 | } | 9191 | } |
9155 | return; | 9192 | return; |
@@ -9173,19 +9210,25 @@ private: | |||
9173 | transformChainValue(pcall, out, ExpUsage::Closure); | 9210 | transformChainValue(pcall, out, ExpUsage::Closure); |
9174 | } | 9211 | } |
9175 | if (usage == ExpUsage::Common) { | 9212 | if (usage == ExpUsage::Common) { |
9213 | out.back().insert(0, indent()); | ||
9176 | out.back().append(nlr(x)); | 9214 | out.back().append(nlr(x)); |
9177 | } | 9215 | } |
9178 | return; | 9216 | return; |
9179 | } else if (auto value = singleValueFrom(tryNode->func)) { | 9217 | } else if (auto value = singleValueFrom(tryFunc)) { |
9180 | BLOCK_START | 9218 | BLOCK_START |
9181 | auto chainValue = value->item.as<ChainValue_t>(); | 9219 | auto chainValue = value->item.as<ChainValue_t>(); |
9182 | BREAK_IF(!chainValue); | 9220 | BREAK_IF(!chainValue); |
9183 | BREAK_IF(!isChainValueCall(chainValue)); | 9221 | BREAK_IF(!isChainValueCall(chainValue)); |
9222 | auto tmpChain = chainValue->new_ptr<ChainValue_t>(); | ||
9223 | tmpChain->items.dup(chainValue->items); | ||
9224 | tmpChain->items.pop_back(); | ||
9225 | auto var = singleVariableFrom(tmpChain, AccessType::None); | ||
9226 | BREAK_IF(var.empty()); | ||
9184 | if (errHandler && getLuaTarget(x) < 502) { | 9227 | if (errHandler && getLuaTarget(x) < 502) { |
9185 | auto tryExp = toAst<Exp_t>("->"sv, x); | 9228 | auto tryExp = toAst<Exp_t>("->"sv, x); |
9186 | auto funLit = simpleSingleValueFrom(tryExp)->value.to<FunLit_t>(); | 9229 | auto funLit = simpleSingleValueFrom(tryExp)->value.to<FunLit_t>(); |
9187 | auto expList = x->new_ptr<ExpList_t>(); | 9230 | auto expList = x->new_ptr<ExpList_t>(); |
9188 | expList->exprs.push_back(tryNode->func); | 9231 | expList->exprs.push_back(tryFunc); |
9189 | auto expListAssign = x->new_ptr<ExpListAssign_t>(); | 9232 | auto expListAssign = x->new_ptr<ExpListAssign_t>(); |
9190 | expListAssign->expList.set(expList); | 9233 | expListAssign->expList.set(expList); |
9191 | auto stmt = x->new_ptr<Statement_t>(); | 9234 | auto stmt = x->new_ptr<Statement_t>(); |
@@ -9210,7 +9253,7 @@ private: | |||
9210 | if (errHandler) { | 9253 | if (errHandler) { |
9211 | auto xpcall = toAst<ChainValue_t>("xpcall()", x); | 9254 | auto xpcall = toAst<ChainValue_t>("xpcall()", x); |
9212 | auto invoke = ast_to<Invoke_t>(xpcall->items.back()); | 9255 | auto invoke = ast_to<Invoke_t>(xpcall->items.back()); |
9213 | invoke->args.push_back(tryNode->func); | 9256 | invoke->args.push_back(tryFunc); |
9214 | invoke->args.push_back(errHandler); | 9257 | invoke->args.push_back(errHandler); |
9215 | for (auto arg : args->objects()) { | 9258 | for (auto arg : args->objects()) { |
9216 | invoke->args.push_back(arg); | 9259 | invoke->args.push_back(arg); |
@@ -9219,7 +9262,7 @@ private: | |||
9219 | } else { | 9262 | } else { |
9220 | auto pcall = toAst<ChainValue_t>("pcall()", x); | 9263 | auto pcall = toAst<ChainValue_t>("pcall()", x); |
9221 | auto invoke = ast_to<Invoke_t>(pcall->items.back()); | 9264 | auto invoke = ast_to<Invoke_t>(pcall->items.back()); |
9222 | invoke->args.push_back(tryNode->func); | 9265 | invoke->args.push_back(tryFunc); |
9223 | for (auto arg : args->objects()) { | 9266 | for (auto arg : args->objects()) { |
9224 | invoke->args.push_back(arg); | 9267 | invoke->args.push_back(arg); |
9225 | } | 9268 | } |
@@ -9227,6 +9270,7 @@ private: | |||
9227 | } | 9270 | } |
9228 | } | 9271 | } |
9229 | if (usage == ExpUsage::Common) { | 9272 | if (usage == ExpUsage::Common) { |
9273 | out.back().insert(0, indent()); | ||
9230 | out.back().append(nlr(x)); | 9274 | out.back().append(nlr(x)); |
9231 | } | 9275 | } |
9232 | return; | 9276 | return; |
@@ -9235,16 +9279,17 @@ private: | |||
9235 | if (errHandler) { | 9279 | if (errHandler) { |
9236 | auto xpcall = toAst<ChainValue_t>("xpcall()", x); | 9280 | auto xpcall = toAst<ChainValue_t>("xpcall()", x); |
9237 | auto invoke = ast_to<Invoke_t>(xpcall->items.back()); | 9281 | auto invoke = ast_to<Invoke_t>(xpcall->items.back()); |
9238 | invoke->args.push_back(tryNode->func); | 9282 | invoke->args.push_back(tryFunc); |
9239 | invoke->args.push_back(errHandler); | 9283 | invoke->args.push_back(errHandler); |
9240 | transformChainValue(xpcall, out, ExpUsage::Closure); | 9284 | transformChainValue(xpcall, out, ExpUsage::Closure); |
9241 | } else { | 9285 | } else { |
9242 | auto pcall = toAst<ChainValue_t>("pcall()", x); | 9286 | auto pcall = toAst<ChainValue_t>("pcall()", x); |
9243 | auto invoke = ast_to<Invoke_t>(pcall->items.back()); | 9287 | auto invoke = ast_to<Invoke_t>(pcall->items.back()); |
9244 | invoke->args.push_back(tryNode->func); | 9288 | invoke->args.push_back(tryFunc); |
9245 | transformChainValue(pcall, out, ExpUsage::Closure); | 9289 | transformChainValue(pcall, out, ExpUsage::Closure); |
9246 | } | 9290 | } |
9247 | if (usage == ExpUsage::Common) { | 9291 | if (usage == ExpUsage::Common) { |
9292 | out.back().insert(0, indent()); | ||
9248 | out.back().append(nlr(x)); | 9293 | out.back().append(nlr(x)); |
9249 | } | 9294 | } |
9250 | } | 9295 | } |