Skip to content

Commit b82d617

Browse files
Merge pull request #850 from akshanshbhatt/pr_star_args
2 parents 9de1f35 + 8cd2b17 commit b82d617

File tree

5 files changed

+55
-12
lines changed

5 files changed

+55
-12
lines changed

src/lpython/parser/parser.yy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,13 @@ parameter_list_starargs
616616
| defparameter_list "," "/" "," defparameter_list "," "*" parameter
617617
"," defparameter_list "," "**" parameter {
618618
$$ = STAR_ARGS_20($1, $5, $8, $10, $13, @$); }
619+
| defparameter_list "," "/" "," "*" "," defparameter_list {
620+
$$ = STAR_ARGS_21($1, $7, @$); }
621+
| defparameter_list "," "/" "," defparameter_list ","
622+
"*" "," defparameter_list { $$ = STAR_ARGS_22($1, $5, $9, @$); }
623+
| defparameter_list "," "*" "," defparameter_list {
624+
$$ = STAR_ARGS_23($1, $5, @$); }
625+
| "*" "," defparameter_list { $$ = STAR_ARGS_24($3, @$); }
619626
;
620627

621628
parameter_list_opt

src/lpython/parser/semantics.h

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,16 @@ Arg** ARG2LIST(Allocator &al, Arg *x) {
376376
return v;
377377
}
378378

