Skip to content

Commit

Permalink
Merge pull request lcompilers#774 from krshrimali/fix/list_comp
Browse files Browse the repository at this point in the history
Fix parsing for list comprehensions (bison) - New Parser
  • Loading branch information
certik committed Jul 18, 2022
2 parents 87639da + 5588f2b commit 7aedd1a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/lpython/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ id_list
id_item
: id_list { $$ = ID_TUPLE_01($1, @$); }
| id_list "," { $$ = ID_TUPLE_03($1, @$); }
| "(" id ")" { $$ = $2; }
| "(" id_list "," ")" { $$ = ID_TUPLE_03($2, @$); }
| "(" id_list "," id ")" { $$ = ID_TUPLE_01(TUPLE_($2, $4), @$); }
;
Expand Down
4 changes: 4 additions & 0 deletions tests/parser/comprehension1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@
distance_orig = [x**2 + y**2 + z**2 for x, y, z in points]

odd_elements = [i for i in main_list if i & 1]

first_ten_elements = [i for (i) in range(10)]

another_ten_elements = [(i) for (i) in range(10)]
4 changes: 2 additions & 2 deletions tests/reference/ast_new-comprehension1-69cf2af.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "ast_new-comprehension1-69cf2af",
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
"infile": "tests/parser/comprehension1.py",
"infile_hash": "9de33dffa08a67963ed9d4578637adba4ad03cf48e3b190aaa4b61de",
"infile_hash": "28749607f6d0963094ffca1a9f62cac0f5292451d4f6a57df0a6edf2",
"outfile": null,
"outfile_hash": null,
"stdout": "ast_new-comprehension1-69cf2af.stdout",
"stdout_hash": "b0d7ea467f9deebb1a94e9b100068a441cca5add58bbaff106da644c",
"stdout_hash": "82857717cc1bc07fb8b46095b37c75b69f1058abfec0899323f19b78",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/ast_new-comprehension1-69cf2af.stdout
Original file line number Diff line number Diff line change
@@ -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)]) ())] [])
(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 Load) (Call (Name range Load) [(ConstantInt 10 ())] []) [] 0)]) ()) (Assign [(Name another_ten_elements Store)] (ListComp (Name i Load) [((Name i Load) (Call (Name range Load) [(ConstantInt 10 ())] []) [] 0)]) ())] [])

0 comments on commit 7aedd1a

Please sign in to comment.