From 3159a45de9e691ad758dcbc933446f61b7ae1940 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 14 Jul 2022 02:48:49 +0800 Subject: fix table matching issue and update doc. --- src/yuescript/yue_compiler.cpp | 72 +++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 25 deletions(-) (limited to 'src') 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; typedef std::list str_list; -const std::string_view version = "0.13.3"sv; +const std::string_view version = "0.13.4"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -1582,12 +1582,22 @@ private: added--; } if (pair.defVal) { - auto stmt = toAst(pair.targetVar + "=nil if "s + pair.targetVar + "==nil", pair.defVal); - auto defAssign = stmt->content.as(); - auto assign = defAssign->action.as(); - assign->values.clear(); - assign->values.push_back(pair.defVal); - transformStatement(stmt, temp); + bool isNil = false; + if (auto v1 = singleValueFrom(pair.defVal)) { + if (auto v2 = v1->item.as()) { + if (auto v3 = v2->value.as()) { + isNil = _parser.toString(v3) == "nil"sv; + } + } + } + if (!isNil) { + auto stmt = toAst(pair.targetVar + "=nil if "s + pair.targetVar + "==nil", pair.defVal); + auto defAssign = stmt->content.as(); + auto assign = defAssign->action.as(); + assign->values.clear(); + assign->values.push_back(pair.defVal); + transformStatement(stmt, temp); + } } continue; } @@ -1709,12 +1719,22 @@ private: } for (const auto& item : destruct.items) { if (item.defVal) { - auto stmt = toAst(item.targetVar + "=nil if "s + item.targetVar + "==nil", item.defVal); - auto defAssign = stmt->content.as(); - auto assign = defAssign->action.as(); - assign->values.clear(); - assign->values.push_back(item.defVal); - transformStatement(stmt, temp); + bool isNil = false; + if (auto v1 = singleValueFrom(item.defVal)) { + if (auto v2 = v1->item.as()) { + if (auto v3 = v2->value.as()) { + isNil = _parser.toString(v3) == "nil"sv; + } + } + } + if (!isNil) { + auto stmt = toAst(item.targetVar + "=nil if "s + item.targetVar + "==nil", item.defVal); + auto defAssign = stmt->content.as(); + auto assign = defAssign->action.as(); + assign->values.clear(); + assign->values.push_back(item.defVal); + transformStatement(stmt, temp); + } } } for (const auto& item : leftPairs) { @@ -7160,32 +7180,34 @@ private: auto assignment = assignmentFrom(static_cast(branch->valueList->exprs.front()), toAst(objVar, branch), branch); auto info = extractDestructureInfo(assignment, true, false); transformAssignment(assignment, temp, true); - temp.push_back(indent() + "if"s); - bool firstItem = true; + str_list conds; for (const auto& destruct : info.first) { for (const auto& item : destruct.items) { - str_list tmp; - transformExp(item.target, tmp, ExpUsage::Closure); - temp.back().append((firstItem ? " " : " and "s) + tmp.back() + " ~= nil"s); - if (firstItem) firstItem = false; + if (!item.defVal) { + transformExp(item.target, conds, ExpUsage::Closure); + conds.back().append(" ~= nil"s); + } } } - temp.back().append(" then"s + nll(branch)); - pushScope(); - transform_plain_body(branch->body, temp, usage, assignList); + if (!conds.empty()) { + temp.push_back(indent() + "if "s + join(conds, " and "sv) + " then"s + nll(branch)); + pushScope(); + } if (!lastBranch) { temp.push_back(indent() + matchVar + " = true"s + nll(branch)); } - popScope(); - if (!lastBranch) { + transform_plain_body(branch->body, temp, usage, assignList); + if (!conds.empty()) { + popScope(); temp.push_back(indent() + "end"s + nll(branch)); + } + if (!lastBranch) { popScope(); temp.push_back(indent() + "end"s + nll(branch)); temp.push_back(indent() + "if not "s + matchVar + " then"s + nll(branch)); pushScope(); addScope++; } else { - temp.push_back(indent() + "end"s + nll(branch)); popScope(); } firstBranch = true; -- cgit v1.2.3-55-g6feb