@@ -876,6 +876,10 @@ class YueCompilerImpl {
876876 return false ;
877877 }
878878
879+ bool isListComp (Comprehension_t* comp) const {
880+ return comp->items .size () == 2 && ast_is<CompFor_t>(comp->items .back ());
881+ }
882+
879883 void markVarLocalConst (const std::string& name) {
880884 auto & scope = _scopes.back ();
881885 scope.vars ->insert_or_assign (name, VarType::LocalConst);
@@ -1474,7 +1478,7 @@ class YueCompilerImpl {
14741478 if (simpleValue->value .is <TableLit_t>()) {
14751479 return true ;
14761480 } else if (auto comp = simpleValue->value .as <Comprehension_t>()) {
1477- if (comp-> items . size () != 2 || !ast_is<CompFor_t> (comp-> items . back () )) {
1481+ if (! isListComp (comp)) {
14781482 return true ;
14791483 }
14801484 }
@@ -1877,7 +1881,7 @@ class YueCompilerImpl {
18771881 case id<Try_t>(): transformTry (static_cast <Try_t*>(value), out, ExpUsage::Common); break ;
18781882 case id<Comprehension_t>(): {
18791883 auto comp = static_cast <Comprehension_t*>(value);
1880- if (comp-> items . size () == 2 && ast_is<CompFor_t>( comp-> items . back () )) {
1884+ if (isListComp ( comp)) {
18811885 transformCompCommon (comp, out);
18821886 } else {
18831887 specialSingleValue = false ;
@@ -2468,7 +2472,7 @@ class YueCompilerImpl {
24682472 case id<Comprehension_t>(): {
24692473 auto comp = static_cast <Comprehension_t*>(value);
24702474 auto expList = assignment->expList .get ();
2471- if (comp-> items . size () == 2 && ast_is<CompFor_t>( comp-> items . back () )) {
2475+ if (isListComp ( comp)) {
24722476 std::string preDefine = getPreDefineLine (assignment);
24732477 transformComprehension (comp, out, ExpUsage::Assignment, expList);
24742478 out.back ().insert (0 , preDefine);
@@ -2893,7 +2897,7 @@ class YueCompilerImpl {
28932897 if (auto tbA = item->get_by_path <TableLit_t>()) {
28942898 tableItems = &tbA->values .objects ();
28952899 } else if (auto tbB = item->get_by_path <Comprehension_t>()) {
2896- if (tbB-> items . size () == 2 && ast_is<CompFor_t>( tbB-> items . back () )) {
2900+ if (isListComp ( tbB)) {
28972901 throw CompileError (" invalid destructure value" sv, tbB);
28982902 }
28992903 tableItems = &tbB->items .objects ();
@@ -2924,7 +2928,7 @@ class YueCompilerImpl {
29242928 }
29252929 case id<Comprehension_t>(): {
29262930 auto table = static_cast <Comprehension_t*>(node);
2927- if (table-> items . size () == 2 && ast_is<CompFor_t>( table-> items . back () )) {
2931+ if (isListComp ( table)) {
29282932 throw CompileError (" invalid destructure value" sv, table);
29292933 }
29302934 tableItems = &table->items .objects ();
@@ -3295,7 +3299,7 @@ class YueCompilerImpl {
32953299 if (auto tab = sVal ->value .as <TableLit_t>()) {
32963300 destructNode = tab;
32973301 } else if (auto comp = sVal ->value .as <Comprehension_t>()) {
3298- if (comp-> items . size () != 2 || !ast_is<CompFor_t> (comp-> items . back () )) {
3302+ if (! isListComp (comp)) {
32993303 destructNode = comp;
33003304 }
33013305 }
@@ -7422,7 +7426,7 @@ class YueCompilerImpl {
74227426 }
74237427 }
74247428 } else if (auto comp = sval->value .as <Comprehension_t>()) {
7425- if (comp-> items . size () != 2 || !ast_is<CompFor_t> (comp-> items . back () )) {
7429+ if (! isListComp (comp)) {
74267430 discrete = inExp->new_ptr <ExpList_t>();
74277431 for (ast_node* val : comp->items .objects ()) {
74287432 if (auto def = ast_cast<NormalDef_t>(val)) {
@@ -8285,7 +8289,7 @@ class YueCompilerImpl {
82858289
82868290 void transformComprehension (Comprehension_t* comp, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr ) {
82878291 auto x = comp;
8288- if (comp-> items . size () != 2 || !ast_is<CompFor_t> (comp-> items . back () )) {
8292+ if (! isListComp (comp)) {
82898293 switch (usage) {
82908294 case ExpUsage::Assignment: {
82918295 auto tableLit = x->new_ptr <TableLit_t>();
0 commit comments