Skip to content

Commit 330e201

Browse files
Merge pull request #994 from akshanshbhatt/pr_968
2 parents ab1794c + 53eae71 commit 330e201

File tree

5 files changed

+24
-3
lines changed

5 files changed

+24
-3
lines changed

src/lpython/parser/parser.yy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ void yyerror(YYLTYPE *yyloc, LFortran::Parser &p, const std::string &msg)
252252
%type <ast> ternary_if_statement
253253
/* %type <ast> list_comprehension */
254254
%type <vec_ast> id_list
255+
%type <vec_ast> id_list_opt
255256
%type <ast> id_item
256257
%type <ast> subscript
257258
%type <comp> comp_for
@@ -788,6 +789,11 @@ dict_list
788789
| dict { LIST_NEW($$); LIST_ADD($$, $1); }
789790
;
790791

792+
id_list_opt
793+
: id_list { $$ = $1; }
794+
| %empty { LIST_NEW($$); }
795+
;
796+
791797
id_list
792798
: id_list "," id_item { $$ = $1; LIST_ADD($$, $3); }
793799
| id_item { LIST_NEW($$); LIST_ADD($$, $1); }
@@ -798,6 +804,8 @@ id_item
798804
| "(" id ")" { $$ = $2; }
799805
| "(" id_list "," ")" { $$ = ID_TUPLE_03($2, @$); }
800806
| "(" id_list "," id_item ")" { $$ = ID_TUPLE_01(TUPLE_($2, $4), @$); }
807+
| "[" id_list_opt "]" { $$ = LIST(SET_EXPR_CTX_02($2, Store), @$); }
808+
| "[" id_list "," "]" { $$ = LIST(SET_EXPR_CTX_02($2, Store), @$); }
801809
;
802810

803811
keyword_item

src/lpython/parser/semantics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ static inline ast_t* SET_EXPR_CTX_01(ast_t* x, expr_contextType ctx) {
8787
SET_EXPR_CTX_(Subscript, ctx)
8888
SET_EXPR_CTX_(Starred, ctx)
8989
SET_EXPR_CTX_(Name, ctx)
90+
SET_EXPR_CTX_(List, ctx)
9091
SET_EXPR_CTX_(Tuple, ctx)
9192
default : { break; }
9293
}

tests/parser/comprehension1.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@
2525

2626
rest = tuple(i for i in range(a.ndim) if i not in axis)
2727

28+
(sstr(x) for [x] in self._module.gens)
29+
30+
func(*[[x*y] for [x] in self._module.gens for [y] in J._module.gens])
31+
32+
(x for [a, b] in y)
33+
34+
(x for [a, (b, c)] in y)
35+
36+
(x for [(b, c)] in y)
37+
38+
(x for [] in y)
39+
2840
# Set Comprehension
2941
newSet = {element*3 for element in myList}
3042

