aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2025-05-07 12:46:39 +0800
committerLi Jin <dragon-fly@qq.com>2025-05-07 12:46:39 +0800
commitc925dbea9c9108a18b9abf4a57d9d59b0ed04652 (patch)
treed26083e7358739961fea4fd1bee094e35be45490 /src
parent4dba7838d2d805f71faa065a3024232b505d9d21 (diff)
downloadyuescript-c925dbea9c9108a18b9abf4a57d9d59b0ed04652.tar.gz
yuescript-c925dbea9c9108a18b9abf4a57d9d59b0ed04652.tar.bz2
yuescript-c925dbea9c9108a18b9abf4a57d9d59b0ed04652.zip
Tried adding more table matching functions.
Diffstat (limited to 'src')
-rw-r--r--src/3rdParty/efsw/FileWatcherGeneric.cpp2
-rw-r--r--src/yuescript/yue_compiler.cpp78
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() {
25} 25}
26 26
27WatchID FileWatcherGeneric::addWatch( const std::string& directory, FileWatchListener* watcher, 27WatchID FileWatcherGeneric::addWatch( const std::string& directory, FileWatchListener* watcher,
28 bool recursive, const std::vector<WatcherOption>& options ) { 28 bool recursive, const std::vector<WatcherOption>& /*options*/ ) {
29 std::string dir( directory ); 29 std::string dir( directory );
30 30
31 FileSystem::dirAddSlashAtEnd( dir ); 31 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<std::string> Metamethods = {
78 "close"s // Lua 5.4 78 "close"s // Lua 5.4
79}; 79};
80 80
81const std::string_view version = "0.27.5"sv; 81const std::string_view version = "0.27.6"sv;
82const std::string_view extension = "yue"sv; 82const std::string_view extension = "yue"sv;
83 83
84class CompileError : public std::logic_error { 84class CompileError : public std::logic_error {
@@ -2807,8 +2807,26 @@ private:
2807 defVal = nd->defVal.get(); 2807 defVal = nd->defVal.get();
2808 } 2808 }
2809 ++index; 2809 ++index;
2810 if (!varDefOnly && !isAssignable(static_cast<Exp_t*>(pair))) { 2810 bool assignable = false;
2811 throw CompileError("can't destructure value"sv, pair); 2811 try {
2812 assignable = isAssignable(static_cast<Exp_t*>(pair));
2813 } catch (const CompileError& e) {
2814 if (!varDefOnly) throw e;
2815 }
2816 if (!assignable && !varDefOnly) {
2817 if (optional) {
2818 if (defVal) {
2819 throw CompileError("default value is not supported here"sv, defVal);
2820 }
2821 auto exp = static_cast<Exp_t*>(pair);
2822 auto chain = exp->new_ptr<ChainValue_t>();
2823 auto indexItem = toAst<Exp_t>(std::to_string(index), exp);
2824 chain->items.push_back(indexItem);
2825 pairs.push_back({exp, Empty, chain, nullptr});
2826 break;
2827 } else {
2828 throw CompileError("can't destructure value"sv, pair);
2829 }
2812 } 2830 }
2813 auto value = singleValueFrom(pair); 2831 auto value = singleValueFrom(pair);
2814 auto item = value->item.get(); 2832 auto item = value->item.get();
@@ -2889,7 +2907,25 @@ private:
2889 } 2907 }
2890 } 2908 }
2891 if (auto exp = np->value.as<Exp_t>()) { 2909 if (auto exp = np->value.as<Exp_t>()) {
2892 if (!varDefOnly && !isAssignable(exp)) throw CompileError("can't do destructure value"sv, exp); 2910 bool assignable = false;
2911 try {
2912 assignable = isAssignable(exp);
2913 } catch (const CompileError& e) {
2914 if (!varDefOnly) throw e;
2915 }
2916 if (!assignable && !varDefOnly) {
2917 if (optional) {
2918 if (defVal) {
2919 throw CompileError("default value is not supported here"sv, defVal);
2920 }
2921 auto chain = exp->new_ptr<ChainValue_t>();
2922 if (keyIndex) chain->items.push_back(keyIndex);
2923 pairs.push_back({exp, Empty, chain, nullptr});
2924 break;
2925 } else {
2926 throw CompileError("can't destructure value"sv, exp);
2927 }
2928 }
2893 auto item = singleValueFrom(exp)->item.get(); 2929 auto item = singleValueFrom(exp)->item.get();
2894 ast_node* subExp = ast_cast<SimpleTable_t>(item); 2930 ast_node* subExp = ast_cast<SimpleTable_t>(item);
2895 if (subExp || (subExp = item->get_by_path<TableLit_t>()) || (subExp = item->get_by_path<Comprehension_t>())) { 2931 if (subExp || (subExp = item->get_by_path<TableLit_t>()) || (subExp = item->get_by_path<Comprehension_t>())) {
@@ -3111,7 +3147,11 @@ private:
3111 break; 3147 break;
3112 default: YUEE("AST node mismatch", destructNode); break; 3148 default: YUEE("AST node mismatch", destructNode); break;
3113 } 3149 }
3114 if (dlist->empty()) throw CompileError("expect items to be destructured"sv, destructNode); 3150 if (dlist->empty()) {
3151 if (!optional) {
3152 throw CompileError("expect items to be destructured"sv, destructNode);
3153 }
3154 }
3115 for (auto item : *dlist) { 3155 for (auto item : *dlist) {
3116 switch (item->get_id()) { 3156 switch (item->get_id()) {
3117 case id<MetaVariablePairDef_t>(): { 3157 case id<MetaVariablePairDef_t>(): {
@@ -3247,7 +3287,9 @@ private:
3247 simpleValue->value.set(tab); 3287 simpleValue->value.set(tab);
3248 auto pairs = destructFromExp(newExp(simpleValue, expr), varDefOnly, optional); 3288 auto pairs = destructFromExp(newExp(simpleValue, expr), varDefOnly, optional);
3249 if (pairs.empty()) { 3289 if (pairs.empty()) {
3250 throw CompileError("expect items to be destructured"sv, tab); 3290 if (!optional) {
3291 throw CompileError("expect items to be destructured"sv, tab);
3292 }
3251 } 3293 }
3252 destruct.items = std::move(pairs); 3294 destruct.items = std::move(pairs);
3253 if (!varDefOnly) { 3295 if (!varDefOnly) {
@@ -3286,7 +3328,7 @@ private:
3286 destruct.valueVar.clear(); 3328 destruct.valueVar.clear();
3287 } 3329 }
3288 } 3330 }
3289 destructs.push_back(destruct); 3331 destructs.push_back(std::move(destruct));
3290 } 3332 }
3291 } 3333 }
3292 } else { 3334 } else {
@@ -10597,8 +10639,9 @@ private:
10597 } 10639 }
10598 temp.back().append(indent() + "if "s + tabCheckVar + " then"s + nll(branch)); 10640 temp.back().append(indent() + "if "s + tabCheckVar + " then"s + nll(branch));
10599 pushScope(); 10641 pushScope();
10600 auto assignment = assignmentFrom(static_cast<Exp_t*>(valueList->exprs.front()), toAst<Exp_t>(objVar, branch), branch); 10642 auto chainValue = toAst<ChainValue_t>(objVar, branch);
10601 auto info = extractDestructureInfo(assignment, true, false); 10643 auto assignment = assignmentFrom(static_cast<Exp_t*>(valueList->exprs.front()), newExp(chainValue, branch), branch);
10644 auto info = extractDestructureInfo(assignment, true, true);
10602 transformAssignment(assignment, temp, true); 10645 transformAssignment(assignment, temp, true);
10603 str_list conds; 10646 str_list conds;
10604 for (const auto& des : info.destructures) { 10647 for (const auto& des : info.destructures) {
@@ -10608,8 +10651,21 @@ private:
10608 const auto& destruct = std::get<Destructure>(des); 10651 const auto& destruct = std::get<Destructure>(des);
10609 for (const auto& item : destruct.items) { 10652 for (const auto& item : destruct.items) {
10610 if (!item.defVal) { 10653 if (!item.defVal) {
10611 transformExp(item.target, conds, ExpUsage::Closure); 10654 if (!isAssignable(item.target)) {
10612 conds.back().append(" ~= nil"s); 10655 transformExp(item.target, conds, ExpUsage::Closure);
10656 auto callable = chainValue->items.front();
10657 auto chain = callable->new_ptr<ChainValue_t>();
10658 chain->items.push_back(callable);
10659 chain->items.dup(item.structure->items);
10660 transformChainValue(chain, conds, ExpUsage::Closure);
10661 auto vStr = conds.back();
10662 conds.pop_back();
10663 conds.back().append(" == "s);
10664 conds.back().append(vStr);
10665 } else {
10666 transformExp(item.target, conds, ExpUsage::Closure);
10667 conds.back().append(" ~= nil"s);
10668 }
10613 } 10669 }
10614 } 10670 }
10615 } 10671 }