diff options
-rw-r--r-- | spec/inputs/syntax.yue | 4 | ||||
-rw-r--r-- | spec/inputs/unicode/syntax.yue | 4 | ||||
-rw-r--r-- | spec/outputs/syntax.lua | 14 | ||||
-rw-r--r-- | spec/outputs/unicode/syntax.lua | 14 | ||||
-rw-r--r-- | spec/outputs/unicode/with.lua | 4 | ||||
-rw-r--r-- | spec/outputs/with.lua | 4 | ||||
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 39 | ||||
-rw-r--r-- | src/yuescript/yue_parser.cpp | 4 |
8 files changed, 66 insertions, 21 deletions
diff --git a/spec/inputs/syntax.yue b/spec/inputs/syntax.yue index 271f43f..a414d6f 100644 --- a/spec/inputs/syntax.yue +++ b/spec/inputs/syntax.yue | |||
@@ -52,9 +52,9 @@ _ = -> | |||
52 | 52 | ||
53 | _ = -> 1,2,34 | 53 | _ = -> 1,2,34 |
54 | 54 | ||
55 | return 5 + () -> 4 + 2 | 55 | do return 5 + () -> 4 + 2 |
56 | 56 | ||
57 | return 5 + (() -> 4) + 2 | 57 | do return 5 + (() -> 4) + 2 |
58 | 58 | ||
59 | print 5 + () -> | 59 | print 5 + () -> |
60 | _ = 34 | 60 | _ = 34 |
diff --git a/spec/inputs/unicode/syntax.yue b/spec/inputs/unicode/syntax.yue index f382688..8a98416 100644 --- a/spec/inputs/unicode/syntax.yue +++ b/spec/inputs/unicode/syntax.yue | |||
@@ -51,9 +51,9 @@ _ = -> | |||
51 | 51 | ||
52 | _ = -> 1,2,34 | 52 | _ = -> 1,2,34 |
53 | 53 | ||
54 | return 5 + () -> 4 + 2 | 54 | do return 5 + () -> 4 + 2 |
55 | 55 | ||
56 | return 5 + (() -> 4) + 2 | 56 | do return 5 + (() -> 4) + 2 |
57 | 57 | ||
58 | 打印 5 + () -> | 58 | 打印 5 + () -> |
59 | _ = 34 | 59 | _ = 34 |
diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index cfe63eb..5fd1821 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua | |||
@@ -40,12 +40,16 @@ end | |||
40 | _ = function() | 40 | _ = function() |
41 | return 1, 2, 34 | 41 | return 1, 2, 34 |
42 | end | 42 | end |
43 | return 5 + function() | 43 | do |
44 | return 4 + 2 | 44 | return 5 + function() |
45 | return 4 + 2 | ||
46 | end | ||
47 | end | ||
48 | do | ||
49 | return 5 + (function() | ||
50 | return 4 | ||
51 | end) + 2 | ||
45 | end | 52 | end |
46 | return 5 + (function() | ||
47 | return 4 | ||
48 | end) + 2 | ||
49 | print(5 + function() | 53 | print(5 + function() |
50 | _ = 34 | 54 | _ = 34 |
51 | return good(nads) | 55 | return good(nads) |
diff --git a/spec/outputs/unicode/syntax.lua b/spec/outputs/unicode/syntax.lua index 9ea8f68..ea97bb9 100644 --- a/spec/outputs/unicode/syntax.lua +++ b/spec/outputs/unicode/syntax.lua | |||
@@ -40,12 +40,16 @@ end | |||
40 | _ = function() | 40 | _ = function() |
41 | return 1, 2, 34 | 41 | return 1, 2, 34 |
42 | end | 42 | end |
43 | return 5 + function() | 43 | do |
44 | return 4 + 2 | 44 | return 5 + function() |
45 | return 4 + 2 | ||
46 | end | ||
47 | end | ||
48 | do | ||
49 | return 5 + (function() | ||
50 | return 4 | ||
51 | end) + 2 | ||
45 | end | 52 | end |
46 | return 5 + (function() | ||
47 | return 4 | ||
48 | end) + 2 | ||
49 | _u6253_u5370(5 + function() | 53 | _u6253_u5370(5 + function() |
50 | _ = 34 | 54 | _ = 34 |
51 | return _u597d(_u7403) | 55 | return _u597d(_u7403) |
diff --git a/spec/outputs/unicode/with.lua b/spec/outputs/unicode/with.lua index 7a5ba00..cfad264 100644 --- a/spec/outputs/unicode/with.lua +++ b/spec/outputs/unicode/with.lua | |||
@@ -123,7 +123,9 @@ do | |||
123 | local _ | 123 | local _ |
124 | _ = function() | 124 | _ = function() |
125 | local _with_0 = _u55e8 | 125 | local _with_0 = _u55e8 |
126 | return _with_0.a, _with_0.b | 126 | do |
127 | return _with_0.a, _with_0.b | ||
128 | end | ||
127 | end | 129 | end |
128 | end | 130 | end |
129 | do | 131 | do |
diff --git a/spec/outputs/with.lua b/spec/outputs/with.lua index b2a1c3b..d880d1e 100644 --- a/spec/outputs/with.lua +++ b/spec/outputs/with.lua | |||
@@ -121,7 +121,9 @@ do | |||
121 | local _ | 121 | local _ |
122 | _ = function() | 122 | _ = function() |
123 | local _with_0 = hi | 123 | local _with_0 = hi |
124 | return _with_0.a, _with_0.b | 124 | do |
125 | return _with_0.a, _with_0.b | ||
126 | end | ||
125 | end | 127 | end |
126 | end | 128 | end |
127 | do | 129 | do |
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))); |