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

#include "PostfixExpression.h"

PostfixExpression::PostfixExpression(std::string name, ContextManager * context_manager):
	 name(name), operation("call"), context_manager(context_manager)
{
		type= "PostfixExpression";
		access_index_expr = nullptr;
}

PostfixExpression::PostfixExpression(std::string name, ContextManager * context_manager, ASTExpression * access_index_expr):
	 name(name),  operation("access"), context_manager(context_manager), access_index_expr(access_index_expr)
{
		type= "PostfixExpression";
}

PostfixExpression::~PostfixExpression() {
	for(auto i = arguments.begin(); i != arguments.end();)
	{
		delete *i;
		i = arguments.erase(i);
	}
}


void PostfixExpression::add_argument(ASTExpression * expression)
{
	arguments.push_back(expression);
}


SenchaObject PostfixExpression::evaluate()
{
	return execute();
}

SenchaObject PostfixExpression::execute() {
	SenchaObject result = SenchaObject();
	if(operation == "call")
		return context_manager->execute_function(name, arguments);
	else if(operation == "access")
	{
		SenchaObject called_value = context_manager->get_top()->get(name);
		SenchaObject access_index = access_index_expr->execute();
		if(access_index.type == SenchaObject::integer_number)
		{
			if(called_value.type == SenchaObject::string_literal)
			{
				called_value = context_manager->get_top()->get_updated_string(name);
				SenchaArray array(name, context_manager, called_value.text);
				return  array.get(access_index.integer);
			}
		}
	}
	result.type = SenchaObject::invalid;
	return result;

}