Skip to content

Commit

Permalink
Merge pull request lcompilers#776 from Thirumalai-Shaktivel/func_def_01
Browse files Browse the repository at this point in the history
Parse slash as an argument in the function definitions
  • Loading branch information
certik committed Jul 18, 2022
2 parents 2677ef1 + 4a0c910 commit 8c90296
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 12 deletions.
26 changes: 25 additions & 1 deletion src/lpython/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ for_statement
sep statements { $$ = FOR_02($2, $4, $7, $11, @$); }
| KW_FOR tuple_item KW_IN expr ":" TK_TYPE_COMMENT TK_NEWLINE statements {
$$ = FOR_03($2, $4, $6, $8, @$); }
| KW_FOR tuple_item KW_IN expr ":" TK_TYPE_COMMENT TK_NEWLINE statements
| KW_FOR tuple_item KW_IN expr ":" TK_TYPE_COMMENT TK_NEWLINE statements
KW_ELSE ":" sep statements { $$ = FOR_04($2, $4, $8, $12, $6, @$); }
;

Expand Down Expand Up @@ -568,6 +568,7 @@ parameter_list_starargs
$$ = STAR_ARGS_03($2, $4, $7, @$); }
| "*" parameter "," "**" parameter { $$ = STAR_ARGS_04($2, $5, @$); }
| "**" parameter { $$ = STAR_ARGS_05($2, @$); }

| defparameter_list "," "*" parameter { $$ = STAR_ARGS_06($1, $4, @$); }
| defparameter_list "," "*" parameter "," defparameter_list {
$$ = STAR_ARGS_07($1, $4, $6, @$); }
Expand All @@ -576,12 +577,35 @@ parameter_list_starargs
| defparameter_list "," "*" parameter "," "**" parameter {
$$ = STAR_ARGS_09($1, $4, $7, @$); }
| defparameter_list "," "**" parameter { $$ = STAR_ARGS_10($1, $4, @$); }

| defparameter_list "," "/" { $$ = STAR_ARGS_11($1, @$); }
| defparameter_list "," "/" "," "*" parameter {
$$ = STAR_ARGS_12($1, $6, @$); }
| defparameter_list "," "/" "," "*" parameter "," defparameter_list {
$$ = STAR_ARGS_13($1, $6, $8, @$); }
| defparameter_list "," "/" "," "*" parameter "," defparameter_list
"," "**" parameter { $$ = STAR_ARGS_14($1, $6, $8, $11, @$); }
| defparameter_list "," "/" "," "**" parameter {
$$ = STAR_ARGS_15($1, $6, @$); }
| defparameter_list "," "/" "," "*" parameter "," "**" parameter {
$$ = STAR_ARGS_16($1, $6, $9, @$); }
| defparameter_list "," "/" "," defparameter_list "," "*" parameter {
$$ = STAR_ARGS_17($1, $5, $8, @$); }
| defparameter_list "," "/" "," defparameter_list "," "**" parameter {
$$ = STAR_ARGS_18($1, $5, $8, @$); }
| defparameter_list "," "/" "," defparameter_list "," "*" parameter
"," "**" parameter { $$ = STAR_ARGS_19($1, $5, $8, $11, @$); }
| defparameter_list "," "/" "," defparameter_list "," "*" parameter
"," defparameter_list "," "**" parameter {
$$ = STAR_ARGS_20($1, $5, $8, $10, $13, @$); }
;

parameter_list_opt
: defparameter_list { $$ = FUNC_ARG_LIST_01($1, @$); }
| parameter_list_starargs { $$ = $1; }
| %empty { $$ = FUNC_ARG_LIST_02(@$); }
| defparameter_list "," "/" "," defparameter_list {
$$ = FUNC_ARG_LIST_03($1, $5, @$); }
;

function_def
Expand Down
35 changes: 35 additions & 0 deletions src/lpython/parser/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ static inline Args *FUNC_ARGS(Allocator &al, Location &l,
nullptr, 0, vararg, 1, nullptr, 0, nullptr, 0, kwarg, 1, nullptr, 0)
#define STAR_ARGS_05(kwarg, l) FUNC_ARGS(p.m_a, l, nullptr, 0, \
nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0, kwarg, 1, nullptr, 0)

