From a89680fe48dd1520843d7629e2006f732e313200 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 28 Jul 2023 15:54:12 +0800 Subject: made a little optimization for try-catch syntax. --- doc/docs/doc/README.md | 9 ++++++++- spec/inputs/try_catch.yue | 6 ++++++ spec/outputs/5.1/try_catch.lua | 4 ++++ spec/outputs/try_catch.lua | 16 ++++++++-------- src/yuescript/yue_compiler.cpp | 16 +++++++++++++++- 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index 92fe435..c237791 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md @@ -971,6 +971,7 @@ thing = {1, 2} print a, b ``` +
 thing = {1, 2}
 
@@ -1295,7 +1296,7 @@ catch err
 
 ## Attributes
 
-Syntax support for Lua 5.4 attributes. But you can use still use `const` declaration and get constant check working when targeting Lua versions below 5.4.
+Syntax support for Lua 5.4 attributes. But you can still use `const` declaration and get constant check working when targeting Lua versions below 5.4.
 
 ```moonscript
 const a = 123
@@ -2374,6 +2375,9 @@ if a in (0, 11)
 
 if a in {1, 3, 5, 7}
   print "checking equality with discrete values"
+
+if a in list
+  print "checking if `a` is in a list"
 ```
 
 
@@ -2390,6 +2394,9 @@ if a in (0, 11)
 
 if a in {1, 3, 5, 7}
   print "checking equality with discrete values"
+
+if a in list
+  print "checking if `a` is in a list"
 
diff --git a/spec/inputs/try_catch.yue b/spec/inputs/try_catch.yue index e38cbef..96a87fc 100644 --- a/spec/inputs/try_catch.yue +++ b/spec/inputs/try_catch.yue @@ -50,5 +50,11 @@ do catch err print err +do + try + func 1, 2, 3 + + try func 1, 2, 3 + nil diff --git a/spec/outputs/5.1/try_catch.lua b/spec/outputs/5.1/try_catch.lua index 577df16..9972dca 100644 --- a/spec/outputs/5.1/try_catch.lua +++ b/spec/outputs/5.1/try_catch.lua @@ -68,4 +68,8 @@ do print(result) end end +do +pcall(func, 1, 2, 3) +pcall(func, 1, 2, 3) +end return nil diff --git a/spec/outputs/try_catch.lua b/spec/outputs/try_catch.lua index 129d412..de52c6c 100644 --- a/spec/outputs/try_catch.lua +++ b/spec/outputs/try_catch.lua @@ -1,8 +1,6 @@ -xpcall(function() - return func(1, 2, 3) -end, function(err) +xpcall(func, function(err) return print(err) -end) +end, 1, 2, 3) xpcall(func, function(err) return print(err) end, 1, 2, 3) @@ -11,11 +9,9 @@ pcall(function() return func(1, 2, 3) end) do - local success, result = xpcall(function() - return func(1, 2, 3) - end, function(err) + local success, result = xpcall(func, function(err) return print(err) - end) + end, 1, 2, 3) success, result = pcall(func, 1, 2, 3) end pcall(tb.func) @@ -58,4 +54,8 @@ do print(result) end end +do +pcall(func, 1, 2, 3) +pcall(func, 1, 2, 3) +end return nil diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 4d8ed86..ed5e849 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -72,7 +72,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.17.12"sv; +const std::string_view version = "0.17.13"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -7951,6 +7951,20 @@ private: body->content.set(tryNode->catchBlock->body); funLit->body.set(body); } + if (auto tryBlock = tryNode->func.as()) { + BLOCK_START + BREAK_IF(tryBlock->statements.size() != 1); + auto stmt = static_cast(tryBlock->statements.front()); + auto expListAssign = stmt->content.as(); + BREAK_IF(expListAssign->action); + auto value = singleValueFrom(expListAssign->expList); + BREAK_IF(!value); + auto chainValue = value->item.as(); + BREAK_IF(!chainValue); + BREAK_IF(!isChainValueCall(chainValue)); + tryNode->func.set(expListAssign->expList->exprs.front()); + BLOCK_END + } if (auto tryBlock = tryNode->func.as()) { auto tryExp = toAst("->"sv, x); auto funLit = simpleSingleValueFrom(tryExp)->value.to(); -- cgit v1.2.3-55-g6feb