diff options
| author | Li Jin <dragon-fly@qq.com> | 2022-11-08 18:10:44 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2022-11-09 11:29:32 +0800 |
| commit | b041d365b88b76418def86d13a8f946dd8a6db73 (patch) | |
| tree | bca73c504d250b15fe40e1718d00f6e8516e5195 /src/yuescript/yue_compiler.cpp | |
| parent | bd10e55e72cfd588d3ed1ef6330bc138a3fe8eff (diff) | |
| download | yuescript-b041d365b88b76418def86d13a8f946dd8a6db73.tar.gz yuescript-b041d365b88b76418def86d13a8f946dd8a6db73.tar.bz2 yuescript-b041d365b88b76418def86d13a8f946dd8a6db73.zip | |
add chain assignment. fix issue #115.
Diffstat (limited to '')
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 45a9bf6..4072974 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
| @@ -59,7 +59,7 @@ namespace yue { | |||
| 59 | 59 | ||
| 60 | typedef std::list<std::string> str_list; | 60 | typedef std::list<std::string> str_list; |
| 61 | 61 | ||
| 62 | const std::string_view version = "0.15.10"sv; | 62 | const std::string_view version = "0.15.11"sv; |
| 63 | const std::string_view extension = "yue"sv; | 63 | const std::string_view extension = "yue"sv; |
| 64 | 64 | ||
| 65 | class YueCompilerImpl { | 65 | class YueCompilerImpl { |
| @@ -1276,6 +1276,7 @@ private: | |||
| 1276 | } | 1276 | } |
| 1277 | break; | 1277 | break; |
| 1278 | } | 1278 | } |
| 1279 | case id<ChainAssign_t>(): transformChainAssign(static_cast<ChainAssign_t*>(content), out); break; | ||
| 1279 | default: YUEE("AST node mismatch", content); break; | 1280 | default: YUEE("AST node mismatch", content); break; |
| 1280 | } | 1281 | } |
| 1281 | if (statement->needSep && !out.empty() && !out.back().empty()) { | 1282 | if (statement->needSep && !out.empty() && !out.back().empty()) { |
| @@ -8004,6 +8005,40 @@ private: | |||
| 8004 | assignment->action.set(tab->assign); | 8005 | assignment->action.set(tab->assign); |
| 8005 | transformAssignment(assignment, out); | 8006 | transformAssignment(assignment, out); |
| 8006 | } | 8007 | } |
| 8008 | |||
| 8009 | void transformChainAssign(ChainAssign_t* chainAssign, str_list& out) { | ||
| 8010 | auto x = chainAssign; | ||
| 8011 | str_list temp; | ||
| 8012 | auto value = chainAssign->assign->values.front(); | ||
| 8013 | bool constVal = false; | ||
| 8014 | if (auto simpleVal = simpleSingleValueFrom(value)) { | ||
| 8015 | constVal = ast_is<const_value_t, Num_t>(simpleVal->value); | ||
| 8016 | } | ||
| 8017 | if (constVal || !singleVariableFrom(value, false).empty()) { | ||
| 8018 | for (auto exp : chainAssign->exprs.objects()) { | ||
| 8019 | transformAssignment(assignmentFrom(static_cast<Exp_t*>(exp), value, exp), temp); | ||
| 8020 | } | ||
| 8021 | out.push_back(join(temp)); | ||
| 8022 | return; | ||
| 8023 | } | ||
| 8024 | auto valName = getUnusedName("_tmp_"); | ||
| 8025 | auto newValue = toAst<Exp_t>(valName, value); | ||
| 8026 | ast_list<false, ExpListAssign_t> assignments; | ||
| 8027 | for (auto exp : chainAssign->exprs.objects()) { | ||
| 8028 | auto assignment = assignmentFrom(static_cast<Exp_t*>(exp), newValue, exp); | ||
| 8029 | assignments.push_back(assignment.get()); | ||
| 8030 | temp.push_back(getPreDefineLine(assignment)); | ||
| 8031 | } | ||
| 8032 | assignments.push_front(assignmentFrom(newValue, value, value)); | ||
| 8033 | temp.push_back(indent() + "do"s + nll(x)); | ||
| 8034 | pushScope(); | ||
| 8035 | for (auto item : assignments.objects()) { | ||
| 8036 | transformAssignment(static_cast<ExpListAssign_t*>(item), temp); | ||
| 8037 | } | ||
| 8038 | popScope(); | ||
| 8039 | temp.push_back(indent() + "end"s + nll(x)); | ||
| 8040 | out.push_back(join(temp)); | ||
| 8041 | } | ||
| 8007 | }; | 8042 | }; |
| 8008 | 8043 | ||
| 8009 | const std::string YueCompilerImpl::Empty; | 8044 | const std::string YueCompilerImpl::Empty; |
