[Q] Macro for defining function from string

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Q] Macro for defining function from string

Sungjin Chun
Hi,
In writing FFI module for C library there’s too much repetition; so I’d like to write some
macro to reduce this repetition.

What I’d like to write is something as follows: (define-my-bindings …)

(define-my-bindinngs “Float”) should emit following code.

(define float-xxx-xxx (foreign-lambda float (string-append “TH” “Float” “Storage_xxx”))

What I cannot do now is generating float-xxx-xxx like function id from “float-xxx-xxx” string.

How can I convert string to the id?

Thanks in advance.
_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users
Reply | Threaded
Open this post in threaded view
|

Re: [Q] Macro for defining function from string

Peter Bex
On Fri, Apr 05, 2019 at 06:42:43PM +0900, Sungjin Chun wrote:

> Hi,
> In writing FFI module for C library there’s too much repetition; so I’d like to write some
> macro to reduce this repetition.
>
> What I’d like to write is something as follows: (define-my-bindings …)
>
> (define-my-bindinngs “Float”) should emit following code.
>
> (define float-xxx-xxx (foreign-lambda float (string-append “TH” “Float” “Storage_xxx”))
>
> What I cannot do now is generating float-xxx-xxx like function id from “float-xxx-xxx” string.
>
> How can I convert string to the id?
Try string->symbol:

http://api.call-cc.org/5/doc/scheme/string-%3Esymbol
also documented here:
http://wiki.call-cc.org/man/5/Module%20scheme#symbols

Cheers,
Peter

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [Q] Macro for defining function from string

Kristian Lein-Mathisen


Here's another example, perhaps easier to follow:

(import-for-syntax (only (chicken string) conc))
(define-syntax syntax-conc
  (er-macro-transformer
   (lambda (x r t)
     (string->symbol (apply conc (intersperse (cdr x) '-))))))

(expand '(syntax-conc "a" "b" "c")) ;; ==> a-b-c

I would recommend using syntax-rules whenever possible, 
and only using er-macro-transformer when you have to. So, your syntax-rules could
expand to syntax-conc.
K.



_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users