rxi / fe

A tiny, embeddable language implemented in ANSI C
MIT License
1.31k stars 82 forks source link

how do you extract values from the code? #15

Open derpyzza opened 2 years ago

derpyzza commented 2 years ago

like if i had a fe file with the code: (= input "hello there") and i wanted to access the value of the input variable in c, how would i do that?

jminor commented 2 years ago

I would try fe_tostring(ctx, fe_symbol(ctx, "myvariable"))

You might have to use fe_eval in between: fe_tostring(ctx, fe_eval(ctx, fe_symbol(ctx, "myvariable")))

There's an example of using fe_eval under "Calling a Function" here: https://github.com/rxi/fe/blob/master/doc/capi.md#calling-a-function

derpyzza commented 2 years ago

thanks for the reply! i'll try that out

ooichu commented 1 year ago
fe_Object *obj = fe_eval(ctx, fe_symbol(ctx, "input"));

Don't forget to take care of restoring the GC stack if you're not doing it inside a fe function. To avoid creating the 'input' symbol every time (via fe_symbol), you can create it beforehand. This will be much faster.