Hello I would like to do something similar to the following within my C program: ***************** char* condition = "(2 < 4) & (3 > 7"); int boole; ... boole = evaluate(condition); ***************** where evaluate is a scheme function already written and compiled by gsc. Most of the documentation I have seen relates more to the reversed situation - calling C functions from scheme. I have taken a look at the client example program in the tests directory, but it is not clear to me exactly how the string is treated by scheme - ie, in what form it is going to be, if that makes any sense. Obviously I would prefer it as a list of some sort, but I don't know if that's possible. I am using Linux and gcc 4, if that makes a difference. Thanks for your time Alex
Afficher les réponses par date
On 29-Jan-09, at 1:44 AM, Alex Ryu wrote:
Hello I would like to do something similar to the following within my C program:
char* condition = "(2 < 4) & (3 > 7"); int boole; ... boole = evaluate(condition);
where evaluate is a scheme function already written and compiled by gsc. Most of the documentation I have seen relates more to the reversed situation - calling C functions from scheme. I have taken a look at the client example program in the tests directory, but it is not clear to me exactly how the string is treated by scheme - ie, in what form it is going to be, if that makes any sense. Obviously I would prefer it as a list of some sort, but I don't know if that's possible. I am using Linux and gcc 4, if that makes a difference. Thanks for your time Alex
The example code in tests/client.c and tests/server.scm is very close to what you want. One difference is that you want the function to return a boolean. The other difference is that in your example you are using an infix expression. Is this really what you want? The code in tests/server.scm uses Scheme's normal prefix syntax. If you really want infix then you could change the eval-string function in tests/server.scm to be
(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
On Thu, Jan 29, 2009 at 6:54 AM, Marc Feeley feeley@iro.umontreal.ca wrote:
On 29-Jan-09, at 1:44 AM, Alex Ryu wrote:
Hello I would like to do something similar to the following within my C program:
char* condition = "(2 < 4) & (3 > 7"); int boole; ... boole = evaluate(condition);
where evaluate is a scheme function already written and compiled by gsc. Most of the documentation I have seen relates more to the reversed situation - calling C functions from scheme. I have taken a look at the client example program in the tests directory, but it is not clear to me exactly how the string is treated by scheme - ie, in what form it is going to be, if that makes any sense. Obviously I would prefer it as a list of some sort, but I don't know if that's possible. I am using Linux and gcc 4, if that makes a difference. Thanks for your time Alex
The example code in tests/client.c and tests/server.scm is very close to what you want. One difference is that you want the function to return a boolean. The other difference is that in your example you are using an infix expression. Is this really what you want? The code in tests/server.scm uses Scheme's normal prefix syntax. If you really want infix then you could change the eval-string function in tests/server.scm to be
(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?
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