unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* simple example to evaluate strings
@ 2013-12-21  3:57 Josh Stratton
  2013-12-21 13:06 ` Paul Emsley
  0 siblings, 1 reply; 2+ messages in thread
From: Josh Stratton @ 2013-12-21  3:57 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 898 bytes --]

I'm completely new to guile and am trying to find a simple example of
evaluating a string inside C with some kind of context.  I want to execute
small self-contained strings provided by a user.  I've looked at various
examples, but everything I've seen seems uses the shell, which seems like
more than I need.  The context doesn't need to persist between evals.

Is there an example that would allow me to do something like this?

float evaluateExpression(char* expression)
{
    if (!initialized) {
        scm_init_guile();
        initialized = true;
    }

    // making stuff up here
    some_guile_context = ...
    some_guile_context.add("someDef", 5); // add some definitionos the
expression might expect to be defined
    some_guile_response = some_guile_context.eval(expression); //
expression might use "someDef" like (* someDef 8)

    return scm_arg_to_float(scm_guile_response[0]);
}

[-- Attachment #2: Type: text/html, Size: 1164 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: simple example to evaluate strings
  2013-12-21  3:57 simple example to evaluate strings Josh Stratton
@ 2013-12-21 13:06 ` Paul Emsley
  0 siblings, 0 replies; 2+ messages in thread
From: Paul Emsley @ 2013-12-21 13:06 UTC (permalink / raw)
  To: Josh Stratton; +Cc: guile-user@gnu.org

On 21/12/13 03:57, Josh Stratton wrote:
> I'm completely new to guile and am trying to find a simple example of 
> evaluating a string inside C with some kind of context.  I want to 
> execute small self-contained strings provided by a user.  I've looked 
> at various examples, but everything I've seen seems uses the shell, 
> which seems like more than I need.  The context doesn't need to 
> persist between evals.
>
> Is there an example that would allow me to do something like this?
>
> float evaluateExpression(char* expression)
> {
>     if (!initialized) {
>         scm_init_guile();
>         initialized = true;
>     }
>
>     // making stuff up here
>     some_guile_context = ...
>     some_guile_context.add("someDef", 5); // add some definitionos the 
> expression might expect to be defined
>     some_guile_response = some_guile_context.eval(expression); // 
> expression might use "someDef" like (* someDef 8)
>
>     return scm_arg_to_float(scm_guile_response[0]);
> }

http://www.gnu.org/software/guile/manual/html_node/Fly-Evaluation.html

SCM r = scm_c_eval_string(expression);
if (scm_is_true(scm_number_p(r))
    value = scm_to_double(r);

But you will probably want to wrap the evaluation of expression in a 
scm_catch().

P.






^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-12-21 13:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-21  3:57 simple example to evaluate strings Josh Stratton
2013-12-21 13:06 ` Paul Emsley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).