From c925dbea9c9108a18b9abf4a57d9d59b0ed04652 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 7 May 2025 12:46:39 +0800 Subject: Tried adding more table matching functions. --- src/3rdParty/efsw/FileWatcherGeneric.cpp | 2 +- src/yuescript/yue_compiler.cpp | 78 +++++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/src/3rdParty/efsw/FileWatcherGeneric.cpp b/src/3rdParty/efsw/FileWatcherGeneric.cpp index 3f3c52e..468d27c 100644 --- a/src/3rdParty/efsw/FileWatcherGeneric.cpp +++ b/src/3rdParty/efsw/FileWatcherGeneric.cpp @@ -25,7 +25,7 @@ FileWatcherGeneric::~FileWatcherGeneric() { } WatchID FileWatcherGeneric::addWatch( const std::string& directory, FileWatchListener* watcher, - bool recursive, const std::vector& options ) { + bool recursive, const std::vector& /*options*/ ) { std::string dir( directory ); FileSystem::dirAddSlashAtEnd( dir ); diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 590c502..06f0d6d 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -78,7 +78,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.27.5"sv; +const std::string_view version = "0.27.6"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -2807,8 +2807,26 @@ private: defVal = nd->defVal.get(); } ++index; - if (!varDefOnly && !isAssignable(static_cast(pair))) { - throw CompileError("can't destructure value"sv, pair); + bool assignable = false; + try { + assignable = isAssignable(static_cast(pair)); + } catch (const CompileError& e) { + if (!varDefOnly) throw e; + } + if (!assignable && !varDefOnly) { + if (optional) { + if (defVal) { + throw CompileError("default value is not supported here"sv, defVal); + } + auto exp = static_cast(pair); + auto chain = exp->new_ptr(); + auto indexItem = toAst(std::to_string(index), exp); + chain->items.push_back(indexItem); + pairs.push_back({exp, Empty, chain, nullptr}); + break; + } else { + throw CompileError("can't destructure value"sv, pair); + } } auto value = singleValueFrom(pair); auto item = value->item.get(); @@ -2889,7 +2907,25 @@ private: } } if (auto exp = np->value.as()) { - if (!varDefOnly && !isAssignable(exp)) throw CompileError("can't do destructure value"sv, exp); + bool assignable = false; + try { + assignable = isAssignable(exp); + } catch (const CompileError& e) { + if (!varDefOnly) throw e; + } + if (!assignable && !varDefOnly) { + if (optional) { + if (defVal) { + throw CompileError("default value is not supported here"sv, defVal); + } + auto chain = exp->new_ptr(); + if (keyIndex) chain->items.push_back(keyIndex); + pairs.push_back({exp, Empty, chain, nullptr}); + break; + } else { + throw CompileError("can't destructure value"sv, exp); + } + } auto item = singleValueFrom(exp)->item.get(); ast_node* subExp = ast_cast(item); if (subExp || (subExp = item->get_by_path()) || (subExp = item->get_by_path())) { @@ -3111,7 +3147,11 @@ private: break; default: YUEE("AST node mismatch", destructNode); break; } - if (dlist->empty()) throw CompileError("expect items to be destructured"sv, destructNode); + if (dlist->empty()) { + if (!optional) { + throw CompileError("expect items to be destructured"sv, destructNode); + } + } for (auto item : *dlist) { switch (item->get_id()) { case id(): { @@ -3247,7 +3287,9 @@ private: simpleValue->value.set(tab); auto pairs = destructFromExp(newExp(simpleValue, expr), varDefOnly, optional); if (pairs.empty()) { - throw CompileError("expect items to be destructured"sv, tab); + if (!optional) { + throw CompileError("expect items to be destructured"sv, tab); + } } destruct.items = std::move(pairs); if (!varDefOnly) { @@ -3286,7 +3328,7 @@ private: destruct.valueVar.clear(); } } - destructs.push_back(destruct); + destructs.push_back(std::move(destruct)); } } } else { @@ -10597,8 +10639,9 @@ private: } temp.back().append(indent() + "if "s + tabCheckVar + " then"s + nll(branch)); pushScope(); - auto assignment = assignmentFrom(static_cast(valueList->exprs.front()), toAst(objVar, branch), branch); - auto info = extractDestructureInfo(assignment, true, false); + auto chainValue = toAst(objVar, branch); + auto assignment = assignmentFrom(static_cast(valueList->exprs.front()), newExp(chainValue, branch), branch); + auto info = extractDestructureInfo(assignment, true, true); transformAssignment(assignment, temp, true); str_list conds; for (const auto& des : info.destructures) { @@ -10608,8 +10651,21 @@ private: const auto& destruct = std::get(des); for (const auto& item : destruct.items) { if (!item.defVal) { - transformExp(item.target, conds, ExpUsage::Closure); - conds.back().append(" ~= nil"s); + if (!isAssignable(item.target)) { + transformExp(item.target, conds, ExpUsage::Closure); + auto callable = chainValue->items.front(); + auto chain = callable->new_ptr(); + chain->items.push_back(callable); + chain->items.dup(item.structure->items); + transformChainValue(chain, conds, ExpUsage::Closure); + auto vStr = conds.back(); + conds.pop_back(); + conds.back().append(" == "s); + conds.back().append(vStr); + } else { + transformExp(item.target, conds, ExpUsage::Closure); + conds.back().append(" ~= nil"s); + } } } } -- cgit v1.2.3-55-g6feb