diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 3a92acf72..dbd045741 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -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}", diff --git a/src/PikaVM.c b/src/PikaVM.c index 53f40d3e9..2592b0a3d 100644 --- a/src/PikaVM.c +++ b/src/PikaVM.c @@ -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; } diff --git a/src/dataString.c b/src/dataString.c index e62d016e4..d63b34bc5 100644 --- a/src/dataString.c +++ b/src/dataString.c @@ -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) { diff --git a/src/dataString.h b/src/dataString.h index 16903af7e..a685b1e26 100644 --- a/src/dataString.h +++ b/src/dataString.h @@ -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 diff --git a/test/builtin-test.cpp b/test/builtin-test.cpp index d4078e34d..3ee546de9 100644 --- a/test/builtin-test.cpp +++ b/test/builtin-test.cpp @@ -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 \ No newline at end of file diff --git a/test/python/builtin/fn_default_tuple.py b/test/python/builtin/fn_default_tuple.py new file mode 100644 index 000000000..eb67dbab2 --- /dev/null +++ b/test/python/builtin/fn_default_tuple.py @@ -0,0 +1,6 @@ + +def test_default_tuple(t=(1, 2)): + print(t) + + +test_default_tuple()