diff options
| author | Li Jin <dragon-fly@qq.com> | 2022-11-08 10:20:52 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2022-11-09 11:29:32 +0800 |
| commit | b9b1cf94e1324e344df6f7204c8d479329c56718 (patch) | |
| tree | b288f3c48bfd17f673968f101f0f6f886f00c3c9 /src/yuescript/yue_compiler.cpp | |
| parent | 95fd6ea93c2af6fb1707e480669d84ebe37fccbe (diff) | |
| download | yuescript-b9b1cf94e1324e344df6f7204c8d479329c56718.tar.gz yuescript-b9b1cf94e1324e344df6f7204c8d479329c56718.tar.bz2 yuescript-b9b1cf94e1324e344df6f7204c8d479329c56718.zip | |
fix issue #114.
Diffstat (limited to '')
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index e061604..d0f2dbb 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
| @@ -14,7 +14,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI | |||
| 14 | #include <unordered_map> | 14 | #include <unordered_map> |
| 15 | #include <unordered_set> | 15 | #include <unordered_set> |
| 16 | #include <vector> | 16 | #include <vector> |
| 17 | #include <optional> | ||
| 18 | 17 | ||
| 19 | #include "yuescript/yue_compiler.h" | 18 | #include "yuescript/yue_compiler.h" |
| 20 | #include "yuescript/yue_parser.h" | 19 | #include "yuescript/yue_parser.h" |
| @@ -60,7 +59,7 @@ namespace yue { | |||
| 60 | 59 | ||
| 61 | typedef std::list<std::string> str_list; | 60 | typedef std::list<std::string> str_list; |
| 62 | 61 | ||
| 63 | const std::string_view version = "0.15.8"sv; | 62 | const std::string_view version = "0.15.9"sv; |
| 64 | const std::string_view extension = "yue"sv; | 63 | const std::string_view extension = "yue"sv; |
| 65 | 64 | ||
| 66 | class YueCompilerImpl { | 65 | class YueCompilerImpl { |
| @@ -2122,7 +2121,8 @@ private: | |||
| 2122 | keyIndex = key; | 2121 | keyIndex = key; |
| 2123 | } else if (auto key = np->key.as<String_t>()) { | 2122 | } else if (auto key = np->key.as<String_t>()) { |
| 2124 | keyIndex = newExp(key, np->key).get(); | 2123 | keyIndex = newExp(key, np->key).get(); |
| 2125 | } else throw std::logic_error(_info.errorMessage("unsupported key for destructuring"sv, np)); | 2124 | } else |
| 2125 | throw std::logic_error(_info.errorMessage("unsupported key for destructuring"sv, np)); | ||
| 2126 | } | 2126 | } |
| 2127 | if (auto exp = np->value.as<Exp_t>()) { | 2127 | if (auto exp = np->value.as<Exp_t>()) { |
| 2128 | if (!isAssignable(exp)) throw std::logic_error(_info.errorMessage("can't do destructure value"sv, exp)); | 2128 | if (!isAssignable(exp)) throw std::logic_error(_info.errorMessage("can't do destructure value"sv, exp)); |
| @@ -2211,7 +2211,8 @@ private: | |||
| 2211 | chain->items.push_back(newExp(key, dp)); | 2211 | chain->items.push_back(newExp(key, dp)); |
| 2212 | } else if (auto key = dp->key.as<Exp_t>()) { | 2212 | } else if (auto key = dp->key.as<Exp_t>()) { |
| 2213 | chain->items.push_back(key); | 2213 | chain->items.push_back(key); |
| 2214 | } else throw std::logic_error(_info.errorMessage("unsupported key for destructuring"sv, dp)); | 2214 | } else |
| 2215 | throw std::logic_error(_info.errorMessage("unsupported key for destructuring"sv, dp)); | ||
| 2215 | } | 2216 | } |
| 2216 | if (auto exp = dp->value.get()) { | 2217 | if (auto exp = dp->value.get()) { |
| 2217 | if (!isAssignable(exp)) throw std::logic_error(_info.errorMessage("can't destructure value"sv, exp)); | 2218 | if (!isAssignable(exp)) throw std::logic_error(_info.errorMessage("can't destructure value"sv, exp)); |
| @@ -6520,6 +6521,13 @@ private: | |||
| 6520 | _buf << "\t\t"sv << baseVar << '[' << key << "]="sv << val << " if "sv << baseVar << '[' << key << "]==nil and (not "sv << cls << " or not "sv << key << "\\match \"^__\")"sv; | 6521 | _buf << "\t\t"sv << baseVar << '[' << key << "]="sv << val << " if "sv << baseVar << '[' << key << "]==nil and (not "sv << cls << " or not "sv << key << "\\match \"^__\")"sv; |
| 6521 | transformBlock(toAst<Block_t>(clearBuf(), x), temp, ExpUsage::Common); | 6522 | transformBlock(toAst<Block_t>(clearBuf(), x), temp, ExpUsage::Common); |
| 6522 | } | 6523 | } |
| 6524 | if (!parentVar.empty()) { | ||
| 6525 | auto key = getUnusedName("_key_"sv); | ||
| 6526 | auto val = getUnusedName("_val_"sv); | ||
| 6527 | _buf << "for "sv << key << ',' << val << " in pairs "sv << parentVar << ".__base"sv << '\n' | ||
| 6528 | << '\t' << baseVar << '[' << key << "]="sv << val << " if "sv << baseVar << '[' << key << "]==nil and "sv << key << "\\match(\"^__\") and not ("sv << key << "==\"__index\" and "sv << val << "=="sv << parentVar << ".__base)"sv; | ||
| 6529 | transformBlock(toAst<Block_t>(clearBuf(), x), temp, ExpUsage::Common); | ||
| 6530 | } | ||
| 6523 | transformAssignment(toAst<ExpListAssign_t>(baseVar + ".__index ?\?= "s + baseVar, classDecl), temp); | 6531 | transformAssignment(toAst<ExpListAssign_t>(baseVar + ".__index ?\?= "s + baseVar, classDecl), temp); |
| 6524 | str_list tmp; | 6532 | str_list tmp; |
| 6525 | if (usage == ExpUsage::Assignment) { | 6533 | if (usage == ExpUsage::Assignment) { |