#define STAR_ARGS_06(args, vararg, l) FUNC_ARGS(p.m_a, l, nullptr, 0, \
args.p, args.n, vararg, 1, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0)
#define STAR_ARGS_07(args, vararg, kwonlyargs, l) FUNC_ARGS(p.m_a, l, nullptr, 0, \
Expand All @@ -373,11 +374,45 @@ static inline Args *FUNC_ARGS(Allocator &al, Location &l,
#define STAR_ARGS_10(args, kwarg, l) FUNC_ARGS(p.m_a, l, nullptr, 0, \
args.p, args.n, nullptr, 0, nullptr, 0, nullptr, 0, kwarg, 1, nullptr, 0)

#define STAR_ARGS_11(posonlyargs, l) FUNC_ARGS(p.m_a, l, \
posonlyargs.p, posonlyargs.n, nullptr, 0, nullptr, 0, nullptr, 0, \
nullptr, 0, nullptr, 0, nullptr, 0)
#define STAR_ARGS_12(posonlyargs, vararg, l) FUNC_ARGS(p.m_a, l, \
posonlyargs.p, posonlyargs.n, nullptr, 0, vararg, 1, nullptr, 0, \
nullptr, 0, nullptr, 0, nullptr, 0)
#define STAR_ARGS_13(posonlyargs, vararg, kwonlyargs, l) FUNC_ARGS(p.m_a, l, \
posonlyargs.p, posonlyargs.n, nullptr, 0, vararg, 1, \
kwonlyargs.p, kwonlyargs.n, nullptr, 0, nullptr, 0, nullptr, 0)
#define STAR_ARGS_14(posonlyargs, vararg, kwonlyargs, kwarg, l) FUNC_ARGS(p.m_a, l, \
posonlyargs.p, posonlyargs.n, nullptr, 0, vararg, 1, \
kwonlyargs.p, kwonlyargs.n, nullptr, 0, kwarg, 1, nullptr, 0)
#define STAR_ARGS_15(posonlyargs, kwarg, l) FUNC_ARGS(p.m_a, l, \
posonlyargs.p, posonlyargs.n, nullptr, 0, nullptr, 0, nullptr, 0, \
nullptr, 0, kwarg, 1, nullptr, 0)
#define STAR_ARGS_16(posonlyargs, vararg, kwarg, l) FUNC_ARGS(p.m_a, l, \
posonlyargs.p, posonlyargs.n, nullptr, 0, vararg, 1, nullptr, 0, \
nullptr, 0, kwarg, 1, nullptr, 0)
#define STAR_ARGS_17(posonlyargs, args, vararg, l) FUNC_ARGS(p.m_a, l, \
posonlyargs.p, posonlyargs.n, args.p, args.n, vararg, 1, nullptr, 0, \
nullptr, 0, nullptr, 0, nullptr, 0)
#define STAR_ARGS_18(posonlyargs, args, kwarg, l) FUNC_ARGS(p.m_a, l, \
posonlyargs.p, posonlyargs.n, args.p, args.n, nullptr, 0, nullptr, 0, \
nullptr, 0, kwarg, 1, nullptr, 0)
#define STAR_ARGS_19(posonlyargs, args, vararg, kwarg, l) FUNC_ARGS(p.m_a, l, \
posonlyargs.p, posonlyargs.n, args.p, args.n, vararg, 1, nullptr, 0, \
nullptr, 0, kwarg, 1, nullptr, 0)
#define STAR_ARGS_20(posonlyargs, args, vararg, kwonlyargs, kwarg, l) \
FUNC_ARGS(p.m_a, l, posonlyargs.p, posonlyargs.n, args.p, args.n, \
vararg, 1, kwonlyargs.p, kwonlyargs.n, nullptr, 0, kwarg, 1, nullptr, 0)

#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, \
nullptr, 0, nullptr, 0)
#define FUNC_ARG_LIST_02(l) FUNC_ARGS(p.m_a, l, nullptr, 0, \
nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0)
#define FUNC_ARG_LIST_03(posonlyargs, args, l) FUNC_ARGS(p.m_a, l, \
posonlyargs.p, posonlyargs.n, args.p, args.n, nullptr, 0, nullptr, 0, \
nullptr, 0, nullptr, 0, nullptr, 0)

