Skip to content

Commit

Permalink
Merge pull request #506 from Thirumalai-Shaktivel/delete
Browse files Browse the repository at this point in the history
Parse other cases of delete and return statement
  • Loading branch information
certik authored May 19, 2022
2 parents e389389 + 82cb5d7 commit 3e0e0ec
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
17 changes: 10 additions & 7 deletions src/lpython/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ void yyerror(YYLTYPE *yyloc, LFortran::Parser &p, const std::string &msg)
%type <ast> tuple_item
%type <ast> ann_assignment_statement
%type <ast> delete_statement
%type <vec_ast> del_target_list
%type <ast> return_statement
%type <ast> expression_statment
%type <vec_ast> module
Expand Down Expand Up @@ -351,6 +350,8 @@ assert_statement

tuple_item
: expr_list { $$ = TUPLE_01($1, @$); }
| expr_list "," { $$ = TUPLE_03($1, @$); }
| "(" expr_list "," ")" { $$ = TUPLE_03($2, @$); }
| "(" expr_list "," expr ")" { $$ = TUPLE_01(TUPLE_($2, $4), @$); }
;

Expand Down Expand Up @@ -387,18 +388,20 @@ ann_assignment_statement
| expr ":" expr "=" expr { $$ = ANNASSIGN_02($1, $3, $5, @$); }
;

del_target_list
: del_target_list "," expr { $$ = $1; LIST_ADD($$, $3); }
| expr { LIST_NEW($$); LIST_ADD($$, $1); }
;

delete_statement
: KW_DEL del_target_list { $$ = DELETE($2, @$); }
: KW_DEL expr_list { $$ = DELETE_01($2, @$); }
| KW_DEL expr_list "," { $$ = DELETE_01($2, @$); }
| KW_DEL "(" ")" { $$ = DELETE_02(@$); }
| KW_DEL "(" expr_list "," ")" {
$$ = DELETE_03(SET_EXPR_CTX_02($3, Del), @$); }
| KW_DEL "(" expr_list "," expr ")" {
$$ = DELETE_03(SET_EXPR_CTX_02(TUPLE_($3, $5), Del), @$); }
;

return_statement
: KW_RETURN { $$ = RETURN_01(@$); }
| KW_RETURN tuple_item { $$ = RETURN_02($2, @$); }
| KW_RETURN "(" ")" { $$ = RETURN_03(@$); }
;

module
Expand Down
40 changes: 32 additions & 8 deletions src/lpython/parser/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,18 @@ static inline Vec<ast_t*> SET_EXPR_CTX_02(Vec<ast_t*> x, expr_contextType ctx) {
#define ANNASSIGN_02(x, y, val, l) make_AnnAssign_t(p.m_a, l, \
EXPR(SET_EXPR_CTX_01(x, Store)), EXPR(y), EXPR(val), 1)

#define DELETE(e, l) make_Delete_t(p.m_a, l, \
#define DELETE_01(e, l) make_Delete_t(p.m_a, l, \
EXPRS(SET_EXPR_CTX_02(e, Del)), e.size())
#define DELETE_02(l) make_Delete_t(p.m_a, l, \
EXPRS(A2LIST(p.m_a, SET_EXPR_CTX_01(TUPLE_EMPTY(l), Del))), 1)
#define DELETE_03(e, l) make_Delete_t(p.m_a, l, \
EXPRS(A2LIST(p.m_a, SET_EXPR_CTX_01(TUPLE_01(e, l), Del))), 1)

#define EXPR_01(e, l) make_Expr_t(p.m_a, l, EXPR(e))

#define RETURN_01(l) make_Return_t(p.m_a, l, nullptr)
#define RETURN_02(e, l) make_Return_t(p.m_a, l, EXPR(e))
#define RETURN_03(l) make_Return_t(p.m_a, l, EXPR(TUPLE_EMPTY(l)))

#define PASS(l) make_Pass_t(p.m_a, l)
#define BREAK(l) make_Break_t(p.m_a, l)
Expand All @@ -134,18 +139,37 @@ static inline Vec<ast_t*> SET_EXPR_CTX_02(Vec<ast_t*> x, expr_contextType ctx) {
#define NON_LOCAL(names, l) make_Nonlocal_t(p.m_a, l, \
REDUCE_ARGS(p.m_a, names), names.size())

