summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/AST/IfNode.cpp
blob: ba80f3df7beaa4d7104d27fc64e3c585f9115c1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * IfNode.cpp
 *
 *  Created on: Nov 18, 2012
 *      Author: attero
 */

#include "IfNode.h"


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

std::string IfNode::debug()
{
	std::string debug_note;
	debug_note = "If node, condition is: " + to_string(evaluate_condition());
	if(evaluate_condition())
		debug_note += "Doing first branch";
	else
		debug_note += "Doing second branch";
	return debug_note;
}

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);
}

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


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

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