diff options
author | Li Jin <dragon-fly@qq.com> | 2024-09-26 22:11:13 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2024-09-26 22:11:13 +0800 |
commit | 7575fe00aad91e0ba943e877ddcd838f76e095c0 (patch) | |
tree | cc1da50dddf61e77f33c026e38afe4ac2ad7d904 /src | |
parent | 461bf7c32408553125d71b23e04e21fed690c4f5 (diff) | |
download | yuescript-7575fe00aad91e0ba943e877ddcd838f76e095c0.tar.gz yuescript-7575fe00aad91e0ba943e877ddcd838f76e095c0.tar.bz2 yuescript-7575fe00aad91e0ba943e877ddcd838f76e095c0.zip |
Fixed a few issues.v0.25.2
* Fixed a `with` block with return statement issue.
* Fixed a switch-when indentation issue.
* Added error reporting for `return` statement not appearing in last block line.
Diffstat (limited to 'src')
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 39 | ||||
-rw-r--r-- | src/yuescript/yue_parser.cpp | 4 |
2 files changed, 38 insertions, 5 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 5d031db..c346891 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.25.1"sv; | 78 | const std::string_view version = "0.25.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 { |
@@ -4490,7 +4490,9 @@ private: | |||
4490 | for (auto it = nodes.begin(); it != nodes.end(); ++it) { | 4490 | for (auto it = nodes.begin(); it != nodes.end(); ++it) { |
4491 | auto node = *it; | 4491 | auto node = *it; |
4492 | auto stmt = static_cast<Statement_t*>(node); | 4492 | auto stmt = static_cast<Statement_t*>(node); |
4493 | if (auto pipeBody = stmt->content.as<PipeBody_t>()) { | 4493 | if (!stmt->appendix && stmt->content.is<Return_t>() && stmt != nodes.back()) { |
4494 | throw CompileError("'return' statement must be the last line in the block"sv, stmt->content); | ||
4495 | } else if (auto pipeBody = stmt->content.as<PipeBody_t>()) { | ||
4494 | auto x = stmt; | 4496 | auto x = stmt; |
4495 | bool cond = false; | 4497 | bool cond = false; |
4496 | BLOCK_START | 4498 | BLOCK_START |
@@ -9151,7 +9153,38 @@ private: | |||
9151 | ifNode->nodes.push_back(with->body); | 9153 | ifNode->nodes.push_back(with->body); |
9152 | transformIf(ifNode, temp, ExpUsage::Common); | 9154 | transformIf(ifNode, temp, ExpUsage::Common); |
9153 | } else { | 9155 | } else { |
9154 | transform_plain_body(with->body, temp, ExpUsage::Common); | 9156 | bool transformed = false; |
9157 | if (auto block = with->body.as<Block_t>()) { | ||
9158 | if (!block->statements.empty()) { | ||
9159 | Statement_t* stmt = static_cast<Statement_t*>(block->statements.back()); | ||
9160 | if (stmt->content.is<Return_t>()) { | ||
9161 | auto newBlock = with->body->new_ptr<Block_t>(); | ||
9162 | newBlock->statements.dup(block->statements); | ||
9163 | newBlock->statements.pop_back(); | ||
9164 | transform_plain_body(newBlock, temp, ExpUsage::Common); | ||
9165 | auto newBody = stmt->new_ptr<Body_t>(); | ||
9166 | newBody->content.set(stmt); | ||
9167 | auto doNode = stmt->new_ptr<Do_t>(); | ||
9168 | doNode->body.set(newBody); | ||
9169 | transformDo(doNode, temp, ExpUsage::Common); | ||
9170 | transformed = true; | ||
9171 | } | ||
9172 | } | ||
9173 | } else { | ||
9174 | auto stmt = with->body.to<Statement_t>(); | ||
9175 | if (stmt->content.is<Return_t>()) { | ||
9176 | auto newBody = stmt->new_ptr<Body_t>(); | ||
9177 | newBody->content.set(stmt); | ||
9178 | auto doNode = stmt->new_ptr<Do_t>(); | ||
9179 | doNode->body.set(newBody); | ||
9180 | transformDo(doNode, temp, ExpUsage::Common); | ||
9181 | temp.back().insert(0, indent()); | ||
9182 | transformed = true; | ||
9183 | } | ||
9184 | } | ||
9185 | if (!transformed) { | ||
9186 | transform_plain_body(with->body, temp, ExpUsage::Common); | ||
9187 | } | ||
9155 | } | 9188 | } |
9156 | _withVars.pop(); | 9189 | _withVars.pop(); |
9157 | if (assignList) { | 9190 | if (assignList) { |
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index e5337b0..93787cd 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp | |||
@@ -369,11 +369,11 @@ YueParser::YueParser() { | |||
369 | Switch = key("switch") >> space >> Exp >> | 369 | Switch = key("switch") >> space >> Exp >> |
370 | space >> Seperator >> ( | 370 | space >> Seperator >> ( |
371 | SwitchCase >> space >> ( | 371 | SwitchCase >> space >> ( |
372 | line_break >> *space_break >> check_indent_match >> space >> SwitchCase >> switch_block | | 372 | switch_block | |
373 | *(space >> SwitchCase) >> -(space >> switch_else) | 373 | *(space >> SwitchCase) >> -(space >> switch_else) |
374 | ) | | 374 | ) | |
375 | +space_break >> advance_match >> space >> SwitchCase >> switch_block >> pop_indent | 375 | +space_break >> advance_match >> space >> SwitchCase >> switch_block >> pop_indent |
376 | ) >> switch_block; | 376 | ); |
377 | 377 | ||
378 | Assignment = -(',' >> space >> ExpList >> space) >> (':' >> Assign | and_('=') >> if_assignment_syntax_error); | 378 | Assignment = -(',' >> space >> ExpList >> space) >> (':' >> Assign | and_('=') >> if_assignment_syntax_error); |
379 | IfCond = disable_chain_rule(disable_arg_table_block_rule(Exp >> -(space >> Assignment))); | 379 | IfCond = disable_chain_rule(disable_arg_table_block_rule(Exp >> -(space >> Assignment))); |