/*
 * BasicExpression.cpp
 *
 *  Created on: Dec 5, 2012
 *      Author: attero
 */

#include "BasicExpression.h"

SenchaObject BasicExpression::execute()
{
	SenchaObject result =  evaluate() ;
	return result;
}


SenchaObject BasicExpression::evaluate()
{
	//TODO refactor it ;)

	SenchaObject left = static_cast<ASTExpression *>(children[0])->evaluate();
	SenchaObject right = static_cast<ASTExpression *>(children[1])->evaluate();


	SenchaObject so;
	if(oper == "+")
	{
		so = SenchaObject(left + right);
	}
	else if(oper == "-")
	{
		so = SenchaObject(left - right);
	}
	else if(oper == "*")
	{
		so = SenchaObject(left * right);
	}
	else if(oper == "/")
	{
		so = SenchaObject(left / right);
	}
	else if(oper == "=")
	{
		so = SenchaObject(right);
	}
	else if(oper == "==")
	{
		so = SenchaObject(right == left);
	}
	else if(oper == "!=")
	{
		so = SenchaObject(right != left);
	}
	else if(oper == ">")
		{
			so = SenchaObject(left > right);
		}
	else if(oper == ">=")
		{
			so = SenchaObject(left >= right);
		}
	else if(oper == "<=")
		{
			so = SenchaObject(left <= right);
		}
	else if(oper == "<")
		{
			so = SenchaObject(left < right);
		}
	else if(oper == "&&")
		{
			so = SenchaObject(left.is_true() && right.is_true());
		}
	else if(oper == "||")
			{
				so = SenchaObject(left.is_true() || right.is_true());
			}
	return so;
}

BasicExpression::BasicExpression(ASTNode * left, ASTNode * right, std::string oper )
{
	this->type= "BasicExpression";
	this->children.push_back(left);
	this->children.push_back(right);
	this->oper = oper;
}

BasicExpression::~BasicExpression() {

}

void BasicExpression::accept(Visitor * visitor)
{
	visitor->visit(this);
}

