[gambit-list] C Embedding question

Marc Feeley feeley at iro.umontreal.ca
Thu Jan 29 10:56:57 EST 2009


On 29-Jan-09, at 10:38 AM, Alex Ryu wrote:

>>
>> (c-define (evaluate str) (char-string) bool "evaluate" "extern"
>> (catch-all-errors
>>   (lambda () (eval (read-from-string (string-append "\\" str  
>> ";"))))))
>>
>> This will use Gambit's SIX parser (Scheme infix syntax extension).
>>
>> This is untested code.
>>
>> Marc
>>
>>
> Thank you for the advice and sorry for being obtuse.  Do I not need a
> write-to-string before the eval?

The C "evaluate" function that you have in your example is returning a  
Boolean value.  So you want the Scheme "evaluate" function to return a  
Boolean value (i.e. #f is false, and anything else is true).  The c- 
define I suggested, i.e.

(c-define (evaluate str) (char-string) bool "evaluate" "extern" ...)

defines the return type to be "bool", so the FFI interface will  
convert the Scheme value that is returned by  the Scheme "eval" into a  
C "bool" value (which by the way is the "int" type in C, and the  
"bool" type in C++).

A call to write-to-string would only be useful if you wanted your C  
"evaluate" function to return a string with the external  
representation of the value returned by "eval".  The c-define would be:

(c-define (evaluate str) (char-string) char-string "evaluate"  
"extern" ...)

You could do that but then in your C code you would have to check if  
the string returned by "evaluate" is "#f", which is clumsy if you are  
really calling "evaluate" for testing conditions.

Marc




More information about the Gambit-list mailing list