On Sat Sep 13, 2025 at 3:07 PM CEST, Diogo via Chicken-users wrote:
>
> So I thought this would work:
>
> (define-struct rbuf
> (cap char)
> (head char)
> (tail char)
> (buf bytevector))
>
> Problem 1: That generates fields with 32 bits, not chars. I am programming a
> microcontroller with CRUNCH and I want to have full control over such types.
> The following worked, however:
>
> (define-struct rbuf
> (cap (typename uint8_t))
> (head (typename uint8_t))
> (tail (typename uint8_t))
> (buf bytevector))
CRUNCH's "char" is not a byte, it must hold UNICODE code points, which
may be unexpected. Using typename is the proper way to handle this, but
perhaps we should add a new type ("byte") or make "char" 8 bits and
add "wchar_t"? I'm open to suggestions.
>
> Problem 2: Besides char not enforcing 8 bit fields, I also had the issue that
> I forgot to include (crunch aggregate-types) and `chicken-crunch` gives no
> reasonable error (no string) and simply returns an error code != 0.
That's a bug in the compiler. The initial treeshake leaves an empty module,
because toplevel calls are silently ignored, to make interoperability
between crunched code and normal Scheme code easier, but this is not
particularly intuitive. I will add an error message in this case,
but I'm not sure how to treat toplevel forms that are not definitions.
Thanks for pointing this out.
>
> Problem 3: Next, I want to define accessors. This is probably my fault, but
> the
> following code
>
> (define-compound-accessors (struct rbuf)
> (mk-rbuf cap head tail buf)
> (cap rbuf-cap)
> (head rbuf-head rbuf-head!)
> (tail rbuf-tail rbuf-tail!)
> (buf rbuf-buf))
>
> generates incorrect/unexpected modifier methods, eg, `rbuf-head!` becomes:
>
> void rbuf_X2dhead_X21(struct rbuf _s, uint8_t _x) { _s.head = _x; }
>
> Only the copy of the struct in the scope of the function gets modified. I was
> expecting to see this:
>
> void rbuf_X2dhead_X21(struct rbuf *_s, uint8_t _x) { _s->head = _x; }
"define-compound-accessors" distinguishes between structs and pointers
to structs and generates different accessors. Just wrap change the
code to "(define-compound-accessors (pointer (struct buf)) ...)",
which will generate a constructor and accessors that return and
accept pointers.
cheers,
felix