Re: Parameterizing inline C code

Diogo via Chicken-users <[email protected]>
Newsgroups gmane.lisp.scheme.chicken
Message-ID <[email protected]>
On Sun, Mar 09, 2025 at 08:46:08PM +0100, felix.winkelmann--- via Chicken-users wrote:
> > the syntax #> ... <# allows us to inline C code in CHICKEN code. Can I
> > "parameterize" it?
> >
> > I'd like to have something like this:
> >
> >     (begin-for-syntax
> >         (define count 0)
> >
> >         (define (foo-transformer x r c)
> >           (let ((name (generate-function-name x)))
> >             #>
> >             void ,name(void) {
> >                 printf("hello world: %s\n", c_special_func(,name));
> >             }
> >             <#
> >         )
> >     )
> >
> >     (define-syntax foo
> >       (er-macro-transformer foo-transformer))
> >
> >     (foo 1) ;; creates void foo_1(void) {...}
> >     (foo 2) ;; creates void foo_2(void) {...}
> >     ...
> >
> > I know that using define-external I could define a function foo_n and then
> > using foreign-safe-lambda* in the function definition of define-external
> > I could adapt the body as well. But that will add a lot of CHICKEN
> > bookkeeping and function calls. In my case I really just want a plain
> > C function to be generated.
> 
> "#> ... <#" is just sugar for "foreign-declare", so nothing prevents you from
> having a macro that constructs a string of arbitrary C code and expanding
> into a "foreign-declare" form. In the example above, you would probably
> replace the inner #> ... <# with something like
> 
> 	(let ((fd (r 'foreign-declare)))
> 	  `(,fd ,(format "void ~a(void) { ... }" name)))

That is excellent! I looked up the documentation for something like that 
but somehow simply missed it. Thank you.
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.