chibi-scheme icon indicating copy to clipboard operation
chibi-scheme copied to clipboard

[Feature request] Variadic C functions

Open lighth7015 opened this issue 5 years ago • 9 comments

It would be nice if there were a way to use the variadic argument machinery from within C, so you don't have to wrap everything scheme.

lighth7015 avatar Mar 11 '19 03:03 lighth7015

What specifically do you want to wrap?

ashinn avatar Mar 11 '19 10:03 ashinn

For example, if you wanted to create an FFI with list-like semantics, you have to pass in a list, you can't just throw all of your parameters at it.

lighth7015 avatar Mar 11 '19 16:03 lighth7015

That's not specific, that's just restating the request.

Do you want to be able to wrap existing variadic C functions like printf, and if so which one?

Or do you want to write new variadic functions in C specifically for Chibi, and if so what?

ashinn avatar Mar 12 '19 15:03 ashinn

I would like to write new variadic functions, specifically for Chibi; I am creating a modern programming language equivalent of a super old old language called FDO, and need to be able to nest scheme structures, in order to represent graphical data structures for an application to be able to parse and display properly.

man_start_object <trigger, "Welcome">
act_replace_select_action
<
	uni_start_stream
	sm_m_send_token_arg <"f1", 32-30>
	uni_end_stream
>
man_end_object

lighth7015 avatar Mar 12 '19 17:03 lighth7015

Since it's a new function specifically for Chibi, why not write it to take a Scheme list? If the primitive is named %act_replace_select_action, it can take a list, and then in Scheme you can write:

(define (act-replace-select-action . args)
  (%act_replace_select_action args))

ashinn avatar Mar 14 '19 14:03 ashinn

This needs to be in C, because a GUI client will be taking in all of this and making sense of it.

lighth7015 avatar Mar 14 '19 16:03 lighth7015

Then you can have 2 C functions: one which takes a Scheme list, and another variadic C function which builds a Scheme list to call the first function.

Although a C function can accept variadic arguments with va_arg, there's no standard way to dynamically generate an argument list for such a C function at runtime.

Chibi's FFI is currently entirely static, so translates directly to C. Allowing dynamic argument lists would require platform-specific assembly (like libffi), which is not something I want to maintain.

ashinn avatar Mar 15 '19 18:03 ashinn

Oh, that's the reason. That's fine; I'll come up with logic to unpack a scheme list. I expect posting many more questions on this thread, then. :p

lighth7015 avatar Mar 16 '19 02:03 lighth7015

So, question- if I use va_list, can I get chibi-scheme to give me a list of passed arguments instead? (Probably not, I was just curious.)

lighth7015 avatar Mar 16 '19 17:03 lighth7015