static inline ast_t *SET_STORE_01(ast_t *x) {
if(is_a<Tuple_t>(*EXPR(x))) {
size_t n_elts = down_cast2<Tuple_t>(x)->n_elts;
for(size_t i=0; i < n_elts; i++) {
SET_EXPR_CTX_01(
(ast_t *) down_cast2<Tuple_t>(x)->m_elts[i], Store);
}
}
return x;
}
static inline Vec<ast_t*> SET_STORE_02(Vec<ast_t*> x) {
for (size_t i=0; i < x.size(); i++) {
SET_STORE_01(x[i]);
}
return x;
}
#define ASSIGNMENT(targets, val, l) make_Assign_t(p.m_a, l, \
EXPRS(SET_EXPR_CTX_02(targets, Store)), targets.size(), \
EXPRS(SET_EXPR_CTX_02(SET_STORE_02(targets), Store)), targets.size(), \
EXPR(val), nullptr)

static inline ast_t* TUPLE_02(Allocator &al, Location &l, Vec<ast_t*> elts) {
if(is_a<expr_t>(*elts[0]) && elts.size() == 1) {
return (ast_t*) elts[0];
}
return make_Tuple_t(al, l, EXPRS(SET_EXPR_CTX_02(elts, Store)), elts.size(),
expr_contextType::Store);
return make_Tuple_t(al, l, EXPRS(elts), elts.size(), expr_contextType::Load);
}
#define TUPLE_01(elts, l) TUPLE_02(p.m_a, l, elts)
#define TUPLE_03(elts, l) make_Tuple_t(p.m_a, l, \
EXPRS(elts), elts.size(), expr_contextType::Load);
#define TUPLE_EMPTY(l) make_Tuple_t(p.m_a, l, \
nullptr, 0, expr_contextType::Load)

Vec<ast_t*> TUPLE_APPEND(Allocator &al, Vec<ast_t*> x, ast_t *y) {
Vec<ast_t*> v;
Expand Down Expand Up @@ -213,10 +237,10 @@ int dot_count = 0;
EXPR(e), STMTS(stmt), stmt.size(), STMTS(A2LIST(p.m_a, orelse)), 1)

#define FOR_01(target, iter, stmts, l) make_For_t(p.m_a, l, \
EXPR(SET_EXPR_CTX_01(target, Store)), EXPR(iter), \
EXPR(SET_EXPR_CTX_01(SET_STORE_01(target), Store)), EXPR(iter), \
STMTS(stmts), stmts.size(), nullptr, 0, nullptr)
#define FOR_02(target, iter, stmts, orelse, l) make_For_t(p.m_a, l, \
EXPR(SET_EXPR_CTX_01(target, Store)), EXPR(iter), \
EXPR(SET_EXPR_CTX_01(SET_STORE_01(target), Store)), EXPR(iter), \
STMTS(stmts), stmts.size(), STMTS(orelse), orelse.size(), nullptr)

#define TRY_01(stmts, except, l) make_Try_t(p.m_a, l, \
Expand Down Expand Up @@ -364,10 +388,10 @@ static inline Args *FUNC_ARGS(Allocator &al, Location &l,
STMTS(stmts), stmts.size(), nullptr, 0, EXPR(return), nullptr)

#define ASYNC_FOR_01(target, iter, stmts, l) make_AsyncFor_t(p.m_a, l, \
EXPR(SET_EXPR_CTX_01(target, Store)), EXPR(iter), \
EXPR(SET_EXPR_CTX_01(SET_STORE_01(target), Store)), EXPR(iter), \
STMTS(stmts), stmts.size(), nullptr, 0, nullptr)
#define ASYNC_FOR_02(target, iter, stmts, orelse, l) make_AsyncFor_t(p.m_a, l, \
EXPR(SET_EXPR_CTX_01(target, Store)), EXPR(iter), \
EXPR(SET_EXPR_CTX_01(SET_STORE_01(target), Store)), EXPR(iter), \
STMTS(stmts), stmts.size(), STMTS(orelse), orelse.size(), nullptr)

#define ASYNC_WITH(items, body, l) make_AsyncWith_t(p.m_a, l, \
Expand Down
12 changes: 12 additions & 0 deletions tests/parser/statements1.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,30 @@
x = y = 1
x, y = 1, 2
x[i] = (1, 2)
x, y, z = t
(x, y, z) = t
x, = t
(x,) = t

x += 1

x: i64
y: i32 = 1

del x
del ()
del (x, y)
del (x, y,)
del x, y,
del x, y

return
return a + b
return x(a)
return ()
return (x, y)
return (x, y, )
return x, y,
return x, y

