Skip to content

Commit 7fd4eed

Browse files
gh-107: Fix TNS slice assignment.
1 parent 23fd086 commit 7fd4eed

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/interpreter.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,6 +2188,8 @@ ExecResult assign_index_chain(Interpreter* interp, Env* env, Expr* idx_expr, Val
21882188
return make_error("Out of memory", stmt_line, stmt_col);
21892189
}
21902190

2191+
bool rhs_applied_directly = false;
2192+
21912193
walker = idx_expr;
21922194
for (size_t i = 0; i < chain_len; i++) {
21932195
nodes[i] = walker;
@@ -2393,6 +2395,7 @@ ExecResult assign_index_chain(Interpreter* interp, Env* env, Expr* idx_expr, Val
23932395
free(starts); free(ends); free(orig_to_out);
23942396
// After slice assignment, set cur to base (no further chaining into this node)
23952397
cur = &base_val;
2398+
rhs_applied_directly = true;
23962399
continue;
23972400
}
23982401

@@ -2457,14 +2460,16 @@ ExecResult assign_index_chain(Interpreter* interp, Env* env, Expr* idx_expr, Val
24572460
}
24582461

24592462
// If we get here, the chain ended after resolving to a tensor element (e.g. a<1> = rhs)
2460-
if (cur->type != VAL_NULL && value_type_to_decl(cur->type) != value_type_to_decl(rhs.type)) {
2461-
out = make_error("Element type mismatch", stmt_line, stmt_col);
2462-
goto cleanup;
2463+
if (!rhs_applied_directly) {
2464+
if (cur->type != VAL_NULL && value_type_to_decl(cur->type) != value_type_to_decl(rhs.type)) {
2465+
out = make_error("Element type mismatch", stmt_line, stmt_col);
2466+
goto cleanup;
2467+
}
2468+
mtx_lock(&g_tns_lock);
2469+
value_free(*cur);
2470+
*cur = value_copy(rhs);
2471+
mtx_unlock(&g_tns_lock);
24632472
}
2464-
mtx_lock(&g_tns_lock);
2465-
value_free(*cur);
2466-
*cur = value_copy(rhs);
2467-
mtx_unlock(&g_tns_lock);
24682473

24692474
out = make_ok(value_null());
24702475

tests/test2.pre

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ ASSERT(NOT(ISTNS(0d5)))
109109
DEL(t)
110110
PRINT("Tensors: PASS\n")
111111

112+
PRINT("Testing slice indexing...")
113+
TNS: t = [0d1, 0d2, 0d3]
114+
t[0d1-0d2] = [0d4, 0d5]
115+
ASSERT(EQ(t, [0d4, 0d5, 0d3]))
116+
ASSERT(EQ(t[0d1-0d2], [0d4, 0d5]))
117+
ASSERT(EQ(t[0d1--0d1], [0d4, 0d5, 0d3]))
118+
t[*] = [0d6, 0d7, 0d8]
119+
ASSERT(EQ(t, [0d6, 0d7, 0d8]))
120+
ASSERT(EQ(t[*], [0d6, 0d7, 0d8]))
121+
DEL(t)
122+
PRINT("Slice indexing: PASS\n")
123+
112124
PRINT("Testing PAUSE/RESUME/PAUSED...")
113125
! Use a deterministic handshake: worker sets `pause_started` and
114126
! waits for `pause_go` to be set. Main waits for `pause_started`, then

0 commit comments

Comments
 (0)