Skip to content

Commit 46bede2

Browse files
Merge pull request #993 from akshanshbhatt/pr_compr_subr
[Parser] Recognise `ListComprehension` Subscript
2 parents 330e201 + 2cc6a88 commit 46bede2

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

src/lpython/parser/parser.yy

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ void yyerror(YYLTYPE *yyloc, LFortran::Parser &p, const std::string &msg)
250250
%type <string> type_comment
251251
%type <ast> string
252252
%type <ast> ternary_if_statement
253-
/* %type <ast> list_comprehension */
253+
%type <ast> comprehension
254254
%type <vec_ast> id_list
255255
%type <vec_ast> id_list_opt
256256
%type <ast> id_item
@@ -908,6 +908,7 @@ subscript
908908
| "{" dict_list comma_opt "}" "[" slice_item "]" {
909909
$$ = SUBSCRIPT_01(DICT_02($2, @$), $6, @$); }
910910
| subscript "[" slice_item "]" { $$ = SUBSCRIPT_01($1, $3, @$); }
911+
| comprehension "[" slice_item "]" { $$ = SUBSCRIPT_01($1, $3, @$); }
911912
;
912913

913914
string
@@ -968,6 +969,13 @@ lambda_expression
968969
$$ = LAMBDA_01($2, $4, @$); }
969970
;
970971

972+
comprehension
973+
: "[" expr comp_for_items "]" { $$ = LIST_COMP_1($2, $3, @$); }
974+
| "{" expr comp_for_items "}" { $$ = SET_COMP_1($2, $3, @$); }
975+
| "{" expr ":" expr comp_for_items "}" { $$ = DICT_COMP_1($2, $4, $5, @$); }
976+
| "(" expr comp_for_items ")" { $$ = COMP_EXPR_1($2, $3, @$); }
977+
;
978+
971979
expr
972980
: id { $$ = $1; }
973981
| TK_INTEGER { $$ = INTEGER($1, @$); }
@@ -1028,12 +1036,7 @@ expr
10281036
| expr "or" expr { $$ = BOOLOP($1, Or, $3, @$); }
10291037
| "not" expr { $$ = UNARY($2, Not, @$); }
10301038

1031-
// Comprehension
1032-
| "[" expr comp_for_items "]" { $$ = LIST_COMP_1($2, $3, @$); }
1033-
| "{" expr comp_for_items "}" { $$ = SET_COMP_1($2, $3, @$); }
1034-
| "{" expr ":" expr comp_for_items "}" { $$ = DICT_COMP_1($2, $4, $5, @$); }
1035-
| "(" expr comp_for_items ")" { $$ = COMP_EXPR_1($2, $3, @$); }
1036-
1039+
| comprehension { $$ = $1; }
10371040
| ternary_if_statement { $$ = $1; }
10381041
| lambda_expression { $$ = $1; }
10391042
;

tests/parser/statements2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414

1515
if args[i][:1] in ['', '.']:
1616
pass
17+
18+
x = [[-c % self for c in reversed(T.rep.rep)][:-1]]
19+
20+
[x for x in G if self.ring.is_unit(x[0])][0]

