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

#include "Assignment.h"

SenchaObject Assignment::evaluate()
{
	return static_cast<ASTExpression *>(children[1])->evaluate();
}

void Assignment::execute()
{
	auto left_value = static_cast<ASTExpression *>(children[0])->evaluate();
	auto right_value = static_cast<ASTExpression *>(children[1])->evaluate();
	static_cast<ASTExpression *>(children[1])->execute_quietly();
	if(left_value.name != "")
	{
		right_value.name = left_value.name;
		context->set(left_value.name, right_value);
	}
	std::cout << right_value.repr() << std::endl;
}

void Assignment::execute_quietly()
{
	auto left_value = static_cast<ASTExpression *>(children[0])->evaluate();
	auto right_value = static_cast<ASTExpression *>(children[1])->evaluate();
	static_cast<ASTExpression *>(children[1])->execute_quietly();
	if(left_value.name != "")
	{
		right_value.name = left_value.name;
		context->set(left_value.name, right_value);
	}

}

void Assignment::add_lvalue(ASTExpression * left)
{
	if(children.size()==0)
		children.push_back(left);
	else
		children[0] = left;
}

void Assignment::add_rvalue(ASTExpression * right)
{
	//TODO should note something if it doesn't look like that. Other possibilities like
	if(children.size()==1)
		children.push_back(right);
	else if(children.size()>1)
		children[1] = right;

}

std::string Assignment::debug()
{
	std::string debug_note = static_cast<ASTExpression *>(children[0])->evaluate().repr();
	debug_note += " = " + static_cast<ASTExpression *>(children[1])->evaluate().repr() + "\n";
	return debug_note;
}

Assignment::Assignment(ASTNode * parent, Context * context)
{
	this->parent = parent;
	this->context = context;
}


Assignment::~Assignment() {
	// TODO Auto-generated destructor stub
}

