diff options
-rw-r--r-- | spec/inputs/cond.yue | 2 | ||||
-rw-r--r-- | spec/outputs/cond.lua | 11 | ||||
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 49 |
3 files changed, 38 insertions, 24 deletions
diff --git a/spec/inputs/cond.yue b/spec/inputs/cond.yue index f5f42a8..5bc6c9b 100644 --- a/spec/inputs/cond.yue +++ b/spec/inputs/cond.yue | |||
@@ -247,6 +247,7 @@ do | |||
247 | a = x and y or v(1) < v(2) < v(3) and b < v(4) < v(5) < v(6) | 247 | a = x and y or v(1) < v(2) < v(3) and b < v(4) < v(5) < v(6) |
248 | a = v(1) < v(2) < v(3) and b < v(4) < v(5) < v(6) or x and y | 248 | a = v(1) < v(2) < v(3) and b < v(4) < v(5) < v(6) or x and y |
249 | a = x and y or v(1) < v(2) < v(3) and b < v(4) < v(5) < v(6) or w and z | 249 | a = x and y or v(1) < v(2) < v(3) and b < v(4) < v(5) < v(6) or w and z |
250 | a = 1 < 2 == v1 > 3 | ||
250 | 251 | ||
251 | local v1, v2, v3, v4, v5, v6 | 252 | local v1, v2, v3, v4, v5, v6 |
252 | a = v1 < v2 < v3 < v4 | 253 | a = v1 < v2 < v3 < v4 |
@@ -258,6 +259,7 @@ do | |||
258 | a = x and y or v1 < v2 < v3 and b < v4 < v5 < v6 | 259 | a = x and y or v1 < v2 < v3 and b < v4 < v5 < v6 |
259 | a = v1 < v2 < v3 and b < v4 < v5 < v6 or x and y | 260 | a = v1 < v2 < v3 and b < v4 < v5 < v6 or x and y |
260 | a = x and y or v1 < v2 < v3 and b < v4 < v5 < v6 or w and z | 261 | a = x and y or v1 < v2 < v3 and b < v4 < v5 < v6 or w and z |
262 | a = 1 < 2 == v1 > 3 | ||
261 | 263 | ||
262 | nil | 264 | nil |
263 | 265 | ||
diff --git a/spec/outputs/cond.lua b/spec/outputs/cond.lua index d7aae02..acbf644 100644 --- a/spec/outputs/cond.lua +++ b/spec/outputs/cond.lua | |||
@@ -503,6 +503,16 @@ do | |||
503 | end | 503 | end |
504 | end | 504 | end |
505 | end)() or w and z | 505 | end)() or w and z |
506 | if not (1 < 2) then | ||
507 | a = false | ||
508 | else | ||
509 | local _cond_0 = v1 | ||
510 | if not (2 == _cond_0) then | ||
511 | a = false | ||
512 | else | ||
513 | a = _cond_0 > 3 | ||
514 | end | ||
515 | end | ||
506 | local v1, v2, v3, v4, v5, v6 | 516 | local v1, v2, v3, v4, v5, v6 |
507 | a = v1 < v2 and v2 < v3 and v3 < v4 | 517 | a = v1 < v2 and v2 < v3 and v3 < v4 |
508 | a = x and y or v1 < v2 and v2 < v3 and v3 < v4 | 518 | a = x and y or v1 < v2 and v2 < v3 and v3 < v4 |
@@ -512,5 +522,6 @@ do | |||
512 | a = x and y or v1 < v2 and v2 < v3 and b < v4 and v4 < v5 and v5 < v6 | 522 | a = x and y or v1 < v2 and v2 < v3 and b < v4 and v4 < v5 and v5 < v6 |
513 | a = v1 < v2 and v2 < v3 and b < v4 and v4 < v5 and v5 < v6 or x and y | 523 | a = v1 < v2 and v2 < v3 and b < v4 and v4 < v5 and v5 < v6 or x and y |
514 | a = x and y or v1 < v2 and v2 < v3 and b < v4 and v4 < v5 and v5 < v6 or w and z | 524 | a = x and y or v1 < v2 and v2 < v3 and b < v4 and v4 < v5 and v5 < v6 or w and z |
525 | a = 1 < 2 and 2 == v1 and v1 > 3 | ||
515 | end | 526 | end |
516 | return nil | 527 | return nil |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 1733967..7fdf45e 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.20.4"sv; | 78 | const std::string_view version = "0.20.5"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 { |
@@ -1042,7 +1042,7 @@ private: | |||
1042 | for (auto it = uname->m_begin.m_it; it != uname->m_end.m_it; ++it) { | 1042 | for (auto it = uname->m_begin.m_it; it != uname->m_end.m_it; ++it) { |
1043 | auto ch = *it; | 1043 | auto ch = *it; |
1044 | if (ch > 255) { | 1044 | if (ch > 255) { |
1045 | buf << "_u"sv << std::hex << ch; | 1045 | buf << "_u"sv << std::hex << static_cast<int>(ch); |
1046 | } else { | 1046 | } else { |
1047 | buf << static_cast<char>(ch); | 1047 | buf << static_cast<char>(ch); |
1048 | } | 1048 | } |
@@ -3438,25 +3438,10 @@ private: | |||
3438 | ifCond->condition.set(condExp); | 3438 | ifCond->condition.set(condExp); |
3439 | ifNode->nodes.push_back(ifCond); | 3439 | ifNode->nodes.push_back(ifCond); |
3440 | ifNode->nodes.push_back(toAst<Statement_t>("false"sv, exp)); | 3440 | ifNode->nodes.push_back(toAst<Statement_t>("false"sv, exp)); |
3441 | YueFormat format{}; | ||
3442 | auto code = ifNode->to_string(&format); | ||
3441 | if (newCondExp) { | 3443 | if (newCondExp) { |
3442 | if (nodes) { | 3444 | if (!nodes) { |
3443 | auto block = exp->new_ptr<Block_t>(); | ||
3444 | auto stmt = exp->new_ptr<Statement_t>(); | ||
3445 | stmt->content.set(preDefine); | ||
3446 | preDefine.set(nullptr); | ||
3447 | block->statements.push_back(stmt); | ||
3448 | auto simpleValue = exp->new_ptr<SimpleValue_t>(); | ||
3449 | simpleValue->value.set(ifNode); | ||
3450 | auto explist = exp->new_ptr<ExpList_t>(); | ||
3451 | explist->exprs.push_back(newExp(simpleValue, exp)); | ||
3452 | auto expListAssign = exp->new_ptr<ExpListAssign_t>(); | ||
3453 | expListAssign->expList.set(explist); | ||
3454 | stmt = exp->new_ptr<Statement_t>(); | ||
3455 | stmt->content.set(expListAssign); | ||
3456 | block->statements.push_back(stmt); | ||
3457 | nodes->push_back(block); | ||
3458 | nodes = &ifNode->nodes; | ||
3459 | } else { | ||
3460 | auto ifNodePrev = exp->new_ptr<If_t>(); | 3445 | auto ifNodePrev = exp->new_ptr<If_t>(); |
3461 | ifNodePrev->type.set(toAst<IfType_t>("unless"sv, exp)); | 3446 | ifNodePrev->type.set(toAst<IfType_t>("unless"sv, exp)); |
3462 | auto ifCondPrev = exp->new_ptr<IfCond_t>(); | 3447 | auto ifCondPrev = exp->new_ptr<IfCond_t>(); |
@@ -3468,6 +3453,22 @@ private: | |||
3468 | newCondExp.set(newExp(simpleValue, exp)); | 3453 | newCondExp.set(newExp(simpleValue, exp)); |
3469 | nodes = &ifNodePrev->nodes; | 3454 | nodes = &ifNodePrev->nodes; |
3470 | } | 3455 | } |
3456 | auto block = exp->new_ptr<Block_t>(); | ||
3457 | auto stmt = exp->new_ptr<Statement_t>(); | ||
3458 | stmt->content.set(preDefine); | ||
3459 | preDefine.set(nullptr); | ||
3460 | block->statements.push_back(stmt); | ||
3461 | auto simpleValue = exp->new_ptr<SimpleValue_t>(); | ||
3462 | simpleValue->value.set(ifNode); | ||
3463 | auto explist = exp->new_ptr<ExpList_t>(); | ||
3464 | explist->exprs.push_back(newExp(simpleValue, exp)); | ||
3465 | auto expListAssign = exp->new_ptr<ExpListAssign_t>(); | ||
3466 | expListAssign->expList.set(explist); | ||
3467 | stmt = exp->new_ptr<Statement_t>(); | ||
3468 | stmt->content.set(expListAssign); | ||
3469 | block->statements.push_back(stmt); | ||
3470 | nodes->push_back(block); | ||
3471 | nodes = &ifNode->nodes; | ||
3471 | } else { | 3472 | } else { |
3472 | auto block = exp->new_ptr<Block_t>(); | 3473 | auto block = exp->new_ptr<Block_t>(); |
3473 | auto stmt = exp->new_ptr<Statement_t>(); | 3474 | auto stmt = exp->new_ptr<Statement_t>(); |
@@ -9175,17 +9176,17 @@ private: | |||
9175 | pushScope(); | 9176 | pushScope(); |
9176 | extraScope = true; | 9177 | extraScope = true; |
9177 | } | 9178 | } |
9178 | auto typeVar = getUnusedName("_type_"); | 9179 | auto typeVar = getUnusedName("_type_"sv); |
9179 | forceAddToScope(typeVar); | 9180 | forceAddToScope(typeVar); |
9180 | tabCheckVar = getUnusedName("_tab_"); | 9181 | tabCheckVar = getUnusedName("_tab_"sv); |
9181 | forceAddToScope(tabCheckVar); | 9182 | forceAddToScope(tabCheckVar); |
9182 | temp.push_back(indent() + "local "s + typeVar + " = "s + globalVar("type", branch) + '(' + objVar + ')' + nll(branch)); | 9183 | temp.push_back(indent() + "local "s + typeVar + " = "s + globalVar("type"sv, branch) + '(' + objVar + ')' + nll(branch)); |
9183 | temp.push_back(indent() + "local "s + tabCheckVar + " = \"table\" == "s + typeVar + " or \"userdata\" == "s + typeVar + nll(branch)); | 9184 | temp.push_back(indent() + "local "s + tabCheckVar + " = \"table\" == "s + typeVar + " or \"userdata\" == "s + typeVar + nll(branch)); |
9184 | } | 9185 | } |
9185 | std::string matchVar; | 9186 | std::string matchVar; |
9186 | bool lastBranch = branches.back() == branch_ && !switchNode->lastBranch; | 9187 | bool lastBranch = branches.back() == branch_ && !switchNode->lastBranch; |
9187 | if (!lastBranch) { | 9188 | if (!lastBranch) { |
9188 | matchVar = getUnusedName("_match_"); | 9189 | matchVar = getUnusedName("_match_"sv); |
9189 | forceAddToScope(matchVar); | 9190 | forceAddToScope(matchVar); |
9190 | temp.push_back(indent() + "local "s + matchVar + " = false"s + nll(branch)); | 9191 | temp.push_back(indent() + "local "s + matchVar + " = false"s + nll(branch)); |
9191 | } | 9192 | } |