2012-12-08 19:59:05 +00:00
|
|
|
/*
|
|
|
|
* Context.cpp
|
|
|
|
*
|
|
|
|
* Created on: Dec 7, 2012
|
|
|
|
* Author: attero
|
|
|
|
*/
|
|
|
|
|
2012-12-30 13:59:30 +00:00
|
|
|
#include "ContextManager.h"
|
2012-12-08 19:59:05 +00:00
|
|
|
|
2012-12-30 13:59:30 +00:00
|
|
|
ContextManager::ContextManager() {
|
2012-12-30 21:37:10 +00:00
|
|
|
contexts["global"] = new Context("global");
|
2012-12-31 16:10:37 +00:00
|
|
|
stack.push(contexts["global"]);
|
2012-12-08 19:59:05 +00:00
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
|
2012-12-30 13:59:30 +00:00
|
|
|
ContextManager::~ContextManager() {
|
2012-12-08 19:59:05 +00:00
|
|
|
}
|
|
|
|
|
2012-12-30 21:37:10 +00:00
|
|
|
Context * ContextManager::create_new_context()
|
2012-12-08 19:59:05 +00:00
|
|
|
{
|
2012-12-30 21:37:10 +00:00
|
|
|
Context * context = new Context("Zdzislaw" + to_string(index));
|
2012-12-08 19:59:05 +00:00
|
|
|
index++;
|
2012-12-30 21:37:10 +00:00
|
|
|
contexts[context->name] = context;
|
2012-12-31 16:10:37 +00:00
|
|
|
stack.push(context);
|
2012-12-30 21:37:10 +00:00
|
|
|
return context;
|
2012-12-08 19:59:05 +00:00
|
|
|
}
|
|
|
|
|
2012-12-31 16:10:37 +00:00
|
|
|
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)
|
|
|
|
{
|
2013-01-01 22:38:10 +00:00
|
|
|
std::vector<SenchaObject> evaluated_arguments;
|
|
|
|
for(auto argument : arguments)
|
|
|
|
evaluated_arguments.push_back((argument->evaluate()));
|
|
|
|
|
2012-12-31 16:10:37 +00:00
|
|
|
SenchaFunction * function = contexts["global"]->registered_sfunctions[name];
|
|
|
|
std::string name_of_context = create_new_context()->name;
|
2012-12-31 17:31:03 +00:00
|
|
|
if( arguments.size() != function->names_of_arguments.size())
|
|
|
|
{
|
|
|
|
result.type = SenchaObject::invalid;
|
|
|
|
return result;
|
|
|
|
}
|
2012-12-31 16:10:37 +00:00
|
|
|
for(unsigned int i = 0; i < function->names_of_arguments.size(); i++)
|
2013-01-01 22:38:10 +00:00
|
|
|
{
|
|
|
|
//std::cout << "I'm adding to context " + get_top()->name + " variable of name: " + function->names_of_arguments[i];
|
|
|
|
//std::cout << "Which should be equal " + evaluated_arguments[i].repr() << std::endl;
|
|
|
|
get_top()->add(function->names_of_arguments[i], evaluated_arguments[i]);
|
|
|
|
}
|
|
|
|
|
2012-12-31 16:10:37 +00:00
|
|
|
result = (*function)();
|
|
|
|
pop_context();
|
|
|
|
destroy_context(name_of_context);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-30 21:37:10 +00:00
|
|
|
Context * ContextManager::get_context(std::string name)
|
2012-12-08 19:59:05 +00:00
|
|
|
{
|
2012-12-30 21:37:10 +00:00
|
|
|
return contexts[name];
|
2012-12-31 16:10:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Context * ContextManager::get_top()
|
|
|
|
{
|
|
|
|
return stack.top();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContextManager::pop_context()
|
|
|
|
{
|
|
|
|
stack.pop();
|
2012-12-08 19:59:05 +00:00
|
|
|
}
|
|
|
|
|
2012-12-30 21:37:10 +00:00
|
|
|
Context * ContextManager::context(std::string name)
|
2012-12-08 19:59:05 +00:00
|
|
|
{
|
2012-12-30 21:37:10 +00:00
|
|
|
if(contexts.count(name) != 0) return contexts[name];
|
|
|
|
else return nullptr;
|
2012-12-08 19:59:05 +00:00
|
|
|
}
|
|
|
|
|
2012-12-30 21:37:10 +00:00
|
|
|
void ContextManager::destroy_context(std::string name)
|
2012-12-08 19:59:05 +00:00
|
|
|
{
|
2012-12-30 21:37:10 +00:00
|
|
|
auto iter = contexts.find(name);
|
|
|
|
if(iter != contexts.end())
|
2012-12-09 11:57:51 +00:00
|
|
|
{
|
2012-12-31 16:10:37 +00:00
|
|
|
delete ((*iter).second);
|
2012-12-30 21:37:10 +00:00
|
|
|
contexts.erase(iter);
|
2012-12-09 11:57:51 +00:00
|
|
|
}
|
2012-12-08 19:59:05 +00:00
|
|
|
}
|