Page 1 of 1

Functions in input during runtime

Posted: Tue May 10, 2011 4:49 pm
by panurge
Hi, there's a little question (an "is it possible?" kind of question) for you gurus :)

I'd like to make a program that works on mathematical functions. I'd like to let the user define it's own functions during runtime. How can i tell my program to evaluate it?

E.g. The user wants to work on the sin(x) function. He writes the string "sin(x)" in a proper window and presses enter: BLAM! the program understands it.

Have i to program a parser? (that would be the long and tricky way)
I know that some languages have a "eval" function so that, if you give them a command like "eval(string)", where string="sin(x)", the resulting action is the evaluation of the sin of x! That would be a very efficient trick to bypass the need of a parser...

The question is a similar trick possible in ZGE?

Thanks in advance!

Posted: Tue May 10, 2011 6:39 pm
by Kjell
Hi panurge,

As far as I know all scripts are compiled by the IDE, and thus no compiler is included in the runner / stand-alone exe's. However, you do have a couple of options ..

- Write your own parser function using the ZLibrary component.
- Write / use a DLL that does the evaluation "externally".
- Use the WebOpen component to resolve the query online ( using PHP ).

Would be nice if a ( sub-set of the ) compiler could be included, but no idea how difficult that is & how much kilobytes that would add.

K

Posted: Wed May 11, 2011 10:41 pm
by panurge
uhmmm, thanks... i'll have to think about it. It's a hard task to reckon oneself's limited skills! :oops:

Posted: Thu May 12, 2011 6:52 am
by VilleK
Kjells reply is correct regarding the options doing this in ZGE.

Your best alternative for quickly doing something like that is probably Javascript. With html5 and the performance of the latest browsers it should work great.

Posted: Thu May 12, 2011 9:27 am
by Kjell
Eh?

You're forgetting that ZGE doesn't include a JS engine :P It'll be returned as plain-text instead of being executed.

A DLL ( dotMath, MTParser, muParser, Ucalc ) is probably your best bet. Keep in mind that ZGE doesn't support doubles, so you might need to make some changes and re-compile the DLL. The oddball solution of using a remote webserver is easiest though. Attached is a very-limited & unsafe* setup ( and a screenshot ).

*You want to add some security before usage on a public server.

K

Posted: Thu May 12, 2011 12:15 pm
by VilleK
What I meant to say was the best non-zge alternative would be javascript.

The remove server example is very clever Kjell.