#define FUNCTION_01(decorator, id, args, stmts, l) \
make_FunctionDef_t(p.m_a, l, name2char(id), args->arguments, \
Expand Down
9 changes: 4 additions & 5 deletions tests/parser/function_def1.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ def check():

check()

# TODO:
# def print_args(*args):
# print(args)
def print_args(*args):
print(args)

# def print_kwargs(**kwargs):
# print(kwargs)
def print_kwargs(**kwargs):
print(kwargs)
33 changes: 33 additions & 0 deletions tests/parser/function_def2.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,39 @@ def test_12(x, **args):
def test_14(x, *y, z, **args: i32) -> i32:
pass

def test_15(a, /):
pass

def test_16(a, /, b, c):
pass

def test_17(a, /, *b):
pass

def test_18(a:i32, /, *b: i64, c: i32, d: i32):
pass

def test_19(a, /, *b, c, **d):
pass

def test_20(a, /, **b):
pass

def test_21(a, /, *b, **c):
pass

def test_22(a, /, b, c, *d):
pass

def test_23(a, /, b, c, **d):
pass

def test_24(a, /, b, c, *d, **e):
pass

def test_25(a, /, b, c, *d, e, **f):
pass

test()
test(x, y)
test(x, y = 1, z = '123')
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/ast_new-function_def1-1a872df.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "ast_new-function_def1-1a872df",
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
"infile": "tests/parser/function_def1.py",
"infile_hash": "9dc3cbaa399b8013e99cf165c76d468cf48edde757daafc4069cda40",
"infile_hash": "96c4a34f72e609e55af1688b793dc2d6fb375f82f661d16de0ea6fa9",
"outfile": null,
"outfile_hash": null,
"stdout": "ast_new-function_def1-1a872df.stdout",
"stdout_hash": "62bdf27fa9795b9cad94f2747ac48dfac223ef7401dc9af0b445c5a8",
"stdout_hash": "ad5ea65f2ef1994819a5c48936f936ea955303c08253419d72aa450b",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/ast_new-function_def1-1a872df.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
the person passed in as
a parameter
" ())) (Expr (Call (Name print Load) [(BinOp (BinOp (ConstantStr "Hello, " ()) Add (Name name Load)) Add (ConstantStr ". Good morning!" ()))] []))] [] () ()) (FunctionDef absolute_value ([] [(num () ())] [] [] [] [] []) [(Expr (ConstantStr "This function returns the absolute
value of the entered number" ())) (If (Compare (Name num Load) GtE [(ConstantInt 0 ())]) [(Return (Name num Load))] [(Return (UnaryOp USub (Name num Load)))])] [] () ()) (FunctionDef combine ([] [(fname () ()) (lname () ())] [] [] [] [] []) [(Expr (Call (Name print Load) [(BinOp (BinOp (Name fname Load) Add (ConstantStr " " ())) Add (Name lname Load))] []))] [] () ()) (FunctionDef tri_recursion ([] [(k () ())] [] [] [] [] []) [(If (Compare (Name k Load) Gt [(ConstantInt 0 ())]) [(Assign [(Name result Store)] (BinOp (Name k Load) Add (Call (Name tri_recursion Load) [(BinOp (Name k Load) Sub (ConstantInt 1 ()))] [])) ()) (Expr (Call (Name print Load) [(Name result Load)] []))] [(Assign [(Name result Store)] (ConstantInt 0 ()) ())]) (Return (Name result Load))] [] () ()) (FunctionDef test ([] [(a (Name i32 Load) ())] [] [] [] [] []) [(Return (BinOp (Name a Load) Add (ConstantInt 10 ())))] [(Name overload Load)] (Name i32 Load) ()) (FunctionDef test ([] [(a (Name i64 Load) ())] [] [] [] [] []) [(Return (BinOp (Name a Load) Add (ConstantInt 10 ())))] [(Name overload Load)] (Name i64 Load) ()) (FunctionDef test ([] [(a (Name bool Load) ())] [] [] [] [] []) [(If (Name a Load) [(Return (ConstantInt 10 ()))] []) (Return (UnaryOp USub (ConstantInt 10 ())))] [(Name overload Load)] (Name i32 Load) ()) (FunctionDef check ([] [] [] [] [] [] []) [(Expr (Call (Name greet Load) [(ConstantStr "Xyz" ())] [])) (Expr (Call (Name print Load) [(Call (Name absolute_value Load) [(ConstantInt 2 ())] [])] [])) (Expr (Call (Name combine Load) [(ConstantStr "LPython" ()) (ConstantStr "Compiler" ())] [])) (Expr (Call (Name print Load) [(ConstantStr "Recursion Example Results: " ())] [])) (Expr (Call (Name tri_recursion Load) [(ConstantInt 6 ())] [])) (Expr (Call (Name print Load) [(Call (Name test Load) [(ConstantInt 15 ())] [])] [])) (Expr (Call (Name print Load) [(Call (Name test Load) [(ConstantBool .true. ())] [])] []))] [] () ()) (Expr (Call (Name check Load) [] []))] [])
value of the entered number" ())) (If (Compare (Name num Load) GtE [(ConstantInt 0 ())]) [(Return (Name num Load))] [(Return (UnaryOp USub (Name num Load)))])] [] () ()) (FunctionDef combine ([] [(fname () ()) (lname () ())] [] [] [] [] []) [(Expr (Call (Name print Load) [(BinOp (BinOp (Name fname Load) Add (ConstantStr " " ())) Add (Name lname Load))] []))] [] () ()) (FunctionDef tri_recursion ([] [(k () ())] [] [] [] [] []) [(If (Compare (Name k Load) Gt [(ConstantInt 0 ())]) [(Assign [(Name result Store)] (BinOp (Name k Load) Add (Call (Name tri_recursion Load) [(BinOp (Name k Load) Sub (ConstantInt 1 ()))] [])) ()) (Expr (Call (Name print Load) [(Name result Load)] []))] [(Assign [(Name result Store)] (ConstantInt 0 ()) ())]) (Return (Name result Load))] [] () ()) (FunctionDef test ([] [(a (Name i32 Load) ())] [] [] [] [] []) [(Return (BinOp (Name a Load) Add (ConstantInt 10 ())))] [(Name overload Load)] (Name i32 Load) ()) (FunctionDef test ([] [(a (Name i64 Load) ())] [] [] [] [] []) [(Return (BinOp (Name a Load) Add (ConstantInt 10 ())))] [(Name overload Load)] (Name i64 Load) ()) (FunctionDef test ([] [(a (Name bool Load) ())] [] [] [] [] []) [(If (Name a Load) [(Return (ConstantInt 10 ()))] []) (Return (UnaryOp USub (ConstantInt 10 ())))] [(Name overload Load)] (Name i32 Load) ()) (FunctionDef check ([] [] [] [] [] [] []) [(Expr (Call (Name greet Load) [(ConstantStr "Xyz" ())] [])) (Expr (Call (Name print Load) [(Call (Name absolute_value Load) [(ConstantInt 2 ())] [])] [])) (Expr (Call (Name combine Load) [(ConstantStr "LPython" ()) (ConstantStr "Compiler" ())] [])) (Expr (Call (Name print Load) [(ConstantStr "Recursion Example Results: " ())] [])) (Expr (Call (Name tri_recursion Load) [(ConstantInt 6 ())] [])) (Expr (Call (Name print Load) [(Call (Name test Load) [(ConstantInt 15 ())] [])] [])) (Expr (Call (Name print Load) [(Call (Name test Load) [(ConstantBool .true. ())] [])] []))] [] () ()) (Expr (Call (Name check Load) [] [])) (FunctionDef print_args ([] [] [(args () ())] [] [] [] []) [(Expr (Call (Name print Load) [(Name args Load)] []))] [] () ()) (FunctionDef print_kwargs ([] [] [] [] [] [(kwargs () ())] []) [(Expr (Call (Name print Load) [(Name kwargs Load)] []))] [] () ())] [])
4 changes: 2 additions & 2 deletions tests/reference/ast_new-function_def2-52c4587.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "ast_new-function_def2-52c4587",
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
"infile": "tests/parser/function_def2.py",
"infile_hash": "44a8d24fcf31af93aea063d957af107d51131ae5eca3762eb0bdbf35",
"infile_hash": "1c1ef66b8a604487d0d72ea97722f252ea0424b9d43beca8ac27d386",
"outfile": null,
"outfile_hash": null,
"stdout": "ast_new-function_def2-52c4587.stdout",
"stdout_hash": "ee51c7c928d4540f2cce026e78ec52910501a82790a7a0cf4ce8e505",
"stdout_hash": "f8afc2e912a93370a4ad5bcb847899a1fabb9785b53d7fe884b5e757",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/ast_new-function_def2-52c4587.stdout
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(Module [(FunctionDef test_01 ([] [] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_02 ([] [] [(x (Name i32 Load) ())] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_03 ([] [] [(x () ())] [(y () ()) (z () ())] [] [] []) [(Pass)] [] () ()) (FunctionDef test_04 ([] [] [(x () ())] [(y () ()) (z () ())] [] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_05 ([] [] [(x () ())] [] [] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_06 ([] [] [] [] [] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_07 ([] [(x (Name i32 Load) ()) (y () ())] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_08 ([] [(x () ())] [(y () ())] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_09 ([] [(x () ())] [(y () ())] [(z () ())] [] [] []) [(Pass)] [] () ()) (FunctionDef test_10 ([] [(x () ())] [(y () ())] [(z () ())] [] [(args (Name i32 Load) ())] []) [(Pass)] [] () ()) (FunctionDef test_11 ([] [(x () ())] [(y () ())] [] [] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_12 ([] [(x () ())] [] [] [] [(args () ())] []) [(Pass)] [] () ()) (Expr (ConstantStr "
TODO: defaults are not stored in AST
" ())) (FunctionDef test_14 ([] [(x () ())] [(y () ())] [(z () ())] [] [(args (Name i32 Load) ())] []) [(Pass)] [] (Name i32 Load) ()) (Expr (Call (Name test Load) [] [])) (Expr (Call (Name test Load) [(Name x Load) (Name y Load)] [])) (Expr (Call (Name test Load) [(Name x Load)] [(y (ConstantInt 1 ())) (z (ConstantStr "123" ()))])) (Expr (Call (Name test Load) [(ConstantInt 100 ())] [(() (Name x Load))])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load)] [(() (Name y Load))])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load) (Name y Load)] [])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load) (Starred (Name y Load) Load)] [])) (Expr (Call (Name test Load) [] [(() (Name x Load)) (() (Name y Load))])) (Expr (Call (Attribute (Name lp Load) test Load) [] [])) (Expr (Call (Attribute (Name lp Load) test Load) [(Name x Load) (Name y Load)] [])) (Expr (Call (Attribute (Name lp Load) test Load) [(Name x Load)] [(y (ConstantInt 1 ())) (z (ConstantStr "123" ()))]))] [])
" ())) (FunctionDef test_14 ([] [(x () ())] [(y () ())] [(z () ())] [] [(args (Name i32 Load) ())] []) [(Pass)] [] (Name i32 Load) ()) (FunctionDef test_15 ([(a () ())] [] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_16 ([(a () ())] [(b () ()) (c () ())] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_17 ([(a () ())] [] [(b () ())] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_18 ([(a (Name i32 Load) ())] [] [(b (Name i64 Load) ())] [(c (Name i32 Load) ()) (d (Name i32 Load) ())] [] [] []) [(Pass)] [] () ()) (FunctionDef test_19 ([(a () ())] [] [(b () ())] [(c () ())] [] [(d () ())] []) [(Pass)] [] () ()) (FunctionDef test_20 ([(a () ())] [] [] [] [] [(b () ())] []) [(Pass)] [] () ()) (FunctionDef test_21 ([(a () ())] [] [(b () ())] [] [] [(c () ())] []) [(Pass)] [] () ()) (FunctionDef test_22 ([(a () ())] [(b () ()) (c () ())] [(d () ())] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_23 ([(a () ())] [(b () ()) (c () ())] [] [] [] [(d () ())] []) [(Pass)] [] () ()) (FunctionDef test_24 ([(a () ())] [(b () ()) (c () ())] [(d () ())] [] [] [(e () ())] []) [(Pass)] [] () ()) (FunctionDef test_25 ([(a () ())] [(b () ()) (c () ())] [(d () ())] [(e () ())] [] [(f () ())] []) [(Pass)] [] () ()) (Expr (Call (Name test Load) [] [])) (Expr (Call (Name test Load) [(Name x Load) (Name y Load)] [])) (Expr (Call (Name test Load) [(Name x Load)] [(y (ConstantInt 1 ())) (z (ConstantStr "123" ()))])) (Expr (Call (Name test Load) [(ConstantInt 100 ())] [(() (Name x Load))])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load)] [(() (Name y Load))])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load) (Name y Load)] [])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load) (Starred (Name y Load) Load)] [])) (Expr (Call (Name test Load) [] [(() (Name x Load)) (() (Name y Load))])) (Expr (Call (Attribute (Name lp Load) test Load) [] [])) (Expr (Call (Attribute (Name lp Load) test Load) [(Name x Load) (Name y Load)] [])) (Expr (Call (Attribute (Name lp Load) test Load) [(Name x Load)] [(y (ConstantInt 1 ())) (z (ConstantStr "123" ()))]))] [])

0 comments on commit 8c90296

Please sign in to comment.