supports slice
This commit is contained in:
parent
32f0135f28
commit
40c106ab6a
@ -170,11 +170,22 @@ def parse_subscript(ctx: Context,
|
||||
value = parse_expr(ctx, sym_table, node.value)
|
||||
if not isinstance(value, ListType):
|
||||
raise CustomError(f'cannot take index of {value}')
|
||||
s = parse_expr(ctx, sym_table, node.slice)
|
||||
i32 = ctx.types['int32']
|
||||
if isinstance(node.slice, ast.Slice):
|
||||
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:
|
||||
s = parse_expr(ctx, sym_table, node.slice)
|
||||
if s == i32:
|
||||
return value.params[0]
|
||||
else:
|
||||
# will support slice
|
||||
raise CustomError(f'index of type {s} is not supported')
|
||||
|
||||
|
@ -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]][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 and 1 == 2', {'a': I})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user