]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreter.h
ea2f77acf7bec9e87656c303c7ce921a2b55c8b9
[bbtk.git] / kernel / src / bbtkInterpreter.h
1 /*=========================================================================
2
3   Program:   bbtk
4   Module:    $RCSfile: bbtkInterpreter.h,v $ $
5   Language:  C++
6   Date:      $Date: 2008/03/19 14:58:13 $
7   Version:   $Revision: 1.16 $
8
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16
17 =========================================================================*/
18 /**
19  *  \file 
20  *  \brief class Interpreter : The bbtk language interpreter
21  */
22 /**
23  *  \class bbtk::Interpreter 
24  *  \brief The bbtk language interpreter
25  */
26
27 #ifndef __bbtkInterpreter_h__
28 #define __bbtkInterpreter_h__
29
30 #include "bbtkVirtualExec.h"
31 #include "bbtkExecuter.h"
32 #include "bbtkTranscriptor.h"
33
34 #include <fstream>
35 #include <deque>
36
37 namespace bbtk
38 {
39
40 #ifdef _USE_WXWIDGETS_
41   class WxConsole;
42 #endif
43   
44   class BBTK_EXPORT Interpreter
45   {
46
47   private:
48
49     /// The enumeration of command codes == Command name
50     typedef enum
51     {
52       cNew,
53       cDelete,
54       cConnect,
55       cExec,
56       cPackage,
57       cEndPackage,
58       cDefine,
59       cEndDefine,
60       cInput,
61       cOutput,
62       cSet,
63       cConfig,  // JPR
64       cReset,   // EED
65       cAuthor, 
66       cCategory, // JPR
67       cDescription,
68       cHelp,
69       cMessage,
70       cInclude,
71       cQuit,
72       cLoad,
73       cUnload,
74       cGraph,
75       cPrint,
76       cIndex,
77       cWorkspace // LG
78     } CommandCodeType;
79
80     /// The structure storing the informations on a command 
81     typedef struct 
82     {
83       std::string category;
84       int argmin, argmax;
85       CommandCodeType code;
86       std::string syntax;
87       std::string help;
88     } CommandInfoType;
89
90     /// The type of dictionnary of commands 
91     typedef std::map<std::string,CommandInfoType> CommandDictType;
92
93   public:
94     /// Constructor
95     Interpreter();
96
97     /// Destructor
98     ~Interpreter();
99
100
101     typedef enum 
102       {
103         OK,
104         ERROR,
105         QUIT
106       }
107       ExitStatus;
108
109     /// Runs the interpretation of a file
110     ExitStatus InterpretFile( const std::string& filename);
111
112     /// Runs the interpretation of a buffer and deletes it !
113     ExitStatus InterpretBuffer( std::stringstream* buffer );
114
115     /// Runs the interpretation of a command
116     ExitStatus InterpretLine( const std::string& line );
117
118
119     /// Launches a command line interpreter (with a prompt)
120     void CommandLineInterpreter();
121
122
123
124     /// Sets the inputs of the workspace :
125     /// the map is passed as is to the Executer
126     void SetInputs(const std::map<std::string,std::string>& m)
127     { mExecuter->SetInputs(m); }
128
129     /// Puts the executer in "no exec" mode,
130     /// which creates but does not execute pipelines.
131     void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
132
133     ///
134     //typedef Executer::DialogModeType DialogModeType;
135     typedef VirtualExec::DialogModeType DialogModeType;
136
137     void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
138
139     /// Sets the bool that indicates wether we are in command line context
140     void SetCommandLine(bool v = true) { mCommandLine = v; }
141
142
143     
144 #ifdef _USE_WXWIDGETS_
145     /// Sets the WxConsole that created the interpreter (if any)
146     void SetWxConsole(WxConsole* c) { mWxConsole = c; }
147     /// Gets the WxConsole that created the interpreter (if any) 
148     WxConsole* GetWxConsole() { return mWxConsole; }
149     /// Gets the WxConsole that created the interpreter (if any) - const 
150     const WxConsole* GetWxConsole() const { return mWxConsole; }
151 #endif
152
153     /// Gets the Executer 
154     VirtualExec* GetExecuter() { return mExecuter; }
155     /// Gets the Executer (const)
156     const VirtualExec* GetExecuter() const { return mExecuter; }
157
158
159   protected:
160     /// Interprets a line 
161     void InterpretLine( const std::string& line, bool &insideComment );
162
163     /// Reads a line from prompt
164     void GetLineFromPrompt( std::string& line );
165
166     /// Splits a line into words
167     void SplitLine ( const std::string& line,
168                      std::vector<std::string>& words );
169
170     /// Executes the right action depending on the command name
171     void InterpretCommand( const std::vector<std::string>& words,
172                            CommandInfoType& info );
173
174     /// Switch to the interpretation of a file
175     void SwitchToFile( const std::string& filename );
176
177    /// Switch to the interpretation of a stringstream
178     void SwitchToStream( std::stringstream* stream );
179
180     /// Closes the currently open file
181     void CloseCurrentFile();
182
183     /// Closes all open files
184     void CloseAllFiles();
185
186     /// Displays help (entry point of any help)
187     void Help(const std::vector<std::string>& words);
188
189     /// Displays help on all the commands
190     void HelpCommands();
191
192     /// Displays help on a particular command 
193     void HelpCommand( const std::string& command );
194
195     ///
196     void Graph(const std::vector<std::string>& words);
197
198     ///
199     void Config() const; 
200     /// 
201     void Print(const std::string&);
202
203     void Index(const std::string& filename, 
204                const std::string& type = "Initials");
205     ///
206     void FindCommandsWithPrefix( char* buf,
207                                  int n,
208                                  std::vector<std::string>& commands );
209
210
211
212   private:
213
214     /// Opens the file fullPathScriptName 
215     /// includeScriptName is the name as given to the include command 
216     void LoadScript( std::string fullPathScriptName,
217                      std::string includeScriptName);
218
219   private:
220
221     //==================================================================
222     // ATTRIBUTES
223
224     /// The command executer used
225     bbtk::VirtualExec* mExecuter;
226
227 #ifdef _USE_WXWIDGETS_
228     /// The WxConsole which uses the interpreter (0 if none)
229     bbtk::WxConsole* mWxConsole;
230 #endif
231
232     /// Vector of open files / buffers (can be stringstream)
233     std::vector<std::istream*> mFile;
234
235     /// Vector of names of open files with full path (as open)
236     std::vector<std::string> mFileName;
237
238     /// Vector of names of open files as given to the include command
239     std::vector<std::string> mIncludeFileName;
240
241     /// Stores the current line number in each open file
242     std::vector<int> mLine;
243
244     /// The dictionnary of commands
245     CommandDictType mCommandDict;
246
247     /// Are we in a command line context ?
248     bool mCommandLine;
249
250     /// The history of commands
251     std::deque< char* > mHistory;
252
253    int bufferNb;
254   };
255   // EO class Interpreter
256
257
258   // The "Quit" exception
259   struct QuitException : public bbtk::Exception
260   {
261     QuitException() : bbtk::Exception("","","") {}
262   };
263
264 }
265 #endif