try to support tuple as default arg, not pass

This commit is contained in:
pikastech 2023-04-29 12:21:29 +08:00
parent 44c23ee10f
commit 95e19ac08a
6 changed files with 62 additions and 23 deletions

View File

@ -21,7 +21,7 @@
// "--gtest_filter=parser.tuple_single"
// "--gtest_filter=parser.*"
// "--gtest_filter=parser.page_add"
"--gtest_filter=builtin.fn_default1"
"--gtest_filter=builtin.fn_default_tuple"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -1216,35 +1216,35 @@ static void _type_list_parse(FunctionArgsInfo* f) {
f->n_positional = 0;
return;
}
int8_t res = strCountSign(f->type_list, ',') + 1;
int8_t x = strCountSign(f->type_list, '*');
int8_t y = strCountSign(f->type_list, '=');
int8_t iArgc = strCountSign(f->type_list, ',') + 1;
int8_t iStar = strCountSign(f->type_list, '*');
int8_t iAssign = strCountSign(f->type_list, '=');
/* default */
if (y > 0) {
res -= y;
if (iAssign > 0) {
iArgc -= iAssign;
f->is_default = PIKA_TRUE;
f->n_default = y;
f->n_default = iAssign;
}
/* vars */
if (x == 1) {
if (iStar == 1) {
f->is_vars = PIKA_TRUE;
f->n_positional = res - 1;
f->n_positional = iArgc - 1;
return;
}
/* kw */
if (x == 2) {
if (iStar == 2) {
f->is_keys = PIKA_TRUE;
f->n_positional = res - 1;
f->n_positional = iArgc - 1;
return;
}
/* vars and kw */
if (x == 3) {
if (iStar == 3) {
f->is_vars = PIKA_TRUE;
f->is_keys = PIKA_TRUE;
f->n_positional = res - 2;
f->n_positional = iArgc - 2;
return;
}
f->n_positional = res;
f->n_positional = iArgc;
return;
}

View File

@ -90,17 +90,45 @@ char* strAppendWithSize(char* strOut, char* pData, int32_t Size) {
return strOut;
}
const char bracketStart[] = {'(', '[', '{', '\'', '\"'};
const char bracketEnd[] = {')', ']', '}', '\'', '\"'};
#define BRACKET_TYPE_NUM (sizeof(bracketStart) / sizeof(char))
int32_t _strCountSign(char* strIn, char sign, PIKA_BOOL bracketDepth0) {
int32_t iCount = 0;
int32_t iTotalDepth = 0;
PIKA_BOOL bEscaped = PIKA_FALSE;
for (size_t i = 0; strIn[i] != '\0'; i++) {
if (!bracketDepth0) {
if (strIn[i] == sign) {
iCount++;
}
continue;
}
char cCurrentChar = strIn[i];
if (cCurrentChar == '\\') {
bEscaped = !bEscaped;
continue;
}
if (!bEscaped) {
for (int j = 0; j < BRACKET_TYPE_NUM; j++) {
if (cCurrentChar == bracketStart[j]) {
iTotalDepth++;
} else if (cCurrentChar == bracketEnd[j]) {
iTotalDepth--;
}
}
}
if (cCurrentChar == sign && iTotalDepth == 0) {
iCount++;
}
bEscaped = PIKA_FALSE;
}
return iCount;
}
int32_t strCountSign(char* strIn, char sign) {
pika_assert(NULL != strIn);
int count = 0;
while (*strIn) {
if (*strIn == sign) {
count++;
}
strIn++;
}
return count;
return _strCountSign(strIn, sign, 0);
}
char* strReplaceChar(char* strIn, char src, char dst) {

View File

@ -72,6 +72,7 @@ int strPathGetFileName(char* input, char* output);
int strGetIndent(char* string);
int strIsBlank(char* string);
int strOnly(char* string, char ch);
int _strCountSign(char* strIn, char sign, PIKA_BOOL bracketDepth0);
#endif
#ifdef __cplusplus

View File

@ -196,6 +196,10 @@ TEST_RUN_SINGLE_FILE_PASS(builtin,
fn_default1,
"test/python/builtin/fn_default1.py")
TEST_RUN_SINGLE_FILE(builtin,
fn_default_tuple,
"test/python/builtin/fn_default_tuple.py")
#endif
TEST_END

View File

@ -0,0 +1,6 @@
def test_default_tuple(t=(1, 2)):
print(t)
test_default_tuple()