aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xdoc/docs/doc/README.md10
-rwxr-xr-xdoc/docs/zh/doc/README.md10
-rw-r--r--spec/inputs/backcall.yue4
-rw-r--r--src/yuescript/yue_ast.cpp6
-rw-r--r--src/yuescript/yue_ast.h8
-rw-r--r--src/yuescript/yue_compiler.cpp34
-rw-r--r--src/yuescript/yue_parser.cpp3
-rw-r--r--src/yuescript/yue_parser.h1
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
1925</pre> 1925</pre>
1926</YueDisplay> 1926</YueDisplay>
1927 1927
1928If you wish to have further code after your backcalls, you can set them aside with a do statement. 1928If 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.
1929 1929
1930```moonscript 1930```moonscript
1931result, msg = do 1931result, msg = do
1932 (data) <- readAsync "filename.txt" 1932 data <- readAsync "filename.txt"
1933 print data 1933 print data
1934 (info) <- processAsync data 1934 info <- processAsync data
1935 check info 1935 check info
1936print result, msg 1936print result, msg
1937``` 1937```
1938<YueDisplay> 1938<YueDisplay>
1939<pre> 1939<pre>
1940result, msg = do 1940result, msg = do
1941 (data) <- readAsync "filename.txt" 1941 data <- readAsync "filename.txt"
1942 print data 1942 print data
1943 (info) <- processAsync data 1943 info <- processAsync data
1944 check info 1944 check info
1945print result, msg 1945print result, msg
1946</pre> 1946</pre>
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
1886</pre> 1886</pre>
1887</YueDisplay> 1887</YueDisplay>
1888 1888
1889如果你希望在反向回调处理后继续编写更多其它的代码,你可以使用do语句将不归属反向回调的代码分开。 1889如果你希望在反向回调处理后继续编写更多其它的代码,可以使用 do 语句将不属于反向回调的代码分隔开。对于非粗箭头函数的反向回调,回调返回值的括号也是可以省略的。
1890 1890
1891```moonscript 1891```moonscript
1892result, msg = do 1892result, msg = do
1893 (data) <- readAsync "文件名.txt" 1893 data <- readAsync "文件名.txt"
1894 print data 1894 print data
1895 (info) <- processAsync data 1895 info <- processAsync data
1896 check info 1896 check info
1897print result, msg 1897print result, msg
1898``` 1898```
1899<YueDisplay> 1899<YueDisplay>
1900<pre> 1900<pre>
1901result, msg = do 1901result, msg = do
1902 (data) <- readAsync "文件名.txt" 1902 data <- readAsync "文件名.txt"
1903 print data 1903 print data
1904 (info) <- processAsync data 1904 info <- processAsync data
1905 check info 1905 check info
1906print result, msg 1906print result, msg
1907</pre> 1907</pre>
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
13 x > 2 13 x > 2
14 14
15do 15do
16 (data) <- http?.get "ajaxtest" 16 data <- http?.get "ajaxtest"
17 body[".result"]\html data 17 body[".result"]\html data
18 (processed) <- http.post "ajaxprocess", data 18 processed <- http.post "ajaxprocess", data
19 body[".result"]\append processed 19 body[".result"]\append processed
20 <- setTimeout 1000 20 <- setTimeout 1000
21 print "done" 21 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 {
331 temp.emplace_back(value->to_string(ud)); 331 temp.emplace_back(value->to_string(ud));
332 return join(temp, " "sv); 332 return join(temp, " "sv);
333} 333}
334std::string SubBackcall_t::to_string(void* ud) const {
335 str_list temp;
336 temp.emplace_back(arrow->to_string(ud));
337 temp.emplace_back(value->to_string(ud));
338 return join(temp, " "sv);
339}
334std::string PipeBody_t::to_string(void* ud) const { 340std::string PipeBody_t::to_string(void* ud) const {
335 auto info = reinterpret_cast<YueFormat*>(ud); 341 auto info = reinterpret_cast<YueFormat*>(ud);
336 str_list temp; 342 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)
841 AST_MEMBER(UnaryExp, &ops, &expos, &inExp) 841 AST_MEMBER(UnaryExp, &ops, &expos, &inExp)
842AST_END(UnaryExp) 842AST_END(UnaryExp)
843 843
844AST_NODE(SubBackcall)
845 ast_ptr<true, FnArrowBack_t> arrow;
846 ast_ptr<true, ChainValue_t> value;
847 AST_MEMBER(SubBackcall, &arrow, &value)
848AST_END(SubBackcall)
849
844AST_NODE(ExpListAssign) 850AST_NODE(ExpListAssign)
845 ast_ptr<true, ExpList_t> expList; 851 ast_ptr<true, ExpList_t> expList;
846 ast_sel<false, Update_t, Assign_t> action; 852 ast_sel<false, Update_t, Assign_t, SubBackcall_t> action;
847 AST_MEMBER(ExpListAssign, &expList, &action) 853 AST_MEMBER(ExpListAssign, &expList, &action)
848AST_END(ExpListAssign) 854AST_END(ExpListAssign)
849 855
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<std::string> Metamethods = {
78 "close"s // Lua 5.4 78 "close"s // Lua 5.4
79}; 79};
80 80
81const std::string_view version = "0.28.1"sv; 81const std::string_view version = "0.28.2"sv;
82const std::string_view extension = "yue"sv; 82const std::string_view extension = "yue"sv;
83 83
84class CompileError : public std::logic_error { 84class CompileError : public std::logic_error {
@@ -4878,6 +4878,38 @@ private:
4878 newBlock->statements.push_back(toAst<Statement_t>("if "s + okVar + " then return ... else error ..."s, x)); 4878 newBlock->statements.push_back(toAst<Statement_t>("if "s + okVar + " then return ... else error ..."s, x));
4879 transformBlock(newBlock, out, usage, assignList, isRoot); 4879 transformBlock(newBlock, out, usage, assignList, isRoot);
4880 return; 4880 return;
4881 } else if (auto expListAssign = stmt->content.as<ExpListAssign_t>();
4882 expListAssign && expListAssign->action && expListAssign->action.is<SubBackcall_t>()) {
4883 auto x = *nodes.begin();
4884 auto newBlock = x->new_ptr<Block_t>();
4885 if (it != nodes.begin()) {
4886 for (auto i = nodes.begin(); i != it; ++i) {
4887 newBlock->statements.push_back(*i);
4888 }
4889 }
4890 auto doBackcall = static_cast<SubBackcall_t*>(expListAssign->action.get());
4891 auto backcall = expListAssign->new_ptr<Backcall_t>();
4892 auto argsDef = backcall->new_ptr<FnArgsDef_t>();
4893 try {
4894 auto defList = toAst<FnArgDefList_t>(YueFormat{}.toString(expListAssign->expList), expListAssign->expList);
4895 argsDef->defList.set(defList);
4896 } catch (const std::exception&) {
4897 throw CompileError("backcall syntax error", backcall);
4898 }
4899 backcall->argsDef.set(argsDef);
4900 backcall->arrow.set(doBackcall->arrow);
4901 backcall->value.set(doBackcall->value);
4902 auto newStmt = backcall->new_ptr<Statement_t>();
4903 newStmt->content.set(backcall);
4904 newStmt->comments.dup(stmt->comments);
4905 newStmt->appendix.set(stmt->appendix);
4906 newBlock->statements.push_back(newStmt);
4907 auto ait = it;
4908 for (auto i = ++ait; i != nodes.end(); ++i) {
4909 newBlock->statements.push_back(*i);
4910 }
4911 transformBlock(newBlock, out, usage, assignList, isRoot);
4912 return;
4881 } 4913 }
4882 if (auto local = stmt->content.as<Local_t>()) { 4914 if (auto local = stmt->content.as<Local_t>()) {
4883 if (!local->collected) { 4915 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() {
889 889
890 FnArrowBack = '<' >> set("-="); 890 FnArrowBack = '<' >> set("-=");
891 Backcall = -(FnArgsDef >> space) >> FnArrowBack >> space >> ChainValue; 891 Backcall = -(FnArgsDef >> space) >> FnArrowBack >> space >> ChainValue;
892 SubBackcall = FnArrowBack >> space >> ChainValue;
892 893
893 PipeBody = Seperator >> 894 PipeBody = Seperator >>
894 pipe_operator >> space >> UnaryExp >> 895 pipe_operator >> space >> UnaryExp >>
@@ -946,7 +947,7 @@ YueParser::YueParser() {
946 UnaryValue | TblComprehension | Comprehension | 947 UnaryValue | TblComprehension | Comprehension |
947 FunLit | Num | VarArg; 948 FunLit | Num | VarArg;
948 949
949 ExpListAssign = ExpList >> -(space >> (Update | Assign)) >> not_(space >> '='); 950 ExpListAssign = ExpList >> -(space >> (Update | Assign | SubBackcall)) >> not_(space >> '=');
950 951
951 IfLine = IfType >> space >> IfCond; 952 IfLine = IfType >> space >> IfCond;
952 WhileLine = WhileType >> space >> Exp; 953 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:
321 AST_RULE(ShortTabAppending); 321 AST_RULE(ShortTabAppending);
322 AST_RULE(FnArrowBack); 322 AST_RULE(FnArrowBack);
323 AST_RULE(Backcall); 323 AST_RULE(Backcall);
324 AST_RULE(SubBackcall);
324 AST_RULE(PipeBody); 325 AST_RULE(PipeBody);
325 AST_RULE(ExpListLow); 326 AST_RULE(ExpListLow);
326 AST_RULE(ExpList); 327 AST_RULE(ExpList);