Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Parser] Parse lambda expressions #902

Merged
merged 2 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions src/lpython/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,12 @@ void yyerror(YYLTYPE *yyloc, LFortran::Parser &p, const std::string &msg)
%type <comp> comp_for
%type <vec_comp> comp_for_items
%type <ast> lambda_expression
%type <vec_arg> lambda_id_list
%type <args> lambda_parameter_list_opt
%type <arg> lambda_parameter
%type <fn_arg> lambda_parameter_list
%type <args_> lambda_parameter_list_no_posonly
%type <var_kw> lambda_parameter_list_starargs
%type <vec_arg> lambda_defparameter_list

// Precedence

Expand Down Expand Up @@ -873,16 +878,55 @@ string
| id TK_STRING { $$ = STRING3($1, $2, @$); }
;

lambda_id_list
: lambda_id_list "," id { $$ = $1; LIST_ADD($$, ARGS_01($3, @$)); }
| id { LIST_NEW($$); LIST_ADD($$, ARGS_01($1, @$)); }
lambda_parameter
: id { $$ = ARGS_01($1, @$); }
| id "=" expr { $$ = ARGS_03($1, $3, @$); }
;

lambda_defparameter_list
: lambda_defparameter_list "," lambda_parameter { $$ = $1; LIST_ADD($$, $3); }
| lambda_parameter { LIST_NEW($$); LIST_ADD($$, $1); }
;

lambda_parameter_list
: lambda_defparameter_list "," "/" comma_opt {
$$ = PARAMETER_LIST_01($1, nullptr); }
| lambda_defparameter_list "," "/" "," lambda_parameter_list_no_posonly {
$$ = PARAMETER_LIST_01($1, $5); }
| lambda_parameter_list_no_posonly { $$ = PARAMETER_LIST_02($1); }
;

lambda_parameter_list_no_posonly
: lambda_defparameter_list comma_opt { $$ = PARAMETER_LIST_03($1, nullptr); }
| lambda_defparameter_list "," lambda_parameter_list_starargs {
$$ = PARAMETER_LIST_03($1, $3); }
| lambda_parameter_list_starargs { $$ = PARAMETER_LIST_04($1); }
;

lambda_parameter_list_starargs
: "*" "," lambda_defparameter_list comma_opt { $$ = PARAMETER_LIST_05($3); }
| "*" "," "**" lambda_parameter comma_opt { $$ = PARAMETER_LIST_06($4); }
| "*" "," lambda_defparameter_list "," "**" lambda_parameter comma_opt {
$$ = PARAMETER_LIST_07($3, $6); }
| "*" lambda_parameter comma_opt { $$ = PARAMETER_LIST_08($2); }
| "*" lambda_parameter "," lambda_defparameter_list comma_opt {
$$ = PARAMETER_LIST_09($2, $4); }
| "*" lambda_parameter "," "**" lambda_parameter comma_opt {
$$ = PARAMETER_LIST_10($2, $5); }
| "*" lambda_parameter "," lambda_defparameter_list ","
"**" lambda_parameter comma_opt { $$ = PARAMETER_LIST_11($2, $4, $7); }
| "**" lambda_parameter comma_opt { $$ = PARAMETER_LIST_06($2); }
;

lambda_parameter_list_opt
: lambda_parameter_list { $$ = FUNC_ARGS_01(p.m_a, @$, $1); }
| %empty { $$ = PARAMETER_LIST_12(@$); }
;


lambda_expression
: KW_LAMBDA ":" expr %prec LAMBDA {
$$ = LAMBDA_01(PARAMETER_LIST_12(@$), $3, @$); }
| KW_LAMBDA lambda_id_list ":" expr %prec LAMBDA {
$$ = LAMBDA_01(FUNC_ARG_LIST_01($2, @$), $4, @$); }
: KW_LAMBDA lambda_parameter_list_opt ":" expr %prec LAMBDA {
$$ = LAMBDA_01($2, $4, @$); }
;

expr
Expand Down
36 changes: 0 additions & 36 deletions src/lpython/parser/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,42 +437,6 @@ static inline Args *FUNC_ARGS_01(Allocator &al, Location &l, Fn_Arg *parameters)
return r;
}

