Skip to content

Add list_insert_at and list_slice builtins (#544)#677

Merged
InauguralPhysicist merged 1 commit into
InauguralSystems:mainfrom
Nitjsefnie-OSC:list-insert-at-slice
Jul 20, 2026
Merged

Add list_insert_at and list_slice builtins (#544)#677
InauguralPhysicist merged 1 commit into
InauguralSystems:mainfrom
Nitjsefnie-OSC:list-insert-at-slice

Conversation

@Nitjsefnie

Copy link
Copy Markdown
Contributor

Closes #544.

Adds the two missing C-backed list builtins so reorder and range-copy no longer go through interpreter-speed hand-rolled loops:

Both are registered next to their duals in register_builtins, added to SANDBOX_ALLOW (both duals were already there — same membership rule: pure, touches only handed-in values), and documented in docs/BUILTINS.md (required by tools/stdlib_index_check.sh).

One C detail worth a glance: the middle-insert path grows the array by appending the old last element (reusing list_append's capacity/arena handling), then memmoves the tail up. Since list_append increfs, the old last element would otherwise be left with one unbalanced reference after the memmove overwrites its original slot — so there's an explicit val_decref(old_last) afterwards to keep its refcount matched to its single remaining slot (it still occupies slot[count] at that point, so the decref can't free it). Net: the inserted value gains one reference, every existing element keeps exactly one.

Tests: tests/test_list_insert_at.eigs and tests/test_list_slice.eigs mirror test_list_remove_at.eigs (insert middle/start/end/out-of-bounds; slice normal/empty/full/clamped) — plus a repeated-middle-insert stress case that also guards the refcount path. Full suite: 3118/3118 passed.


Generated by Claude Opus 4.8 (brief, review), Kimi K3 (implementation)

Adds the C-backed list_insert_at of [list, i, v] (dual of list_remove_at) and
list_slice of [list, start, end] (dual of copy_into), so reorder and range-copy
no longer go through interpreter-speed hand-rolled loops. Registered and tested
alongside the existing list builtins.

Closes InauguralSystems#544

Co-Authored-By: Kimi K3 <noreply@kimi.com>
@InauguralPhysicist
InauguralPhysicist merged commit 72afd7e into InauguralSystems:main Jul 20, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

list_insert_at and list_slice builtins — the missing duals of list_remove_at and copy_into

2 participants