Functions in input during runtime

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
User avatar
panurge
Posts: 22
Joined: Sat Jul 04, 2009 11:08 pm

Functions in input during runtime

Post 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!
User avatar
Kjell
Posts: 1924
Joined: Sat Feb 23, 2008 11:15 pm

Post 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
User avatar
panurge
Posts: 22
Joined: Sat Jul 04, 2009 11:08 pm

Post by panurge »

uhmmm, thanks... i'll have to think about it. It's a hard task to reckon oneself's limited skills! :oops:
User avatar
VilleK
Site Admin
Posts: 2371
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Post 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.
User avatar
Kjell
Posts: 1924
Joined: Sat Feb 23, 2008 11:15 pm

Post 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
Attachments
eval.zip
(670 Bytes) Downloaded 492 times
eval.jpg
eval.jpg (5.79 KiB) Viewed 7864 times
User avatar
VilleK
Site Admin
Posts: 2371
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Post by VilleK »

What I meant to say was the best non-zge alternative would be javascript.

The remove server example is very clever Kjell.
Post Reply