From 0ca14d8e6c37f5d1360178034851f36c636a7a43 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 9 Nov 2023 18:30:16 +0800 Subject: fix a missing case for condition chaining. --- spec/inputs/cond.yue | 2 ++ spec/outputs/cond.lua | 11 ++++++++++ src/yuescript/yue_compiler.cpp | 49 +++++++++++++++++++++--------------------- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/spec/inputs/cond.yue b/spec/inputs/cond.yue index f5f42a8..5bc6c9b 100644 --- a/spec/inputs/cond.yue +++ b/spec/inputs/cond.yue @@ -247,6 +247,7 @@ do a = x and y or v(1) < v(2) < v(3) and b < v(4) < v(5) < v(6) a = v(1) < v(2) < v(3) and b < v(4) < v(5) < v(6) or x and y a = x and y or v(1) < v(2) < v(3) and b < v(4) < v(5) < v(6) or w and z + a = 1 < 2 == v1 > 3 local v1, v2, v3, v4, v5, v6 a = v1 < v2 < v3 < v4 @@ -258,6 +259,7 @@ do a = x and y or v1 < v2 < v3 and b < v4 < v5 < v6 a = v1 < v2 < v3 and b < v4 < v5 < v6 or x and y a = x and y or v1 < v2 < v3 and b < v4 < v5 < v6 or w and z + a = 1 < 2 == v1 > 3 nil diff --git a/spec/outputs/cond.lua b/spec/outputs/cond.lua index d7aae02..acbf644 100644 --- a/spec/outputs/cond.lua +++ b/spec/outputs/cond.lua @@ -503,6 +503,16 @@ do end end end)() or w and z + if not (1 < 2) then + a = false + else + local _cond_0 = v1 + if not (2 == _cond_0) then + a = false + else + a = _cond_0 > 3 + end + end local v1, v2, v3, v4, v5, v6 a = v1 < v2 and v2 < v3 and v3 < v4 a = x and y or v1 < v2 and v2 < v3 and v3 < v4 @@ -512,5 +522,6 @@ do a = x and y or v1 < v2 and v2 < v3 and b < v4 and v4 < v5 and v5 < v6 a = v1 < v2 and v2 < v3 and b < v4 and v4 < v5 and v5 < v6 or x and y a = x and y or v1 < v2 and v2 < v3 and b < v4 and v4 < v5 and v5 < v6 or w and z + a = 1 < 2 and 2 == v1 and v1 > 3 end return nil diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 1733967..7fdf45e 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -75,7 +75,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.20.4"sv; +const std::string_view version = "0.20.5"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -1042,7 +1042,7 @@ private: for (auto it = uname->m_begin.m_it; it != uname->m_end.m_it; ++it) { auto ch = *it; if (ch > 255) { - buf << "_u"sv << std::hex << ch; + buf << "_u"sv << std::hex << static_cast(ch); } else { buf << static_cast(ch); } @@ -3438,25 +3438,10 @@ private: ifCond->condition.set(condExp); ifNode->nodes.push_back(ifCond); ifNode->nodes.push_back(toAst("false"sv, exp)); + YueFormat format{}; + auto code = ifNode->to_string(&format); if (newCondExp) { - if (nodes) { - auto block = exp->new_ptr(); - auto stmt = exp->new_ptr(); - stmt->content.set(preDefine); - preDefine.set(nullptr); - block->statements.push_back(stmt); - auto simpleValue = exp->new_ptr(); - simpleValue->value.set(ifNode); - auto explist = exp->new_ptr(); - explist->exprs.push_back(newExp(simpleValue, exp)); - auto expListAssign = exp->new_ptr(); - expListAssign->expList.set(explist); - stmt = exp->new_ptr(); - stmt->content.set(expListAssign); - block->statements.push_back(stmt); - nodes->push_back(block); - nodes = &ifNode->nodes; - } else { + if (!nodes) { auto ifNodePrev = exp->new_ptr(); ifNodePrev->type.set(toAst("unless"sv, exp)); auto ifCondPrev = exp->new_ptr(); @@ -3468,6 +3453,22 @@ private: newCondExp.set(newExp(simpleValue, exp)); nodes = &ifNodePrev->nodes; } + auto block = exp->new_ptr(); + auto stmt = exp->new_ptr(); + stmt->content.set(preDefine); + preDefine.set(nullptr); + block->statements.push_back(stmt); + auto simpleValue = exp->new_ptr(); + simpleValue->value.set(ifNode); + auto explist = exp->new_ptr(); + explist->exprs.push_back(newExp(simpleValue, exp)); + auto expListAssign = exp->new_ptr(); + expListAssign->expList.set(explist); + stmt = exp->new_ptr(); + stmt->content.set(expListAssign); + block->statements.push_back(stmt); + nodes->push_back(block); + nodes = &ifNode->nodes; } else { auto block = exp->new_ptr(); auto stmt = exp->new_ptr(); @@ -9175,17 +9176,17 @@ private: pushScope(); extraScope = true; } - auto typeVar = getUnusedName("_type_"); + auto typeVar = getUnusedName("_type_"sv); forceAddToScope(typeVar); - tabCheckVar = getUnusedName("_tab_"); + tabCheckVar = getUnusedName("_tab_"sv); forceAddToScope(tabCheckVar); - temp.push_back(indent() + "local "s + typeVar + " = "s + globalVar("type", branch) + '(' + objVar + ')' + nll(branch)); + temp.push_back(indent() + "local "s + typeVar + " = "s + globalVar("type"sv, branch) + '(' + objVar + ')' + nll(branch)); temp.push_back(indent() + "local "s + tabCheckVar + " = \"table\" == "s + typeVar + " or \"userdata\" == "s + typeVar + nll(branch)); } std::string matchVar; bool lastBranch = branches.back() == branch_ && !switchNode->lastBranch; if (!lastBranch) { - matchVar = getUnusedName("_match_"); + matchVar = getUnusedName("_match_"sv); forceAddToScope(matchVar); temp.push_back(indent() + "local "s + matchVar + " = false"s + nll(branch)); } -- cgit v1.2.3-55-g6feb