From 2f61682aea987cdc5dd1cf44097dbbc28a7cbd2b Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 28 May 2025 22:15:35 +0800 Subject: Replace `try!` with `try?`. --- doc/docs/doc/README.md | 20 ++++++++++---------- doc/docs/zh/doc/README.md | 18 +++++++++--------- spec/inputs/try_catch.yue | 20 ++++++++++---------- src/yuescript/yue_ast.cpp | 7 ++----- src/yuescript/yue_ast.h | 7 ++----- src/yuescript/yue_compiler.cpp | 8 ++++---- src/yuescript/yue_parser.cpp | 3 +-- src/yuescript/yue_parser.h | 1 - 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 -### Try! +### Try? -`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. +`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. ```moonscript -a, b, c = try! func! +a, b, c = try? func! -- with nil coalescing operator -a = (try! func!) ?? "default" +a = (try? func!) ?? "default" -- as function argument -f try! func! +f try? func! -- with catch block -f try! +f try? print 123 func! catch e @@ -1539,16 +1539,16 @@ catch e ```
-a, b, c = try! func!
+a, b, c = try? func!
 
 -- with nil coalescing operator
-a = (try! func!) ?? "default"
+a = (try? func!) ?? "default"
 
 -- as function argument
-f try! func!
+f try? func!
 
 -- with catch block
-f try!
+f try?
   print 123
   func!
 catch 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
 
 ### 错误处理简化
 
-`try!` 是 `try` 的简化语法,它不再返回 `try` 语句的布尔状态,并在成功时直接返回 `try` 代码块的结果,失败时返回 `nil` 值而非错误对象。
+`try?` 是 `try` 的功能简化语法,它不再返回 `try` 语句的布尔状态,并在成功时直接返回 `try` 代码块的结果,失败时返回 `nil` 值而非错误对象。
 
 ```moonscript
-a, b, c = try! func!
+a, b, c = try? func!
 
 -- 与空值合并运算符一起使用
-a = (try! func!) ?? "default"
+a = (try? func!) ?? "default"
 
 -- 作为函数参数
-f try! func!
+f try? func!
 
 -- 带 catch 块的 try!
-f try!
+f try?
   print 123
   func!
 catch e
@@ -1537,16 +1537,16 @@ catch e
 ```
 
 
-a, b, c = try! func!
+a, b, c = try? func!
 
 -- 与空值合并运算符一起使用
-a = (try! func!) ?? "default"
+a = (try? func!) ?? "default"
 
 -- 作为函数参数
-f try! func!
+f try? func!
 
 -- 带 catch 块的 try!
-f try!
+f try?
   print 123
   func!
 catch 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 = ->
 
 	do
 		local func
-		a, b, c = try! func!
+		a, b, c = try? func!
 	
 	do
-		a, b, c = try! func!
+		a, b, c = try? func!
 
 	do
-		a = (try! func!) ?? "default"
+		a = (try? func!) ?? "default"
 
 	do
-		f try! func!
+		f try? func!
 	
 	do
-		f try!
+		f try?
 			print 123
 			func!
 		catch e
@@ -165,19 +165,19 @@ do
 
 	do
 		local func
-		a, b, c = try! func!
+		a, b, c = try? func!
 	
 	do
-		a, b, c = try! func!
+		a, b, c = try? func!
 
 	do
-		a = (try! func!) ?? "default"
+		a = (try? func!) ?? "default"
 
 	do
-		f try! func!
+		f try? func!
 	
 	do
-		f try!
+		f try?
 			print 123
 			func!
 		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 {
 	info->popScope();
 	return line + '\n' + blockStr;
 }
-std::string Omit_t::to_string(void*) const {
-	return "!"s;
-}
 std::string Try_t::to_string(void* ud) const {
 	auto info = reinterpret_cast(ud);
 	str_list temp;
 	temp.emplace_back("try"s);
-	if (omit) {
-		temp.back() += '!';
+	if (eop) {
+		temp.back() += eop->to_string(ud);
 	}
 	if (func.is()) {
 		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)
 	AST_MEMBER(CatchBlock, &err, &block)
 AST_END(CatchBlock)
 
-AST_LEAF(Omit)
-AST_END(Omit)
-
 AST_NODE(Try)
-	ast_ptr omit;
+	ast_ptr eop;
 	ast_sel func;
 	ast_ptr catchBlock;
-	AST_MEMBER(Try, &omit, &func, &catchBlock)
+	AST_MEMBER(Try, &eop, &func, &catchBlock)
 AST_END(Try)
 
 AST_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 Metamethods = {
 	"close"s // Lua 5.4
 };
 
-const std::string_view version = "0.28.6"sv;
+const std::string_view version = "0.28.7"sv;
 const std::string_view extension = "yue"sv;
 
 class CompileError : public std::logic_error {
@@ -2332,7 +2332,7 @@ private:
 			}
 			case id(): {
 				auto tryNode = static_cast(value);
-				if (tryNode->omit) {
+				if (tryNode->eop) {
 					auto assignList = assignment->expList.get();
 					std::string preDefine = getPreDefineLine(assignment);
 					transformTry(tryNode, out, ExpUsage::Assignment, assignList);
@@ -10055,7 +10055,7 @@ private:
 
 	void transformTry(Try_t* tryNode, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) {
 		auto x = tryNode;
-		if (tryNode->omit && usage == ExpUsage::Assignment) {
+		if (tryNode->eop && usage == ExpUsage::Assignment) {
 			str_list rets;
 			pushScope();
 			auto okVar = getUnusedName("_ok_"sv);
@@ -10080,7 +10080,7 @@ private:
 			transformAssignment(assignment, out);
 			return;
 		}
-		if (tryNode->omit && usage != ExpUsage::Common) {
+		if (tryNode->eop && usage != ExpUsage::Common) {
 			auto okVar = getUnusedName("_ok_"sv);
 			auto code = "do\n\t"s + okVar + ", ... = try nil\n\t... if "s + okVar;
 			auto doNode = toAst(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() {
 		return true;
 	});
 
-	Omit = expr('!');
 	CatchBlock = line_break >> *space_break >> check_indent_match >> space >> key("catch") >> space >> Variable >> space >> in_block;
-	Try = key("try") >> -Omit >> space >> (in_block | Exp) >> -CatchBlock;
+	Try = key("try") >> -ExistentialOp >> space >> (in_block | Exp) >> -CatchBlock;
 
 	list_value =
 		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:
 	AST_RULE(ForEach);
 	AST_RULE(Do);
 	AST_RULE(CatchBlock);
-	AST_RULE(Omit);
 	AST_RULE(Try);
 	AST_RULE(Comprehension);
 	AST_RULE(CompValue);
-- 
cgit v1.2.3-55-g6feb