1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00
This commit is contained in:
Ibrahim Numanagić 2023-02-02 09:45:40 -08:00
parent ad7409b5ee
commit 8fb65dcaa6
2 changed files with 23 additions and 3 deletions

View File

@ -745,14 +745,18 @@ TypecheckVisitor::transformStaticTupleIndex(const ClassTypePtr &tuple,
sliceAdjustIndices(sz, &start, &stop, step);
// Generate a sub-tuple
auto var = N<IdExpr>(ctx->cache->getTemporaryVar("tup"));
auto ass = N<AssignStmt>(var, expr);
std::vector<ExprPtr> te;
for (auto i = start; (step > 0) ? (i < stop) : (i > stop); i += step) {
if (i < 0 || i >= sz)
E(Error::TUPLE_RANGE_BOUNDS, index, sz - 1, i);
te.push_back(N<DotExpr>(clone(expr), classItem->fields[i].name));
te.push_back(N<DotExpr>(clone(var), classItem->fields[i].name));
}
return {true, transform(N<CallExpr>(
N<DotExpr>(format(TYPE_TUPLE "{}", te.size()), "__new__"), te))};
ExprPtr e = transform(N<StmtExpr>(
std::vector<StmtPtr>{ass},
N<CallExpr>(N<DotExpr>(format(TYPE_TUPLE "{}", te.size()), "__new__"), te)));
return {true, e};
}
return {false, nullptr};

View File

@ -208,6 +208,22 @@ print a[1] #: 2s
print a[0:2], a[:2], a[1:] #: (1, '2s') (1, '2s') ('2s', 3.3)
print a[0:3:2], a[-1:] #: (1, 3.3) (3.3)
#%% static_index_side,barebones
def foo(a):
print(a)
return a
print (foo(2), foo(1))[::-1]
#: 2
#: 1
#: (1, 2)
print (foo(1), foo(2), foo(3), foo(4))[2]
#: 1
#: 2
#: 3
#: 4
#: 3
#%% static_index_lenient,barebones
a = (1, 2)
print a[3:5] #: ()