anistark / anistark/waspy

refactor: share the negative-index normalize and clamp sequence, and name the insert scratch locals

Ouverte Adaptée aux débutants
#122 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
collections good first issue priority-low
Langage dominant
Rust
Étoiles
22
Forks
6
Merge moyen
1 j 3 h
PR mergées (30 j)
6

Description

## Summary

#121 fixed `list.insert` by adding a negative-index normalize-and-clamp sequence to its match arm. That sequence is semantically identical to one the slice arm already has, and the arm addresses its scratch locals by raw offset rather than by name. Both are worth cleaning up in one follow-up. #121 has merged, so this is ready to pick up.

## Current state

- The slice arm carries a `normalize_and_clamp` closure that normalizes a negative bound by adding the length, then applies `max(0)` and `min(length)`: [src/compiler/expression.rs:5954](https://github.com/anistark/waspy/blob/2141159/src/compiler/expression.rs#L5954)
- The `insert` arm emits the same three `select` steps inline, 23 instruction emissions duplicating that logic: [src/compiler/expression.rs:9878](https://github.com/anistark/waspy/blob/2141159/src/compiler/expression.rs#L9878)
- The same arm writes `ctx.temp_local + 6` about fifteen times and `ctx.temp_local + 2` several times, with the meaning carried only by a trailing comment on the first use: [src/compiler/expression.rs:9857](https://github.com/anistark/waspy/blob/2141159/src/compiler/expression.rs#L9857)
- Neighbouring helpers in the same file bind names first, for example `let newlen = ctx.temp_local + 6;` at [src/compiler/expression.rs:355](https://github.com/anistark/waspy/blob/2141159/src/compiler/expression.rs#L355) and `let cap = ctx.temp_local + 11;` at [src/compiler/expression.rs:1296](https://github.com/anistark/waspy/blob/2141159/src/compiler/expression.rs#L1296)

## Proposed change

- Lift the closure to a free `fn emit_normalize_and_clamp(func, index_local, len_local)` and call it from both the slice arm and the `insert` arm.
- Bind `let index = ctx.temp_local + 6;` and `let length = ctx.temp_local + 2;` at the top of the `insert` arm and use those names throughout, matching the style of the surrounding helpers.

## Acceptance criteria

- One helper emits the normalize-and-clamp sequence, and both call sites use it.
- The `insert` arm addresses its scratch locals by name, and the helper's doc comment records which scratch offsets it owns.
- The existing suite passes unchanged, including `list_insert_honours_position` and the slice tests. This is a pure refactor, so no test needs new expectations.

## References

- Follow-up to #121, which introduced the duplicated sequence.

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Start in src/compiler/expression.rs at the slice arm near line 5954 and the insert arm near lines 9857-9878; compare the existing closure and scratch-local uses, along with the nearby named-local examples. Confirm that one shared normalize-and-clamp helper is used by both arms, the insert locals are named, and the unchanged suite passes, including list_insert_honours_position and the slice tests.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
rust, wasm
Domaine
compilers
Type d'issue
Refactorisation
Difficulté
2/5
Temps estimé
1-3 heures
Activité
Active
Clarté
Clairement spécifiée
Accessibilité débutants
85/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.