global a
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/ast_new-statements1-e081093.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "ast_new-statements1-e081093",
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
"infile": "tests/parser/statements1.py",
"infile_hash": "9110211f2f42a7add5e5f1d8d27205c9b52f90a922e82c5f1c47fabc",
"infile_hash": "7db69bcb75890d3456083fca500a074cd0f410a3b6333dbe68bc8543",
"outfile": null,
"outfile_hash": null,
"stdout": "ast_new-statements1-e081093.stdout",
"stdout_hash": "fbd2a56fb302bc7ecc52f37193f7646e3eb3c0e78d18d19c451b7427",
"stdout_hash": "212ee41d38af63bdaf1402f51a9f611bcc292462f82af27515b92f7b",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/ast_new-statements1-e081093.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(Module [(Pass) (Break) (Continue) (Raise () ()) (Raise (Call (Name NameError Load) [(ConstantStr "String" ())] []) ()) (Raise (Name RuntimeError Load) (Name exc Load)) (Assert (Compare (Call (Name len Load) [(Name marks Load)] []) NotEq [(ConstantInt 0 ())]) (ConstantStr "List is empty." ())) (Assert (Compare (Name x Load) Eq [(ConstantStr "String" ())]) ()) (Assign [(Name x Store)] (ConstantInt 1 ()) ()) (Assign [(Tuple [(Name x Store) (Name y Store)] Store)] (Call (Name x Load) [] []) ()) (Assign [(Name x Store) (Name y Store)] (ConstantInt 1 ()) ()) (Assign [(Tuple [(Name x Store) (Name y Store)] Store)] (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ())] Store) ()) (Assign [(Subscript (Name x Load) (Name i Load) Store)] (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ())] Store) ()) (AugAssign (Name x Store) Add (ConstantInt 1 ())) (AnnAssign (Name x Store) (Name i64 Load) () 1) (AnnAssign (Name y Store) (Name i32 Load) (ConstantInt 1 ()) 1) (Delete [(Name x Del)]) (Delete [(Name x Del) (Name y Del)]) (Return ()) (Return (BinOp (Name a Load) Add (Name b Load))) (Return (Call (Name x Load) [(Name a Load)] [])) (Return (Tuple [(Name x Store) (Name y Store)] Store)) (Global [a]) (Global [a b]) (Nonlocal [a]) (Nonlocal [a b]) (Expr (ConstantInt 123 ())) (Expr (UnaryOp USub (ConstantInt 123 ()))) (Expr (UnaryOp USub (ConstantInt 291 ()))) (Expr (ConstantInt 6844 ())) (Expr (UnaryOp USub (ConstantInt 83 ()))) (Expr (ConstantInt 87 ())) (Expr (UnaryOp USub (ConstantInt 13 ()))) (Expr (ConstantInt 13 ())) (Expr (ConstantFloat 123.000000 ())) (Expr (ConstantFloat 123.450000 ())) (Expr (ConstantFloat 123400000000.000000 ())) (Expr (BinOp (ConstantInt 12 ()) Add (ConstantComplex 0.000000 3.000000 ()))) (Expr (BinOp (ConstantFloat 0.120000 ()) Add (ConstantComplex 0.000000 0.001000 ()))) (Expr (ConstantStr "String" ())) (Expr (ConstantStr "String String" ())) (Expr (ConstantStr "String String" ())) (Expr (ConstantStr "String String" ())) (Expr (Subscript (ConstantStr "String String" ()) (Slice (ConstantInt 1 ()) () ()) Load)) (Assign [(Name x Store)] (ConstantStr "String String" ()) ()) (Assign [(Name x Store)] (BinOp (ConstantStr "String " ()) Add (ConstantStr "String" ())) ()) (Assign [(Name x Store)] (ConstantStr "String String" ()) ()) (Assign [(Name x Store)] (ConstantStr "String String" ()) ()) (Assign [(Name x Store)] (ConstantStr "String " ()) ()) (Expr (ConstantStr "String" ())) (Expr (ConstantBool .true. ())) (Expr (ConstantBool .false. ())) (Expr (BinOp (BinOp (Name x Load) Add (Name y Load)) Mult (Name z Load))) (Expr (BinOp (Name x Load) Sub (Name y Load))) (Expr (BinOp (Name x Load) Mult (Name y Load))) (Expr (BinOp (Name x Load) Div (Name y Load))) (Expr (BinOp (Name x Load) Mod (Name y Load))) (Expr (UnaryOp USub (Name y Load))) (Expr (UnaryOp UAdd (Name y Load))) (Expr (UnaryOp Invert (Name y Load))) (Expr (BinOp (Name x Load) Pow (Name y Load))) (Expr (BinOp (Name x Load) FloorDiv (Name y Load))) (Expr (BinOp (Name x Load) MatMult (Name y Load))) (Expr (BinOp (Name x Load) BitAnd (Name y Load))) (Expr (BinOp (Name x Load) BitOr (Name y Load))) (Expr (BinOp (Name x Load) BitXor (Name y Load))) (Expr (BinOp (Name x Load) LShift (Name y Load))) (Expr (BinOp (Name x Load) RShift (Name y Load))) (Expr (Compare (Name x Load) Eq [(Name y Load)])) (Expr (Compare (Name x Load) NotEq [(Name y Load)])) (Expr (Compare (Name x Load) Lt [(Name y Load)])) (Expr (Compare (Name x Load) LtE [(Name y Load)])) (Expr (Compare (Name x Load) Gt [(Name y Load)])) (Expr (Compare (Name x Load) GtE [(Name y Load)])) (AnnAssign (Name i Store) (Name i32 Load) (ConstantInt 4 ()) 1) (If (Compare (ConstantInt 2 ()) Gt [(Name i Load)]) [(Pass)] []) (If (Compare (Name i Load) Gt [(ConstantInt 5 ())]) [(Break)] []) (If (BoolOp And [(Compare (Name i Load) Eq [(ConstantInt 5 ())]) (Compare (Name i Load) Lt [(ConstantInt 10 ())])]) [(Assign [(Name i Store)] (ConstantInt 3 ()) ())] []) (For (Name i Store) (Call (Name range Load) [(Name N Load)] []) [(Assign [(Subscript (Name c Load) (Name i Load) Store)] (BinOp (Subscript (Name a Load) (Name i Load) Load) Add (BinOp (Name scalar Load) Mult (Subscript (Name b Load) (Name i Load) Load))) ())] [] ()) (Assign [(Name x Store)] (NamedExpr (Name y Store) (ConstantInt 0 ())) ()) (If (NamedExpr (Name a Store) (Call (Name ord Load) [(ConstantStr "3" ())] [])) [(Assign [(Name x Store)] (ConstantInt 1 ()) ())] []) (Assign [(Name a Store)] (Set [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())]) ())] [])
(Module [(Pass) (Break) (Continue) (Raise () ()) (Raise (Call (Name NameError Load) [(ConstantStr "String" ())] []) ()) (Raise (Name RuntimeError Load) (Name exc Load)) (Assert (Compare (Call (Name len Load) [(Name marks Load)] []) NotEq [(ConstantInt 0 ())]) (ConstantStr "List is empty." ())) (Assert (Compare (Name x Load) Eq [(ConstantStr "String" ())]) ()) (Assign [(Name x Store)] (ConstantInt 1 ()) ()) (Assign [(Tuple [(Name x Store) (Name y Store)] Store)] (Call (Name x Load) [] []) ()) (Assign [(Name x Store) (Name y Store)] (ConstantInt 1 ()) ()) (Assign [(Tuple [(Name x Store) (Name y Store)] Store)] (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ())] Load) ()) (Assign [(Subscript (Name x Load) (Name i Load) Store)] (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ())] Load) ()) (Assign [(Tuple [(Name x Store) (Name y Store) (Name z Store)] Store)] (Name t Load) ()) (Assign [(Tuple [(Name x Store) (Name y Store) (Name z Store)] Store)] (Name t Load) ()) (Assign [(Tuple [(Name x Store)] Store)] (Name t Load) ()) (Assign [(Tuple [(Name x Store)] Store)] (Name t Load) ()) (AugAssign (Name x Store) Add (ConstantInt 1 ())) (AnnAssign (Name x Store) (Name i64 Load) () 1) (AnnAssign (Name y Store) (Name i32 Load) (ConstantInt 1 ()) 1) (Delete [(Name x Del)]) (Delete [(Tuple [] Del)]) (Delete [(Tuple [(Name x Del) (Name y Del)] Del)]) (Delete [(Tuple [(Name x Del) (Name y Del)] Del)]) (Delete [(Name x Del) (Name y Del)]) (Delete [(Name x Del) (Name y Del)]) (Return ()) (Return (BinOp (Name a Load) Add (Name b Load))) (Return (Call (Name x Load) [(Name a Load)] [])) (Return (Tuple [] Load)) (Return (Tuple [(Name x Load) (Name y Load)] Load)) (Return (Tuple [(Name x Load) (Name y Load)] Load)) (Return (Tuple [(Name x Load) (Name y Load)] Load)) (Return (Tuple [(Name x Load) (Name y Load)] Load)) (Global [a]) (Global [a b]) (Nonlocal [a]) (Nonlocal [a b]) (Expr (ConstantInt 123 ())) (Expr (UnaryOp USub (ConstantInt 123 ()))) (Expr (UnaryOp USub (ConstantInt 291 ()))) (Expr (ConstantInt 6844 ())) (Expr (UnaryOp USub (ConstantInt 83 ()))) (Expr (ConstantInt 87 ())) (Expr (UnaryOp USub (ConstantInt 13 ()))) (Expr (ConstantInt 13 ())) (Expr (ConstantFloat 123.000000 ())) (Expr (ConstantFloat 123.450000 ())) (Expr (ConstantFloat 123400000000.000000 ())) (Expr (BinOp (ConstantInt 12 ()) Add (ConstantComplex 0.000000 3.000000 ()))) (Expr (BinOp (ConstantFloat 0.120000 ()) Add (ConstantComplex 0.000000 0.001000 ()))) (Expr (ConstantStr "String" ())) (Expr (ConstantStr "String String" ())) (Expr (ConstantStr "String String" ())) (Expr (ConstantStr "String String" ())) (Expr (Subscript (ConstantStr "String String" ()) (Slice (ConstantInt 1 ()) () ()) Load)) (Assign [(Name x Store)] (ConstantStr "String String" ()) ()) (Assign [(Name x Store)] (BinOp (ConstantStr "String " ()) Add (ConstantStr "String" ())) ()) (Assign [(Name x Store)] (ConstantStr "String String" ()) ()) (Assign [(Name x Store)] (ConstantStr "String String" ()) ()) (Assign [(Name x Store)] (ConstantStr "String " ()) ()) (Expr (ConstantStr "String" ())) (Expr (ConstantBool .true. ())) (Expr (ConstantBool .false. ())) (Expr (BinOp (BinOp (Name x Load) Add (Name y Load)) Mult (Name z Load))) (Expr (BinOp (Name x Load) Sub (Name y Load))) (Expr (BinOp (Name x Load) Mult (Name y Load))) (Expr (BinOp (Name x Load) Div (Name y Load))) (Expr (BinOp (Name x Load) Mod (Name y Load))) (Expr (UnaryOp USub (Name y Load))) (Expr (UnaryOp UAdd (Name y Load))) (Expr (UnaryOp Invert (Name y Load))) (Expr (BinOp (Name x Load) Pow (Name y Load))) (Expr (BinOp (Name x Load) FloorDiv (Name y Load))) (Expr (BinOp (Name x Load) MatMult (Name y Load))) (Expr (BinOp (Name x Load) BitAnd (Name y Load))) (Expr (BinOp (Name x Load) BitOr (Name y Load))) (Expr (BinOp (Name x Load) BitXor (Name y Load))) (Expr (BinOp (Name x Load) LShift (Name y Load))) (Expr (BinOp (Name x Load) RShift (Name y Load))) (Expr (Compare (Name x Load) Eq [(Name y Load)])) (Expr (Compare (Name x Load) NotEq [(Name y Load)])) (Expr (Compare (Name x Load) Lt [(Name y Load)])) (Expr (Compare (Name x Load) LtE [(Name y Load)])) (Expr (Compare (Name x Load) Gt [(Name y Load)])) (Expr (Compare (Name x Load) GtE [(Name y Load)])) (AnnAssign (Name i Store) (Name i32 Load) (ConstantInt 4 ()) 1) (If (Compare (ConstantInt 2 ()) Gt [(Name i Load)]) [(Pass)] []) (If (Compare (Name i Load) Gt [(ConstantInt 5 ())]) [(Break)] []) (If (BoolOp And [(Compare (Name i Load) Eq [(ConstantInt 5 ())]) (Compare (Name i Load) Lt [(ConstantInt 10 ())])]) [(Assign [(Name i Store)] (ConstantInt 3 ()) ())] []) (For (Name i Store) (Call (Name range Load) [(Name N Load)] []) [(Assign [(Subscript (Name c Load) (Name i Load) Store)] (BinOp (Subscript (Name a Load) (Name i Load) Load) Add (BinOp (Name scalar Load) Mult (Subscript (Name b Load) (Name i Load) Load))) ())] [] ()) (Assign [(Name x Store)] (NamedExpr (Name y Store) (ConstantInt 0 ())) ()) (If (NamedExpr (Name a Store) (Call (Name ord Load) [(ConstantStr "3" ())] [])) [(Assign [(Name x Store)] (ConstantInt 1 ()) ())] []) (Assign [(Name a Store)] (Set [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())]) ())] [])

0 comments on commit 3e0e0ec

Please sign in to comment.