diff options
author | Li Jin <dragon-fly@qq.com> | 2025-05-21 18:10:32 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2025-05-21 18:10:32 +0800 |
commit | b21621692e877e5a44508b90049c2bc75091ebac (patch) | |
tree | 8adecb25d6a955d23cc55dbcf9d4867b2918eec9 /src | |
parent | 040f6802ef02f6541e0213de0440f9b1b758d9fc (diff) | |
download | yuescript-b21621692e877e5a44508b90049c2bc75091ebac.tar.gz yuescript-b21621692e877e5a44508b90049c2bc75091ebac.tar.bz2 yuescript-b21621692e877e5a44508b90049c2bc75091ebac.zip |
Allowed backcall without parentheses.v0.28.2
Diffstat (limited to 'src')
-rw-r--r-- | src/yuescript/yue_ast.cpp | 6 | ||||
-rw-r--r-- | src/yuescript/yue_ast.h | 8 | ||||
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 34 | ||||
-rw-r--r-- | src/yuescript/yue_parser.cpp | 3 | ||||
-rw-r--r-- | src/yuescript/yue_parser.h | 1 |
5 files changed, 49 insertions, 3 deletions
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 | } |
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 | } | ||
334 | std::string PipeBody_t::to_string(void* ud) const { | 340 | std::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) |
842 | AST_END(UnaryExp) | 842 | AST_END(UnaryExp) |
843 | 843 | ||
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 | |||
844 | AST_NODE(ExpListAssign) | 850 | AST_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) |
848 | AST_END(ExpListAssign) | 854 | AST_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 | ||
81 | const std::string_view version = "0.28.1"sv; | 81 | const std::string_view version = "0.28.2"sv; |
82 | const std::string_view extension = "yue"sv; | 82 | const std::string_view extension = "yue"sv; |
83 | 83 | ||
84 | class CompileError : public std::logic_error { | 84 | class 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); |