aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2025-05-28 22:15:35 +0800
committerLi Jin <dragon-fly@qq.com>2025-05-28 22:15:35 +0800
commit2f61682aea987cdc5dd1cf44097dbbc28a7cbd2b (patch)
tree13e1de7ce06df0d80c41f9213a18acbc8415eca9
parent5604bbbb80bfcedb4a9085b90864e221f8104b33 (diff)
downloadyuescript-2f61682aea987cdc5dd1cf44097dbbc28a7cbd2b.tar.gz
yuescript-2f61682aea987cdc5dd1cf44097dbbc28a7cbd2b.tar.bz2
yuescript-2f61682aea987cdc5dd1cf44097dbbc28a7cbd2b.zip
Replace `try!` with `try?`.
-rwxr-xr-xdoc/docs/doc/README.md20
-rwxr-xr-xdoc/docs/zh/doc/README.md18
-rw-r--r--spec/inputs/try_catch.yue20
-rw-r--r--src/yuescript/yue_ast.cpp7
-rw-r--r--src/yuescript/yue_ast.h7
-rw-r--r--src/yuescript/yue_compiler.cpp8
-rw-r--r--src/yuescript/yue_parser.cpp3
-rw-r--r--src/yuescript/yue_parser.h1
8 files changed, 38 insertions, 46 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md
index 1d9c8ad..c0312f7 100755
--- a/doc/docs/doc/README.md
+++ b/doc/docs/doc/README.md
@@ -1516,21 +1516,21 @@ catch err
1516</pre> 1516</pre>
1517</YueDisplay> 1517</YueDisplay>
1518 1518
1519### Try! 1519### Try?
1520 1520
1521`try!` is a more concise error handling syntax that omit the boolean status from the `try` statement, and it will return the result from the try block when success, otherwise return nil instead of error object. 1521`try?` is a simplified use for error handling syntax that omit the boolean status from the `try` statement, and it will return the result from the try block when success, return nil instead of error object otherwise.
1522 1522
1523```moonscript 1523```moonscript
1524a, b, c = try! func! 1524a, b, c = try? func!
1525 1525
1526-- with nil coalescing operator 1526-- with nil coalescing operator
1527a = (try! func!) ?? "default" 1527a = (try? func!) ?? "default"
1528 1528
1529-- as function argument 1529-- as function argument
1530f try! func! 1530f try? func!
1531 1531
1532-- with catch block 1532-- with catch block
1533f try! 1533f try?
1534 print 123 1534 print 123
1535 func! 1535 func!
1536catch e 1536catch e
@@ -1539,16 +1539,16 @@ catch e
1539``` 1539```
1540<YueDisplay> 1540<YueDisplay>
1541<pre> 1541<pre>
1542a, b, c = try! func! 1542a, b, c = try? func!
1543 1543
1544-- with nil coalescing operator 1544-- with nil coalescing operator
1545a = (try! func!) ?? "default" 1545a = (try? func!) ?? "default"
1546 1546
1547-- as function argument 1547-- as function argument
1548f try! func! 1548f try? func!
1549 1549
1550-- with catch block 1550-- with catch block
1551f try! 1551f try?
1552 print 123 1552 print 123
1553 func! 1553 func!
1554catch e 1554catch e
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md
index 0fa1fed..b4e594c 100755
--- a/doc/docs/zh/doc/README.md
+++ b/doc/docs/zh/doc/README.md
@@ -1516,19 +1516,19 @@ catch err
1516 1516
1517### 错误处理简化 1517### 错误处理简化
1518 1518
1519`try!` 是 `try` 的简化语法,它不再返回 `try` 语句的布尔状态,并在成功时直接返回 `try` 代码块的结果,失败时返回 `nil` 值而非错误对象。 1519`try?` 是 `try` 的功能简化语法,它不再返回 `try` 语句的布尔状态,并在成功时直接返回 `try` 代码块的结果,失败时返回 `nil` 值而非错误对象。
1520 1520
1521```moonscript 1521```moonscript
1522a, b, c = try! func! 1522a, b, c = try? func!
1523 1523
1524-- 与空值合并运算符一起使用 1524-- 与空值合并运算符一起使用
1525a = (try! func!) ?? "default" 1525a = (try? func!) ?? "default"
1526 1526
1527-- 作为函数参数 1527-- 作为函数参数
1528f try! func! 1528f try? func!
1529 1529
1530-- 带 catch 块的 try! 1530-- 带 catch 块的 try!
1531f try! 1531f try?
1532 print 123 1532 print 123
1533 func! 1533 func!
1534catch e 1534catch e
@@ -1537,16 +1537,16 @@ catch e
1537``` 1537```
1538<YueDisplay> 1538<YueDisplay>
1539<pre> 1539<pre>
1540a, b, c = try! func! 1540a, b, c = try? func!
1541 1541
1542-- 与空值合并运算符一起使用 1542-- 与空值合并运算符一起使用
1543a = (try! func!) ?? "default" 1543a = (try? func!) ?? "default"
1544 1544
1545-- 作为函数参数 1545-- 作为函数参数
1546f try! func! 1546f try? func!
1547 1547
1548-- 带 catch 块的 try! 1548-- 带 catch 块的 try!
1549f try! 1549f try?
1550 print 123 1550 print 123
1551 func! 1551 func!
1552catch e 1552catch e
diff --git a/spec/inputs/try_catch.yue b/spec/inputs/try_catch.yue
index f2583b7..6c29a52 100644
--- a/spec/inputs/try_catch.yue
+++ b/spec/inputs/try_catch.yue
@@ -71,19 +71,19 @@ f = ->
71 71
72 do 72 do
73 local func 73 local func
74 a, b, c = try! func! 74 a, b, c = try? func!
75 75
76 do 76 do
77 a, b, c = try! func! 77 a, b, c = try? func!
78 78
79 do 79 do
80 a = (try! func!) ?? "default" 80 a = (try? func!) ?? "default"
81 81
82 do 82 do
83 f try! func! 83 f try? func!
84 84
85 do 85 do
86 f try! 86 f try?
87 print 123 87 print 123
88 func! 88 func!
89 catch e 89 catch e
@@ -165,19 +165,19 @@ do
165 165
166 do 166 do
167 local func 167 local func
168 a, b, c = try! func! 168 a, b, c = try? func!
169 169
170 do 170 do
171 a, b, c = try! func! 171 a, b, c = try? func!
172 172
173 do 173 do
174 a = (try! func!) ?? "default" 174 a = (try? func!) ?? "default"
175 175
176 do 176 do
177 f try! func! 177 f try? func!
178 178
179 do 179 do
180 f try! 180 f try?
181 print 123 181 print 123
182 func! 182 func!
183 catch e 183 catch e
diff --git a/src/yuescript/yue_ast.cpp b/src/yuescript/yue_ast.cpp
index be10859..c4f133b 100644
--- a/src/yuescript/yue_ast.cpp
+++ b/src/yuescript/yue_ast.cpp
@@ -609,15 +609,12 @@ std::string CatchBlock_t::to_string(void* ud) const {
609 info->popScope(); 609 info->popScope();
610 return line + '\n' + blockStr; 610 return line + '\n' + blockStr;
611} 611}
612std::string Omit_t::to_string(void*) const {
613 return "!"s;
614}
615std::string Try_t::to_string(void* ud) const { 612std::string Try_t::to_string(void* ud) const {
616 auto info = reinterpret_cast<YueFormat*>(ud); 613 auto info = reinterpret_cast<YueFormat*>(ud);
617 str_list temp; 614 str_list temp;
618 temp.emplace_back("try"s); 615 temp.emplace_back("try"s);
619 if (omit) { 616 if (eop) {
620 temp.back() += '!'; 617 temp.back() += eop->to_string(ud);
621 } 618 }
622 if (func.is<Exp_t>()) { 619 if (func.is<Exp_t>()) {
623 temp.back() += (" "s + func->to_string(ud)); 620 temp.back() += (" "s + func->to_string(ud));
diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h
index 393f374..6bdc31b 100644
--- a/src/yuescript/yue_ast.h
+++ b/src/yuescript/yue_ast.h
@@ -383,14 +383,11 @@ AST_NODE(CatchBlock)
383 AST_MEMBER(CatchBlock, &err, &block) 383 AST_MEMBER(CatchBlock, &err, &block)
384AST_END(CatchBlock) 384AST_END(CatchBlock)
385 385
386AST_LEAF(Omit)
387AST_END(Omit)
388
389AST_NODE(Try) 386AST_NODE(Try)
390 ast_ptr<false, Omit_t> omit; 387 ast_ptr<false, ExistentialOp_t> eop;
391 ast_sel<true, Block_t, Exp_t> func; 388 ast_sel<true, Block_t, Exp_t> func;
392 ast_ptr<false, CatchBlock_t> catchBlock; 389 ast_ptr<false, CatchBlock_t> catchBlock;
393 AST_MEMBER(Try, &omit, &func, &catchBlock) 390 AST_MEMBER(Try, &eop, &func, &catchBlock)
394AST_END(Try) 391AST_END(Try)
395 392
396AST_NODE(Comprehension) 393AST_NODE(Comprehension)
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp
index 35745f2..4bac51b 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.6"sv; 81const std::string_view version = "0.28.7"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 {
@@ -2332,7 +2332,7 @@ private:
2332 } 2332 }
2333 case id<Try_t>(): { 2333 case id<Try_t>(): {
2334 auto tryNode = static_cast<Try_t*>(value); 2334 auto tryNode = static_cast<Try_t*>(value);
2335 if (tryNode->omit) { 2335 if (tryNode->eop) {
2336 auto assignList = assignment->expList.get(); 2336 auto assignList = assignment->expList.get();
2337 std::string preDefine = getPreDefineLine(assignment); 2337 std::string preDefine = getPreDefineLine(assignment);
2338 transformTry(tryNode, out, ExpUsage::Assignment, assignList); 2338 transformTry(tryNode, out, ExpUsage::Assignment, assignList);
@@ -10055,7 +10055,7 @@ private:
10055 10055
10056 void transformTry(Try_t* tryNode, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { 10056 void transformTry(Try_t* tryNode, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) {
10057 auto x = tryNode; 10057 auto x = tryNode;
10058 if (tryNode->omit && usage == ExpUsage::Assignment) { 10058 if (tryNode->eop && usage == ExpUsage::Assignment) {
10059 str_list rets; 10059 str_list rets;
10060 pushScope(); 10060 pushScope();
10061 auto okVar = getUnusedName("_ok_"sv); 10061 auto okVar = getUnusedName("_ok_"sv);
@@ -10080,7 +10080,7 @@ private:
10080 transformAssignment(assignment, out); 10080 transformAssignment(assignment, out);
10081 return; 10081 return;
10082 } 10082 }
10083 if (tryNode->omit && usage != ExpUsage::Common) { 10083 if (tryNode->eop && usage != ExpUsage::Common) {
10084 auto okVar = getUnusedName("_ok_"sv); 10084 auto okVar = getUnusedName("_ok_"sv);
10085 auto code = "do\n\t"s + okVar + ", ... = try nil\n\t... if "s + okVar; 10085 auto code = "do\n\t"s + okVar + ", ... = try nil\n\t... if "s + okVar;
10086 auto doNode = toAst<Do_t>(code, x); 10086 auto doNode = toAst<Do_t>(code, x);
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp
index 2b0aea8..0cf7f05 100644
--- a/src/yuescript/yue_parser.cpp
+++ b/src/yuescript/yue_parser.cpp
@@ -501,9 +501,8 @@ YueParser::YueParser() {
501 return true; 501 return true;
502 }); 502 });
503 503
504 Omit = expr('!');
505 CatchBlock = line_break >> *space_break >> check_indent_match >> space >> key("catch") >> space >> Variable >> space >> in_block; 504 CatchBlock = line_break >> *space_break >> check_indent_match >> space >> key("catch") >> space >> Variable >> space >> in_block;
506 Try = key("try") >> -Omit >> space >> (in_block | Exp) >> -CatchBlock; 505 Try = key("try") >> -ExistentialOp >> space >> (in_block | Exp) >> -CatchBlock;
507 506
508 list_value = 507 list_value =
509 and_( 508 and_(
diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h
index 99f3d45..0d9db19 100644
--- a/src/yuescript/yue_parser.h
+++ b/src/yuescript/yue_parser.h
@@ -347,7 +347,6 @@ private:
347 AST_RULE(ForEach); 347 AST_RULE(ForEach);
348 AST_RULE(Do); 348 AST_RULE(Do);
349 AST_RULE(CatchBlock); 349 AST_RULE(CatchBlock);
350 AST_RULE(Omit);
351 AST_RULE(Try); 350 AST_RULE(Try);
352 AST_RULE(Comprehension); 351 AST_RULE(Comprehension);
353 AST_RULE(CompValue); 352 AST_RULE(CompValue);