fixed type guard
This commit is contained in:
parent
0453273d8b
commit
b2ec75e157
|
@ -1,4 +1,4 @@
|
||||||
I = TypeVar('I', int32, Vec)
|
I = TypeVar('I', int32, float, Vec)
|
||||||
|
|
||||||
class Vec:
|
class Vec:
|
||||||
v: list[int32]
|
v: list[int32]
|
||||||
|
@ -6,8 +6,10 @@ class Vec:
|
||||||
self.v = v
|
self.v = v
|
||||||
|
|
||||||
def __add__(self, other: I) -> Vec:
|
def __add__(self, other: I) -> Vec:
|
||||||
if other is int32:
|
if type(other) == int32:
|
||||||
return Vec([v + other for v in self.v])
|
return Vec([v + other for v in self.v])
|
||||||
|
elif type(other) == float:
|
||||||
|
return Vec([v + int32(other) for v in self.v])
|
||||||
else:
|
else:
|
||||||
return Vec([self.v[i] + other.v[i] for i in range(len(self.v))])
|
return Vec([self.v[i] + other.v[i] for i in range(len(self.v))])
|
||||||
|
|
||||||
|
|
|
@ -115,14 +115,14 @@ def parse_if_stmt(ctx: Context,
|
||||||
return_ty: Type,
|
return_ty: Type,
|
||||||
node):
|
node):
|
||||||
if isinstance(node.test, ast.Compare) and \
|
if isinstance(node.test, ast.Compare) and \
|
||||||
|
isinstance(node.test.left, ast.Call) and \
|
||||||
|
isinstance(node.test.left.func, ast.Name) and \
|
||||||
|
node.test.left.func.id == 'type' and \
|
||||||
|
len(node.test.left.args) == 1 and \
|
||||||
len(node.test.ops) == 1 and \
|
len(node.test.ops) == 1 and \
|
||||||
(isinstance(node.test.ops[0], ast.Is) or\
|
(isinstance(node.test.ops[0], ast.Eq) or\
|
||||||
isinstance(node.test.ops[0], ast.IsNot)):
|
isinstance(node.test.ops[0], ast.NotEq)):
|
||||||
if not isinstance(node.test.left, ast.Name):
|
t = parse_expr(ctx, sym_table, node.test.left.args[0])
|
||||||
raise CustomError(
|
|
||||||
'type guard only support testing variables',
|
|
||||||
node.test)
|
|
||||||
t = parse_expr(ctx, sym_table, node.test.left)
|
|
||||||
if not isinstance(t, TypeVariable) or len(t.constraints) < 2:
|
if not isinstance(t, TypeVariable) or len(t.constraints) < 2:
|
||||||
raise CustomError(
|
raise CustomError(
|
||||||
'type guard only support basic type variables with constraints',
|
'type guard only support basic type variables with constraints',
|
||||||
|
|
Loading…
Reference in New Issue