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. --- 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 + 5 files changed, 49 insertions(+), 3 deletions(-) (limited to 'src') 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