Skip to main content
Participant
November 23, 2007
Answered

simple expression evaluator

  • November 23, 2007
  • 4 replies
  • 264 views
Hi all,

need some help with an expression evaluation:
user inputs a string like "x*x+dx*dy" or "x*dx-y*dy" and I would like Flash to covert this to an actionscriptline so in the code it appears like:

x=3;
dx=0.6;
dy=0.1;
aa=x*x+dx*dy;
trace(aa);

and the output would be: 9.06

Now my problem is converting the operator.
Anybody for a solution for this newbie?

thnx
Rob
This topic has been closed for replies.
Correct answer robbie50
ok, thnx, i will follow your advice

Rob

4 replies

Inspiring
November 26, 2007
You're welcome. Its probably the easy/lazy way out in terms of advice...
That code is probably overkill if its only ever going to be addition and subtraction. But if there's the potential for your expressions to have (for example) trig functions or more complicated operators then it saves you writing it.
robbie50AuthorCorrect answer
Participant
November 26, 2007
ok, thnx, i will follow your advice

Rob
Inspiring
November 25, 2007
Ok, so I think my advice stands then...

You could write your own expression parser, using eval("x") when you came to parse an x in the string etc...and its probably not too complicated to do if you're just dealing with multiplication and addition.... or you could use existing code like the example in the link I posted earlier to parse the expression (I've used that code before for something more complex).

If you were writing your own parser you could use eval("x") ...but if it were me... I would do the opposite. That is to split and join the string with the variable names, subsitituting their values into the string and then run it through that expression parser. Its basically just calculating a string version of a numeric expression then with no variables.

For your variable --> value substitution (split and then join) you need to do it in the order of the

dx and dy

then x and y

so you get it to work correctly.
Inspiring
November 24, 2007
I'm not sure I understand your exact requirements.

Do you want to evaluate the string (calculate its value) based on the actual values of the variables?

For that you could use String methods to replace the text names of the variables with their acual values
aa=stringExpression.split("dx").join(String(dx));
etc...
And then use a some 3rd party expression parsing code
E.g. http://flashgizmo.com/parser.html

robbie50Author
Participant
November 25, 2007
thank you for your reply,
I would like the string to be used as an actionscriptline in Flash; so eg...if the input of a user would be: x*x+dy*dy
then the as code would be:

aa=x*x+dy*dy;

so if x=3 and dy = 0.1 the value of aa would be 9.06.

But if a user should input x+dy then the code would be:
aa=x+dy;
and the value of aa would be 3.01.
So the user is writing the programming line after aa= ;

Splitting the string is no problem and with eval("x") I can change the string into a variable, but the problem is the operator * since eval("*") doesn't work...