summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/Context.cpp
blob: e9b16c33a62df802d92ec2bc518dbc0790445fdb (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 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;


}