1 /*=========================================================================
4 Module: $RCSfile: bbtkInterpreter.h,v $ $
6 Date: $Date: 2008/03/19 14:58:13 $
7 Version: $Revision: 1.16 $
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.
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.
17 =========================================================================*/
20 * \brief class Interpreter : The bbtk language interpreter
23 * \class bbtk::Interpreter
24 * \brief The bbtk language interpreter
27 #ifndef __bbtkInterpreter_h__
28 #define __bbtkInterpreter_h__
30 #include "bbtkVirtualExec.h"
31 #include "bbtkExecuter.h"
32 #include "bbtkTranscriptor.h"
40 #ifdef _USE_WXWIDGETS_
44 class BBTK_EXPORT Interpreter
49 /// The enumeration of command codes == Command name
80 /// The structure storing the informations on a command
90 /// The type of dictionnary of commands
91 typedef std::map<std::string,CommandInfoType> CommandDictType;
109 /// Runs the interpretation of a file
110 ExitStatus InterpretFile( const std::string& filename);
112 /// Runs the interpretation of a buffer and deletes it !
113 ExitStatus InterpretBuffer( std::stringstream* buffer );
115 /// Runs the interpretation of a command
116 ExitStatus InterpretLine( const std::string& line );
119 /// Launches a command line interpreter (with a prompt)
120 void CommandLineInterpreter();
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); }
129 /// Puts the executer in "no exec" mode,
130 /// which creates but does not execute pipelines.
131 void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
134 //typedef Executer::DialogModeType DialogModeType;
135 typedef VirtualExec::DialogModeType DialogModeType;
137 void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
139 /// Sets the bool that indicates wether we are in command line context
140 void SetCommandLine(bool v = true) { mCommandLine = v; }
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; }
153 /// Gets the Executer
154 VirtualExec* GetExecuter() { return mExecuter; }
155 /// Gets the Executer (const)
156 const VirtualExec* GetExecuter() const { return mExecuter; }
160 /// Interprets a line
161 void InterpretLine( const std::string& line, bool &insideComment );
163 /// Reads a line from prompt
164 void GetLineFromPrompt( std::string& line );
166 /// Splits a line into words
167 void SplitLine ( const std::string& line,
168 std::vector<std::string>& words );
170 /// Executes the right action depending on the command name
171 void InterpretCommand( const std::vector<std::string>& words,
172 CommandInfoType& info );
174 /// Switch to the interpretation of a file
175 void SwitchToFile( const std::string& filename );
177 /// Switch to the interpretation of a stringstream
178 void SwitchToStream( std::stringstream* stream );
180 /// Closes the currently open file
181 void CloseCurrentFile();
183 /// Closes all open files
184 void CloseAllFiles();
186 /// Displays help (entry point of any help)
187 void Help(const std::vector<std::string>& words);
189 /// Displays help on all the commands
192 /// Displays help on a particular command
193 void HelpCommand( const std::string& command );
196 void Graph(const std::vector<std::string>& words);
201 void Print(const std::string&);
203 void Index(const std::string& filename,
204 const std::string& type = "Initials");
206 void FindCommandsWithPrefix( char* buf,
208 std::vector<std::string>& commands );
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);
221 //==================================================================
224 /// The command executer used
225 bbtk::VirtualExec* mExecuter;
227 #ifdef _USE_WXWIDGETS_
228 /// The WxConsole which uses the interpreter (0 if none)
229 bbtk::WxConsole* mWxConsole;
232 /// Vector of open files / buffers (can be stringstream)
233 std::vector<std::istream*> mFile;
235 /// Vector of names of open files with full path (as open)
236 std::vector<std::string> mFileName;
238 /// Vector of names of open files as given to the include command
239 std::vector<std::string> mIncludeFileName;
241 /// Stores the current line number in each open file
242 std::vector<int> mLine;
244 /// The dictionnary of commands
245 CommandDictType mCommandDict;
247 /// Are we in a command line context ?
250 /// The history of commands
251 std::deque< char* > mHistory;
255 // EO class Interpreter
258 // The "Quit" exception
259 struct QuitException : public bbtk::Exception
261 QuitException() : bbtk::Exception("","","") {}