title: Creating a Basic Formula Editor in JavaScript
body: <p>I'm working on creating a basic RPG game engine prototype using JavaScript and canvas.  I'm still working out some design specs on paper, and I've hit a bit of a problem I'm not quite sure how to tackle.</p>
  
  <p>I will have a Character object that will have an array of Attribute objects.  Attributes will look something like this:</p>
  
  <pre><code>function(name, value){
      this.name = name;
      this.value = value;
      ...
  }
  </code></pre>
  
  <p>A Character will also have "skills" that are calculated off attributes.  A skills value can also be determined by a formula entered by the user.  A legit formula would look something like this:</p>
  
  <pre><code>((@attribute1Name + (@attribute2Name / 2) * 5)
  </code></pre>
  
  <p>where any text following the @ sign represents the name of an attribute belonging to that character.  The formula will be entered into a text field as a string.</p>
  
  <p>What I'm having a problem with is understanding the proper way to parse and evaluate this formula.  Initially, my plan was to do a simple replace on the attribute names and eval the expression (if invalid, the eval would fail).  However, this presents a problem as it would allow for JavaScript injection into the field.  I'm assuming I'll need some kind of FSM similar to an infix calculator to solve this, but I'm a little rusty on my computation theory (thanks corporate world!).  I'm really not asking for someone to just hand me the code so much as I'd like to get your input on what is the best solution to this problem?</p>
  
  <p>EDIT: Thanks for the responses.  Unfortunately life has kept me busy and I haven't tried a solution yet.  Will update when I get a result (good or bad).</p>