summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/AST/BasicExpression.cpp
blob: ab38dded30129e1c9c1578da6143d516cec6a58b (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * BasicExpression.cpp
 *
 *  Created on: Dec 5, 2012
 *      Author: attero
 */

#include "BasicExpression.h"

void BasicExpression::set_operator(std::string op)
{
	this->oper = op;
}

void BasicExpression::set_left_operand(ASTNode * left)
{
	if(!this->children_set)
	{
		children.push_back(left);
	}
	else
	{
		this->children[0] = left;
	}

	if(children.size() == 2)
		children_set = true;
}

void BasicExpression::set_right_operand(ASTNode * right)
{
	if(!this->children_set)
		{
			children.push_back(right);

		}
		else
		{
			this->children[1] = right;
		}

		if(children.size() == 2)
			children_set = true;
}
void BasicExpression::execute()
{
	//children[0]->execute();
	//children[1]->execute();
	std::cout << evaluate().repr() << std::endl;
}

void BasicExpression::execute_quietly()
{
	evaluate();
}
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);
	}
	return so;
}

BasicExpression::BasicExpression(ASTNode * parent) {
	this->parent = parent;
	children_set = false;
}

BasicExpression::~BasicExpression() {
	//Do nothing
}


std::string BasicExpression::debug()
{
	std::string debug_note = "Basic expression:\n";
	debug_note += "left operand: \n";
	debug_note += this->children[0]->debug();
	debug_note += "\nright operand:\n" + this->children[1]->debug();
	debug_note += "\nOperator: " + this->oper +"\n###\n";
	return debug_note;
}