/*
 * Assignment.cpp
 *
 *  Created on: Dec 7, 2012
 *      Author: attero
 */

#include "Assignment.h"

SenchaObject Assignment::evaluate()
{
	//TODO do something different with name evaluation
	auto left_value = static_cast<ASTExpression *>(children[0])->evaluate();
	if(left_value.name != "")
	{
		name = left_value.name;
	}
	auto right_value = static_cast<ASTExpression *>(children[1])->evaluate();
	auto context = context_manager->get_top();
	context->add(name, right_value);

	left_value = right_value;
	left_value.name = name;
	return left_value;
}


SenchaObject Assignment::execute()
{
	return evaluate();
}


Assignment::Assignment(ContextManager * context_manager, std::string name, ASTExpression * left, ASTExpression * right)
{
	this->context_manager = context_manager;
	this->name_of_context = "global";
	this->type = "Assignment";
	this->children.push_back(left);
	this->children.push_back(right);
	this->name = name;
}


Assignment::~Assignment() {
	for(auto it = children.begin(); it != children.end(); )
	{
		delete *it;
		it = children.erase(it);
	}
}

