1 /*=========================================================================
4 Module: $RCSfile: bbtkInterpreter.h,v $ $
6 Date: $Date: 2008/03/18 12:51:26 $
7 Version: $Revision: 1.15 $
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;
100 /// Launches a command line interpreter (with a prompt)
101 void CommandLineInterpreter();
103 /// Sets the inputs of the workspace :
104 /// the map is passed as is to the Executer
105 void SetInputs(const std::map<std::string,std::string>& m)
106 { mExecuter->SetInputs(m); }
108 /// Puts the executer in "no exec" mode,
109 /// which creates but does not execute pipelines.
110 void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
113 //typedef Executer::DialogModeType DialogModeType;
114 typedef VirtualExec::DialogModeType DialogModeType;
116 void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
118 /// Runs the interpretation of a file
119 void InterpretFile( const std::string& filename);
121 /// Runs the interpretation of a buffer and deletes it !
122 void InterpretBuffer( std::stringstream* buffer );
124 /// Interprets a line (either from a file or typed interactively)
125 void InterpretLine( const std::string& line, bool &insideComment );
127 /// Reads a line from prompt
128 void GetLineFromPrompt( std::string& line );
130 /// Splits a line into words
131 void SplitLine ( const std::string& line,
132 std::vector<std::string>& words );
134 /// Executes the right action depending on the command name
135 void InterpretCommand( const std::vector<std::string>& words,
136 CommandInfoType& info );
138 /// Switch to the interpretation of a file
139 void SwitchToFile( const std::string& filename );
141 /// Switch to the interpretation of a stringstream
142 void SwitchToStream( std::stringstream* stream );
144 /// Closes the currently open file
145 void CloseCurrentFile();
147 /// Closes all open files
148 void CloseAllFiles();
150 /// Displays help (entry point of any help)
151 void Help(const std::vector<std::string>& words);
153 /// Displays help on all the commands
156 /// Displays help on a particular command
157 void HelpCommand( const std::string& command );
160 void Graph(const std::vector<std::string>& words);
165 void Print(const std::string&);
167 void Index(const std::string& filename,
168 const std::string& type = "Initials");
170 void FindCommandsWithPrefix( char* buf,
172 std::vector<std::string>& commands );
174 /// Sets the bool that indicates wether we are in command line context
175 void SetCommandLine(bool v = true) { mCommandLine = v; }
177 #ifdef _USE_WXWIDGETS_
178 /// Sets the WxConsole that created the interpreter (if any)
179 void SetWxConsole(WxConsole* c) { mWxConsole = c; }
180 /// Gets the WxConsole that created the interpreter (if any)
181 WxConsole* GetWxConsole() { return mWxConsole; }
182 /// Gets the WxConsole that created the interpreter (if any) - const
183 const WxConsole* GetWxConsole() const { return mWxConsole; }
186 /// Gets the Executer
187 VirtualExec* GetExecuter() { return mExecuter; }
188 /// Gets the Executer (const)
189 const VirtualExec* GetExecuter() const { return mExecuter; }
193 /// Opens the file fullPathScriptName
194 /// includeScriptName is the name as given to the include command
195 void LoadScript( std::string fullPathScriptName,
196 std::string includeScriptName);
200 //==================================================================
203 /// The command executer used
204 bbtk::VirtualExec* mExecuter;
206 #ifdef _USE_WXWIDGETS_
207 /// The WxConsole which uses the interpreter (0 if none)
208 bbtk::WxConsole* mWxConsole;
211 /// Vector of open files / buffers (can be stringstream)
212 std::vector<std::istream*> mFile;
214 /// Vector of names of open files with full path (as open)
215 std::vector<std::string> mFileName;
217 /// Vector of names of open files as given to the include command
218 std::vector<std::string> mIncludeFileName;
220 /// Stores the current line number in each open file
221 std::vector<int> mLine;
223 /// The dictionnary of commands
224 CommandDictType mCommandDict;
226 /// Are we in a command line context ?
229 /// The history of commands
230 std::deque< char* > mHistory;
234 // EO class Interpreter
237 // The "Quit" exception
238 struct QuitException : public bbtk::Exception
240 QuitException() : bbtk::Exception("","","") {}