/*
 * IfNode.cpp
 *
 *  Created on: Nov 18, 2012
 *      Author: attero
 */

#include "IfNode.h"


IfNode::IfNode() {
	is_else = false;
	this->type = "IfNode";
}


bool IfNode::evaluate_condition()
{
	auto condition = static_cast<ASTExpression * >(children[0])->evaluate();
	return condition.is_true();
}

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

void IfNode::add_condition(ASTExpression * expression)
{
	children.push_back(expression);
}

SenchaObject IfNode::execute()
{
	if(evaluate_condition())
	{
		return children[1]->execute();
	}
	else if(is_else)
	{
		return children[2]->execute();
	}
	return SenchaObject();
}


void IfNode::add_body(ASTStatement * statement)
{
	children.push_back(statement);
}

void IfNode::add_else_block(ASTStatement * statement)
{
	is_else = true;
	children.push_back(statement);
}
