ajhahn.de
← Flash
Flash 28 lines
// Wrapping compound assignment: `+%=`, `-%=`, and `*%=` store the wrapping
// result back into the target — the assignment form of the `+%`/`-%`/`*%`
// binops. The shapes here are the ones systems code leans on: folding into
// an accumulator array and advancing a monotone ring head that is allowed
// to wrap.
//
// This example lives in examples/register/ (not examples/) because the
// wrapping compound assigns are post-v0.5 grammar: the frozen stage0
// bootstrap compiler cannot parse them, so the file rides the fixpoint
// corpus (stage1 vs stage2) instead of the stage0-facing suites.

pub fn fold(state *mut [8]u32, a u32, b u32) {
    state[0] +%= a
    state[1] +%= b
}

pub const Ring = struct {
    head u64 = 0,
    tail u64 = 0,

    pub fn push(self *mut Ring, size u64) {
        self.head +%= 1
        if self.head -% self.tail > size {
            self.tail = self.head -% size
        }
    }
}