#include "__for.h"
#include "Exception.h"
#include <iostream>

namespace jet {

   __for::__for(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local) : Tag(in, parentOut, global, parent, this) {
      double counter = 0.0f;   
      bool nameDefined = keywordDefined("name");
      if(keywordDefined("start")) {
	 counter = resolveKeyword("start").asDouble();
	 keywords["start"].reset();
      }
      if(!keywordDefined("end")) 
	throw coreutils::Exception("for tag requires end keyword.");
      if(!keywordDefined("step")) 
	throw coreutils::Exception("for tag requires step keyword.");
      for(double ix = counter; ix <= resolveKeyword("end").asDouble(); ix += resolveKeyword("step").asDouble()) {
	 resolveKeyword("end").reset();
	 resolveKeyword("step").reset();	 
	 if(nameDefined) {
	    if(!keywordDefined("scope") || (resolveKeyword("scope") == "global"))
	      global.variables[resolveKeyword("name")] = ix;
	    else if(resolveKeyword("scope") == "local")
	      this->local->variables[resolveKeyword("name")] = ix;
	    else if(resolveKeyword("scope") == "parent")
	      parent->local->variables[resolveKeyword("name")] = ix;
	    else
	      throw coreutils::Exception("scope value is not valid.");
	 }	 
	 processContainer(container);
	 container.reset();
      }         
   }
   
}