static inline Args *FUNC_ARGS(Allocator &al, Location &l,
Arg** m_posonlyargs, size_t n_posonlyargs,
Arg** m_args, size_t n_args,
Arg** m_vararg, size_t n_vararg,
Arg** m_kwonlyargs, size_t n_kwonlyargs,
Arg** m_kwarg, size_t n_kwarg) {
// TODO: Use parameter_list for LAMBDA instead of lambda_id_list,
// after which this function can be removed.
Args *r = al.allocate<Args>();
Vec<expr_t*> defaults;
defaults.reserve(al, 4);
Vec<expr_t*> kw_defaults;
kw_defaults.reserve(al, 4);

FUNC_ARGS1_(posonlyargs);
FUNC_ARGS1_(args);
FUNC_ARGS1_(vararg);
FUNC_ARGS1_(kwonlyargs);
FUNC_ARGS1_(kwarg);

r->arguments.loc = l;
FUNC_ARGS_(posonlyargs, false);
FUNC_ARGS_(args, false);
FUNC_ARGS_(vararg, false);
FUNC_ARGS_(kwonlyargs, true);
FUNC_ARGS_(kwarg, true);
r->arguments.m_kw_defaults = kw_defaults.p;
r->arguments.n_kw_defaults = kw_defaults.n;
r->arguments.m_defaults = defaults.p;
r->arguments.n_defaults = defaults.n;
return r;
}

#define FUNC_ARG_LIST_01(args, l) FUNC_ARGS(p.m_a, l, nullptr, 0, \
args.p, args.n, nullptr, 0, nullptr, 0, nullptr,0)

#define ARGS_01(arg, l) FUNC_ARG(p.m_a, l, \
name2char((ast_t *)arg), nullptr, nullptr)
#define ARGS_02(arg, annotation, l) FUNC_ARG(p.m_a, l, \
Expand Down
70 changes: 70 additions & 0 deletions tests/parser/lambda2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# AST tests only
verify_matching_signatures(lambda x=0: 0, lambda x=0: 0)

input_func = lambda prompt="": next(gen)

cast[key] = lambda x, k=key: array(x, copy=False).astype(k)

x = lambda func=self._try_call,attr=attr : func(attr)

fpos32 = lambda x, **k: np.format_float_positional(np.float32(x), **k)

m = lambda self, *args, **kw: func(self, *args, **kw)

lambda *x, y, z: test

lambda *x, y, z, **args: test

lambda *x, **args: test

lambda **args: test

lambda x, y: test

lambda x, *y: test

lambda x, *y, z: test

lambda x, *y, z, **args: test

lambda x, *y, **args: test

lambda x, **args: test

lambda x, *y, z, **args: test

lambda a, /, b, c: test

lambda a, /, *b: test

lambda a, /, *b, c, d: test

lambda a, /, *b, c, **d: test

lambda a, /, **b: test

lambda a, /, *b, **c: test

lambda a, /, b, c, *d: test

lambda a, /, b, c, **d: test

lambda a, /, b, c, *d, **e: test

lambda a, /, b, c, *d, e, **f: test

lambda *, a: test

lambda *, a, **b: test

lambda a, *, b: test

lambda a, *, b, **c: test

lambda a, /, *, b: test

lambda a, /, b, *, c: test

lambda a, /, *, c, **d: test

