Skip to content

Commit

Permalink
Fixed a few issues.
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
pigpigyyy committed Sep 26, 2024
1 parent 461bf7c commit 7575fe0
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 21 deletions.
4 changes: 2 additions & 2 deletions spec/inputs/syntax.yue
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ _ = ->

_ = -> 1,2,34

return 5 + () -> 4 + 2
do return 5 + () -> 4 + 2

return 5 + (() -> 4) + 2
do return 5 + (() -> 4) + 2

print 5 + () ->
_ = 34
Expand Down
4 changes: 2 additions & 2 deletions spec/inputs/unicode/syntax.yue
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ _ = ->

_ = -> 1,2,34

return 5 + () -> 4 + 2
do return 5 + () -> 4 + 2

return 5 + (() -> 4) + 2
do return 5 + (() -> 4) + 2

打印 5 + () ->
_ = 34
Expand Down
14 changes: 9 additions & 5 deletions spec/outputs/syntax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ end
_ = function()
return 1, 2, 34
end
return 5 + function()
return 4 + 2
do
return 5 + function()
return 4 + 2
end
end
do
return 5 + (function()
return 4
end) + 2
end
return 5 + (function()
return 4
end) + 2
print(5 + function()
_ = 34
return good(nads)
Expand Down
14 changes: 9 additions & 5 deletions spec/outputs/unicode/syntax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ end
_ = function()
return 1, 2, 34
end
return 5 + function()
return 4 + 2
do
return 5 + function()
return 4 + 2
end
end
do
return 5 + (function()
return 4
end) + 2
end
return 5 + (function()
return 4
end) + 2
_u6253_u5370(5 + function()
_ = 34
return _u597d(_u7403)
Expand Down
4 changes: 3 additions & 1 deletion spec/outputs/unicode/with.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ do
local _
_ = function()
local _with_0 = _u55e8
return _with_0.a, _with_0.b
do
return _with_0.a, _with_0.b
end
end
end
do
Expand Down
4 changes: 3 additions & 1 deletion spec/outputs/with.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ do
local _
_ = function()
local _with_0 = hi
return _with_0.a, _with_0.b
do
return _with_0.a, _with_0.b
end
end
end
do
Expand Down
39 changes: 36 additions & 3 deletions src/yuescript/yue_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static std::unordered_set<std::string> Metamethods = {
"close"s // Lua 5.4
};

const std::string_view version = "0.25.1"sv;
const std::string_view version = "0.25.2"sv;
const std::string_view extension = "yue"sv;

class CompileError : public std::logic_error {
Expand Down Expand Up @@ -4490,7 +4490,9 @@ class YueCompilerImpl {
for (auto it = nodes.begin(); it != nodes.end(); ++it) {
auto node = *it;
auto stmt = static_cast<Statement_t*>(node);
if (auto pipeBody = stmt->content.as<PipeBody_t>()) {
if (!stmt->appendix && stmt->content.is<Return_t>() && stmt != nodes.back()) {
throw CompileError("'return' statement must be the last line in the block"sv, stmt->content);
} else if (auto pipeBody = stmt->content.as<PipeBody_t>()) {
auto x = stmt;
bool cond = false;
BLOCK_START
Expand Down Expand Up @@ -9151,7 +9153,38 @@ class YueCompilerImpl {
ifNode->nodes.push_back(with->body);
transformIf(ifNode, temp, ExpUsage::Common);
} else {
transform_plain_body(with->body, temp, ExpUsage::Common);
bool transformed = false;
if (auto block = with->body.as<Block_t>()) {
if (!block->statements.empty()) {
Statement_t* stmt = static_cast<Statement_t*>(block->statements.back());
if (stmt->content.is<Return_t>()) {
auto newBlock = with->body->new_ptr<Block_t>();
newBlock->statements.dup(block->statements);
newBlock->statements.pop_back();
transform_plain_body(newBlock, temp, ExpUsage::Common);
auto newBody = stmt->new_ptr<Body_t>();
newBody->content.set(stmt);
auto doNode = stmt->new_ptr<Do_t>();
doNode->body.set(newBody);
transformDo(doNode, temp, ExpUsage::Common);
transformed = true;
}
}
} else {
auto stmt = with->body.to<Statement_t>();
if (stmt->content.is<Return_t>()) {
auto newBody = stmt->new_ptr<Body_t>();
newBody->content.set(stmt);
auto doNode = stmt->new_ptr<Do_t>();
doNode->body.set(newBody);
transformDo(doNode, temp, ExpUsage::Common);
temp.back().insert(0, indent());
transformed = true;
}
}
if (!transformed) {
transform_plain_body(with->body, temp, ExpUsage::Common);
}
}
_withVars.pop();
if (assignList) {
Expand Down
4 changes: 2 additions & 2 deletions src/yuescript/yue_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ YueParser::YueParser() {
Switch = key("switch") >> space >> Exp >>
space >> Seperator >> (
SwitchCase >> space >> (
line_break >> *space_break >> check_indent_match >> space >> SwitchCase >> switch_block |
switch_block |
*(space >> SwitchCase) >> -(space >> switch_else)
) |
+space_break >> advance_match >> space >> SwitchCase >> switch_block >> pop_indent
) >> switch_block;
);

Assignment = -(',' >> space >> ExpList >> space) >> (':' >> Assign | and_('=') >> if_assignment_syntax_error);
IfCond = disable_chain_rule(disable_arg_table_block_rule(Exp >> -(space >> Assignment)));
Expand Down

0 comments on commit 7575fe0

Please sign in to comment.