Skip to content

Commit

Permalink
Add initial support for multiple single line stmts in one line.
Browse files Browse the repository at this point in the history
  • Loading branch information
akshanshbhatt committed Aug 4, 2022
1 parent ead3e00 commit ee875ad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
29 changes: 22 additions & 7 deletions src/lpython/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ void yyerror(YYLTYPE *yyloc, LFortran::Parser &p, const std::string &msg)
%type <ast> tuple_list
%type <ast> statement
%type <vec_ast> statements
%type <vec_ast> single_line_statements
%type <vec_ast> statements1
%type <vec_ast> single_line_multi_statements
%type <vec_ast> single_line_multi_statements_opt
%type <ast> single_line_statement
%type <ast> multi_line_statement
%type <ast> augassign_statement
Expand All @@ -195,7 +198,6 @@ void yyerror(YYLTYPE *yyloc, LFortran::Parser &p, const std::string &msg)
%type <ast> assert_statement
%type <ast> import_statement
%type <ast> global_statement
%type <ast> if_statement_single
%type <ast> nonlocal_statement
%type <ast> assignment_statement
%type <vec_ast> target_list
Expand Down Expand Up @@ -306,6 +308,23 @@ statements1
| statement { LIST_NEW($$); LIST_ADD($$, $1); }
;

single_line_statements
: single_line_multi_statements TK_NEWLINE { $$ = $1; }
| single_line_multi_statements TK_COMMENT TK_NEWLINE { $$ = $1; }
| single_line_statement TK_NEWLINE { $$ = A2LIST(p.m_a, $1); }
| single_line_statement TK_COMMENT TK_NEWLINE { $$ = A2LIST(p.m_a, $1); }
;

single_line_multi_statements
: single_line_multi_statements_opt single_line_statement { $$ = $1; LIST_ADD($$, $2); }
| single_line_multi_statements_opt single_line_statement ";" { $$ = $1; LIST_ADD($$, $2); }
;

single_line_multi_statements_opt
: single_line_multi_statements_opt single_line_statement ";" { $$ = $1; LIST_ADD($$, $2); }
| single_line_statement ";" { LIST_NEW($$); LIST_ADD($$, $1); }
;

statement
: single_line_statement sep { $$ = $1; }
| multi_line_statement
Expand All @@ -326,7 +345,6 @@ single_line_statement
| continue_statement
| import_statement
| global_statement
| if_statement_single
| nonlocal_statement
;

Expand Down Expand Up @@ -468,10 +486,6 @@ global_statement
: KW_GLOBAL expr_list { $$ = GLOBAL($2, @$); }
;

if_statement_single
: KW_IF expr TK_COLON single_line_statement { $$ = IF_01($2, $4, @$); }
;

ternary_if_statement
: expr KW_IF expr KW_ELSE expr { $$ = TERNARY($3, $1, $5, @$); }
;
Expand All @@ -489,7 +503,8 @@ elif_statement
;

if_statement
: KW_IF expr ":" sep statements { $$ = IF_STMT_01($2, $5, @$); }
: KW_IF expr ":" single_line_statements { $$ = IF_STMT_01($2, $4, @$); }
| KW_IF expr ":" sep statements { $$ = IF_STMT_01($2, $5, @$); }
| KW_IF expr ":" sep statements KW_ELSE ":" sep statements {
$$ = IF_STMT_02($2, $5, $9, @$); }
| KW_IF expr ":" sep statements elif_statement {
Expand Down
7 changes: 0 additions & 7 deletions src/lpython/parser/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,6 @@ int dot_count = 0;
mod2char(p.m_a, module), names.p, names.size(), dot_count); \
dot_count = 0

#define IF_01(test, body, l) make_If_t(p.m_a, l, \
/*test*/ EXPR(test), \
/*body*/ STMTS(A2LIST(p.m_a, body)), \
/*n_body*/ 1, \
/*a_orelse*/ nullptr, \
/*n_orelse*/ 0)

#define IF_STMT_01(e, stmt, l) make_If_t(p.m_a, l, \
EXPR(e), STMTS(stmt), stmt.size(), nullptr, 0)
#define IF_STMT_02(e, stmt, orelse, l) make_If_t(p.m_a, l, \
Expand Down

0 comments on commit ee875ad

Please sign in to comment.