summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/ContextManager.cpp
blob: 4378b12e74b992fc83808727007f33ee3da2e3f6 (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
/*
 * Context.cpp
 *
 *  Created on: Dec 7, 2012
 *      Author: attero
 */

#include "ContextManager.h"

ContextManager::ContextManager() {
	contexts["global"] = new Context("global");
	stack.push(contexts["global"]);
	index = 0;
}

ContextManager::~ContextManager() {
}

Context * ContextManager::create_new_context()
{
	Context * context = new Context("Zdzislaw" + to_string(index));
	index++;
	contexts[context->name] = context;
	stack.push(context);
	return context;
}

SenchaObject ContextManager::execute_function(std::string name, std::vector<ASTExpression *> arguments)
{
	SenchaObject result;
	if(contexts["global"]->registered_functions.count(name) == 1)
	{
		result = contexts["global"]->registered_functions[name](arguments);
	}

	else if(contexts["global"]->registered_sfunctions.count(name) == 1)
	{
		SenchaFunction * function = contexts["global"]->registered_sfunctions[name];
		std::string name_of_context = create_new_context()->name;
		for(unsigned int i = 0; i < function->names_of_arguments.size(); i++)
		get_top()->add(function->names_of_arguments[i], arguments[i]->evaluate());
		result = (*function)();
		pop_context();
		destroy_context(name_of_context);
	}
	return result;

}

Context * ContextManager::get_context(std::string name)
{
	return contexts[name];

}

Context * ContextManager::get_top()
{
	return stack.top();
}

void ContextManager::pop_context()
{
	stack.pop();
}

Context * ContextManager::context(std::string name)
{
	if(contexts.count(name) != 0) return contexts[name];
	else return nullptr;
}

void ContextManager::destroy_context(std::string name)
{
	auto iter = contexts.find(name);
	if(iter != contexts.end())
	{
		delete ((*iter).second);
		contexts.erase(iter);
	}
}