allow complex assignment in class definition

This commit is contained in:
blueloveTH 2023-11-30 17:53:11 +08:00
parent 438857a1f5
commit 74e31b36ed
3 changed files with 12 additions and 8 deletions

View File

@ -41,3 +41,4 @@ The easiest way to test a feature is to [try it on your browser](https://pocketp
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. 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. 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.

View File

@ -728,15 +728,8 @@ __EAT_DOTS_END:
int n = 0; int n = 0;
while(match(TK("="))){ while(match(TK("="))){
EXPR_TUPLE(); 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; n += 1;
} }
if(ctx()->is_compiling_class && n>1){
SyntaxError("can't assign to multiple targets in class definition");
}
// stack size is n+1 // stack size is n+1
Expr_ val = ctx()->s_expr.popx(); Expr_ val = ctx()->s_expr.popx();
val->emit_(ctx()); val->emit_(ctx());

View File

@ -119,3 +119,13 @@ class A:
assert A.x == 2 assert A.x == 2
assert A.y == 1 assert A.y == 1
assert A.z == 3 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