1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2007-2008 - INRIA - Bruno JOFRET
4 *
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
6 *
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
13 *
14 */
15
16 /**
17 ** \file symbol/context.hh
18 ** \brief Define class Context.
19 */
20
21 #ifndef __CONTEXT_HXX__
22 #define __CONTEXT_HXX__
23
24 #include "function.hxx"
25 #include "variables.hxx"
26 #include "libraries.hxx"
27
28 extern "C"
29 {
30 #include "dynlib_ast.h"
31 }
32
33 namespace symbol
34 {
35
36 /** \brief Define class Context.
37 */
38 class EXTERN_AST Context
39 {
40 public:
41 typedef std::map<Symbol, Variable*> VarList;
42 typedef std::stack<VarList*> VarStack;
43
44 static Context* getInstance(void);
45
46 static void destroyInstance(void);
47
48 /** Open a context scope i.e
49 ** open the heap table one
50 ** and the env table too. */
51 void scope_begin();
52
53 /** Close a context scope i.e
54 ** close the heap table one
55 ** and the env table too. */
56 void scope_end();
57
58 /** If key was associated to some Entry_T in the open scopes, return the
59 ** most recent insertion. Otherwise return the empty pointer. */
60 types::InternalType* get(const Symbol& key);
61 types::InternalType* get(const Variable* _var);
62 Variable* getOrCreate(const Symbol& _key);
63 int getLevel(const Symbol & _key) const;
64
65 /** If key was associated to some Entry_T in the last opened scope, return it.
66 ** Otherwise return the empty pointer. */
67 types::InternalType* getCurrentLevel(const Symbol& key);
68 types::InternalType* getCurrentLevel(Variable* _var);
69
70 /** If key was associated to some Entry_T in the open scopes, return the
71 ** most recent insertion DESPITE the current/last one. Otherwise return the empty pointer. */
72 types::InternalType* getAllButCurrentLevel(const Symbol& key);
73 types::InternalType* getAtLevel(const Symbol& key, int level = SCOPE_ALL);
74
75 /** If key was associated to some Entry_T in the open scopes, return the
76 ** most recent insertion. Otherwise return the empty pointer. */
77 types::InternalType* getFunction(const Symbol& key);
78
79 /*return function list in the module _stModuleName*/
80 int getFunctionList(std::list<Symbol>& lst, const std::wstring& _stModuleName);
81 /*return function list in the module _stModuleName*/
82 int getFunctionList(std::list<types::Callable *>& lst, std::wstring _stModuleName);
83
84 int getConsoleVarsName(std::list<std::wstring>& lst);
85 int getVarsName(std::list<std::wstring>& lst);
86 int getMacrosName(std::list<std::wstring>& lst);
87 int getFunctionsName(std::list<std::wstring>& lst);
88 int getCurrentScope(std::list<std::pair<std::wstring, int>>& lst, bool bSorted);
89 int getVarsInfoForWho(std::list<std::pair<std::wstring, int>>& lst, bool bSorted);
90 int getGlobalInfoForWho(std::list<std::pair<std::wstring, int>>& lst, bool bSorted);
91 int getWhereIs(std::list<std::wstring>& lst, const std::wstring& _str);
92 int getLibrariesList(std::list<std::wstring>& lst);
93 int getVarsToVariableBrowser(std::list<Variable*>& lst);
94 int getLibsToVariableBrowser(std::list<Library*>& lst);
95 /* global functions */
96
97 /*return global variable visibility status*/
98 bool isGlobalVisible(const Symbol& key);
99
100 /*return global variable existance status*/
101 bool isGlobal(const Symbol& key);
102
103 /*remove global variable and all visibility references */
104 //clearglobal("a")
105 bool removeGlobal(const Symbol& key);
106
107 /*remove all global variables and references */
108 //clearglobal
109 void removeGlobalAll();
110 void clearAll();
111
112 //predef
113 void protect();
114 void unprotect();
115 bool isprotected(const Symbol& key);
116 bool isprotected(Variable* _var);
117 int protectedVars(std::list<std::wstring>& lst);
118
119 /*set variable visible/hidden in current global scope*/
120 void setGlobalVisible(const Symbol& key, bool bVisible);
121 void setGlobal(const Symbol& key);
122
123 types::InternalType* getGlobalValue(const Symbol& _key);
124
125 /*add symbol and value in the stack*/
126 bool put(const Symbol& _key, types::InternalType* _pIT);
127 bool put(Variable* _var, types::InternalType* _pIT);
128 /*add symbol and value in the previous scope*/
129 bool putInPreviousScope(Variable* _var, types::InternalType* _pIT);
130
131 /* remove symbol/value association */
132 //clear("a")
133 bool remove(const Symbol& key);
134 //clear();
135 bool removeAll();
136
137 bool addFunction(types::Function *_info);
138 bool addMacro(types::Macro *_info);
139 bool addMacroFile(types::MacroFile *_info);
140 void print(std::wostream& ostr, bool bSorted = false) const;
141 int getScopeLevel();
142 bool isValidVariableName(const wchar_t*);
143 bool isValidVariableName(const char*);
144
isOriginalSymbol(const symbol::Symbol & sym) const145 inline bool isOriginalSymbol(const symbol::Symbol & sym) const
146 {
147 return getLevel(sym) == 0;
148 }
149
150 private:
151
152 types::InternalType* get(const Symbol& key, int _iLevel);
153 bool clearCurrentScope(bool _bClose);
154 void updateProtection(bool protect);
155
156 std::list<Symbol>* globals;
157 VarStack varStack;
158 Variables variables;
159 Libraries libraries;
160 VarList* console;
161 int m_iLevel;
162
163 Context();
164 ~Context();
165
166 static Context* me;
167 };
168
operator <<(std::wostream & ostr,const Context & ctx)169 inline std::wostream& operator<< (std::wostream& ostr, const Context &ctx)
170 {
171 ctx.print(ostr);
172 return ostr;
173 }
174
175 }
176 #endif /* !__CONTEXT_HXX__ */
177