More on context manager.

functions
Justyna Ilczuk 2012-12-30 22:37:10 +01:00
parent f23c85949f
commit e01bc95615
4 changed files with 146 additions and 75 deletions

80
Sencha-lang/Context.cpp Normal file
View File

@ -0,0 +1,80 @@
/*
* Context.cpp
*
* Created on: Dec 30, 2012
* Author: att
*/
#include "Context.h"
Context::Context(std::string name) {
// TODO Auto-generated constructor stub
this->index = 0;
this->name = name;
}
Context::~Context() {
// TODO Auto-generated destructor stub
}
void Context::register_function(std::string name, PointerToNativeFunction f)
{
registered_functions[name] = f;
}
SenchaObject Context::execute_native_function(std::string name, std::vector<ASTExpression *> arguments)
{
SenchaObject result;
if(registered_functions.count(name) == 1)
result = registered_functions[name](arguments);
return result;
}
ObjectIndex Context::add_to_store(SenchaObject & object)
{
index++;
object_store[index] = object;
return index;
}
SenchaObject Context::get_from_store(ObjectIndex index)
{
return object_store[index];
}
void Context::add(std::string name, SenchaObject object)
{
interpreter_context[name] = add_to_store(object);
}
void Context::set(std::string name, SenchaObject object)
{
if(interpreter_context[name] != 0)
{
object_store[interpreter_context[name]] = object;
}
else
{
add(name, object);
}
}
SenchaObject Context::get(std::string name)
{
return get_from_store(interpreter_context[name]);
}
std::string Context::debug() {
std::string debug_note = "";
for( auto iter = this->interpreter_context.begin(); iter != this->interpreter_context.end(); iter++)
{
debug_note += "Context: " + to_string((*iter).second) + ": " + (*iter).first + " " + object_store[(*iter).second].repr() + "\n";
}
return debug_note;
}

43
Sencha-lang/Context.h Normal file
View File

@ -0,0 +1,43 @@
/*
* Context.h
*
* Created on: Dec 30, 2012
* Author: att
*/
#ifndef CONTEXT_H_
#define CONTEXT_H_
#include "Utils/to_string.h"
class Context {
public:
Context(std::string name);
unsigned int index;
std::string name;
std::map<ObjectIndex, SenchaObject> object_store;
ObjectIndex add_to_store(SenchaObject & object);
SenchaObject get_from_store(ObjectIndex index);
typedef SenchaObject (*PointerToNativeFunction)(std::vector<ASTExpression *>);
std::map<std::string, PointerToNativeFunction> registered_functions;
void register_function(std::string name, PointerToNativeFunction f);
SenchaObject execute_native_function(std::string name, std::vector<ASTExpression *> arguments);
//Overload it to use contexts
typedef std::map<std::string, ObjectIndex> ExecutionContext;
ExecutionContext interpreter_context;
std::string debug() ;
void add(std::string name, SenchaObject object);
void set(std::string name, SenchaObject object);
SenchaObject get(std::string name);
virtual ~Context();
};
#endif /* CONTEXT_H_ */

View File

@ -8,69 +8,38 @@
#include "ContextManager.h" #include "ContextManager.h"
ContextManager::ContextManager() { ContextManager::ContextManager() {
contexts["global"] = new Context("global");
index = 0; index = 0;
} }
ContextManager::~ContextManager() { ContextManager::~ContextManager() {
} }
void ContextManager::register_function(std::string name, PointerToNativeFunction f) Context * ContextManager::create_new_context()
{
registered_functions[name] = f;
}
SenchaObject ContextManager::execute_native_function(std::string name, std::vector<ASTExpression *> arguments)
{
SenchaObject result;
if(registered_functions.count(name) == 1)
result = registered_functions[name](arguments);
return result;
}
ObjectIndex ContextManager::add_to_store(SenchaObject & object)
{ {
Context * context = new Context("Zdzislaw" + to_string(index));
index++; index++;
object_store[index] = object; contexts[context->name] = context;
return context;
return index;
} }
SenchaObject ContextManager::get_from_store(ObjectIndex index) Context * ContextManager::get_context(std::string name)
{ {
return object_store[index]; return contexts[name];
} }
void ContextManager::add(std::string name, SenchaObject object) Context * ContextManager::context(std::string name)
{ {
interpreter_context[name] = add_to_store(object); if(contexts.count(name) != 0) return contexts[name];
else return nullptr;
} }
void ContextManager::set(std::string name, SenchaObject object) void ContextManager::destroy_context(std::string name)
{ {
if(interpreter_context[name] != 0) auto iter = contexts.find(name);
if(iter != contexts.end())
{ {
object_store[interpreter_context[name]] = object; delete (*iter);
} contexts.erase(iter);
else
{
add(name, object);
} }
} }
SenchaObject ContextManager::get(std::string name)
{
return get_from_store(interpreter_context[name]);
}
std::string ContextManager::debug() {
std::string debug_note = "";
for( auto iter = this->interpreter_context.begin(); iter != this->interpreter_context.end(); iter++)
{
debug_note += "Context: " + to_string((*iter).second) + ": " + (*iter).first + " " + object_store[(*iter).second].repr() + "\n";
}
return debug_note;
}

View File

@ -13,7 +13,8 @@
#include <iostream> #include <iostream>
#include "Elements/SenchaObject.h" #include "Elements/SenchaObject.h"
#include "AST/ASTExpression.h" #include "AST/ASTExpression.h"
#include "Context.h"
#include "Utils/to_string.h"
typedef unsigned long ObjectIndex; typedef unsigned long ObjectIndex;
typedef unsigned long FunctionIndex; typedef unsigned long FunctionIndex;
@ -21,34 +22,12 @@ typedef unsigned long FunctionIndex;
class ContextManager { class ContextManager {
public: public:
ContextManager(); ContextManager();
typedef unsigned long ObjectIndex; std::map<std::string, Context *> contexts;
ObjectIndex index; Context * create_new_context();
void destroy_context(std::string name);
std::map<ObjectIndex, SenchaObject> object_store; Context * get_context(std::string name);
ObjectIndex add_to_store(SenchaObject & object); Context * context(std::string name);
SenchaObject get_from_store(ObjectIndex index); unsigned int index;
typedef SenchaObject (*PointerToNativeFunction)(std::vector<ASTExpression *>);
std::map<std::string, PointerToNativeFunction> registered_functions;
void register_function(std::string name, PointerToNativeFunction f);
SenchaObject execute_native_function(std::string name, std::vector<ASTExpression *> arguments);
//Overload it to use contexts
void add(std::string name, SenchaObject object);
void set(std::string name, SenchaObject object);
SenchaObject get(std::string name);
typedef std::map<std::string, ObjectIndex> ExecutionContext;
ExecutionContext interpreter_context;
std::string debug() ;
ExecutionContext global_context;
std::map<FunctionIndex , ExecutionContext *> function_contexts;
virtual ~ContextManager(); virtual ~ContextManager();
}; };