aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2022-07-14 02:48:49 +0800
committerLi Jin <dragon-fly@qq.com>2022-07-14 02:48:49 +0800
commit3159a45de9e691ad758dcbc933446f61b7ae1940 (patch)
treeb695f8356986accc84c82ece09eb427a2b1db230 /src
parenta1d341085eed96d567329a30f2cf57c95fe6f071 (diff)
downloadyuescript-3159a45de9e691ad758dcbc933446f61b7ae1940.tar.gz
yuescript-3159a45de9e691ad758dcbc933446f61b7ae1940.tar.bz2
yuescript-3159a45de9e691ad758dcbc933446f61b7ae1940.zip
fix table matching issue and update doc.
Diffstat (limited to 'src')
-rwxr-xr-xsrc/yuescript/yue_compiler.cpp72
1 files changed, 47 insertions, 25 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp
index c68746c..2565878 100755
--- a/src/yuescript/yue_compiler.cpp
+++ b/src/yuescript/yue_compiler.cpp
@@ -56,7 +56,7 @@ using namespace parserlib;
56 56
57typedef std::list<std::string> str_list; 57typedef std::list<std::string> str_list;
58 58
59const std::string_view version = "0.13.3"sv; 59const std::string_view version = "0.13.4"sv;
60const std::string_view extension = "yue"sv; 60const std::string_view extension = "yue"sv;
61 61
62class YueCompilerImpl { 62class YueCompilerImpl {
@@ -1582,12 +1582,22 @@ private:
1582 added--; 1582 added--;
1583 } 1583 }
1584 if (pair.defVal) { 1584 if (pair.defVal) {
1585 auto stmt = toAst<Statement_t>(pair.targetVar + "=nil if "s + pair.targetVar + "==nil", pair.defVal); 1585 bool isNil = false;
1586 auto defAssign = stmt->content.as<ExpListAssign_t>(); 1586 if (auto v1 = singleValueFrom(pair.defVal)) {
1587 auto assign = defAssign->action.as<Assign_t>(); 1587 if (auto v2 = v1->item.as<SimpleValue_t>()) {
1588 assign->values.clear(); 1588 if (auto v3 = v2->value.as<const_value_t>()) {
1589 assign->values.push_back(pair.defVal); 1589 isNil = _parser.toString(v3) == "nil"sv;
1590 transformStatement(stmt, temp); 1590 }
1591 }
1592 }
1593 if (!isNil) {
1594 auto stmt = toAst<Statement_t>(pair.targetVar + "=nil if "s + pair.targetVar + "==nil", pair.defVal);
1595 auto defAssign = stmt->content.as<ExpListAssign_t>();
1596 auto assign = defAssign->action.as<Assign_t>();
1597 assign->values.clear();
1598 assign->values.push_back(pair.defVal);
1599 transformStatement(stmt, temp);
1600 }
1591 } 1601 }
1592 continue; 1602 continue;
1593 } 1603 }
@@ -1709,12 +1719,22 @@ private:
1709 } 1719 }
1710 for (const auto& item : destruct.items) { 1720 for (const auto& item : destruct.items) {
1711 if (item.defVal) { 1721 if (item.defVal) {
1712 auto stmt = toAst<Statement_t>(item.targetVar + "=nil if "s + item.targetVar + "==nil", item.defVal); 1722 bool isNil = false;
1713 auto defAssign = stmt->content.as<ExpListAssign_t>(); 1723 if (auto v1 = singleValueFrom(item.defVal)) {
1714 auto assign = defAssign->action.as<Assign_t>(); 1724 if (auto v2 = v1->item.as<SimpleValue_t>()) {
1715 assign->values.clear(); 1725 if (auto v3 = v2->value.as<const_value_t>()) {
1716 assign->values.push_back(item.defVal); 1726 isNil = _parser.toString(v3) == "nil"sv;
1717 transformStatement(stmt, temp); 1727 }
1728 }
1729 }
1730 if (!isNil) {
1731 auto stmt = toAst<Statement_t>(item.targetVar + "=nil if "s + item.targetVar + "==nil", item.defVal);
1732 auto defAssign = stmt->content.as<ExpListAssign_t>();
1733 auto assign = defAssign->action.as<Assign_t>();
1734 assign->values.clear();
1735 assign->values.push_back(item.defVal);
1736 transformStatement(stmt, temp);
1737 }
1718 } 1738 }
1719 } 1739 }
1720 for (const auto& item : leftPairs) { 1740 for (const auto& item : leftPairs) {
@@ -7160,32 +7180,34 @@ private:
7160 auto assignment = assignmentFrom(static_cast<Exp_t*>(branch->valueList->exprs.front()), toAst<Exp_t>(objVar, branch), branch); 7180 auto assignment = assignmentFrom(static_cast<Exp_t*>(branch->valueList->exprs.front()), toAst<Exp_t>(objVar, branch), branch);
7161 auto info = extractDestructureInfo(assignment, true, false); 7181 auto info = extractDestructureInfo(assignment, true, false);
7162 transformAssignment(assignment, temp, true); 7182 transformAssignment(assignment, temp, true);
7163 temp.push_back(indent() + "if"s); 7183 str_list conds;
7164 bool firstItem = true;
7165 for (const auto& destruct : info.first) { 7184 for (const auto& destruct : info.first) {
7166 for (const auto& item : destruct.items) { 7185 for (const auto& item : destruct.items) {
7167 str_list tmp; 7186 if (!item.defVal) {
7168 transformExp(item.target, tmp, ExpUsage::Closure); 7187 transformExp(item.target, conds, ExpUsage::Closure);
7169 temp.back().append((firstItem ? " " : " and "s) + tmp.back() + " ~= nil"s); 7188 conds.back().append(" ~= nil"s);
7170 if (firstItem) firstItem = false; 7189 }
7171 } 7190 }
7172 } 7191 }
7173 temp.back().append(" then"s + nll(branch)); 7192 if (!conds.empty()) {
7174 pushScope(); 7193 temp.push_back(indent() + "if "s + join(conds, " and "sv) + " then"s + nll(branch));
7175 transform_plain_body(branch->body, temp, usage, assignList); 7194 pushScope();
7195 }
7176 if (!lastBranch) { 7196 if (!lastBranch) {
7177 temp.push_back(indent() + matchVar + " = true"s + nll(branch)); 7197 temp.push_back(indent() + matchVar + " = true"s + nll(branch));
7178 } 7198 }
7179 popScope(); 7199 transform_plain_body(branch->body, temp, usage, assignList);
7180 if (!lastBranch) { 7200 if (!conds.empty()) {
7201 popScope();
7181 temp.push_back(indent() + "end"s + nll(branch)); 7202 temp.push_back(indent() + "end"s + nll(branch));
7203 }
7204 if (!lastBranch) {
7182 popScope(); 7205 popScope();
7183 temp.push_back(indent() + "end"s + nll(branch)); 7206 temp.push_back(indent() + "end"s + nll(branch));
7184 temp.push_back(indent() + "if not "s + matchVar + " then"s + nll(branch)); 7207 temp.push_back(indent() + "if not "s + matchVar + " then"s + nll(branch));
7185 pushScope(); 7208 pushScope();
7186 addScope++; 7209 addScope++;
7187 } else { 7210 } else {
7188 temp.push_back(indent() + "end"s + nll(branch));
7189 popScope(); 7211 popScope();
7190 } 7212 }
7191 firstBranch = true; 7213 firstBranch = true;