order pb's methods by topic, and read rock-mode in the literal

A method can call one written below it now (sex#44), so pb's read as
an API: making one, asking about it, reading it, then the private
helpers. A path inside a compound literal follows the pointer
(sex#45). bytes keeps its local: the window starts where self stood
before it moves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-09 18:17:55 +03:00
parent fee9a1c28e
commit 10ec6236fd
2 changed files with 27 additions and 32 deletions

View File

@@ -12,23 +12,31 @@
(pub record pb ((p (* const u8)) (end (* const u8)) (err bool)))
;; TODO sex#44: the methods are ordered so each calls only those above
;; it; order them by topic once a method can call one below.
(ops pb
(pub fn of ((data (* const void)) (len size-t)) pb
(var p (* const u8) data)
(return #(pb : p (+ p len) false)))
(fn fail ((self (* pb))) void
(= (. self err) true)
(= (. self p) (. self end)))
(pub fn more ((self (* const pb))) bool
(return (< (. self p) (. self end))))
(pub fn len ((self (* const pb))) size-t
(return (cast (- (. self end) (. self p)) size-t)))
;; True when the rest of the window spells exactly S, e.g. a map
;; entry's key against "riverDistance".
(pub fn equals ((self (* const pb)) (s (* const char))) bool
(var n size-t (.len self))
(return (&& (== (strlen s) n) (== (memcmp (. self p) s n) 0))))
;; The next field's number, or 0 at the end. WIRE gets its wire type.
(pub fn field ((self (* pb)) (wire (* int))) u32
(if (! (.more self))
(return 0))
(var key u64 (.varint self))
(= (* wire) (cast (& key 7) int))
(return (cast (>> key 3) u32)))
(pub fn varint ((self (* pb))) u64
(var v u64 0)
(for (var shift int 0) (< shift 64) (+= shift 7)
@@ -47,20 +55,11 @@
(pub fn int32 ((self (* pb))) i32
(return (cast (.varint self) i32)))
;; The next field's number, or 0 at the end. WIRE gets its wire type.
(pub fn field ((self (* pb)) (wire (* int))) u32
(if (! (.more self))
(return 0))
(var key u64 (.varint self))
(= (* wire) (cast (& key 7) int))
(return (cast (>> key 3) u32)))
;; A length-delimited field as a reader of its own: a submessage, a
;; string, or a packed array.
(pub fn bytes ((self (* pb))) pb
(var len u64 (.varint self))
;; TODO sex#45: use (. self p) in the literals once member access
;; inside a compound literal follows the pointer.
;; where the window starts, before self moves past it
(var p (* const u8) (. self p))
(if (> len (.len self))
(do
@@ -69,21 +68,19 @@
(+= (. self p) len)
(return #(pb : p (+ p len) false)))
;; True when the rest of the window spells exactly S, e.g. a map
;; entry's key against "riverDistance".
(pub fn equals ((self (* const pb)) (s (* const char))) bool
(var n size-t (.len self))
(return (&& (== (strlen s) n) (== (memcmp (. self p) s n) 0))))
(fn advance ((self (* pb)) (n size-t)) void
(if (> n (.len self))
(.fail self)
(+= (. self p) n)))
(pub fn skip ((self (* pb)) (wire int)) void
(switch wire
(case WIRE-VARINT (.varint self) (break))
(case WIRE-BYTES (.bytes self) (break))
(case WIRE-FIXED64 (.advance self 8) (break))
(case WIRE-FIXED32 (.advance self 4) (break))
(default (.fail self)))))
(default (.fail self))))
(fn advance ((self (* pb)) (n size-t)) void
(if (> n (.len self))
(.fail self)
(+= (. self p) n)))
(fn fail ((self (* pb))) void
(= (. self err) true)
(= (. self p) (. self end))))

View File

@@ -908,11 +908,9 @@
(if (cursor-block a (& bx) (& bz))
(do
(var value int (rock-query-value a))
;; TODO sex#45: use (. a rock-mode) in the literal once member
;; access inside a compound literal follows the pointer.
(var mode int (. a rock-mode))
(var mineral int (if (!= (& (. a overlays) MINERAL-OVERLAY-BIT) 0) (. a mineral) -1))
(hover-worker/ask (. a hover-worker) #(hover-query : bx bz mode value mineral))))
(hover-worker/ask (. a hover-worker)
#(hover-query : bx bz (. a rock-mode) value mineral))))
(= (. a hover) (hover-worker/result (. a hover-worker))))
(fn rock-name ((a (* app)) (rock int)) (* const char)