Parcourir la source

support `in` op for **kw

Lyon il y a 2 ans
Parent
commit
13bfc829aa

+ 12 - 0
examples/builtins/kw.py

@@ -0,0 +1,12 @@
+def test_kw(**kw):
+    if 'a' in kw:
+        return kw['a']
+    if 'b' in kw:
+        return kw['b']
+
+a = test_kw(a=1)
+b = test_kw(b=2)
+assert a == 1
+assert b == 2
+
+print('PASS')

+ 3 - 10
package/PikaStdLib/PikaStdData_Dict.c

@@ -189,16 +189,9 @@ int PikaStdData_dict_keys___len__(PikaObj* self) {
 }
 }
 
 
 int dict_contains(PikaDict* dict, Arg* key) {
 int dict_contains(PikaDict* dict, Arg* key) {
-    int i = 0;
-    while (PIKA_TRUE) {
-        Arg* item = args_getArgByIndex(_OBJ2KEYS(dict), i);
-        if (NULL == item) {
-            break;
-        }
-        if (arg_isEqual(item, key)) {
-            return PIKA_TRUE;
-        }
-        i++;
+    Hash hash = hash_time33(arg_getStr(key));
+    if (args_isArgExist_hash(_OBJ2DICT(dict), hash)) {
+        return pika_true;
     }
     }
     return pika_false;
     return pika_false;
 }
 }

+ 1 - 1
port/linux/.vscode/launch.json

@@ -12,7 +12,7 @@
             // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
             // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
             "args": [
             "args": [
                 // "--gtest_filter=vm.keyword_2"
                 // "--gtest_filter=vm.keyword_2"
-                "--gtest_filter=builtin.repl_mode"
+                // "--gtest_filter=builtin.kw"
             ],
             ],
             "stopAtEntry": false,
             "stopAtEntry": false,
             "cwd": "${workspaceFolder}",
             "cwd": "${workspaceFolder}",

+ 3 - 10
port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c

@@ -189,16 +189,9 @@ int PikaStdData_dict_keys___len__(PikaObj* self) {
 }
 }
 
 
 int dict_contains(PikaDict* dict, Arg* key) {
 int dict_contains(PikaDict* dict, Arg* key) {
-    int i = 0;
-    while (PIKA_TRUE) {
-        Arg* item = args_getArgByIndex(_OBJ2KEYS(dict), i);
-        if (NULL == item) {
-            break;
-        }
-        if (arg_isEqual(item, key)) {
-            return PIKA_TRUE;
-        }
-        i++;
+    Hash hash = hash_time33(arg_getStr(key));
+    if (args_isArgExist_hash(_OBJ2DICT(dict), hash)) {
+        return pika_true;
     }
     }
     return pika_false;
     return pika_false;
 }
 }

+ 12 - 0
port/linux/test/python/builtins/kw.py

@@ -0,0 +1,12 @@
+def test_kw(**kw):
+    if 'a' in kw:
+        return kw['a']
+    if 'b' in kw:
+        return kw['b']
+
+a = test_kw(a=1)
+b = test_kw(b=2)
+assert a == 1
+assert b == 2
+
+print('PASS')

+ 1 - 1
src/PikaVersion.h

@@ -2,4 +2,4 @@
 #define PIKA_VERSION_MINOR 12
 #define PIKA_VERSION_MINOR 12
 #define PIKA_VERSION_MICRO 5
 #define PIKA_VERSION_MICRO 5
 
 
-#define PIKA_EDIT_TIME "2023/09/07 14:46:48"
+#define PIKA_EDIT_TIME "2023/09/10 10:22:49"