2012-12-03 22:40:47 +00:00
|
|
|
/*
|
|
|
|
* IfNode.cpp
|
|
|
|
*
|
|
|
|
* Created on: Nov 18, 2012
|
|
|
|
* Author: attero
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "IfNode.h"
|
|
|
|
|
|
|
|
|
|
|
|
IfNode::IfNode(ASTNode * parent) {
|
|
|
|
this->parent = parent;
|
2012-12-09 16:42:13 +00:00
|
|
|
is_else = false;
|
2012-12-22 10:06:55 +00:00
|
|
|
this->type = "IfNode";
|
2012-12-09 16:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IfNode::evaluate_condition()
|
|
|
|
{
|
|
|
|
auto condition = static_cast<ASTExpression * >(children[0])->evaluate();
|
|
|
|
return condition.is_true();
|
2012-12-03 22:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IfNode::~IfNode() {
|
2012-12-17 08:25:58 +00:00
|
|
|
for(auto it = children.begin(); it != children.end(); )
|
|
|
|
{
|
|
|
|
delete *it;
|
|
|
|
it = children.erase(it);
|
|
|
|
}
|
2012-12-03 22:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IfNode::add_condition(ASTExpression * expression)
|
|
|
|
{
|
|
|
|
children.push_back(expression);
|
|
|
|
}
|
|
|
|
|
2012-12-09 16:42:13 +00:00
|
|
|
void IfNode::execute()
|
|
|
|
{
|
|
|
|
if(evaluate_condition())
|
|
|
|
{
|
|
|
|
children[1]->execute();
|
|
|
|
}
|
|
|
|
else if(is_else)
|
|
|
|
{
|
|
|
|
children[2]->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-03 22:40:47 +00:00
|
|
|
void IfNode::add_body(ASTStatement * statement)
|
|
|
|
{
|
|
|
|
children.push_back(statement);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IfNode::add_else_block(ASTStatement * statement)
|
|
|
|
{
|
2012-12-09 16:42:13 +00:00
|
|
|
is_else = true;
|
2012-12-03 22:40:47 +00:00
|
|
|
children.push_back(statement);
|
|
|
|
}
|