Просмотр исходного кода

fix https://github.com/pocketpy/pocketpy/issues/171

blueloveTH 2 лет назад
Родитель
Сommit
b65cf89d22
2 измененных файлов с 39 добавлено и 1 удалено
  1. 11 0
      src/compiler.cpp
  2. 28 1
      tests/21_functions.py

+ 11 - 0
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;

+ 28 - 1
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}
+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,))