diff options
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 41 | ||||
-rw-r--r-- | 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: | |||
5862 | }); | 5862 | }); |
5863 | } | 5863 | } |
5864 | 5864 | ||
5865 | void addDoToLastLineReturn(ast_node* body) { | ||
5866 | if (auto block = ast_cast<Block_t>(body); body && !block->statements.empty()) { | ||
5867 | auto last = static_cast<Statement_t*>(block->statements.back()); | ||
5868 | if (last->content.is<Return_t>()) { | ||
5869 | auto doNode = last->new_ptr<Do_t>(); | ||
5870 | auto newBody = last->new_ptr<Body_t>(); | ||
5871 | auto newStmt = last->new_ptr<Statement_t>(); | ||
5872 | newStmt->content.set(last->content); | ||
5873 | newBody->content.set(newStmt); | ||
5874 | doNode->body.set(newBody); | ||
5875 | auto simpleValue = last->new_ptr<SimpleValue_t>(); | ||
5876 | simpleValue->value.set(doNode); | ||
5877 | auto expList = last->new_ptr<ExpList_t>(); | ||
5878 | expList->exprs.push_back(newExp(simpleValue, last)); | ||
5879 | auto expListAssign = last->new_ptr<ExpListAssign_t>(); | ||
5880 | expListAssign->expList.set(expList); | ||
5881 | last->content.set(expListAssign); | ||
5882 | } | ||
5883 | } | ||
5884 | } | ||
5885 | |||
5865 | void transformLoopBody(ast_node* body, str_list& out, const std::string& appendContent, ExpUsage usage, ExpList_t* assignList = nullptr) { | 5886 | void transformLoopBody(ast_node* body, str_list& out, const std::string& appendContent, ExpUsage usage, ExpList_t* assignList = nullptr) { |
5866 | str_list temp; | 5887 | str_list temp; |
5867 | bool extraDo = false; | 5888 | bool extraDo = false; |
@@ -5896,24 +5917,7 @@ private: | |||
5896 | extraLabel = temp.back(); | 5917 | extraLabel = temp.back(); |
5897 | temp.pop_back(); | 5918 | temp.pop_back(); |
5898 | } | 5919 | } |
5899 | if (auto block = ast_cast<Block_t>(body); body && !block->statements.empty()) { | 5920 | addDoToLastLineReturn(body); |
5900 | auto last = static_cast<Statement_t*>(block->statements.back()); | ||
5901 | if (last->content.is<Return_t>()) { | ||
5902 | auto doNode = last->new_ptr<Do_t>(); | ||
5903 | auto newBody = last->new_ptr<Body_t>(); | ||
5904 | auto newStmt = last->new_ptr<Statement_t>(); | ||
5905 | newStmt->content.set(last->content); | ||
5906 | newBody->content.set(newStmt); | ||
5907 | doNode->body.set(newBody); | ||
5908 | auto simpleValue = last->new_ptr<SimpleValue_t>(); | ||
5909 | simpleValue->value.set(doNode); | ||
5910 | auto expList = last->new_ptr<ExpList_t>(); | ||
5911 | expList->exprs.push_back(newExp(simpleValue, last)); | ||
5912 | auto expListAssign = last->new_ptr<ExpListAssign_t>(); | ||
5913 | expListAssign->expList.set(expList); | ||
5914 | last->content.set(expListAssign); | ||
5915 | } | ||
5916 | } | ||
5917 | } | 5921 | } |
5918 | transform_plain_body(body, temp, usage, assignList); | 5922 | transform_plain_body(body, temp, usage, assignList); |
5919 | if (withContinue) { | 5923 | if (withContinue) { |
@@ -5992,6 +5996,7 @@ private: | |||
5992 | extraLabel = temp.back(); | 5996 | extraLabel = temp.back(); |
5993 | temp.pop_back(); | 5997 | temp.pop_back(); |
5994 | } | 5998 | } |
5999 | addDoToLastLineReturn(body); | ||
5995 | } | 6000 | } |
5996 | transform_plain_body(body, temp, ExpUsage::Common); | 6001 | transform_plain_body(body, temp, ExpUsage::Common); |
5997 | if (withContinue) { | 6002 | 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) { | |||
103 | config.module = lua_tostring(L, -1); | 103 | config.module = lua_tostring(L, -1); |
104 | } | 104 | } |
105 | lua_pop(L, 1); | 105 | lua_pop(L, 1); |
106 | lua_pushliteral(L, "target"); | ||
107 | lua_gettable(L, -2); | ||
108 | if (lua_isstring(L, -1) != 0) { | ||
109 | config.options["target"] = lua_tostring(L, -1); | ||
110 | } | ||
111 | lua_pop(L, 1); | ||
106 | } | 112 | } |
107 | std::string s(input, size); | 113 | std::string s(input, size); |
108 | auto result = yue::YueCompiler(L, nullptr, sameModule).compile(s, config); | 114 | auto result = yue::YueCompiler(L, nullptr, sameModule).compile(s, config); |