Structs in CRUNCH

Diogo via Chicken-users <[email protected]>
Newsgroups gmane.lisp.scheme.chicken
Message-ID <[email protected]>
Dear CRUNCHers, I am trying to create a simple ring buffer and here are a few
issues I encountered.

The ring buffer is supposed to be very small and only contain bytes. It is
sufficient to have 4 fields: capacity, head, tail, and byte array.

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))

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.

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; }


Any feedback is appreciated. Thank you in advance!

Best,
- Diogo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.