summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/Context.h
blob: bd537b1fb2c5f3450f8490e4c3b82990327a1233 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Context.h
 *
 *  Created on: Dec 30, 2012
 *      Author: att
 */

#ifndef CONTEXT_H_
#define CONTEXT_H_
#include <map>
#include <string>
#include "Utils/to_string.h"
#include "Elements/SenchaObject.h"
#include "Elements/SenchaFunction.h"
#include "AST/ASTExpression.h"



/**
 * Context is object used to store variables for some execution context.
 * It's most useful in function calls. Every function has to have its own execution contexts,
 * for its variables. Arguments are passed to functions by predefining them in new Context, assigned to
 * function.
 */
class Context {
public:
	/**
	 * Context has unique name, which is passed to it in constructor.
	 */
	Context(std::string name);
	std::string name;

	/**
	 * Typedef to make further declarations easier to read.
	 */
	typedef SenchaObject (*PointerToNativeFunction)(std::vector<ASTExpression *>);

	/**
	 * Function have to be declared, which means here, registered before usage.
	 * You can register them with register_function functions.
	 */
	void register_function(std::string name, PointerToNativeFunction f);
	void register_function(std::string name, SenchaFunction * f);
	/**
	 * Map with function written in C++ stored like {"my_function" : &my_function}
	 */
	std::map<std::string, PointerToNativeFunction> registered_functions;
	/**
	 * Map with function written in Sencha stored like {"my_function" : &my_function}
	 */
	std::map<std::string, SenchaFunction *> registered_sfunctions;

	/**
	 * contains_function("x") returns true if x can be found in registered native (c++) or sencha functions.
	 */
	bool contains_function(std::string name);

	/**
	 * contains_function("x") returns true if x can be found in registered sencha functions.
	 */
	bool contains_sfunction(std::string name);

	/**
	 * contains_function("x") returns true if x can be found in registered native (c++).
	 */
	bool contains_nfunction(std::string name);

	/**
	 * Executes function written in c++
	 */
	SenchaObject execute_native_function(std::string name, std::vector<ASTExpression *> arguments);

	/**
	 * get_updated_string(name) return string of given name with all updates done on its elements such as name[9] = "a";
	 */
	SenchaObject get_updated_string(std::string name);

	/**
	 * debug() provides very useful info about context. What variables are stored, what are their values.
	 */
	std::string debug() ;

	/**
	 * add(name, value) adds value called name to object store.
	 */
	void add(std::string name, SenchaObject object);

	/**
	 * get(name) returns value from object store. If there isn't such a value. It returns SenchaObject();
	 */
	SenchaObject get(std::string name);


	virtual ~Context();

private:
	unsigned int index;
	std::map<std::string, SenchaObject> object_store;
};

#endif /* CONTEXT_H_ */