diff options
| -rw-r--r-- | src/yuescript/yue_ast.cpp | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/yuescript/yue_ast.cpp b/src/yuescript/yue_ast.cpp index 3953f97..8c19df8 100644 --- a/src/yuescript/yue_ast.cpp +++ b/src/yuescript/yue_ast.cpp | |||
| @@ -1151,18 +1151,32 @@ std::string TableLit_t::to_string(void* ud) const { | |||
| 1151 | return "{ }"s; | 1151 | return "{ }"s; |
| 1152 | } | 1152 | } |
| 1153 | bool hasInBlockExp = false; | 1153 | bool hasInBlockExp = false; |
| 1154 | bool hasCommentOrEmpty = false; | ||
| 1155 | ast_node* lastValueNode = nullptr; | ||
| 1154 | for (auto value : values.objects()) { | 1156 | for (auto value : values.objects()) { |
| 1157 | if (ast_is<YueLineComment_t, YueMultilineComment_t, EmptyLine_t>(value)) { | ||
| 1158 | hasCommentOrEmpty = true; | ||
| 1159 | continue; | ||
| 1160 | } | ||
| 1161 | lastValueNode = value; | ||
| 1155 | if (isInBlockExp(value, value == values.back())) { | 1162 | if (isInBlockExp(value, value == values.back())) { |
| 1156 | hasInBlockExp = true; | 1163 | hasInBlockExp = true; |
| 1157 | break; | ||
| 1158 | } | 1164 | } |
| 1159 | } | 1165 | } |
| 1160 | if (hasInBlockExp) { | 1166 | if (hasInBlockExp || hasCommentOrEmpty) { |
| 1161 | str_list temp; | 1167 | str_list temp; |
| 1162 | temp.emplace_back("{"s); | 1168 | temp.emplace_back("{"s); |
| 1163 | info->pushScope(); | 1169 | info->pushScope(); |
| 1164 | for (auto value : values.objects()) { | 1170 | for (auto value : values.objects()) { |
| 1165 | temp.emplace_back(info->ind() + value->to_string(ud)); | 1171 | if (ast_is<EmptyLine_t>(value)) { |
| 1172 | temp.emplace_back(""s); | ||
| 1173 | continue; | ||
| 1174 | } | ||
| 1175 | auto valueStr = value->to_string(ud); | ||
| 1176 | if (!ast_is<YueLineComment_t, YueMultilineComment_t>(value) && value != lastValueNode) { | ||
| 1177 | valueStr += ','; | ||
| 1178 | } | ||
| 1179 | temp.emplace_back(info->ind() + valueStr); | ||
| 1166 | } | 1180 | } |
| 1167 | info->popScope(); | 1181 | info->popScope(); |
| 1168 | temp.emplace_back(info->ind() + '}'); | 1182 | temp.emplace_back(info->ind() + '}'); |
| @@ -1175,6 +1189,7 @@ std::string TableLit_t::to_string(void* ud) const { | |||
| 1175 | return '{' + join(temp, ", "sv) + '}'; | 1189 | return '{' + join(temp, ", "sv) + '}'; |
| 1176 | } | 1190 | } |
| 1177 | } | 1191 | } |
| 1192 | |||
| 1178 | std::string TableBlock_t::to_string(void* ud) const { | 1193 | std::string TableBlock_t::to_string(void* ud) const { |
| 1179 | auto info = reinterpret_cast<YueFormat*>(ud); | 1194 | auto info = reinterpret_cast<YueFormat*>(ud); |
| 1180 | str_list temp; | 1195 | str_list temp; |
