5 Replies Latest reply on Nov 26, 2007 8:27 AM by Greg Dove

    simple expression evaluator

      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:


      and the output would be: 9.06

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

        • 1. Re: simple expression evaluator
          Greg Dove Level 4
          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
          And then use a some 3rd party expression parsing code
          E.g. http://flashgizmo.com/parser.html

          • 2. Re: simple expression evaluator
            robbie50 Level 1
            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:


            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:
            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...

            • 3. simple expression evaluator
              Greg Dove Level 4
              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.
              • 4. Re: simple expression evaluator
                robbie50 Level 1
                ok, thnx, i will follow your advice

                • 5. Re: simple expression evaluator
                  Greg Dove Level 4
                  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.