2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008-2008 - DIGITEO - Antoine ELIAS
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12 #include "context.hxx"
13 #include "internal.hxx"
14 #include "function.hxx"
16 #include "macrofile.hxx"
17 #include "variables.hxx"
26 varStack.push(new VarList());
27 globals = new std::list<Symbol>();
32 while (!varStack.empty())
34 VarList * pVL = varStack.top();
45 Context* Context::getInstance(void)
54 void Context::destroyInstance(void)
62 void Context::scope_begin()
65 varStack.push(new VarList());
68 void Context::clearAll()
74 void Context::scope_end()
76 //clear varList of current scope
77 if (varStack.empty() == false)
79 clearCurrentScope(true);
85 bool Context::clearCurrentScope(bool _bClose)
92 VarList* varList = varStack.top();
93 std::map<Symbol, Variable*>::iterator it = varList->begin();
94 for (; it != varList->end() ; ++it)
96 if (it->second->empty() == false && it->second->top()->m_iLevel == m_iLevel)
98 ScopedVariable * pSV = it->second->top();
99 types::InternalType * pIT = pSV->m_pIT;
118 Variable* Context::getOrCreate(const Symbol& _key)
120 return variables.getOrCreate(_key);
123 types::InternalType* Context::get(const Symbol& _key)
125 return get(_key, -1);
128 types::InternalType* Context::get(const Variable* _var)
130 types::InternalType* pIT = _var->get();
135 pIT = libraries.get(_var->getSymbol(), -1);
138 put((Variable*)_var, pIT);
145 types::InternalType* Context::get(const Symbol& _key, int _iLevel)
147 types::InternalType* pIT = NULL;
148 if (_iLevel == m_iLevel || _iLevel == -1)
150 //look for in current VarList
151 VarList::iterator it = varStack.top()->find(_key);
152 if (it != varStack.top()->end())
154 if (it->second->empty() == false)
156 return it->second->top()->m_pIT;
163 pIT = variables.get(_key, _iLevel);
167 pIT = libraries.get(_key, _iLevel);
170 //add symbol to current scope
179 types::InternalType* Context::getCurrentLevel(const Symbol& _key)
181 return variables.get(_key, m_iLevel);
184 types::InternalType* Context::getAllButCurrentLevel(const Symbol& _key)
186 return variables.getAllButCurrentLevel(_key, m_iLevel);
189 types::InternalType* Context::getFunction(const Symbol& _key)
194 std::list<Symbol>* Context::getFunctionList(std::wstring _stModuleName)
196 return variables.getFunctionList(_stModuleName, m_iLevel);
199 std::list<std::wstring>* Context::getVarsName()
201 return variables.getVarsName();
204 std::list<std::wstring>* Context::getMacrosName()
206 std::list<std::wstring>* vars = variables.getMacrosName();
207 std::list<std::wstring>* libs = libraries.getMacrosName();
208 vars->insert(vars->end(), libs->begin(), libs->end());
213 std::list<std::wstring>* Context::getFunctionsName()
215 return variables.getFunctionsName();
218 void Context::put(Variable* _var, types::InternalType* _pIT)
220 _var->put(_pIT, m_iLevel);
221 if (varStack.empty() == false)
223 (*varStack.top())[_var->getSymbol()] = _var;
226 if (_pIT->isLibrary())
228 Library* lib = libraries.getOrCreate(_var->getSymbol());
229 lib->put((types::Library*)_pIT, m_iLevel);
233 void Context::put(const Symbol& _key, types::InternalType* _pIT)
235 Variable* var = variables.getOrCreate(_key);
239 bool Context::remove(const Symbol& _key)
241 if (variables.remove(_key, m_iLevel))
243 varStack.top()->erase(_key);
244 libraries.remove(_key, m_iLevel);
251 bool Context::removeAll()
253 return clearCurrentScope(false);
256 bool Context::putInPreviousScope(Variable* _var, types::InternalType* _pIT)
258 //add variable in previous scope
259 variables.putInPreviousScope(_var, _pIT, m_iLevel - 1);
261 //add variable in stack of using variables
262 if (varStack.empty() == false)
264 VarList * list = varStack.top();
266 if (varStack.empty() == false)
268 (*varStack.top())[_var->getSymbol()] = _var;
275 bool Context::addFunction(types::Function *_info)
277 Variable* var = variables.getOrCreate(Symbol(_info->getName()));
278 variables.putInPreviousScope(var, _info, 0);
282 bool Context::addMacro(types::Macro *_info)
284 put(Symbol(_info->getName()), _info);
288 bool Context::addMacroFile(types::MacroFile *_info)
290 put(Symbol(_info->getName()), _info);
294 bool Context::isGlobalVisible(const Symbol& _key)
296 return variables.isGlobalVisible(_key, m_iLevel);
299 /*return global variable existance status*/
300 bool Context::isGlobal(const Symbol& _key)
302 return variables.isGlobal(_key, m_iLevel);
305 types::InternalType* Context::getGlobalValue(const Symbol& _key)
307 return variables.getGlobalValue(_key);
310 void Context::setGlobalVisible(const Symbol& _key, bool bVisible)
312 variables.setGlobalVisible(_key, bVisible, m_iLevel);
315 void Context::setGlobal(const Symbol& _key)
317 variables.setGlobal(_key);
318 globals->push_back(_key);
321 void Context::removeGlobal(const Symbol& _key)
323 variables.removeGlobal(_key, m_iLevel);
326 void Context::removeGlobalAll()
328 std::list<Symbol>::iterator it = globals->begin();
329 for (; it != globals->end(); ++it)
337 void Context::print(std::wostream& ostr) const
339 ostr << L" Environment Variables:" << std::endl;
340 ostr << L"==========================" << std::endl;
343 int Context::getScopeLevel()