fixed type guard

pull/14/head
pca006132 2020-12-23 11:54:48 +08:00 committed by pca006132
parent 0453273d8b
commit b2ec75e157
2 changed files with 11 additions and 9 deletions

View File

@ -1,4 +1,4 @@
I = TypeVar('I', int32, Vec)
I = TypeVar('I', int32, float, Vec)
class Vec:
v: list[int32]
@ -6,8 +6,10 @@ class Vec:
self.v = v
def __add__(self, other: I) -> Vec:
if other is int32:
if type(other) == int32:
return Vec([v + other for v in self.v])
elif type(other) == float:
return Vec([v + int32(other) for v in self.v])
else:
return Vec([self.v[i] + other.v[i] for i in range(len(self.v))])

View File

@ -115,14 +115,14 @@ def parse_if_stmt(ctx: Context,
return_ty: Type,
node):
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 \
(isinstance(node.test.ops[0], ast.Is) or\
isinstance(node.test.ops[0], ast.IsNot)):
if not isinstance(node.test.left, ast.Name):
raise CustomError(
'type guard only support testing variables',
node.test)
t = parse_expr(ctx, sym_table, node.test.left)
(isinstance(node.test.ops[0], ast.Eq) or\
isinstance(node.test.ops[0], ast.NotEq)):
t = parse_expr(ctx, sym_table, node.test.left.args[0])
if not isinstance(t, TypeVariable) or len(t.constraints) < 2:
raise CustomError(
'type guard only support basic type variables with constraints',