sencha-lang/Sencha-lang/ContextManager.cpp

89 lines
2.0 KiB
C++
Raw Normal View History

2012-12-08 19:59:05 +00:00
/*
* Context.cpp
*
* Created on: Dec 7, 2012
* Author: attero
*/
#include "ContextManager.h"
2012-12-08 19:59:05 +00:00
ContextManager::ContextManager() {
2012-12-30 21:37:10 +00:00
contexts["global"] = new Context("global");
stack.push(contexts["global"]);
2012-12-08 19:59:05 +00:00
index = 0;
}
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
{
2013-01-13 16:36:38 +00:00
Context * context = new Context("abcd" + to_string(index));
2012-12-08 19:59:05 +00:00
index++;
2012-12-30 21:37:10 +00:00
contexts[context->name] = context;
stack.push(context);
2012-12-30 21:37:10 +00:00
return context;
2012-12-08 19:59:05 +00:00
}
SenchaObject ContextManager::execute_function(std::string name, std::vector<ASTExpression *> arguments)
{
SenchaObject result;
2013-01-04 12:24:14 +00:00
if(contexts["global"]->contains_nfunction(name))
{
result = contexts["global"]->registered_functions[name](arguments);
}
2013-01-04 12:24:14 +00:00
else if(contexts["global"]->contains_sfunction(name))
{
std::vector<SenchaObject> evaluated_arguments;
for(auto argument : arguments)
evaluated_arguments.push_back((argument->evaluate()));
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;
}
for(unsigned int i = 0; i < function->names_of_arguments.size(); i++)
{
//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]);
}
result = (*function)();
pop_context();
destroy_context(name_of_context);
}
return result;
}
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
{
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
}