cosmetic, rename functions
[scilab.git] / scilab / modules / types / src / cpp / macrofile.cpp
1 /*
2 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 *  Copyright (C) 2009-2010 - DIGITEO - Bruno JOFRET
4 *  Copyright (C) 2009-2009 - DIGITEO - Antoine ELIAS
5 *
6 *  This file must be used under the terms of the CeCILL.
7 *  This source file is licensed as described in the file COPYING, which
8 *  you should have received as part of this distribution.  The terms
9 *  are also available at
10 *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11 *
12 */
13
14 #include <sstream>
15 #include "macrofile.hxx"
16 #include "context.hxx"
17 #include "localization.h"
18 #include "scilabWrite.hxx"
19 #include "parser.hxx"
20 #include "configvariable.hxx"
21
22 using namespace ast;
23 namespace types
24 {
25 MacroFile::MacroFile(wstring _stName, wstring _stPath, wstring _stModule) : Callable(), m_stPath(_stPath), m_pMacro(NULL)
26 {
27     setName(_stName);
28     setModule(_stModule);
29 }
30
31 MacroFile::~MacroFile()
32 {
33     if (m_pMacro)
34     {
35         delete m_pMacro;
36     }
37 }
38
39 InternalType* MacroFile::clone()
40 {
41     IncreaseRef();
42     return this;
43 }
44
45 void MacroFile::whoAmI()
46 {
47     std::cout << "types::MacroFile";
48 }
49
50 InternalType::RealType MacroFile::getType(void)
51 {
52     return RealMacroFile;
53 }
54
55 bool MacroFile::toString(std::wostringstream& ostr)
56 {
57     ostr << L"FIXME : Implement MacroFile::toString" << std::endl;
58     return true;
59 }
60
61 Callable::ReturnValue MacroFile::call(typed_list &in, optional_list &opt, int _iRetCount, typed_list &out, ast::ConstVisitor* execFunc)
62 {
63     ReturnValue RetVal = Callable::OK;
64
65     parse();
66     if (m_pMacro)
67     {
68         ReturnValue Val =  m_pMacro->call(in, opt, _iRetCount, out, execFunc);
69         return Val;
70     }
71     else
72     {
73         return Callable::Error;
74     }
75 }
76
77 bool MacroFile::parse(void)
78 {
79
80     if (m_pMacro == NULL)
81     {
82         //load file, only for the first call
83         Parser parser;
84         parser.parseFile(m_stPath, L"parse macro file");
85         if (parser.getExitStatus() !=  Parser::Succeded)
86         {
87             scilabWriteW(L"Unable to parse ");
88             scilabWriteW(m_stPath.c_str());
89             scilabWriteW(L"\n\n");
90             scilabWriteW(parser.getErrorMessage());
91             return false;
92         }
93
94         //find FunctionDec
95         FunctionDec* pFD = NULL;
96
97         std::list<Exp *>::iterator j;
98         std::list<Exp *>LExp = ((SeqExp*)parser.getTree())->exps_get();
99
100         for (j = LExp.begin() ; j != LExp.end() ; j++)
101         {
102             pFD = dynamic_cast<FunctionDec*>(*j);
103             if (pFD) // &&      pFD->name_get() == m_stName
104             {
105                 symbol::Context* pContext = symbol::Context::getInstance();
106                 types::InternalType* pFunc = pContext->getFunction(pFD->name_get());
107                 if (pFunc && pFunc->isMacroFile())
108                 {
109                     MacroFile* pMacro = pContext->getFunction(pFD->name_get())->getAs<MacroFile>();
110                     if (pMacro->m_pMacro == NULL)
111                     {
112                         std::list<Var *>::const_iterator        i;
113
114                         //get input parameters list
115                         std::list<symbol::Symbol> *pVarList = new std::list<symbol::Symbol>();
116                         ArrayListVar *pListVar = (ArrayListVar *)&pFD->args_get();
117                         for (i = pListVar->vars_get().begin() ; i != pListVar->vars_get().end() ; i++)
118                         {
119                             pVarList->push_back(((SimpleVar*)(*i))->name_get());
120                         }
121
122                         //get output parameters list
123                         std::list<symbol::Symbol> *pRetList = new std::list<symbol::Symbol>();
124                         ArrayListVar *pListRet = (ArrayListVar *)&pFD->returns_get();
125                         for (i = pListRet->vars_get().begin() ; i != pListRet->vars_get().end() ; i++)
126                         {
127                             pRetList->push_back(((SimpleVar*)(*i))->name_get());
128                         }
129
130                         //types::Macro macro(VarList, RetList, (SeqExp&)e.body_get());
131                         //types::Macro *pMacro = new types::Macro(m_stName, *pVarList, *pRetList, (SeqExp&)e.body_get());
132
133                         pMacro->m_pMacro = new Macro(m_wstName, *pVarList, *pRetList, (SeqExp&)pFD->body_get(), m_wstModule);
134                     }
135                 }
136             }
137         }
138     }
139     return true;
140 }
141 }