#include "__set.h"
#include "Exception.h"
#include "Expression.h"
#include <iostream>

namespace jet {
   
   __set::__set(coreutils::ZString &in, coreutils::MString &parent, Global &global) : Tag(in, parent, global) {
      output = false;
      processContainer(container);
      if(!variableDefined("name"))
	throw coreutils::Exception("set tag must have name defined.");
      if(!variableDefined("expr") && variableDefined("value") && hasContainer)
	throw coreutils::Exception("set tag cannot have both value and a container.");
      if(variableDefined("expr") && !variableDefined("value") && hasContainer)
	throw coreutils::Exception("set tag cannot have both expr and a container.");
      if(variableDefined("expr") && variableDefined("value") && !hasContainer)
	throw coreutils::Exception("set tag cannot have both expr and value.");
      if(!variableDefined("expr") && !variableDefined("value") && !hasContainer)
	throw coreutils::Exception("set tag must have a value, expr or a container.");
      if(!variableDefined("scope") || (variables["scope"] == "global")) {
	 if(variableDefined("expr")) {
	    if(variableDefined("eval"))
	      throw coreutils::Exception("Cannot use eval with expr.");
	    global.variables[variables["name"]] = Expression(variables["expr"]).string;	    
	 } else if(hasContainer) {
	    if(evaluate) {
	       global.variables[variables["name"]] = out;	       
	    } else {	   
	       global.variables[variables["name"]] = container;
	    }	    
	 } else
	   global.variables[variables["name"]] = variables["value"];
      } else {
	 
      }
   }      
}
