Re: General-purpose Scheme groveler hga@xxxxxx 27 Jul 2020 15:32 UTC

[ This copy sent to the list after Lassi's agreement. ]

Should this be CC'ed to the SRFI 198 list?

This is cool enough I'm already thinking about how I can use
it, vs. e.g. the Chibi automagic FFI generator.

However, currently the SRFI omits your catalog APIs in favor
of a future SRFI, in large part because I expect that people
will use the SRFI in an ad hoc manner.  Which for libraries
like libsodium is pretty much the only want to use it, there's
no organization to its errors beyond the function reporting an
error.  And of course to keep the API as small as is reasonable.

Is that decision wrong?

To recap, here's the omitted APIs from https://github.com/lassik/scheme-foreign-error-wip

(provide-foreign-error error-set symbol number messages)
(foreign-error-sets) → list of symbols
(foreign-error-set:fold kons knil error-set)
(foreign-error-set:by-symbol set symbol) → ferr
(foreign-error-set:by-number set number) → ferr

- Harold

----- Original message -----
From: Lassi Kortela <xxxxxx@lassi.io>
Date: Monday, July 27, 2020 10:00 AM

Just wrote this: <https://github.com/lassik/scheme-c-groveler>

It lets you write Scheme code like this:

(let ((g (make-c-groveler)))
   (grovel-c-include g "sys/stat.h")
   (grovel-c-include g "sys/utsname.h")
   (grovel-c-include g "errno.h")
   (grovel-c-include g "sqlite3.h")
   (grovel-c-struct-size g "stat")
   (grovel-c-struct-field-size g "stat" "st_mtime")
   (grovel-c-struct-field-offset g "stat" "st_mtime")
   (grovel-c-struct-field-size g "utsname" "sysname")
   (grovel-c-constant-integer-ifdef g "EADDRINUSE")
   (grovel-c-type-signedness g "char")
   (grovel-c-type-signedness g "pid_t")
   (grovel-c-type-signedness g "size_t")
   (grovel-c-type-size g "pid_t")
   (grovel-c-constant-integer-ifdef g "SQLITE_READONLY")
   (write-string (c-groveler->string g)))

And emits a C program to looks for those constants you wanted.

The C program prints them in a standard LOSE (line-oriented
S-expressions) format that looks like this:

(size struct-stat 144)
(size struct-stat.st_mtime 8)
(offset struct-stat.st_mtime 48)
(size struct-utsname.sysname 256)
(value EADDRINUSE 48)
(signedness char signed)
(signedness pid_t signed)
(signedness size_t unsigned)
(size pid_t 4)
(value SQLITE_READONLY 8)

We can use this kind of tool to generate error catalogs for SRFI 198.
I'll submit a SRFI about the groveler at some point (feedback on it is
welcome). It would be nice if all C-facing Scheme implementations had a
standard groveler interface and it emitted a standard LOSE format as
above. The SRFI could also provide a procedure to run the C compiler and
parse the LOSE output into Scheme instead of merely printing out the C
program.