Skip to content

Commit

Permalink
fix invalid formation.
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed Dec 14, 2023
1 parent 5490a7c commit 8cff607
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 35 deletions.
15 changes: 1 addition & 14 deletions spec/outputs/lists.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,20 +313,7 @@ do
end
c = (1 == a)
c = (1 == a)
do
local _check_0 = {
1
}
local _find_0 = false
for _index_0 = 1, #_check_0 do
local _item_0 = _check_0[_index_0]
if _item_0 == a then
_find_0 = true
break
end
end
c = _find_0
end
c = (1 == a)
end
do
a, b = hello[1], hello[2]
Expand Down
123 changes: 106 additions & 17 deletions src/yuescript/yue_ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,20 +822,27 @@ std::string Slice_t::to_string(void* ud) const {
if (startValue.is<Exp_t>()) {
temp.emplace_back(startValue->to_string(ud));
}
temp.emplace_back(", "s);
if (stopValue.is<Exp_t>()) {
temp.emplace_back(stopValue->to_string(ud));
temp.emplace_back();
temp.emplace_back(", "s + stopValue->to_string(ud));
} else {
temp.emplace_back(","s);
}
if (stepValue.is<Exp_t>()) {
temp.emplace_back(", "s + stepValue->to_string(ud));
}
auto valueStr = join(temp);
return '[' + (valueStr[0] == '[' ? " "s : ""s) + valueStr + ']';
}
static bool isInBlockExp(ast_node* node) {
static bool isInBlockExp(ast_node* node, bool last = false) {
if (auto exp = ast_cast<Exp_t>(node)) {
auto unaryExp = static_cast<UnaryExp_t*>(exp->pipeExprs.front());
auto value = static_cast<Value_t*>(unaryExp->expos.front());
UnaryExp_t* unaryExp = nullptr;
if (exp->opValues.empty()) {
unaryExp = static_cast<UnaryExp_t*>(exp->pipeExprs.back());
} else {
unaryExp = static_cast<UnaryExp_t*>(static_cast<ExpOpValue_t*>(exp->opValues.back())->pipeExprs.back());
}
auto value = static_cast<Value_t*>(unaryExp->expos.back());
if (auto simpleValue = value->item.as<SimpleValue_t>()) {
if (!ast_is<TableLit_t, ConstValue_t, Num_t, VarArg_t,
TblComprehension_t, Comprehension_t>(simpleValue->value)) {
Expand All @@ -845,9 +852,76 @@ static bool isInBlockExp(ast_node* node) {
if (ast_is<InvokeArgs_t>(chainValue->items.back())) {
return true;
}
} else if (!last && value->item.is<SimpleTable_t>()) {
return true;
}
} else if (ast_is<TableBlock_t>(node)) {
return true;
} else {
switch (node->get_id()) {
case id<VariablePairDef_t>(): {
auto pair = static_cast<VariablePairDef_t*>(node);
if (pair->defVal) {
return true;
}
return false;
}
case id<NormalPairDef_t>(): {
auto pair = static_cast<NormalPairDef_t*>(node);
if (pair->defVal) {
return true;
}
return isInBlockExp(pair->pair->value);
}
case id<SpreadExp_t>(): {
auto pair = static_cast<SpreadExp_t*>(node);
return isInBlockExp(pair->exp);
}
case id<NormalDef_t>(): {
auto pair = static_cast<NormalDef_t*>(node);
if (pair->defVal) {
return true;
}
return isInBlockExp(pair->item);
}
case id<MetaVariablePairDef_t>(): {
auto pair = static_cast<MetaVariablePairDef_t*>(node);
if (pair->defVal) {
return true;
}
return false;
}
case id<MetaNormalPairDef_t>(): {
auto pair = static_cast<MetaNormalPairDef_t*>(node);
if (pair->defVal) {
return true;
}
return isInBlockExp(pair->pair->value);
}
case id<VariablePair_t>(): {
return false;
}
case id<NormalPair_t>(): {
auto pair = static_cast<NormalPair_t*>(node);
return isInBlockExp(pair->value);
}
case id<MetaVariablePair_t>(): {
return false;
}
case id<MetaNormalPair_t>(): {
auto pair = static_cast<MetaNormalPair_t*>(node);
return isInBlockExp(pair->value);
}
case id<TableBlockIndent_t>(): {
return true;
}
case id<SpreadListExp_t>(): {
auto pair = static_cast<SpreadListExp_t*>(node);
return isInBlockExp(pair->exp);
}
default:
return false;
}
}
return false;
}
Expand All @@ -865,7 +939,7 @@ std::string Invoke_t::to_string(void* ud) const {
auto info = reinterpret_cast<YueFormat*>(ud);
bool hasInBlockExp = false;
for (auto arg : args.objects()) {
if (arg != args.back() && isInBlockExp(arg)) {
if (isInBlockExp(arg, arg == args.back())) {
hasInBlockExp = true;
break;
}
Expand Down Expand Up @@ -899,15 +973,30 @@ std::string TableLit_t::to_string(void* ud) const {
if (values.empty()) {
return "{ }"s;
}
str_list temp;
temp.emplace_back("{"s);
info->pushScope();
bool hasInBlockExp = false;
for (auto value : values.objects()) {
temp.emplace_back(info->ind() + value->to_string(ud));
if (isInBlockExp(value, value == values.back())) {
hasInBlockExp = true;
break;
}
}
if (hasInBlockExp) {
str_list temp;
temp.emplace_back("{"s);
info->pushScope();
for (auto value : values.objects()) {
temp.emplace_back(info->ind() + value->to_string(ud));
}
info->popScope();
temp.emplace_back(info->ind() + '}');
return join(temp, "\n"sv);
} else {
str_list temp;
for (auto value : values.objects()) {
temp.emplace_back(value->to_string(ud));
}
return '{' + join(temp, ", "sv) + '}';
}
info->popScope();
temp.emplace_back(info->ind() + '}');
return join(temp, "\n"sv);
}
std::string TableBlock_t::to_string(void* ud) const {
auto info = reinterpret_cast<YueFormat*>(ud);
Expand Down Expand Up @@ -1108,7 +1197,7 @@ std::string FnArgDefList_t::to_string(void* ud) const {
bool hasInBlockExp = false;
for (auto def : definitions.objects()) {
auto argDef = static_cast<FnArgDef_t*>(def);
if (argDef->defaultValue && isInBlockExp(argDef->defaultValue)) {
if (argDef->defaultValue && isInBlockExp(argDef->defaultValue, argDef == definitions.back())) {
hasInBlockExp = true;
break;
}
Expand Down Expand Up @@ -1144,7 +1233,7 @@ std::string FnArgsDef_t::to_string(void* ud) const {
if (defList) {
for (auto def : defList->definitions.objects()) {
auto argDef = static_cast<FnArgDef_t*>(def);
if (argDef->defaultValue && isInBlockExp(argDef->defaultValue)) {
if (argDef->defaultValue && isInBlockExp(argDef->defaultValue, argDef == defList->definitions.back())) {
hasInBlockExp = true;
break;
}
Expand Down Expand Up @@ -1250,7 +1339,7 @@ std::string InvokeArgs_t::to_string(void* ud) const {
auto info = reinterpret_cast<YueFormat*>(ud);
bool hasInBlockExp = false;
for (auto arg : args.objects()) {
if (isInBlockExp(arg)) {
if (isInBlockExp(arg, arg == args.back())) {
hasInBlockExp = true;
break;
}
Expand Down Expand Up @@ -1309,7 +1398,7 @@ std::string InDiscrete_t::to_string(void* ud) const {
for (auto value : values.objects()) {
temp.emplace_back(value->to_string(ud));
}
return '{' + join(temp, ", "sv) + '}';
return '[' + join(temp, ", "sv) + (temp.size() == 1 ? ",]"s : "]"s);
}
std::string In_t::to_string(void* ud) const {
return (not_ ? "not "s : ""s) + "in "s + item->to_string(ud);
Expand Down
7 changes: 4 additions & 3 deletions src/yuescript/yue_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static std::unordered_set<std::string> Metamethods = {
"close"s // Lua 5.4
};

const std::string_view version = "0.21.2"sv;
const std::string_view version = "0.21.3"sv;
const std::string_view extension = "yue"sv;

class CompileError : public std::logic_error {
Expand Down Expand Up @@ -7713,8 +7713,9 @@ class YueCompilerImpl {
clsDecl = value->get_by_path<SimpleValue_t, ClassDecl_t>();
BLOCK_END
} else if (auto expList = expListFrom(statement)) {
auto value = singleValueFrom(expList);
clsDecl = value->get_by_path<SimpleValue_t, ClassDecl_t>();
if (auto value = singleValueFrom(expList)) {
clsDecl = value->get_by_path<SimpleValue_t, ClassDecl_t>();
}
}
if (clsDecl) {
std::string clsName;
Expand Down
2 changes: 1 addition & 1 deletion src/yuescript/yue_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ YueParser::YueParser() {
NotIn = true_();
InDiscrete =
'[' >> Seperator >> space >> exp_not_tab >> (+(space >> ',' >> space >> exp_not_tab) | space >> ',') >> space >> ']' |
'{' >> Seperator >> space >> exp_not_tab >> *(space >> ',' >> space >> exp_not_tab) >> space >> '}';
'{' >> Seperator >> space >> exp_not_tab >> *(space >> ',' >> space >> exp_not_tab | space >> ',') >> space >> '}';
In = -(key("not") >> NotIn >> space) >> key("in") >> space >> (InDiscrete | and_(key("not")) >> confusing_unary_not_error | Exp);

UnaryOperator =
Expand Down

0 comments on commit 8cff607

Please sign in to comment.