From a66ec0d18eba6b38fad25cc88c82f7532d689670 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 9 Feb 2022 10:52:49 +0800 Subject: fix a case nil coalesing used as update assignment. --- spec/inputs/nil_coalesing.yue | 5 +++++ spec/outputs/nil_coalesing.lua | 25 +++++++++++++++++++++++++ src/yuescript/yue_compiler.cpp | 14 ++++++++------ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/spec/inputs/nil_coalesing.yue b/spec/inputs/nil_coalesing.yue index facd64c..76915c7 100644 --- a/spec/inputs/nil_coalesing.yue +++ b/spec/inputs/nil_coalesing.yue @@ -39,5 +39,10 @@ do do a = 1 ?? 2 ?? 3 +do + a.b.c ??= 1 + a = b.x ?? c.x ?? d.x + a.c ??= 1 + nil diff --git a/spec/outputs/nil_coalesing.lua b/spec/outputs/nil_coalesing.lua index b434671..e1547df 100644 --- a/spec/outputs/nil_coalesing.lua +++ b/spec/outputs/nil_coalesing.lua @@ -153,4 +153,29 @@ do end end end +do + local _obj_0 = a.b + if _obj_0.c == nil then + _obj_0.c = 1 + end + local a + do + local _exp_0 = b.x + if _exp_0 ~= nil then + a = _exp_0 + else + do + local _exp_1 = c.x + if _exp_1 ~= nil then + a = _exp_1 + else + a = d.x + end + end + end + end + if a.c == nil then + a.c = 1 + end +end return nil diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 852a0f4..db225f6 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -60,7 +60,7 @@ using namespace parserlib; typedef std::list str_list; -const std::string_view version = "0.9.7"sv; +const std::string_view version = "0.9.8"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -2081,8 +2081,9 @@ private: rightExp->pipeExprs.dup(leftExp->pipeExprs); rightExp->opValues.dup(leftExp->opValues); rightExp->nilCoalesed.set(update->value); - transformNilCoalesedExp(rightExp, out, ExpUsage::Assignment, assignment->expList, true); - if (!defs.empty()) out.back().insert(0, defs + nll(x)); + transformNilCoalesedExp(rightExp, temp, ExpUsage::Assignment, assignment->expList, true); + if (!defs.empty()) temp.back().insert(0, defs + nll(x)); + out.push_back(join(temp)); return; } auto defs = getPredefine(assignment); @@ -2495,7 +2496,7 @@ private: assignment->expList.set(expList); assignment->action.set(assign); transformAssignment(assignment, temp); - return true; + return forAssignment; } return false; }; @@ -2552,8 +2553,9 @@ private: assign->values.push_back(exp->nilCoalesed); auto defs = getPredefine(assignment); if (!defs.empty()) temp.push_back(defs + nll(x)); - prepareValue(); - _buf << indent() << "if "sv << objVar << " == nil then"sv << nll(x); + transformExp(left, temp, ExpUsage::Closure); + _buf << indent() << "if "sv << temp.back() << " == nil then"sv << nll(x); + temp.pop_back(); temp.push_back(clearBuf()); } pushScope(); -- cgit v1.2.3-55-g6feb