From f48e99462a9fac6b4e5ad1b823b78d263025970c Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sat, 5 Mar 2022 13:10:12 +0800 Subject: fix issue #82. --- spec/outputs/loops.lua | 16 ++++++++++------ src/yuescript/yue_compiler.cpp | 23 ++++++++++++++++++++--- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/spec/outputs/loops.lua b/spec/outputs/loops.lua index a905e4e..aa69840 100644 --- a/spec/outputs/loops.lua +++ b/spec/outputs/loops.lua @@ -171,11 +171,13 @@ for i = 1, 10 do while true do local _continue_0 = false repeat - if not true then - _continue_0 = true + do + if not true then + _continue_0 = true + break + end break end - break _continue_0 = true until true if not _continue_0 then @@ -313,11 +315,13 @@ end while true do local _continue_0 = false repeat - if false then - _continue_0 = true + do + if false then + _continue_0 = true + break + end break end - break _continue_0 = true until true if not _continue_0 then diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 6ae4771..45d7820 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.10.4"sv; +const std::string_view version = "0.10.5"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -4844,17 +4844,34 @@ private: } return traversal::Continue; }); + bool extraDo = false; if (withContinue) { + if (auto block = ast_cast(body)) { + if (!block->statements.empty()) { + auto stmt = static_cast(block->statements.back()); + if (auto breakLoop = ast_cast(stmt->content)) { + extraDo = _parser.toString(breakLoop) == "break"sv; + } + } + } auto continueVar = getUnusedName("_continue_"sv); addToScope(continueVar); + _continueVars.push(continueVar); _buf << indent() << "local "sv << continueVar << " = false"sv << nll(body); _buf << indent() << "repeat"sv << nll(body); - temp.push_back(clearBuf()); - _continueVars.push(continueVar); pushScope(); + if (extraDo) { + _buf << indent() << "do"sv << nll(body); + pushScope(); + } + temp.push_back(clearBuf()); } transform_plain_body(body, temp, usage, assignList); if (withContinue) { + if (extraDo) { + popScope(); + _buf << indent() << "end"sv << nll(body); + } if (!appendContent.empty()) { _buf << indent() << appendContent; } -- cgit v1.2.3-55-g6feb