tests/reference/ast_new-statements2-c4cdc5f.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "ast_new-statements2-c4cdc5f",
33
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
44
"infile": "tests/parser/statements2.py",
5-
"infile_hash": "582c1146456415a483654db39eb660b14593a6e72f353a61ed716b4b",
5+
"infile_hash": "e16da92e6b14ae0db259cf323ccb98390c5fcee8f199914e3f1a9525",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "ast_new-statements2-c4cdc5f.stdout",
9-
"stdout_hash": "d9c72edbe478ee87b62391369aba8651ca66af28000332f6a38eac37",
9+
"stdout_hash": "6c1548bf54cb810ff6bc25816b0a63484aa60192e6fe246df412cf50",
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 [(Expr (Call (Call (Call (Name getattr Load) [(Name x Load) (Name y Load)] []) [(Name a Load) (Name b Load)] []) [(ConstantInt 5 ())] [(a (Name a Load)) (b (ConstantInt 0 ()))])) (Expr (Call (Call (Name func Load) [(Name a Load) (Name _op Load)] []) [(Name s Load)] [])) (Expr (Call (Call (Name test Load) [] []) [] [])) (Assign [(Name a Store)] (Subscript (List [(ConstantInt 1 ()) (ConstantInt 2 ())] Load) (ConstantInt 0 ()) Load) ()) (Assign [(Tuple [(Name a Store) (Name b Store)] Store)] (Subscript (BinOp (List [(ConstantInt 1 ()) (ConstantInt 2 ())] Load) Add (List [(ConstantInt 0 ()) (ConstantInt 0 ())] Load)) (Slice () (ConstantInt 2 ()) ()) Load) ()) (Assign [(Tuple [(Name a Store) (Name b Store)] Store)] (Subscript (BinOp (Call (Attribute (Name c Load) d Load) [] []) Add (List [(ConstantInt 1 ()) (ConstantInt 0 ())] Load)) (Slice () () ()) Load) ()) (Expr (Subscript (Dict [(ConstantStr "a" ()) (ConstantStr "b" ())] [(Name a Load) (Name b Load)]) (Name val Load) Load)) (If (Compare (Subscript (Subscript (Name args Load) (Name i Load) Load) (Slice () (ConstantInt 1 ()) ()) Load) In [(List [(ConstantStr "" ()) (ConstantStr "." ())] Load)]) [(Pass)] [])] [])
1+
(Module [(Expr (Call (Call (Call (Name getattr Load) [(Name x Load) (Name y Load)] []) [(Name a Load) (Name b Load)] []) [(ConstantInt 5 ())] [(a (Name a Load)) (b (ConstantInt 0 ()))])) (Expr (Call (Call (Name func Load) [(Name a Load) (Name _op Load)] []) [(Name s Load)] [])) (Expr (Call (Call (Name test Load) [] []) [] [])) (Assign [(Name a Store)] (Subscript (List [(ConstantInt 1 ()) (ConstantInt 2 ())] Load) (ConstantInt 0 ()) Load) ()) (Assign [(Tuple [(Name a Store) (Name b Store)] Store)] (Subscript (BinOp (List [(ConstantInt 1 ()) (ConstantInt 2 ())] Load) Add (List [(ConstantInt 0 ()) (ConstantInt 0 ())] Load)) (Slice () (ConstantInt 2 ()) ()) Load) ()) (Assign [(Tuple [(Name a Store) (Name b Store)] Store)] (Subscript (BinOp (Call (Attribute (Name c Load) d Load) [] []) Add (List [(ConstantInt 1 ()) (ConstantInt 0 ())] Load)) (Slice () () ()) Load) ()) (Expr (Subscript (Dict [(ConstantStr "a" ()) (ConstantStr "b" ())] [(Name a Load) (Name b Load)]) (Name val Load) Load)) (If (Compare (Subscript (Subscript (Name args Load) (Name i Load) Load) (Slice () (ConstantInt 1 ()) ()) Load) In [(List [(ConstantStr "" ()) (ConstantStr "." ())] Load)]) [(Pass)] []) (Assign [(Name x Store)] (List [(Subscript (ListComp (BinOp (UnaryOp USub (Name c Load)) Mod (Name self Load)) [((Name c Store) (Call (Name reversed Load) [(Attribute (Attribute (Name T Load) rep Load) rep Load)] []) [] 0)]) (Slice () (UnaryOp USub (ConstantInt 1 ())) ()) Load)] Load) ()) (Expr (Subscript (ListComp (Name x Load) [((Name x Store) (Name G Load) [(Call (Attribute (Attribute (Name self Load) ring Load) is_unit Load) [(Subscript (Name x Load) (ConstantInt 0 ()) Load)] [])] 0)]) (ConstantInt 0 ()) Load))] [])

0 commit comments

Comments
 (0)