lambda a, /, b, *, c, **d: test
13 changes: 13 additions & 0 deletions tests/reference/ast_new-lambda2-d84336e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "ast_new-lambda2-d84336e",
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
"infile": "tests/parser/lambda2.py",
"infile_hash": "fd8ff7be24e81b33516b111731ced148b85298a647ee2a0a0b571cfa",
"outfile": null,
"outfile_hash": null,
"stdout": "ast_new-lambda2-d84336e.stdout",
"stdout_hash": "02af3cdbd330aef51113ef12f054e2b544b96dc68d58b7b5cbc24423",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
1 change: 1 addition & 0 deletions tests/reference/ast_new-lambda2-d84336e.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(Module [(Expr (Call (Name verify_matching_signatures Load) [(Lambda ([] [(x () ())] [] [] [] [] [(ConstantInt 0 ())]) (ConstantInt 0 ())) (Lambda ([] [(x () ())] [] [] [] [] [(ConstantInt 0 ())]) (ConstantInt 0 ()))] [])) (Assign [(Name input_func Store)] (Lambda ([] [(prompt () ())] [] [] [] [] [(ConstantStr "" ())]) (Call (Name next Load) [(Name gen Load)] [])) ()) (Assign [(Subscript (Name cast Load) (Name key Load) Store)] (Lambda ([] [(x () ()) (k () ())] [] [] [] [] [(Name key Load)]) (Call (Attribute (Call (Name array Load) [(Name x Load)] [(copy (ConstantBool .false. ()))]) astype Load) [(Name k Load)] [])) ()) (Assign [(Name x Store)] (Lambda ([] [(func () ()) (attr () ())] [] [] [] [] [(Attribute (Name self Load) _try_call Load) (Name attr Load)]) (Call (Name func Load) [(Name attr Load)] [])) ()) (Assign [(Name fpos32 Store)] (Lambda ([] [(x () ())] [] [] [] [(k () ())] []) (Call (Attribute (Name np Load) format_float_positional Load) [(Call (Attribute (Name np Load) float32 Load) [(Name x Load)] [])] [(() (Name k Load))])) ()) (Assign [(Name m Store)] (Lambda ([] [(self () ())] [(args () ())] [] [] [(kw () ())] []) (Call (Name func Load) [(Name self Load) (Starred (Name args Load) Load)] [(() (Name kw Load))])) ()) (Expr (Lambda ([] [] [(x () ())] [(y () ()) (z () ())] [] [] []) (Name test Load))) (Expr (Lambda ([] [] [(x () ())] [(y () ()) (z () ())] [] [(args () ())] []) (Name test Load))) (Expr (Lambda ([] [] [(x () ())] [] [] [(args () ())] []) (Name test Load))) (Expr (Lambda ([] [] [] [] [] [(args () ())] []) (Name test Load))) (Expr (Lambda ([] [(x () ()) (y () ())] [] [] [] [] []) (Name test Load))) (Expr (Lambda ([] [(x () ())] [(y () ())] [] [] [] []) (Name test Load))) (Expr (Lambda ([] [(x () ())] [(y () ())] [(z () ())] [] [] []) (Name test Load))) (Expr (Lambda ([] [(x () ())] [(y () ())] [(z () ())] [] [(args () ())] []) (Name test Load))) (Expr (Lambda ([] [(x () ())] [(y () ())] [] [] [(args () ())] []) (Name test Load))) (Expr (Lambda ([] [(x () ())] [] [] [] [(args () ())] []) (Name test Load))) (Expr (Lambda ([] [(x () ())] [(y () ())] [(z () ())] [] [(args () ())] []) (Name test Load))) (Expr (Lambda ([(a () ())] [(b () ()) (c () ())] [] [] [] [] []) (Name test Load))) (Expr (Lambda ([(a () ())] [] [(b () ())] [] [] [] []) (Name test Load))) (Expr (Lambda ([(a () ())] [] [(b () ())] [(c () ()) (d () ())] [] [] []) (Name test Load))) (Expr (Lambda ([(a () ())] [] [(b () ())] [(c () ())] [] [(d () ())] []) (Name test Load))) (Expr (Lambda ([(a () ())] [] [] [] [] [(b () ())] []) (Name test Load))) (Expr (Lambda ([(a () ())] [] [(b () ())] [] [] [(c () ())] []) (Name test Load))) (Expr (Lambda ([(a () ())] [(b () ()) (c () ())] [(d () ())] [] [] [] []) (Name test Load))) (Expr (Lambda ([(a () ())] [(b () ()) (c () ())] [] [] [] [(d () ())] []) (Name test Load))) (Expr (Lambda ([(a () ())] [(b () ()) (c () ())] [(d () ())] [] [] [(e () ())] []) (Name test Load))) (Expr (Lambda ([(a () ())] [(b () ()) (c () ())] [(d () ())] [(e () ())] [] [(f () ())] []) (Name test Load))) (Expr (Lambda ([] [] [] [(a () ())] [] [] []) (Name test Load))) (Expr (Lambda ([] [] [] [(a () ())] [] [(b () ())] []) (Name test Load))) (Expr (Lambda ([] [(a () ())] [] [(b () ())] [] [] []) (Name test Load))) (Expr (Lambda ([] [(a () ())] [] [(b () ())] [] [(c () ())] []) (Name test Load))) (Expr (Lambda ([(a () ())] [] [] [(b () ())] [] [] []) (Name test Load))) (Expr (Lambda ([(a () ())] [(b () ())] [] [(c () ())] [] [] []) (Name test Load))) (Expr (Lambda ([(a () ())] [] [] [(c () ())] [] [(d () ())] []) (Name test Load))) (Expr (Lambda ([(a () ())] [(b () ())] [] [(c () ())] [] [(d () ())] []) (Name test Load)))] [])
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ ast_new = true
filename = "parser/lambda1.py"
ast_new = true

[[test]]
filename = "parser/lambda2.py"
ast_new = true

[[test]]
filename = "parser/tuple1.py"
ast_new = true
Expand Down