diff options
| author | Li Jin <dragon-fly@qq.com> | 2022-02-21 17:08:58 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2022-02-21 17:08:58 +0800 |
| commit | 00acd729f39c7e6b5db35c6c8a49ba5c55382e58 (patch) | |
| tree | 1338128b83f24646198def33b581979297044f63 /src/yuescript/yue_compiler.cpp | |
| parent | b668018486572362b7cbba5257baa7a37ef8d01d (diff) | |
| download | yuescript-00acd729f39c7e6b5db35c6c8a49ba5c55382e58.tar.gz yuescript-00acd729f39c7e6b5db35c6c8a49ba5c55382e58.tar.bz2 yuescript-00acd729f39c7e6b5db35c6c8a49ba5c55382e58.zip | |
add an extra idiom for Lua pcall / xpcall: print result if success, result = try func
Diffstat (limited to '')
| -rwxr-xr-x | src/yuescript/yue_compiler.cpp | 71 |
1 files changed, 24 insertions, 47 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 41bf207..32a3335 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
| @@ -60,7 +60,7 @@ using namespace parserlib; | |||
| 60 | 60 | ||
| 61 | typedef std::list<std::string> str_list; | 61 | typedef std::list<std::string> str_list; |
| 62 | 62 | ||
| 63 | const std::string_view version = "0.10.1"sv; | 63 | const std::string_view version = "0.10.2"sv; |
| 64 | const std::string_view extension = "yue"sv; | 64 | const std::string_view extension = "yue"sv; |
| 65 | 65 | ||
| 66 | class YueCompilerImpl { | 66 | class YueCompilerImpl { |
| @@ -922,13 +922,8 @@ private: | |||
| 922 | case id<if_line_t>(): { | 922 | case id<if_line_t>(): { |
| 923 | auto if_line = static_cast<if_line_t*>(appendix->item.get()); | 923 | auto if_line = static_cast<if_line_t*>(appendix->item.get()); |
| 924 | auto ifNode = x->new_ptr<If_t>(); | 924 | auto ifNode = x->new_ptr<If_t>(); |
| 925 | auto ifType = toAst<IfType_t>("if"sv, x); | 925 | ifNode->type.set(if_line->type); |
| 926 | ifNode->type.set(ifType); | 926 | ifNode->nodes.push_back(if_line->condition); |
| 927 | |||
| 928 | auto ifCond = x->new_ptr<IfCond_t>(); | ||
| 929 | ifCond->condition.set(if_line->condition); | ||
| 930 | ifCond->assign.set(if_line->assign); | ||
| 931 | ifNode->nodes.push_back(ifCond); | ||
| 932 | 927 | ||
| 933 | auto stmt = x->new_ptr<Statement_t>(); | 928 | auto stmt = x->new_ptr<Statement_t>(); |
| 934 | stmt->content.set(statement->content); | 929 | stmt->content.set(statement->content); |
| @@ -947,33 +942,6 @@ private: | |||
| 947 | statement->content.set(expListAssign); | 942 | statement->content.set(expListAssign); |
| 948 | break; | 943 | break; |
| 949 | } | 944 | } |
| 950 | case id<unless_line_t>(): { | ||
| 951 | auto unless_line = static_cast<unless_line_t*>(appendix->item.get()); | ||
| 952 | auto ifNode = x->new_ptr<If_t>(); | ||
| 953 | auto ifType = toAst<IfType_t>("unless"sv, x); | ||
| 954 | ifNode->type.set(ifType); | ||
| 955 | |||
| 956 | auto ifCond = x->new_ptr<IfCond_t>(); | ||
| 957 | ifCond->condition.set(unless_line->condition); | ||
| 958 | ifNode->nodes.push_back(ifCond); | ||
| 959 | |||
| 960 | auto stmt = x->new_ptr<Statement_t>(); | ||
| 961 | stmt->content.set(statement->content); | ||
| 962 | ifNode->nodes.push_back(stmt); | ||
| 963 | |||
| 964 | statement->appendix.set(nullptr); | ||
| 965 | auto simpleValue = x->new_ptr<SimpleValue_t>(); | ||
| 966 | simpleValue->value.set(ifNode); | ||
| 967 | auto value = x->new_ptr<Value_t>(); | ||
| 968 | value->item.set(simpleValue); | ||
| 969 | auto exp = newExp(value, x); | ||
| 970 | auto exprList = x->new_ptr<ExpList_t>(); | ||
| 971 | exprList->exprs.push_back(exp); | ||
| 972 | auto expListAssign = x->new_ptr<ExpListAssign_t>(); | ||
| 973 | expListAssign->expList.set(exprList); | ||
| 974 | statement->content.set(expListAssign); | ||
| 975 | break; | ||
| 976 | } | ||
| 977 | case id<CompInner_t>(): { | 945 | case id<CompInner_t>(): { |
| 978 | auto compInner = appendix->item.to<CompInner_t>(); | 946 | auto compInner = appendix->item.to<CompInner_t>(); |
| 979 | auto comp = x->new_ptr<Comprehension_t>(); | 947 | auto comp = x->new_ptr<Comprehension_t>(); |
| @@ -2185,11 +2153,11 @@ private: | |||
| 2185 | } | 2153 | } |
| 2186 | 2154 | ||
| 2187 | void transformCond(const node_container& nodes, str_list& out, ExpUsage usage, bool unless, ExpList_t* assignList) { | 2155 | void transformCond(const node_container& nodes, str_list& out, ExpUsage usage, bool unless, ExpList_t* assignList) { |
| 2188 | std::vector<ast_ptr<false, ast_node>> ns(false); | 2156 | std::vector<ast_ptr<false, ast_node>> ns; |
| 2189 | for (auto it = nodes.rbegin(); it != nodes.rend(); ++it) { | 2157 | for (auto it = nodes.rbegin(); it != nodes.rend(); ++it) { |
| 2190 | ns.push_back(*it); | 2158 | ns.push_back(*it); |
| 2191 | if (auto cond = ast_cast<IfCond_t>(*it)) { | 2159 | if (auto cond = ast_cast<IfCond_t>(*it)) { |
| 2192 | if (*it != nodes.front() && cond->assign) { | 2160 | if (*it != nodes.front() && cond->condition.is<assignment_t>()) { |
| 2193 | auto x = *it; | 2161 | auto x = *it; |
| 2194 | auto newIf = x->new_ptr<If_t>(); | 2162 | auto newIf = x->new_ptr<If_t>(); |
| 2195 | newIf->type.set(toAst<IfType_t>("if"sv, x)); | 2163 | newIf->type.set(toAst<IfType_t>("if"sv, x)); |
| @@ -2245,11 +2213,11 @@ private: | |||
| 2245 | default: YUEE("AST node mismatch", node); break; | 2213 | default: YUEE("AST node mismatch", node); break; |
| 2246 | } | 2214 | } |
| 2247 | } | 2215 | } |
| 2248 | auto assign = ifCondPairs.front().first->assign.get(); | 2216 | auto asmt = ifCondPairs.front().first->condition.as<assignment_t>(); |
| 2249 | bool storingValue = false; | 2217 | bool storingValue = false; |
| 2250 | ast_ptr<false, ExpListAssign_t> extraAssignment; | 2218 | ast_ptr<false, ExpListAssign_t> extraAssignment; |
| 2251 | if (assign) { | 2219 | if (asmt) { |
| 2252 | auto exp = ifCondPairs.front().first->condition.get(); | 2220 | ast_ptr<false, ast_node> exp = asmt->expList->exprs.front(); |
| 2253 | auto x = exp; | 2221 | auto x = exp; |
| 2254 | bool lintGlobal = _config.lintGlobalVariable; | 2222 | bool lintGlobal = _config.lintGlobalVariable; |
| 2255 | _config.lintGlobalVariable = false; | 2223 | _config.lintGlobalVariable = false; |
| @@ -2258,8 +2226,8 @@ private: | |||
| 2258 | if (var.empty()) { | 2226 | if (var.empty()) { |
| 2259 | storingValue = true; | 2227 | storingValue = true; |
| 2260 | auto desVar = getUnusedName("_des_"sv); | 2228 | auto desVar = getUnusedName("_des_"sv); |
| 2261 | if (assign->values.objects().size() == 1) { | 2229 | if (asmt->assign->values.objects().size() == 1) { |
| 2262 | auto var = singleVariableFrom(assign->values.objects().front()); | 2230 | auto var = singleVariableFrom(asmt->assign->values.objects().front()); |
| 2263 | if (!var.empty() && isLocal(var)) { | 2231 | if (!var.empty() && isLocal(var)) { |
| 2264 | desVar = var; | 2232 | desVar = var; |
| 2265 | storingValue = false; | 2233 | storingValue = false; |
| @@ -2267,13 +2235,17 @@ private: | |||
| 2267 | } | 2235 | } |
| 2268 | if (storingValue) { | 2236 | if (storingValue) { |
| 2269 | if (usage != ExpUsage::Closure) { | 2237 | if (usage != ExpUsage::Closure) { |
| 2270 | temp.push_back(indent() + "do"s + nll(assign)); | 2238 | temp.push_back(indent() + "do"s + nll(asmt)); |
| 2271 | pushScope(); | 2239 | pushScope(); |
| 2272 | } | 2240 | } |
| 2241 | asmt->expList->exprs.pop_front(); | ||
| 2273 | auto expList = toAst<ExpList_t>(desVar, x); | 2242 | auto expList = toAst<ExpList_t>(desVar, x); |
| 2274 | auto assignment = x->new_ptr<ExpListAssign_t>(); | 2243 | auto assignment = x->new_ptr<ExpListAssign_t>(); |
| 2244 | for (auto expr : asmt->expList->exprs.objects()) { | ||
| 2245 | expList->exprs.push_back(expr); | ||
| 2246 | } | ||
| 2275 | assignment->expList.set(expList); | 2247 | assignment->expList.set(expList); |
| 2276 | assignment->action.set(assign); | 2248 | assignment->action.set(asmt->assign); |
| 2277 | transformAssignment(assignment, temp); | 2249 | transformAssignment(assignment, temp); |
| 2278 | } | 2250 | } |
| 2279 | { | 2251 | { |
| @@ -2292,22 +2264,27 @@ private: | |||
| 2292 | if (!isDefined(var)) { | 2264 | if (!isDefined(var)) { |
| 2293 | storingValue = true; | 2265 | storingValue = true; |
| 2294 | if (usage != ExpUsage::Closure) { | 2266 | if (usage != ExpUsage::Closure) { |
| 2295 | temp.push_back(indent() + "do"s + nll(assign)); | 2267 | temp.push_back(indent() + "do"s + nll(asmt)); |
| 2296 | pushScope(); | 2268 | pushScope(); |
| 2297 | } | 2269 | } |
| 2298 | } | 2270 | } |
| 2299 | auto expList = x->new_ptr<ExpList_t>(); | 2271 | auto expList = x->new_ptr<ExpList_t>(); |
| 2300 | expList->exprs.push_back(exp); | 2272 | expList->exprs.push_back(exp); |
| 2273 | asmt->expList->exprs.pop_front(); | ||
| 2274 | for (auto expr : asmt->expList->exprs.objects()) { | ||
| 2275 | expList->exprs.push_back(expr); | ||
| 2276 | } | ||
| 2301 | auto assignment = x->new_ptr<ExpListAssign_t>(); | 2277 | auto assignment = x->new_ptr<ExpListAssign_t>(); |
| 2302 | assignment->expList.set(expList); | 2278 | assignment->expList.set(expList); |
| 2303 | assignment->action.set(assign); | 2279 | assignment->action.set(asmt->assign); |
| 2304 | transformAssignment(assignment, temp); | 2280 | transformAssignment(assignment, temp); |
| 2281 | ifCondPairs.front().first->condition.set(exp); | ||
| 2305 | } | 2282 | } |
| 2306 | } | 2283 | } |
| 2307 | for (const auto& pair : ifCondPairs) { | 2284 | for (const auto& pair : ifCondPairs) { |
| 2308 | if (pair.first) { | 2285 | if (pair.first) { |
| 2309 | str_list tmp; | 2286 | str_list tmp; |
| 2310 | auto condition = pair.first->condition.get(); | 2287 | auto condition = pair.first->condition.to<Exp_t>(); |
| 2311 | auto condStr = transformCondExp(condition, unless); | 2288 | auto condStr = transformCondExp(condition, unless); |
| 2312 | if (unless) unless = false; | 2289 | if (unless) unless = false; |
| 2313 | _buf << indent(); | 2290 | _buf << indent(); |
