Compare commits

...

6 Commits

Author SHA1 Message Date
blueloveTH
05e1de4101 allow vec2(vec2i) 2024-11-12 21:35:56 +08:00
blueloveTH
6266161118 fix ** associativity 2024-11-12 19:06:17 +08:00
blueloveTH
cbae815d37 Revert "update parse_expression"
This reverts commit 8e3c3354498e1c960185e74522d78befd0e469f4.
2024-11-12 18:59:52 +08:00
blueloveTH
8e3c335449 update parse_expression 2024-11-12 18:52:42 +08:00
blueloveTH
068ec948ec ... 2024-11-12 18:16:28 +08:00
blueloveTH
5a47ae6f42 fix True not False bug 2024-11-12 18:12:58 +08:00
5 changed files with 34 additions and 5 deletions

View File

@ -48,7 +48,10 @@ class vec2(_vecF['vec2']):
def with_y(self, y: float) -> vec2: ... def with_y(self, y: float) -> vec2: ...
def with_z(self, z: float) -> vec3: ... def with_z(self, z: float) -> vec3: ...
@overload
def __init__(self, x: float, y: float) -> None: ... def __init__(self, x: float, y: float) -> None: ...
@overload
def __init__(self, xy: vec2i) -> None: ...
def rotate(self, radians: float) -> vec2: ... def rotate(self, radians: float) -> vec2: ...
@ -159,7 +162,10 @@ class vec3(_vecF['vec3']):
def with_z(self, z: float) -> vec3: ... def with_z(self, z: float) -> vec3: ...
def with_xy(self, xy: vec2) -> vec3: ... def with_xy(self, xy: vec2) -> vec3: ...
@overload
def __init__(self, x: float, y: float, z: float) -> None: ... def __init__(self, x: float, y: float, z: float) -> None: ...
@overload
def __init__(self, xyz: vec3i) -> None: ...

View File

@ -1400,7 +1400,9 @@ static Error* parse_expression(Compiler* self, int precedence, bool allow_slice)
TokenIndex op = curr()->type; TokenIndex op = curr()->type;
advance(); advance();
PrattCallback infix = rules[op].infix; PrattCallback infix = rules[op].infix;
assert(infix != NULL); if(infix == NULL) {
return SyntaxError(self, "expected an infix operator, got %s", TokenSymbols[op]);
}
check(infix(self)); check(infix(self));
} }
return NULL; return NULL;
@ -1650,7 +1652,12 @@ static Error* exprBinaryOp(Compiler* self) {
Error* err; Error* err;
int line = prev()->line; int line = prev()->line;
TokenIndex op = prev()->type; TokenIndex op = prev()->type;
check(parse_expression(self, rules[op].precedence + 1, false)); int precedence = rules[op].precedence;
if(op != TK_POW) {
// if not right associative, increase precedence
precedence += 1;
}
check(parse_expression(self, precedence, false));
BinaryExpr* e = BinaryExpr__new(line, op, false); BinaryExpr* e = BinaryExpr__new(line, op, false);
if(op == TK_IN || op == TK_NOT_IN) { if(op == TK_IN || op == TK_NOT_IN) {
e->lhs = Ctx__s_popx(ctx()); e->lhs = Ctx__s_popx(ctx());

View File

@ -110,11 +110,18 @@ static py_Ref _const(py_Type type, const char* name) {
#define DEF_VECTOR_OPS(D) \ #define DEF_VECTOR_OPS(D) \
static bool vec##D##__new__(int argc, py_Ref argv) { \ static bool vec##D##__new__(int argc, py_Ref argv) { \
PY_CHECK_ARGC(D + 1); \
c11_vec##D res; \ c11_vec##D res; \
if(argc == 2) { \
PY_CHECK_ARG_TYPE(1, tp_vec##D##i); \
c11_vec##D##i v = py_tovec##D##i(&argv[1]); \
for(int i = 0; i < D; i++) \
res.data[i] = v.data[i]; \
} else { \
PY_CHECK_ARGC(D + 1); \
for(int i = 0; i < D; i++) { \ for(int i = 0; i < D; i++) { \
if(!py_castfloat32(&argv[i + 1], &res.data[i])) return false; \ if(!py_castfloat32(&argv[i + 1], &res.data[i])) return false; \
} \ } \
} \
py_newvec##D(py_retval(), res); \ py_newvec##D(py_retval(), res); \
return true; \ return true; \
} \ } \

View File

@ -400,3 +400,8 @@ a[vec2i(1, 2)] = 1
assert a[vec2i(1, 2)] == 1 assert a[vec2i(1, 2)] == 1
a[vec3i(1, 2, 3)] = 2 a[vec3i(1, 2, 3)] = 2
assert a[vec3i(1, 2, 3)] == 2 assert a[vec3i(1, 2, 3)] == 2
assert vec2(vec2i.LEFT) == vec2(-1, 0)
assert vec2(vec2i.RIGHT) == vec2(1, 0)
assert vec3(vec3i.ONE) == vec3(1, 1, 1)
assert vec3(vec3i.ZERO) == vec3(0, 0, 0)

View File

@ -47,3 +47,7 @@ assert A()[::, :] == (slice(None, None, None), slice(None, None, None))
assert A()[::, :2] == (slice(None, None, None), slice(None, 2, None)) assert A()[::, :2] == (slice(None, None, None), slice(None, 2, None))
assert A()['b':'c':1, :] == (slice('b', 'c', 1), slice(None, None, None)) assert A()['b':'c':1, :] == (slice('b', 'c', 1), slice(None, None, None))
assert A()[1:2, :A()[3:4, ::-1]] == (slice(1, 2, None), slice(None, (slice(3, 4, None), slice(None, None, -1)), None)) assert A()[1:2, :A()[3:4, ::-1]] == (slice(1, 2, None), slice(None, (slice(3, 4, None), slice(None, None, -1)), None))
# test right associative
assert 2**2**3 == 256
assert (2**2**3)**2 == 65536