diff options
author | Li Jin <dragon-fly@qq.com> | 2024-03-04 11:13:18 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2024-03-04 11:13:18 +0800 |
commit | 4e9a508a11c16db9aeff44b27e88713ab413bff7 (patch) | |
tree | 2ef6f1e4904e32379e67b1c35b7bc8031685088a /src/yuescript/yue_compiler.cpp | |
parent | 412bc3d7606cb0d07c39861c7ae4e89c7139da31 (diff) | |
download | yuescript-4e9a508a11c16db9aeff44b27e88713ab413bff7.tar.gz yuescript-4e9a508a11c16db9aeff44b27e88713ab413bff7.tar.bz2 yuescript-4e9a508a11c16db9aeff44b27e88713ab413bff7.zip |
add default return declaration for function literal.v0.22.2
Diffstat (limited to '')
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 0d38fa5..f23d5f0 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -75,7 +75,7 @@ static std::unordered_set<std::string> Metamethods = { | |||
75 | "close"s // Lua 5.4 | 75 | "close"s // Lua 5.4 |
76 | }; | 76 | }; |
77 | 77 | ||
78 | const std::string_view version = "0.22.1"sv; | 78 | const std::string_view version = "0.22.2"sv; |
79 | const std::string_view extension = "yue"sv; | 79 | const std::string_view extension = "yue"sv; |
80 | 80 | ||
81 | class CompileError : public std::logic_error { | 81 | class CompileError : public std::logic_error { |
@@ -3787,11 +3787,44 @@ private: | |||
3787 | str_list temp; | 3787 | str_list temp; |
3788 | if (auto argsDef = funLit->argsDef.get()) { | 3788 | if (auto argsDef = funLit->argsDef.get()) { |
3789 | transformFnArgsDef(argsDef, temp); | 3789 | transformFnArgsDef(argsDef, temp); |
3790 | } | ||
3791 | if (funLit->defaultReturn) { | ||
3792 | auto newBlock = funLit->new_ptr<Block_t>(); | ||
3793 | if (funLit->body) { | ||
3794 | auto last = lastStatementFrom(funLit->body); | ||
3795 | if (!last->appendix && last->content.is<Return_t>() && !funLit->defaultReturn.is<DefaultValue_t>()) { | ||
3796 | throw CompileError("duplicated return statement", last->content); | ||
3797 | } | ||
3798 | auto content = funLit->body->content.get(); | ||
3799 | switch (content->get_id()) { | ||
3800 | case id<Block_t>(): { | ||
3801 | auto block = static_cast<Block_t*>(content); | ||
3802 | newBlock->statements.dup(block->statements); | ||
3803 | break; | ||
3804 | } | ||
3805 | case id<Statement_t>(): { | ||
3806 | newBlock->statements.push_back(content); | ||
3807 | break; | ||
3808 | } | ||
3809 | default: YUEE("AST node mismatch", content); break; | ||
3810 | } | ||
3811 | } | ||
3812 | if (funLit->defaultReturn.is<ExpListLow_t>()) { | ||
3813 | auto returnNode = newBlock->new_ptr<Return_t>(); | ||
3814 | returnNode->valueList.set(funLit->defaultReturn); | ||
3815 | auto stmt = newBlock->new_ptr<Statement_t>(); | ||
3816 | stmt->content.set(returnNode); | ||
3817 | newBlock->statements.push_back(stmt); | ||
3818 | } | ||
3819 | transformBlock(newBlock, temp, ExpUsage::Common); | ||
3820 | } else { | ||
3790 | if (funLit->body) { | 3821 | if (funLit->body) { |
3791 | transformBody(funLit->body, temp, ExpUsage::Return); | 3822 | transformBody(funLit->body, temp, ExpUsage::Return); |
3792 | } else { | 3823 | } else { |
3793 | temp.push_back(Empty); | 3824 | temp.push_back(Empty); |
3794 | } | 3825 | } |
3826 | } | ||
3827 | if (auto argsDef = funLit->argsDef.get()) { | ||
3795 | auto it = temp.begin(); | 3828 | auto it = temp.begin(); |
3796 | auto& args = *it; | 3829 | auto& args = *it; |
3797 | auto& initArgs = *(++it); | 3830 | auto& initArgs = *(++it); |
@@ -3811,11 +3844,6 @@ private: | |||
3811 | _buf << " end"sv; | 3844 | _buf << " end"sv; |
3812 | } | 3845 | } |
3813 | } else { | 3846 | } else { |
3814 | if (funLit->body) { | ||
3815 | transformBody(funLit->body, temp, ExpUsage::Return); | ||
3816 | } else { | ||
3817 | temp.push_back(Empty); | ||
3818 | } | ||
3819 | auto& bodyCodes = temp.back(); | 3847 | auto& bodyCodes = temp.back(); |
3820 | _buf << "function("sv << (isFatArrow ? "self"s : Empty) << ')'; | 3848 | _buf << "function("sv << (isFatArrow ? "self"s : Empty) << ')'; |
3821 | if (!bodyCodes.empty()) { | 3849 | if (!bodyCodes.empty()) { |