tests/reference/ast_new-comprehension1-69cf2af.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "ast_new-comprehension1-69cf2af",
33
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
44
"infile": "tests/parser/comprehension1.py",
5-
"infile_hash": "86299dc3e7927f4d06bb55bce36c88a6833bb6e1801a3f1c758c6266",
5+
"infile_hash": "2e36e18908781ccd0324deffa39c1e3390d55d0a0990d86863683571",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "ast_new-comprehension1-69cf2af.stdout",
9-
"stdout_hash": "2215f7cfded5c53e72d9f7768eb71be7d0514402ad2ec993ed1b577e",
9+
"stdout_hash": "02fdc12b682be415fff7808905a203e51383c78f9695094debcfdbb4",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(Module [(Assign [(Name fruits Store)] (ListComp (Name f Load) [((Name f Store) (Name fruit_list Load) [(Call (Attribute (Name f Load) startswith Load) [(ConstantStr "a" ())] [])] 0)]) ()) (Assign [(Name fruit_list Store)] (ListComp (Name fruit Load) [((Name fruit Store) (Name fruits Load) [] 0)]) ()) (Assign [(Name sum_cord Store)] (ListComp (BinOp (Name x Load) Add (Name y Load)) [((Tuple [(Name x Store) (Name y Store)] Store) (Name points Load) [(BoolOp And [(Compare (Name x Load) Gt [(ConstantInt 0 ())]) (Compare (Name y Load) Gt [(ConstantInt 0 ())])])] 0)]) ()) (Assign [(Name transform_1 Store)] (ListComp (BinOp (BinOp (ConstantInt 2 ()) Mult (Name x Load)) Add (ConstantInt 6 ())) [((Name x Store) (Call (Name range Load) [(ConstantInt 10 ())] []) [] 0)]) ()) (Assign [(Name distance_orig Store)] (ListComp (BinOp (BinOp (BinOp (Name x Load) Pow (ConstantInt 2 ())) Add (BinOp (Name y Load) Pow (ConstantInt 2 ()))) Add (BinOp (Name z Load) Pow (ConstantInt 2 ()))) [((Tuple [(Name x Store) (Name y Store) (Name z Store)] Store) (Name points Load) [] 0)]) ()) (Assign [(Name odd_elements Store)] (ListComp (Name i Load) [((Name i Store) (Name main_list Load) [(BinOp (Name i Load) BitAnd (ConstantInt 1 ()))] 0)]) ()) (Assign [(Name first_ten_elements Store)] (ListComp (Name i Load) [((Name i Store) (Call (Name range Load) [(ConstantInt 10 ())] []) [] 0)]) ()) (Assign [(Name another_ten_elements Store)] (ListComp (Name i Load) [((Name i Store) (Call (Name range Load) [(ConstantInt 10 ())] []) [] 0)]) ()) (Assign [(Name comp Store)] (ListComp (BinOp (Name i Load) Pow (ConstantInt 2 ())) [((Name i Store) (Call (Name range Load) [(ConstantInt 10 ())] []) [(BoolOp And [(Compare (Name i Load) NotIn [(List [(ConstantInt 3 ()) (ConstantInt 5 ()) (ConstantInt 7 ())] Load)]) (Compare (Name i Load) In [(Name list3 Load)])])] 0)]) ()) (Assign [(Name prm_tup Store)] (Call (Name tuple Load) [(GeneratorExp (Call (Name next Load) [(Name parameters Load)] []) [((Name _ Store) (Attribute (Name i Load) __parameters__ Load) [] 0)])] []) ()) (Assign [(Name args Store)] (Call (Attribute (ConstantStr ", " ()) join Load) [(GeneratorExp (Call (Name _to_str Load) [(Name i Load)] []) [((Name i Store) (Attribute (Name self Load) __args__ Load) [] 0)])] []) ()) (Assign [(Name rest Store)] (Call (Name tuple Load) [(GeneratorExp (Name i Load) [((Name i Store) (Call (Name range Load) [(Attribute (Name a Load) ndim Load)] []) [(Compare (Name i Load) NotIn [(Name axis Load)])] 0)])] []) ()) (Assign [(Name newSet Store)] (SetComp (BinOp (Name element Load) Mult (ConstantInt 3 ())) [((Name element Store) (Name myList Load) [] 0)]) ()) (Assign [(Name newSet Store)] (SetComp (BinOp (Name element Load) Mult (ConstantInt 3 ())) [((Name element Store) (Name myList Load) [(Compare (BinOp (Name element Load) Mod (ConstantInt 2 ())) Eq [(ConstantInt 0 ())])] 0)]) ()) (Expr (DictComp (Name x Load) (BinOp (Name x Load) Pow (ConstantInt 3 ())) [((Name x Store) (Call (Name range Load) [(ConstantInt 10 ())] []) [(Compare (BinOp (BinOp (Name x Load) Pow (ConstantInt 3 ())) Mod (ConstantInt 4 ())) Eq [(ConstantInt 0 ())])] 0)])) (Assign [(Name square_dict Store)] (DictComp (Name num Load) (BinOp (Name num Load) Mult (Name num Load)) [((Name num Store) (Call (Name range Load) [(ConstantInt 1 ()) (ConstantInt 11 ())] []) [] 0)]) ()) (Expr (GeneratorExp (Subscript (Name string Load) (Name i Load) Load) [((Name i Store) (Call (Name range Load) [(BinOp (Call (Name len Load) [(Name string Load)] []) Sub (ConstantInt 1 ())) (UnaryOp USub (ConstantInt 1 ())) (UnaryOp USub (ConstantInt 1 ()))] []) [] 0)])) (Assign [(Name k Store)] (GeneratorExp (BinOp (Name j Load) Add (Name k Load)) [((Tuple [(Name j Store) (Name k Store)] Store) (Call (Name range Load) [(ConstantInt 10 ())] []) [(Compare (Name j Load) Gt [(ConstantInt 0 ())])] 0)]) ()) (Expr (GeneratorExp (BinOp (BinOp (Name left Load) Add (Name size Load)) Add (Name right Load)) [((Tuple [(Name size Store) (Tuple [(Name left Store) (Name right Store)] Store)] Store) (Call (Name zip Load) [(Attribute (Name array Load) shape Load) (Name pad_width Load)] []) [] 0)])) (Assign [(Name viter Store)] (GeneratorExp (Tuple [(Name i Load) (Name j Load)] Load) [((Tuple [(Tuple [(Name i Store) (Name _ Store)] Store) (Tuple [(Name j Store) (Name _ Store)] Store)] Store) (Call (Name zip Load) [(Subscript (Name newargs Load) (Slice (ConstantInt 1 ()) () ()) Load) (Subscript (Name args Load) (Slice (ConstantInt 1 ()) () ()) Load)] []) [] 0)]) ())] [])
1+
(Module [(Assign [(Name fruits Store)] (ListComp (Name f Load) [((Name f Store) (Name fruit_list Load) [(Call (Attribute (Name f Load) startswith Load) [(ConstantStr "a" ())] [])] 0)]) ()) (Assign [(Name fruit_list Store)] (ListComp (Name fruit Load) [((Name fruit Store) (Name fruits Load) [] 0)]) ()) (Assign [(Name sum_cord Store)] (ListComp (BinOp (Name x Load) Add (Name y Load)) [((Tuple [(Name x Store) (Name y Store)] Store) (Name points Load) [(BoolOp And [(Compare (Name x Load) Gt [(ConstantInt 0 ())]) (Compare (Name y Load) Gt [(ConstantInt 0 ())])])] 0)]) ()) (Assign [(Name transform_1 Store)] (ListComp (BinOp (BinOp (ConstantInt 2 ()) Mult (Name x Load)) Add (ConstantInt 6 ())) [((Name x Store) (Call (Name range Load) [(ConstantInt 10 ())] []) [] 0)]) ()) (Assign [(Name distance_orig Store)] (ListComp (BinOp (BinOp (BinOp (Name x Load) Pow (ConstantInt 2 ())) Add (BinOp (Name y Load) Pow (ConstantInt 2 ()))) Add (BinOp (Name z Load) Pow (ConstantInt 2 ()))) [((Tuple [(Name x Store) (Name y Store) (Name z Store)] Store) (Name points Load) [] 0)]) ()) (Assign [(Name odd_elements Store)] (ListComp (Name i Load) [((Name i Store) (Name main_list Load) [(BinOp (Name i Load) BitAnd (ConstantInt 1 ()))] 0)]) ()) (Assign [(Name first_ten_elements Store)] (ListComp (Name i Load) [((Name i Store) (Call (Name range Load) [(ConstantInt 10 ())] []) [] 0)]) ()) (Assign [(Name another_ten_elements Store)] (ListComp (Name i Load) [((Name i Store) (Call (Name range Load) [(ConstantInt 10 ())] []) [] 0)]) ()) (Assign [(Name comp Store)] (ListComp (BinOp (Name i Load) Pow (ConstantInt 2 ())) [((Name i Store) (Call (Name range Load) [(ConstantInt 10 ())] []) [(BoolOp And [(Compare (Name i Load) NotIn [(List [(ConstantInt 3 ()) (ConstantInt 5 ()) (ConstantInt 7 ())] Load)]) (Compare (Name i Load) In [(Name list3 Load)])])] 0)]) ()) (Assign [(Name prm_tup Store)] (Call (Name tuple Load) [(GeneratorExp (Call (Name next Load) [(Name parameters Load)] []) [((Name _ Store) (Attribute (Name i Load) __parameters__ Load) [] 0)])] []) ()) (Assign [(Name args Store)] (Call (Attribute (ConstantStr ", " ()) join Load) [(GeneratorExp (Call (Name _to_str Load) [(Name i Load)] []) [((Name i Store) (Attribute (Name self Load) __args__ Load) [] 0)])] []) ()) (Assign [(Name rest Store)] (Call (Name tuple Load) [(GeneratorExp (Name i Load) [((Name i Store) (Call (Name range Load) [(Attribute (Name a Load) ndim Load)] []) [(Compare (Name i Load) NotIn [(Name axis Load)])] 0)])] []) ()) (Expr (GeneratorExp (Call (Name sstr Load) [(Name x Load)] []) [((List [(Name x Store)] Store) (Attribute (Attribute (Name self Load) _module Load) gens Load) [] 0)])) (Expr (Call (Name func Load) [(Starred (ListComp (List [(BinOp (Name x Load) Mult (Name y Load))] Load) [((List [(Name x Store)] Store) (Attribute (Attribute (Name self Load) _module Load) gens Load) [] 0) ((List [(Name y Store)] Store) (Attribute (Attribute (Name J Load) _module Load) gens Load) [] 0)]) Load)] [])) (Expr (GeneratorExp (Name x Load) [((List [(Name a Store) (Name b Store)] Store) (Name y Load) [] 0)])) (Expr (GeneratorExp (Name x Load) [((List [(Name a Store) (Tuple [(Name b Store) (Name c Store)] Store)] Store) (Name y Load) [] 0)])) (Expr (GeneratorExp (Name x Load) [((List [(Tuple [(Name b Store) (Name c Store)] Store)] Store) (Name y Load) [] 0)])) (Expr (GeneratorExp (Name x Load) [((List [] Store) (Name y Load) [] 0)])) (Assign [(Name newSet Store)] (SetComp (BinOp (Name element Load) Mult (ConstantInt 3 ())) [((Name element Store) (Name myList Load) [] 0)]) ()) (Assign [(Name newSet Store)] (SetComp (BinOp (Name element Load) Mult (ConstantInt 3 ())) [((Name element Store) (Name myList Load) [(Compare (BinOp (Name element Load) Mod (ConstantInt 2 ())) Eq [(ConstantInt 0 ())])] 0)]) ()) (Expr (DictComp (Name x Load) (BinOp (Name x Load) Pow (ConstantInt 3 ())) [((Name x Store) (Call (Name range Load) [(ConstantInt 10 ())] []) [(Compare (BinOp (BinOp (Name x Load) Pow (ConstantInt 3 ())) Mod (ConstantInt 4 ())) Eq [(ConstantInt 0 ())])] 0)])) (Assign [(Name square_dict Store)] (DictComp (Name num Load) (BinOp (Name num Load) Mult (Name num Load)) [((Name num Store) (Call (Name range Load) [(ConstantInt 1 ()) (ConstantInt 11 ())] []) [] 0)]) ()) (Expr (GeneratorExp (Subscript (Name string Load) (Name i Load) Load) [((Name i Store) (Call (Name range Load) [(BinOp (Call (Name len Load) [(Name string Load)] []) Sub (ConstantInt 1 ())) (UnaryOp USub (ConstantInt 1 ())) (UnaryOp USub (ConstantInt 1 ()))] []) [] 0)])) (Assign [(Name k Store)] (GeneratorExp (BinOp (Name j Load) Add (Name k Load)) [((Tuple [(Name j Store) (Name k Store)] Store) (Call (Name range Load) [(ConstantInt 10 ())] []) [(Compare (Name j Load) Gt [(ConstantInt 0 ())])] 0)]) ()) (Expr (GeneratorExp (BinOp (BinOp (Name left Load) Add (Name size Load)) Add (Name right Load)) [((Tuple [(Name size Store) (Tuple [(Name left Store) (Name right Store)] Store)] Store) (Call (Name zip Load) [(Attribute (Name array Load) shape Load) (Name pad_width Load)] []) [] 0)])) (Assign [(Name viter Store)] (GeneratorExp (Tuple [(Name i Load) (Name j Load)] Load) [((Tuple [(Tuple [(Name i Store) (Name _ Store)] Store) (Tuple [(Name j Store) (Name _ Store)] Store)] Store) (Call (Name zip Load) [(Subscript (Name newargs Load) (Slice (ConstantInt 1 ()) () ()) Load) (Subscript (Name args Load) (Slice (ConstantInt 1 ()) () ()) Load)] []) [] 0)]) ())] [])

0 commit comments

Comments
 (0)