diff options
author | Li Jin <dragon-fly@qq.com> | 2021-08-09 15:43:43 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2021-08-09 15:43:43 +0800 |
commit | f9859f5c6739c9a0d491ba64e241d147d4114ecb (patch) | |
tree | a5992757b1f7d1a5b913419ec1b68d474bacc87a | |
parent | 3bef9e2dc90def7569b7814d59d2f2b5dfa4bd3c (diff) | |
download | yuescript-f9859f5c6739c9a0d491ba64e241d147d4114ecb.tar.gz yuescript-f9859f5c6739c9a0d491ba64e241d147d4114ecb.tar.bz2 yuescript-f9859f5c6739c9a0d491ba64e241d147d4114ecb.zip |
fix issue #62. The update statement is now creating local variable.
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 13 | ||||
-rw-r--r-- | src/yuescript/yue_parser.cpp | 4 |
2 files changed, 11 insertions, 6 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 90fc60f..f1b6253 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -38,7 +38,7 @@ extern "C" { | |||
38 | 38 | ||
39 | namespace yue { | 39 | namespace yue { |
40 | using namespace std::string_view_literals; | 40 | using namespace std::string_view_literals; |
41 | using namespace std::literals::string_literals; | 41 | using namespace std::string_literals; |
42 | using namespace parserlib; | 42 | using namespace parserlib; |
43 | 43 | ||
44 | #define BLOCK_START do { | 44 | #define BLOCK_START do { |
@@ -56,7 +56,7 @@ using namespace parserlib; | |||
56 | 56 | ||
57 | typedef std::list<std::string> str_list; | 57 | typedef std::list<std::string> str_list; |
58 | 58 | ||
59 | const std::string_view version = "0.7.15"sv; | 59 | const std::string_view version = "0.7.16"sv; |
60 | const std::string_view extension = "yue"sv; | 60 | const std::string_view extension = "yue"sv; |
61 | 61 | ||
62 | class YueCompilerImpl { | 62 | class YueCompilerImpl { |
@@ -1722,7 +1722,8 @@ private: | |||
1722 | auto leftValue = singleValueFrom(leftExp); | 1722 | auto leftValue = singleValueFrom(leftExp); |
1723 | if (!leftValue) throw std::logic_error(_info.errorMessage("left hand expression is not assignable"sv, leftExp)); | 1723 | if (!leftValue) throw std::logic_error(_info.errorMessage("left hand expression is not assignable"sv, leftExp)); |
1724 | if (auto chain = leftValue->item.as<ChainValue_t>()) { | 1724 | if (auto chain = leftValue->item.as<ChainValue_t>()) { |
1725 | if (specialChainValue(chain) == ChainType::Metatable) {throw std::logic_error(_info.errorMessage("can not apply update to a metatable"sv, leftExp)); | 1725 | if (specialChainValue(chain) == ChainType::Metatable) { |
1726 | throw std::logic_error(_info.errorMessage("can not apply update to a metatable"sv, leftExp)); | ||
1726 | } | 1727 | } |
1727 | auto tmpChain = x->new_ptr<ChainValue_t>(); | 1728 | auto tmpChain = x->new_ptr<ChainValue_t>(); |
1728 | for (auto item : chain->items.objects()) { | 1729 | for (auto item : chain->items.objects()) { |
@@ -1747,6 +1748,7 @@ private: | |||
1747 | chain->items.clear(); | 1748 | chain->items.clear(); |
1748 | chain->items.dup(tmpChain->items); | 1749 | chain->items.dup(tmpChain->items); |
1749 | } | 1750 | } |
1751 | auto defs = getPredefine(assignment); | ||
1750 | transformValue(leftValue, temp); | 1752 | transformValue(leftValue, temp); |
1751 | auto left = std::move(temp.back()); | 1753 | auto left = std::move(temp.back()); |
1752 | temp.pop_back(); | 1754 | temp.pop_back(); |
@@ -1756,7 +1758,10 @@ private: | |||
1756 | if (!singleValueFrom(update->value)) { | 1758 | if (!singleValueFrom(update->value)) { |
1757 | right = '(' + right + ')'; | 1759 | right = '(' + right + ')'; |
1758 | } | 1760 | } |
1759 | _buf << join(temp) << indent() << left << " = "sv << left << | 1761 | _buf << join(temp); |
1762 | if (!defs.empty()) _buf << defs; | ||
1763 | else _buf << indent() << left; | ||
1764 | _buf << " = "sv << left << | ||
1760 | ' ' << _parser.toString(update->op) << ' ' << right << nll(assignment); | 1765 | ' ' << _parser.toString(update->op) << ' ' << right << nll(assignment); |
1761 | out.push_back(clearBuf()); | 1766 | out.push_back(clearBuf()); |
1762 | break; | 1767 | break; |
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index c79d58e..f530776 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp | |||
@@ -705,8 +705,8 @@ namespace Utils { | |||
705 | 705 | ||
706 | void trim(std::string& str) { | 706 | void trim(std::string& str) { |
707 | if (str.empty()) return; | 707 | if (str.empty()) return; |
708 | str.erase(0, str.find_first_not_of(" \t\n")); | 708 | str.erase(0, str.find_first_not_of(" \t\r\n")); |
709 | str.erase(str.find_last_not_of(" \t\n") + 1); | 709 | str.erase(str.find_last_not_of(" \t\r\n") + 1); |
710 | } | 710 | } |
711 | } | 711 | } |
712 | 712 | ||