From caafcb55427150d010d3feefcfd012263a919d25 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 30 Sep 2022 15:39:11 +0800 Subject: add compiler option and fix an issue with return statement in loop working with continue. --- src/yuescript/yue_compiler.cpp | 41 +++++++++++++++++++++++------------------ src/yuescript/yuescript.cpp | 6 ++++++ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 672f2d4..bdb07e0 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -5862,6 +5862,27 @@ private: }); } + void addDoToLastLineReturn(ast_node* body) { + if (auto block = ast_cast(body); body && !block->statements.empty()) { + auto last = static_cast(block->statements.back()); + if (last->content.is()) { + auto doNode = last->new_ptr(); + auto newBody = last->new_ptr(); + auto newStmt = last->new_ptr(); + newStmt->content.set(last->content); + newBody->content.set(newStmt); + doNode->body.set(newBody); + auto simpleValue = last->new_ptr(); + simpleValue->value.set(doNode); + auto expList = last->new_ptr(); + expList->exprs.push_back(newExp(simpleValue, last)); + auto expListAssign = last->new_ptr(); + expListAssign->expList.set(expList); + last->content.set(expListAssign); + } + } + } + void transformLoopBody(ast_node* body, str_list& out, const std::string& appendContent, ExpUsage usage, ExpList_t* assignList = nullptr) { str_list temp; bool extraDo = false; @@ -5896,24 +5917,7 @@ private: extraLabel = temp.back(); temp.pop_back(); } - if (auto block = ast_cast(body); body && !block->statements.empty()) { - auto last = static_cast(block->statements.back()); - if (last->content.is()) { - auto doNode = last->new_ptr(); - auto newBody = last->new_ptr(); - auto newStmt = last->new_ptr(); - newStmt->content.set(last->content); - newBody->content.set(newStmt); - doNode->body.set(newBody); - auto simpleValue = last->new_ptr(); - simpleValue->value.set(doNode); - auto expList = last->new_ptr(); - expList->exprs.push_back(newExp(simpleValue, last)); - auto expListAssign = last->new_ptr(); - expListAssign->expList.set(expList); - last->content.set(expListAssign); - } - } + addDoToLastLineReturn(body); } transform_plain_body(body, temp, usage, assignList); if (withContinue) { @@ -5992,6 +5996,7 @@ private: extraLabel = temp.back(); temp.pop_back(); } + addDoToLastLineReturn(body); } transform_plain_body(body, temp, ExpUsage::Common); if (withContinue) { diff --git a/src/yuescript/yuescript.cpp b/src/yuescript/yuescript.cpp index 06feb21..0dca1f7 100644 --- a/src/yuescript/yuescript.cpp +++ b/src/yuescript/yuescript.cpp @@ -103,6 +103,12 @@ static int yuetolua(lua_State* L) { config.module = lua_tostring(L, -1); } lua_pop(L, 1); + lua_pushliteral(L, "target"); + lua_gettable(L, -2); + if (lua_isstring(L, -1) != 0) { + config.options["target"] = lua_tostring(L, -1); + } + lua_pop(L, 1); } std::string s(input, size); auto result = yue::YueCompiler(L, nullptr, sameModule).compile(s, config); -- cgit v1.2.3-55-g6feb