From b65cf89d2272cfc45fe831be1c4868c63eea3dab Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 4 Feb 2024 19:30:51 +0800 Subject: [PATCH] fix https://github.com/pocketpy/pocketpy/issues/171 --- src/compiler.cpp | 11 +++++++++++ tests/21_functions.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/compiler.cpp b/src/compiler.cpp index 72c26f04..0173a491 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -1155,6 +1155,17 @@ __EAT_DOTS_END: case TK("False"): return VAR(false); case TK("None"): return vm->None; case TK("..."): return vm->Ellipsis; + case TK("("): { + List cpnts; + while(true) { + cpnts.push_back(read_literal()); + if(curr().type == TK(")")) break; + consume(TK(",")); + if(curr().type == TK(")")) break; + } + consume(TK(")")); + return VAR(Tuple(std::move(cpnts))); + } default: break; } return nullptr; diff --git a/tests/21_functions.py b/tests/21_functions.py index 1d01be0e..82960592 100644 --- a/tests/21_functions.py +++ b/tests/21_functions.py @@ -91,4 +91,31 @@ S, kwargs = g(1, 2, 3, 4, 5, c=4, e=5, f=6) # S = 1 + 2 + 4 + 2 + 12 = 21 assert S == 21 -assert kwargs == {'e': 5, 'f': 6} \ No newline at end of file +assert kwargs == {'e': 5, 'f': 6} + +# test tuple defaults + +def f(a=(1,)): + return a +assert f() == (1,) + +def f(a=(1,2)): + return a +assert f() == (1,2) + +def f(a=(1,2,3)): + return a +assert f() == (1,2,3) + +def f(a=(1,2,3,)): + return a +assert f() == (1,2,3) + +def f(a=(1,(2,3))): + return a +assert f() == (1,(2,3)) + +def f(a=((1,2),3), b=(4,)): + return a, b + +assert f() == (((1,2),3), (4,))