From b21621692e877e5a44508b90049c2bc75091ebac Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 21 May 2025 18:10:32 +0800 Subject: Allowed backcall without parentheses. --- doc/docs/doc/README.md | 10 +++++----- doc/docs/zh/doc/README.md | 10 +++++----- spec/inputs/backcall.yue | 4 ++-- src/yuescript/yue_ast.cpp | 6 ++++++ src/yuescript/yue_ast.h | 8 +++++++- src/yuescript/yue_compiler.cpp | 34 +++++++++++++++++++++++++++++++++- src/yuescript/yue_parser.cpp | 3 ++- src/yuescript/yue_parser.h | 1 + 8 files changed, 61 insertions(+), 15 deletions(-) diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index 502f7b6..4a408fe 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md @@ -1925,22 +1925,22 @@ x * 2 -If you wish to have further code after your backcalls, you can set them aside with a do statement. +If you wish to have further code after your backcalls, you can set them aside with a do statement. And the parentheses can be omitted with non-fat arrow functions. ```moonscript result, msg = do - (data) <- readAsync "filename.txt" + data <- readAsync "filename.txt" print data - (info) <- processAsync data + info <- processAsync data check info print result, msg ```
 result, msg = do
-  (data) <- readAsync "filename.txt"
+  data <- readAsync "filename.txt"
   print data
-  (info) <- processAsync data
+  info <- processAsync data
   check info
 print result, msg
 
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index e5efe3b..d7b8361 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md @@ -1886,22 +1886,22 @@ x * 2
-如果你希望在反向回调处理后继续编写更多其它的代码,你可以使用do语句将不归属反向回调的代码分开。 +如果你希望在反向回调处理后继续编写更多其它的代码,可以使用 do 语句将不属于反向回调的代码分隔开。对于非粗箭头函数的反向回调,回调返回值的括号也是可以省略的。 ```moonscript result, msg = do - (data) <- readAsync "文件名.txt" + data <- readAsync "文件名.txt" print data - (info) <- processAsync data + info <- processAsync data check info print result, msg ```
 result, msg = do
-  (data) <- readAsync "文件名.txt"
+  data <- readAsync "文件名.txt"
   print data
-  (info) <- processAsync data
+  info <- processAsync data
   check info
 print result, msg
 
diff --git a/spec/inputs/backcall.yue b/spec/inputs/backcall.yue index 8aadc71..e6b8c21 100644 --- a/spec/inputs/backcall.yue +++ b/spec/inputs/backcall.yue @@ -13,9 +13,9 @@ do x > 2 do - (data) <- http?.get "ajaxtest" + data <- http?.get "ajaxtest" body[".result"]\html data - (processed) <- http.post "ajaxprocess", data + processed <- http.post "ajaxprocess", data body[".result"]\append processed <- setTimeout 1000 print "done" diff --git a/src/yuescript/yue_ast.cpp b/src/yuescript/yue_ast.cpp index 56e3447..fdb1d20 100644 --- a/src/yuescript/yue_ast.cpp +++ b/src/yuescript/yue_ast.cpp @@ -331,6 +331,12 @@ std::string Backcall_t::to_string(void* ud) const { temp.emplace_back(value->to_string(ud)); return join(temp, " "sv); } +std::string SubBackcall_t::to_string(void* ud) const { + str_list temp; + temp.emplace_back(arrow->to_string(ud)); + temp.emplace_back(value->to_string(ud)); + return join(temp, " "sv); +} std::string PipeBody_t::to_string(void* ud) const { auto info = reinterpret_cast(ud); str_list temp; diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 8501a19..0c15fac 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -841,9 +841,15 @@ AST_NODE(UnaryExp) AST_MEMBER(UnaryExp, &ops, &expos, &inExp) AST_END(UnaryExp) +AST_NODE(SubBackcall) + ast_ptr arrow; + ast_ptr value; + AST_MEMBER(SubBackcall, &arrow, &value) +AST_END(SubBackcall) + AST_NODE(ExpListAssign) ast_ptr expList; - ast_sel action; + ast_sel action; AST_MEMBER(ExpListAssign, &expList, &action) AST_END(ExpListAssign) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 5cd2804..60ed7ac 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -78,7 +78,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.28.1"sv; +const std::string_view version = "0.28.2"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -4878,6 +4878,38 @@ private: newBlock->statements.push_back(toAst("if "s + okVar + " then return ... else error ..."s, x)); transformBlock(newBlock, out, usage, assignList, isRoot); return; + } else if (auto expListAssign = stmt->content.as(); + expListAssign && expListAssign->action && expListAssign->action.is()) { + auto x = *nodes.begin(); + auto newBlock = x->new_ptr(); + if (it != nodes.begin()) { + for (auto i = nodes.begin(); i != it; ++i) { + newBlock->statements.push_back(*i); + } + } + auto doBackcall = static_cast(expListAssign->action.get()); + auto backcall = expListAssign->new_ptr(); + auto argsDef = backcall->new_ptr(); + try { + auto defList = toAst(YueFormat{}.toString(expListAssign->expList), expListAssign->expList); + argsDef->defList.set(defList); + } catch (const std::exception&) { + throw CompileError("backcall syntax error", backcall); + } + backcall->argsDef.set(argsDef); + backcall->arrow.set(doBackcall->arrow); + backcall->value.set(doBackcall->value); + auto newStmt = backcall->new_ptr(); + newStmt->content.set(backcall); + newStmt->comments.dup(stmt->comments); + newStmt->appendix.set(stmt->appendix); + newBlock->statements.push_back(newStmt); + auto ait = it; + for (auto i = ++ait; i != nodes.end(); ++i) { + newBlock->statements.push_back(*i); + } + transformBlock(newBlock, out, usage, assignList, isRoot); + return; } if (auto local = stmt->content.as()) { if (!local->collected) { diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 045e2c7..cd1fd48 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -889,6 +889,7 @@ YueParser::YueParser() { FnArrowBack = '<' >> set("-="); Backcall = -(FnArgsDef >> space) >> FnArrowBack >> space >> ChainValue; + SubBackcall = FnArrowBack >> space >> ChainValue; PipeBody = Seperator >> pipe_operator >> space >> UnaryExp >> @@ -946,7 +947,7 @@ YueParser::YueParser() { UnaryValue | TblComprehension | Comprehension | FunLit | Num | VarArg; - ExpListAssign = ExpList >> -(space >> (Update | Assign)) >> not_(space >> '='); + ExpListAssign = ExpList >> -(space >> (Update | Assign | SubBackcall)) >> not_(space >> '='); IfLine = IfType >> space >> IfCond; WhileLine = WhileType >> space >> Exp; diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index 63afcb9..4488685 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -321,6 +321,7 @@ private: AST_RULE(ShortTabAppending); AST_RULE(FnArrowBack); AST_RULE(Backcall); + AST_RULE(SubBackcall); AST_RULE(PipeBody); AST_RULE(ExpListLow); AST_RULE(ExpList); -- cgit v1.2.3-55-g6feb