379-
#define FUNC_ARGS_(x) \
379+
#define FUNC_ARGS_(x, kw) \
380380
Vec<arg_t> _m_##x; \
381381
_m_##x.reserve(al, 4); \
382382
if(n_##x > 0) { \
383383
for(size_t i = 0; i < n_##x; i++) { \
384384
_m_##x.push_back(al, m_##x[i]->_arg); \
385-
if(m_##x[i]->default_value) { \
385+
if(m_##x[i]->default_value && !kw) { \
386386
defaults.push_back(al, m_##x[i]->defaults); \
387+
} else if (m_##x[i]->default_value){ \
388+
kw_defaults.push_back(al, m_##x[i]->defaults); \
387389
} \
388390
} \
389391
}
@@ -397,12 +399,14 @@ static inline Args *FUNC_ARGS(Allocator &al, Location &l,
397399
Args *r = al.allocate<Args>();
398400
Vec<expr_t*> defaults;
399401
defaults.reserve(al, 4);
402+
Vec<expr_t*> kw_defaults;
403+
kw_defaults.reserve(al, 4);
400404

401-
FUNC_ARGS_(posonlyargs);
402-
FUNC_ARGS_(args);
403-
FUNC_ARGS_(vararg);
404-
FUNC_ARGS_(kwonlyargs);
405-
FUNC_ARGS_(kwarg);
405+
FUNC_ARGS_(posonlyargs, false);
406+
FUNC_ARGS_(args, false);
407+
FUNC_ARGS_(vararg, false);
408+
FUNC_ARGS_(kwonlyargs, true);
409+
FUNC_ARGS_(kwarg, true);
406410

407411
r->arguments.loc = l;
408412
r->arguments.m_posonlyargs = _m_posonlyargs.p;
@@ -413,8 +417,8 @@ static inline Args *FUNC_ARGS(Allocator &al, Location &l,
413417
r->arguments.n_vararg = _m_vararg.n;
414418
r->arguments.m_kwonlyargs = _m_kwonlyargs.p;
415419
r->arguments.n_kwonlyargs = _m_kwonlyargs.n;
416-
r->arguments.m_kw_defaults = nullptr;
417-
r->arguments.n_kw_defaults = 0;
420+
r->arguments.m_kw_defaults = kw_defaults.p;
421+
r->arguments.n_kw_defaults = kw_defaults.n;
418422
r->arguments.m_kwarg = _m_kwarg.p;
419423
r->arguments.n_kwarg = _m_kwarg.n;
420424
r->arguments.m_defaults = defaults.p;
@@ -498,6 +502,16 @@ static inline void ADD_TYPE_COMMENT_(LFortran::Parser &p, Location l,
498502
FUNC_ARGS(p.m_a, l, posonlyargs.p, posonlyargs.n, \
499503
args.p, args.n, ARG2LIST(p.m_a, vararg), 1, \
500504
kwonlyargs.p, kwonlyargs.n, ARG2LIST(p.m_a, kwarg), 1)
505+
#define STAR_ARGS_21(posonlyargs, kwonlyargs, l) FUNC_ARGS(p.m_a, l, \
506+
posonlyargs.p, posonlyargs.n, nullptr, 0, nullptr, 0, \
507+
kwonlyargs.p, kwonlyargs.n, nullptr, 0)
508+
#define STAR_ARGS_22(posonlyargs, args, kwonlyargs, l) FUNC_ARGS(p.m_a, l, \
509+
posonlyargs.p, posonlyargs.n, args.p, args.n, nullptr, 0, \
510+
kwonlyargs.p, kwonlyargs.n, nullptr, 0)
511+
#define STAR_ARGS_23(args, kwonlyargs, l) FUNC_ARGS(p.m_a, l, nullptr, 0, \
512+
args.p, args.n, nullptr, 0, kwonlyargs.p, kwonlyargs.n, nullptr, 0)
513+
#define STAR_ARGS_24(kwonlyargs, l) FUNC_ARGS(p.m_a, l, nullptr, 0, \
514+
nullptr, 0, nullptr, 0, kwonlyargs.p, kwonlyargs.n, nullptr, 0)
501515

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

tests/parser/function_def3.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,25 @@ def test_01(a, # type:int
4747
def test_02(a, # type:int
4848
b):
4949
pass
50+
51+
def quantiles(dist, /, *, n):
52+
...
53+
54+
def quantiles(dist, /, *, n=4, method='exclusive'):
55+
...
56+
57+
def func(self, param1, param2, /, param3, *, param4, param5):
58+
...
59+
60+
def func(self, param1, param2, /, param3=7, *, param4, param5):
61+
...
62+
63+
def func(self, param1, param2, /, param3, param3_1=2, *, param4, param5):
64+
...
65+
66+
67+
def add(a, b, *, c, d):
68+
...
69+
70+
def func(*, param4, param5):
71+
...

tests/reference/ast_new-function_def3-f66064a.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "ast_new-function_def3-f66064a",
33
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
44
"infile": "tests/parser/function_def3.py",
5-
"infile_hash": "92f1995802c2cf446ff865c20616ac65d9c3bd2c52357710d970db4f",
5+
"infile_hash": "b5e4acc3d99fc5138d53737183c77f69cc9233435673ff1119cb21df",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "ast_new-function_def3-f66064a.stdout",
9-
"stdout_hash": "05191d8dc81986b7c1eec58c9415d3a78e08543fc621fb873afbd6f1",
9+
"stdout_hash": "45c49bce6580785f59584280023443fcd850c772c5fe6bd2ba414b7b",
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 [(FunctionDef main0 ([] [(i () ())] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef example ([] [(text () ()) (width () ()) (fill_char () ())] [] [] [] [] [(ConstantInt 80 ()) (ConstantStr "-" ())]) [(Return ())] [] () "(...) -> str") (Expr (Call (Name example Load) [(Name text Load)] [(width (ConstantInt 80 ())) (fill_char (ConstantStr "-" ()))])) (FunctionDef test_1 ([] [(x () ())] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_2 ([] [(y () ())] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef func ([] [(a () "str") (b () "int") (c (Name str Load) "str")] [] [] [] [] [(ConstantInt 80 ()) (ConstantStr "-" ())]) [(Pass)] [] () ()) (FunctionDef abc ([] [(a () ()) (b () "int") (c () "list[str]")] [] [] [] [] [(List [(ConstantStr "-" ())] Load)]) [(Return ())] [] () "(...) -> str") (FunctionDef test_01 ([] [(a () "int") (b () ())] [] [] [] [] [(ConstantInt 0 ())]) [(Pass)] [] () ()) (FunctionDef test_02 ([] [(a () "int") (b () ())] [] [] [] [] []) [(Pass)] [] () ())] [(TypeIgnore 0 "[misc]") (TypeIgnore 0 "")])
1+
(Module [(FunctionDef main0 ([] [(i () ())] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef example ([] [(text () ()) (width () ()) (fill_char () ())] [] [] [] [] [(ConstantInt 80 ()) (ConstantStr "-" ())]) [(Return ())] [] () "(...) -> str") (Expr (Call (Name example Load) [(Name text Load)] [(width (ConstantInt 80 ())) (fill_char (ConstantStr "-" ()))])) (FunctionDef test_1 ([] [(x () ())] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_2 ([] [(y () ())] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef func ([] [(a () "str") (b () "int") (c (Name str Load) "str")] [] [] [] [] [(ConstantInt 80 ()) (ConstantStr "-" ())]) [(Pass)] [] () ()) (FunctionDef abc ([] [(a () ()) (b () "int") (c () "list[str]")] [] [] [] [] [(List [(ConstantStr "-" ())] Load)]) [(Return ())] [] () "(...) -> str") (FunctionDef test_01 ([] [(a () "int") (b () ())] [] [] [] [] [(ConstantInt 0 ())]) [(Pass)] [] () ()) (FunctionDef test_02 ([] [(a () "int") (b () ())] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef quantiles ([(dist () ())] [] [] [(n () ())] [] [] []) [(Expr (ConstantEllipsis ()))] [] () ()) (FunctionDef quantiles ([(dist () ())] [] [] [(n () ()) (method () ())] [(ConstantInt 4 ()) (ConstantStr "exclusive" ())] [] []) [(Expr (ConstantEllipsis ()))] [] () ()) (FunctionDef func ([(self () ()) (param1 () ()) (param2 () ())] [(param3 () ())] [] [(param4 () ()) (param5 () ())] [] [] []) [(Expr (ConstantEllipsis ()))] [] () ()) (FunctionDef func ([(self () ()) (param1 () ()) (param2 () ())] [(param3 () ())] [] [(param4 () ()) (param5 () ())] [] [] [(ConstantInt 7 ())]) [(Expr (ConstantEllipsis ()))] [] () ()) (FunctionDef func ([(self () ()) (param1 () ()) (param2 () ())] [(param3 () ()) (param3_1 () ())] [] [(param4 () ()) (param5 () ())] [] [] [(ConstantInt 2 ())]) [(Expr (ConstantEllipsis ()))] [] () ()) (FunctionDef add ([] [(a () ()) (b () ())] [] [(c () ()) (d () ())] [] [] []) [(Expr (ConstantEllipsis ()))] [] () ()) (FunctionDef func ([] [] [] [(param4 () ()) (param5 () ())] [] [] []) [(Expr (ConstantEllipsis ()))] [] () ())] [(TypeIgnore 0 "[misc]") (TypeIgnore 0 "")])

0 commit comments

Comments
 (0)