Răsfoiți Sursa

allow complex assignment in class definition

blueloveTH 2 ani în urmă
părinte
comite
74e31b36ed
3 a modificat fișierele cu 12 adăugiri și 8 ștergeri
  1. 2 1
      docs/features/differences.md
  2. 0 7
      src/compiler.cpp
  3. 10 0
      tests/40_class.py

+ 2 - 1
docs/features/differences.md

@@ -40,4 +40,5 @@ The easiest way to test a feature is to [try it on your browser](https://pocketp
 7. Raw string cannot have boundary quotes in it, even escaped. See [#55](https://github.com/blueloveTH/pocketpy/issues/55).
 8. In a starred unpacked assignment, e.g. `a, b, *c = x`, the starred variable can only be presented in the last position. `a, *b, c = x` is not supported.
 9. A `Tab` is equivalent to 4 spaces. You can mix `Tab` and spaces in indentation, but it is not recommended.
-10. `%`, `&`, `//`, `^` and `|` for `int` behave the same as C, not python.
+10. `%`, `&`, `//`, `^` and `|` for `int` behave the same as C, not python.
+11. `str.split` and `str.splitlines` will remove all empty entries.

+ 0 - 7
src/compiler.cpp

@@ -728,15 +728,8 @@ __EAT_DOTS_END:
                 int n = 0;
                 while(match(TK("="))){
                     EXPR_TUPLE();
-                    Expr* _tp = ctx()->s_expr.top().get();
-                    if(ctx()->is_compiling_class && _tp->is_tuple()){
-                        SyntaxError("can't use unpack tuple in class definition");
-                    }
                     n += 1;
                 }
-                if(ctx()->is_compiling_class && n>1){
-                    SyntaxError("can't assign to multiple targets in class definition");
-                }
                 // stack size is n+1
                 Expr_ val = ctx()->s_expr.popx();
                 val->emit_(ctx());

+ 10 - 0
tests/40_class.py

@@ -119,3 +119,13 @@ class A:
 assert A.x == 2
 assert A.y == 1
 assert A.z == 3
+
+class MyClass:
+    a = 1,2,3
+    b, c = 1, 2
+    d = b + c
+
+assert MyClass.a == (1, 2, 3)
+assert MyClass.b == 1
+assert MyClass.c == 2
+assert MyClass.d == 3