title: QUEUE and 'IF' 'ELSE' 'WHILE' statements from scratch and running functions
body: <p>I'm making a game engine in javascript, and needed some way to code some actions.</p>
  
  <p>The code is here:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;script&gt;
  var engine={}, actions={}; engine.atomStack=new Array();
  events = {
      1: ["action1|5","action2|2;2","action1|2"],
      2: ["action2|5;2","action2|2;2"],
      3: ["action2|5;2","action1|2"] };
  
  engine.runatomStack = function(){
      while(engine.atomStack.length &gt; 0){
          var actionToRun = engine.atomStack.shift();
          actionToRun[0](actionToRun[1]);  } };
  
  eventActivate = function(event) {
      for (var i = 0; i &lt; events[event].length ; i++) {
          var actionAndParam = events[event][i].split('|');
          translateActions(actionAndParam[0],actionAndParam[1]);  } };
  
  engine.action1 = function( param ) {
      console.log("executed action 1, param "+param[0]); }
  
  engine.action2 = function( param ) {
      console.log("executed action 2, params "+param[0]+" "+param[1]); }
  
  actions.action1 = function( param ) {
      var params = param.split(';');
      engine.atomStack.push([engine.action1,params]); }
  
  actions.action2 = function( param ) {
      var params = param.split(';');
      params[1]=parseInt(params[1],10)+2
      engine.atomStack.push([engine.action2,params]); }
  
  translateActions = function(action, param) { actions[action](param); };
  &lt;/script&gt; &lt;/body&gt; &lt;/html&gt;
  </code></pre>
  
  <p>Something happens, and I need to run the actions inside an event. I call eventActivate passing the event that should happen. The function translateAction read this information and calls the function that set up the actions. My logic is based that a level contain events, an event can contain actions, and each different action contain atoms.</p>
  
  <p>Example: at some point you call eventActivate(1) and that will push the relative events on the stack. Then from time to time the engine is used and calls engine.runatomStack() to execute whatever is there.</p>
  
  <pre><code>//engine.atomStack is Array [  ]
  
  eventActivate(2)
  //engine.atomStack is Array [ Array[2], Array[2] ]
  
  engine.runatomStack()
  
  //prints:
  //   "executed action 2, params 5 4" example.html:18
  //   "executed action 2, params 2 4" example.html:18
  
  //engine.atomStack is Array [  ]
  </code></pre>
  
  <p>Ok, so my engine start to grow and all and now I think I need to add IF/ELSE statements and WHILE/BREAK loops. I have some ideas on implementation but wanted help to what's works best using this queue. Sorry if it's duplicate, but couldn't find help using Google.</p>
  
  <p>I thought something like, if I had events:</p>
  
  <pre><code>4: ["action2|5;2","IF|condition","action2|2;2","END|"]
  5: ["action2|5;2","IF|condition","action2|2;2","ELSE|","action1|2","END|"]
  </code></pre>
  
  <p>I'm not sure how exactly to go, what's works best...</p>
  
  <p>Link to jsFiddle version: <a href="http://jsfiddle.net/e3b0kocc/" rel="nofollow">http://jsfiddle.net/e3b0kocc/</a></p>