anistark / anistark/waspy

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

未关闭 适合新手
#122 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
collections good first issue priority-low
主要语言
Rust
星标
22
派生
6
平均合并
1 天 3 小时
30 天内合并 PR
6

描述

## 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.

贡献指南

打开贡献指南

调研方向

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.

由索引模型根据 Issue 内容生成。

评估

技术栈
rust, wasm
领域
compilers
Issue 类型
重构
难度
2/5
预计耗时
1-3 小时
活跃度
活跃
描述清晰度
描述清楚
新手友好度
85/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。