summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/AST/BasicExpression.cpp
blob: a2fab441321c962396ff338be6e9c5c38eea71a9 (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
/*
 * BasicExpression.cpp
 *
 *  Created on: Dec 5, 2012
 *      Author: attero
 */

#include "BasicExpression.h"

void BasicExpression::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);
	}
	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);
}