supports slice

This commit is contained in:
pca006132 2020-12-21 09:51:05 +08:00 committed by pca006132
parent 32f0135f28
commit 40c106ab6a
2 changed files with 21 additions and 5 deletions

View File

@ -170,11 +170,22 @@ def parse_subscript(ctx: Context,
value = parse_expr(ctx, sym_table, node.value) value = parse_expr(ctx, sym_table, node.value)
if not isinstance(value, ListType): if not isinstance(value, ListType):
raise CustomError(f'cannot take index of {value}') raise CustomError(f'cannot take index of {value}')
s = parse_expr(ctx, sym_table, node.slice)
i32 = ctx.types['int32'] i32 = ctx.types['int32']
if s == i32: if isinstance(node.slice, ast.Slice):
return value.params[0] if node.slice.lower is not None:
if parse_expr(ctx, sym_table, node.slice.lower) != i32:
raise CustomError(f'slice index must be int32')
if node.slice.upper is not None:
if parse_expr(ctx, sym_table, node.slice.upper) != i32:
raise CustomError(f'slice index must be int32')
if node.slice.step is not None:
if parse_expr(ctx, sym_table, node.slice.step) != i32:
raise CustomError(f'slice index must be int32')
return value
else: else:
# will support slice s = parse_expr(ctx, sym_table, node.slice)
raise CustomError(f'index of type {s} is not supported') if s == i32:
return value.params[0]
else:
raise CustomError(f'index of type {s} is not supported')

View File

@ -69,6 +69,11 @@ test_expr('a - a', {'a': A})
test_expr('[1, 2, 3][2]') test_expr('[1, 2, 3][2]')
test_expr('[[1], [2], [3]][2]') test_expr('[[1], [2], [3]][2]')
test_expr('[[1], [2], [3]][a]', {'a': i32}) test_expr('[[1], [2], [3]][a]', {'a': i32})
test_expr('[][:]', {})
test_expr('[1,2,3][:]', {})
test_expr('[1,2,3][1:3]', {})
test_expr('[1,2,3][:a:2]', {'a': I})
test_expr('[1,2,3][:a:2]', {'a': i32})
test_expr('a == a == a', {'a': I}) test_expr('a == a == a', {'a': I})
test_expr('a == a and 1 == 2', {'a': I}) test_expr('a == a and 1 == 2', {'a': I})