diff options
| author | Li Jin <dragon-fly@qq.com> | 2025-05-07 18:16:22 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2025-05-07 18:16:22 +0800 |
| commit | d905d9744344da7376ae56e7dc270a81da33d15f (patch) | |
| tree | 3d3674024308ee663763bed14ffd8dee8da666ed /src | |
| parent | c925dbea9c9108a18b9abf4a57d9d59b0ed04652 (diff) | |
| download | yuescript-d905d9744344da7376ae56e7dc270a81da33d15f.tar.gz yuescript-d905d9744344da7376ae56e7dc270a81da33d15f.tar.bz2 yuescript-d905d9744344da7376ae56e7dc270a81da33d15f.zip | |
Updated.
Diffstat (limited to 'src')
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 66 |
1 files changed, 38 insertions, 28 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 06f0d6d..bc4574b 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
| @@ -2814,19 +2814,19 @@ private: | |||
| 2814 | if (!varDefOnly) throw e; | 2814 | if (!varDefOnly) throw e; |
| 2815 | } | 2815 | } |
| 2816 | if (!assignable && !varDefOnly) { | 2816 | if (!assignable && !varDefOnly) { |
| 2817 | if (optional) { | 2817 | if (optional) break; |
| 2818 | if (defVal) { | 2818 | throw CompileError("can't destructure value"sv, pair); |
| 2819 | throw CompileError("default value is not supported here"sv, defVal); | 2819 | } |
| 2820 | } | 2820 | if (optional && varDefOnly && !assignable) { |
| 2821 | auto exp = static_cast<Exp_t*>(pair); | 2821 | if (defVal) { |
| 2822 | auto chain = exp->new_ptr<ChainValue_t>(); | 2822 | throw CompileError("default value is not supported here"sv, defVal); |
| 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 | } | 2823 | } |
| 2824 | auto exp = static_cast<Exp_t*>(pair); | ||
| 2825 | auto chain = exp->new_ptr<ChainValue_t>(); | ||
| 2826 | auto indexItem = toAst<Exp_t>(std::to_string(index), exp); | ||
| 2827 | chain->items.push_back(indexItem); | ||
| 2828 | pairs.push_back({exp, Empty, chain, nullptr}); | ||
| 2829 | break; | ||
| 2830 | } | 2830 | } |
| 2831 | auto value = singleValueFrom(pair); | 2831 | auto value = singleValueFrom(pair); |
| 2832 | auto item = value->item.get(); | 2832 | auto item = value->item.get(); |
| @@ -2914,17 +2914,17 @@ private: | |||
| 2914 | if (!varDefOnly) throw e; | 2914 | if (!varDefOnly) throw e; |
| 2915 | } | 2915 | } |
| 2916 | if (!assignable && !varDefOnly) { | 2916 | if (!assignable && !varDefOnly) { |
| 2917 | if (optional) { | 2917 | if (optional) break; |
| 2918 | if (defVal) { | 2918 | throw CompileError("can't destructure value"sv, pair); |
| 2919 | throw CompileError("default value is not supported here"sv, defVal); | 2919 | } |
| 2920 | } | 2920 | if (optional && varDefOnly && !assignable) { |
| 2921 | auto chain = exp->new_ptr<ChainValue_t>(); | 2921 | if (defVal) { |
| 2922 | if (keyIndex) chain->items.push_back(keyIndex); | 2922 | throw CompileError("default value is not supported here"sv, defVal); |
| 2923 | pairs.push_back({exp, Empty, chain, nullptr}); | ||
| 2924 | break; | ||
| 2925 | } else { | ||
| 2926 | throw CompileError("can't destructure value"sv, exp); | ||
| 2927 | } | 2923 | } |
| 2924 | auto chain = exp->new_ptr<ChainValue_t>(); | ||
| 2925 | if (keyIndex) chain->items.push_back(keyIndex); | ||
| 2926 | pairs.push_back({exp, Empty, chain, nullptr}); | ||
| 2927 | break; | ||
| 2928 | } | 2928 | } |
| 2929 | auto item = singleValueFrom(exp)->item.get(); | 2929 | auto item = singleValueFrom(exp)->item.get(); |
| 2930 | ast_node* subExp = ast_cast<SimpleTable_t>(item); | 2930 | ast_node* subExp = ast_cast<SimpleTable_t>(item); |
| @@ -10652,16 +10652,26 @@ private: | |||
| 10652 | for (const auto& item : destruct.items) { | 10652 | for (const auto& item : destruct.items) { |
| 10653 | if (!item.defVal) { | 10653 | if (!item.defVal) { |
| 10654 | if (!isAssignable(item.target)) { | 10654 | if (!isAssignable(item.target)) { |
| 10655 | transformExp(item.target, conds, ExpUsage::Closure); | ||
| 10656 | auto callable = chainValue->items.front(); | 10655 | auto callable = chainValue->items.front(); |
| 10657 | auto chain = callable->new_ptr<ChainValue_t>(); | 10656 | auto chain = callable->new_ptr<ChainValue_t>(); |
| 10658 | chain->items.push_back(callable); | 10657 | chain->items.push_back(callable); |
| 10659 | chain->items.dup(item.structure->items); | 10658 | chain->items.dup(item.structure->items); |
| 10660 | transformChainValue(chain, conds, ExpUsage::Closure); | 10659 | if (specialChainValue(chain) == ChainType::Common) { |
| 10661 | auto vStr = conds.back(); | 10660 | transformChainValue(chain, conds, ExpUsage::Closure); |
| 10662 | conds.pop_back(); | 10661 | auto vStr = conds.back(); |
| 10663 | conds.back().append(" == "s); | 10662 | conds.pop_back(); |
| 10664 | conds.back().append(vStr); | 10663 | transformExp(item.target, conds, ExpUsage::Closure); |
| 10664 | conds.back().append(" == "s); | ||
| 10665 | conds.back().append(vStr); | ||
| 10666 | } else { | ||
| 10667 | auto varName = getUnusedName("_val_"sv); | ||
| 10668 | auto vExp = toAst<Exp_t>(varName, chain); | ||
| 10669 | auto asmt = assignmentFrom(vExp, newExp(chain, chain), chain); | ||
| 10670 | transformAssignment(asmt, temp); | ||
| 10671 | transformExp(item.target, conds, ExpUsage::Closure); | ||
| 10672 | conds.back().append(" == "s); | ||
| 10673 | conds.back().append(varName); | ||
| 10674 | } | ||
| 10665 | } else { | 10675 | } else { |
| 10666 | transformExp(item.target, conds, ExpUsage::Closure); | 10676 | transformExp(item.target, conds, ExpUsage::Closure); |
| 10667 | conds.back().append(" ~= nil"s); | 10677 | conds.back().append(" ~= nil"s); |
