From dce38caac22a024026d19f556aefe0669f97403c Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 29 Jun 2023 14:23:44 +0800 Subject: fix assignment to in-expression with discrete values issue from #140 --- spec/inputs/in_expression.yue | 2 ++ spec/outputs/in_expression.lua | 5 +++++ src/yuescript/yue_ast.h | 2 +- src/yuescript/yue_compiler.cpp | 10 ++++++++-- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/spec/inputs/in_expression.yue b/spec/inputs/in_expression.yue index 2756fe8..dacf820 100644 --- a/spec/inputs/in_expression.yue +++ b/spec/inputs/in_expression.yue @@ -4,6 +4,8 @@ a, b = x(...) not in [1, 3], 2 d = (tb.x.y ...) not in [1, 3] +has = "foo" in { "bar", "foo" } + if a in {1} and b in {2, 3, 4} or c in [1, 10] print a, b, c diff --git a/spec/outputs/in_expression.lua b/spec/outputs/in_expression.lua index 061431f..8b37a72 100644 --- a/spec/outputs/in_expression.lua +++ b/spec/outputs/in_expression.lua @@ -11,6 +11,11 @@ do local _val_0 = (tb.x.y(...)) d = not (1 <= _val_0 and _val_0 <= 3) end +local has +do + local _val_0 = "foo" + has = "bar" == _val_0 or "foo" == _val_0 +end if (1 == a) and (2 == b or 3 == b or 4 == b) or (function() local _val_0 = c return 1 <= _val_0 and _val_0 <= 10 diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index c47ba8a..85a1e52 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -910,7 +910,7 @@ struct YueFormat { int tabSpaces = 4; std::string toString(ast_node* node); - Converter converter; + Converter converter{}; void pushScope(); void popScope(); std::string convert(const ast_node* node); diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index c1426f9..efd0aed 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -72,7 +72,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.17.2"sv; +const std::string_view version = "0.17.3"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -5429,7 +5429,13 @@ private: for (auto exp : discrete->values.objects()) { transformExp(static_cast(exp), tmp, ExpUsage::Closure); } - _buf << indent() << "return "sv; + if (usage == ExpUsage::Assignment) { + str_list tmpList; + transformExp(static_cast(assignList->exprs.front()), tmpList, ExpUsage::Closure); + _buf << indent() << tmpList.back() << " = "sv; + } else { + _buf << indent() << "return "sv; + } if (unary_exp->inExp->not_) { _buf << "not ("sv; } -- cgit v1.2.3-55-g6feb