Skip to content

Commit b216216

Browse files
committed
Allowed backcall without parentheses.
1 parent 040f680 commit b216216

8 files changed

Lines changed: 61 additions & 15 deletions

File tree

doc/docs/doc/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,22 +1925,22 @@ x * 2
19251925
</pre>
19261926
</YueDisplay>
19271927

1928-
If you wish to have further code after your backcalls, you can set them aside with a do statement.
1928+
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.
19291929

19301930
```moonscript
19311931
result, msg = do
1932-
(data) <- readAsync "filename.txt"
1932+
data <- readAsync "filename.txt"
19331933
print data
1934-
(info) <- processAsync data
1934+
info <- processAsync data
19351935
check info
19361936
print result, msg
19371937
```
19381938
<YueDisplay>
19391939
<pre>
19401940
result, msg = do
1941-
(data) <- readAsync "filename.txt"
1941+
data <- readAsync "filename.txt"
19421942
print data
1943-
(info) <- processAsync data
1943+
info <- processAsync data
19441944
check info
19451945
print result, msg
19461946
</pre>

doc/docs/zh/doc/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,22 +1886,22 @@ x * 2
18861886
</pre>
18871887
</YueDisplay>
18881888

1889-
如果你希望在反向回调处理后继续编写更多其它的代码,你可以使用do语句将不归属反向回调的代码分开
1889+
如果你希望在反向回调处理后继续编写更多其它的代码,可以使用 do 语句将不属于反向回调的代码分隔开。对于非粗箭头函数的反向回调,回调返回值的括号也是可以省略的
18901890

18911891
```moonscript
18921892
result, msg = do
1893-
(data) <- readAsync "文件名.txt"
1893+
data <- readAsync "文件名.txt"
18941894
print data
1895-
(info) <- processAsync data
1895+
info <- processAsync data
18961896
check info
18971897
print result, msg
18981898
```
18991899
<YueDisplay>
19001900
<pre>
19011901
result, msg = do
1902-
(data) <- readAsync "文件名.txt"
1902+
data <- readAsync "文件名.txt"
19031903
print data
1904-
(info) <- processAsync data
1904+
info <- processAsync data
19051905
check info
19061906
print result, msg
19071907
</pre>

spec/inputs/backcall.yue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ do
1313
x > 2
1414

1515
do
16-
(data) <- http?.get "ajaxtest"
16+
data <- http?.get "ajaxtest"
1717
body[".result"]\html data
18-
(processed) <- http.post "ajaxprocess", data
18+
processed <- http.post "ajaxprocess", data
1919
body[".result"]\append processed
2020
<- setTimeout 1000
2121
print "done"

src/yuescript/yue_ast.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,12 @@ std::string Backcall_t::to_string(void* ud) const {
331331
temp.emplace_back(value->to_string(ud));
332332
return join(temp, " "sv);
333333
}
334+
std::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+
}
334340
std::string PipeBody_t::to_string(void* ud) const {
335341
auto info = reinterpret_cast<YueFormat*>(ud);
336342
str_list temp;

src/yuescript/yue_ast.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,15 @@ AST_NODE(UnaryExp)
841841
AST_MEMBER(UnaryExp, &ops, &expos, &inExp)
842842
AST_END(UnaryExp)
843843

844+
AST_NODE(SubBackcall)
845+
ast_ptr<true, FnArrowBack_t> arrow;
846+
ast_ptr<true, ChainValue_t> value;
847+
AST_MEMBER(SubBackcall, &arrow, &value)
848+
AST_END(SubBackcall)
849+
844850
AST_NODE(ExpListAssign)
845851
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;
847853
AST_MEMBER(ExpListAssign, &expList, &action)
848854
AST_END(ExpListAssign)
849855

src/yuescript/yue_compiler.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static std::unordered_set<std::string> Metamethods = {
7878
"close"s // Lua 5.4
7979
};
8080

81-
const std::string_view version = "0.28.1"sv;
81+
const std::string_view version = "0.28.2"sv;
8282
const std::string_view extension = "yue"sv;
8383

8484
class CompileError : public std::logic_error {
@@ -4878,6 +4878,38 @@ class YueCompilerImpl {
48784878
newBlock->statements.push_back(toAst<Statement_t>("if "s + okVar + " then return ... else error ..."s, x));
48794879
transformBlock(newBlock, out, usage, assignList, isRoot);
48804880
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;
48814913
}
48824914
if (auto local = stmt->content.as<Local_t>()) {
48834915
if (!local->collected) {

src/yuescript/yue_parser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ YueParser::YueParser() {
889889

890890
FnArrowBack = '<' >> set("-=");
891891
Backcall = -(FnArgsDef >> space) >> FnArrowBack >> space >> ChainValue;
892+
SubBackcall = FnArrowBack >> space >> ChainValue;
892893

893894
PipeBody = Seperator >>
894895
pipe_operator >> space >> UnaryExp >>
@@ -946,7 +947,7 @@ YueParser::YueParser() {
946947
UnaryValue | TblComprehension | Comprehension |
947948
FunLit | Num | VarArg;
948949

949-
ExpListAssign = ExpList >> -(space >> (Update | Assign)) >> not_(space >> '=');
950+
ExpListAssign = ExpList >> -(space >> (Update | Assign | SubBackcall)) >> not_(space >> '=');
950951

951952
IfLine = IfType >> space >> IfCond;
952953
WhileLine = WhileType >> space >> Exp;

src/yuescript/yue_parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ class YueParser {
321321
AST_RULE(ShortTabAppending);
322322
AST_RULE(FnArrowBack);
323323
AST_RULE(Backcall);
324+
AST_RULE(SubBackcall);
324325
AST_RULE(PipeBody);
325326
AST_RULE(ExpListLow);
326327
AST_RULE(ExpList);

0 commit comments

Comments
 (0)