]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreter.h
no message
[bbtk.git] / kernel / src / bbtkInterpreter.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkInterpreter.h,v $
4   Language:  C++
5   Date:      $Date: 2010/09/17 15:56:52 $
6   Version:   $Revision: 1.43 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  *  \file 
33  *  \brief class Interpreter : The bbtk language interpreter
34  */
35 /**
36  *  \class bbtk::Interpreter 
37  *  \brief The bbtk language interpreter
38  */
39
40 #ifndef __bbtkInterpreter_h__
41 #define __bbtkInterpreter_h__
42
43 #include "bbtkVirtualExec.h"
44 #include "bbtkExecuter.h"
45 #include "bbtkInterpreterVirtual.h"
46
47 #include <fstream>
48 #include <deque>
49
50 // Signal/slot mechanism for 'break' commands
51 //#include <boost/signal.hpp>
52 //#include <boost/bind.hpp>
53
54 namespace bbtk
55 {
56
57 #ifdef USE_WXWIDGETS
58   class WxConsole;
59 #endif
60   
61 /* EED Borrame  
62   //=======================================================================
63   class BBTK_EXPORT InterpreterUser
64   {
65   public: 
66     InterpreterUser() {}
67     virtual ~InterpreterUser() {}
68
69     virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
70     virtual void InterpreterUserViewHtmlPage(const std::string&) {}
71   };
72   //=======================================================================
73 */
74         
75
76 /* EED Borrame
77   //=======================================================================
78   class BBTK_EXPORT InterpreterException : public Exception
79   {
80   public:
81     InterpreterException( const std::string& message,
82                       bool in_script_file,
83                       const std::string& script_file,
84                       int script_line 
85                       );
86     InterpreterException( const Exception& excep,
87                       bool in_script_file,
88                       const std::string& script_file,
89                       int script_line 
90                       );
91     ~InterpreterException() throw() {}
92
93     bool IsInScriptFile() const { return mInScriptFile; }
94     const std::string& GetScriptFile() const { return mScriptFile; }
95     int GetScriptLine() const { return mScriptLine; }
96   private:
97     bool mInScriptFile;
98     std::string mScriptFile;
99     int mScriptLine;
100   };
101   //=======================================================================
102 */
103         
104         
105         
106   /*
107   //=======================================================================
108   // The "Quit" exception
109   class BBTK_EXPORT QuitException : public InterpreterError
110   {
111   public:
112     QuitException( bool in_script_file,
113                    const std::string& script_file,
114                    int script_line 
115                    )
116       : InterpreterError("QUIT",in_script_file,script_file,script_line) 
117     {}
118     ~QuitException() throw() {}
119   };
120   //=======================================================================
121   const std::string BREAK("BREAK");
122   //=======================================================================
123   // The "Break" exception
124   class BBTK_EXPORT BreakException : public InterpreterError
125   {
126   public:
127     BreakException( bool in_script_file,
128                     std::string script_file,
129                     int script_line 
130                     )
131       : InterpreterError(BREAK,in_script_file,script_file,script_line) 
132     { std::cout << "BUILDING BREAK"<<std::endl; }
133     ~BreakException() throw() {}
134   };
135   //=======================================================================
136   */
137
138   //=======================================================================
139   class BBTK_EXPORT Interpreter : public InterpreterVirtual
140   {
141     BBTK_OBJECT_INTERFACE_NO_CONDES(Interpreter);
142     typedef Object InterpreterVirtual;
143   public:
144     static Pointer New(const std::string& cpp_file = "");
145     static Pointer New(VirtualExec::Pointer);
146
147 /* EED Borrame    
148     typedef enum 
149       {
150         Interpreter_OK,
151         Interpreter_ERROR,
152         Interpreter_QUIT,
153         Interpreter_BREAK,
154       }
155       ExitStatus;
156
157     /// Runs the interpretation of a file
158     ExitStatus InterpretFile( const std::string& filename, bool source = false);
159
160     /// Runs the interpretation of a buffer and deletes it !
161     ExitStatus InterpretBuffer( std::stringstream* buffer );
162
163     /// Runs the interpretation of a command
164     ExitStatus InterpretLine( const std::string& line );
165
166     /// Runs the interpretation of the currently open streams
167     ExitStatus InterpretCurrentStreams();
168
169     /// Launches a command line interpreter (with a prompt)
170     void CommandLineInterpreter();
171 */
172
173
174     /// Sets the inputs of the workspace :
175     /// the map is passed as is to the Executer
176     void SetInputs(const std::map<std::string,std::string>& m)
177     { mVirtualExecuter->SetInputs(m); }
178
179     /// Puts the executer in "no exec" mode,
180     /// which creates but does not execute pipelines.
181     void SetNoExecMode(bool m) { mVirtualExecuter->SetNoExecMode(m); }
182
183     /// Puts the executer in "no error" mode, 
184     /// Errors do not stop execution (but warnings are produced)
185     void SetNoErrorMode(bool m) { mVirtualExecuter->SetNoErrorMode(m);}
186     ///
187     //typedef Executer::DialogModeType DialogModeType;
188     typedef VirtualExec::DialogModeType DialogModeType;
189
190     void SetDialogMode(DialogModeType t) { mVirtualExecuter->SetDialogMode(t);}
191
192 /*EED Borrame     
193     /// Sets the bool that indicates wether we are in command line context
194     void SetCommandLine(bool v = true) { mCommandLine = v; }
195
196     void SetThrow(bool b) { mThrow = b; }
197  
198 #ifdef USE_WXWIDGETS
199     /// Sets the user of the interpreter (if any)
200     void SetUser(InterpreterUser* c) { mUser = c; }
201     /// Gets the InterpreterUser of this 
202     InterpreterUser* GetUser() { return mUser; }
203     /// Gets the InterpreterUser of this (const)
204     const InterpreterUser* GetUser() const { return mUser; }
205
206 #endif
207  */
208
209     /// Gets the Executer 
210     VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
211
212     /*
213     // For 'break' commands observation
214     typedef boost::signals::trackable BreakObserverType;
215     typedef boost::signal<void ()>  BreakSignalType;
216     typedef BreakSignalType::slot_function_type BreakCallbackType;
217   
218     // Adds a callback when 'break' command issued
219     void AddBreakObserver( BreakCallbackType );
220     */
221
222   protected:
223
224 /* EED Borrame    
225     /// The enumeration of command codes == Command name
226     typedef enum
227     {
228       cBreak, // LG 12/12/08 : Stops the current script execution (if not exec frozen) - used in tutorial + debugging 
229       cClear, // LG 12/12/08 : Clears the current complex black box (e.g. workspace) - used in tours
230       cNew,
231       cDelete,
232       cConnect,
233       cExec,
234       cPackage,
235       cEndPackage,
236       cDefine,
237       cEndDefine,
238       cInput,
239       cOutput,
240       cSet,
241       cConfig,  // JPR
242       cReset,   // EED
243       cAuthor, 
244       cCategory, // JPR
245       cDescription,
246       cHelp,
247       cMessage,
248       cInclude,
249       cQuit,
250       cLoad,
251       cUnload,
252       cGraph,
253       cPrint,
254       cIndex,
255       cKind, // LG
256       cNewGUI, // LG
257       cWorkspace, // LG
258       cDebug // LG
259     } CommandCodeType;
260
261     /// The structure storing the informations on a command 
262     typedef struct 
263     {
264       std::string keyword;
265       int argmin, argmax;
266       CommandCodeType code;
267       std::string syntax;
268       std::string help;
269     } CommandInfoType;
270
271     /// The type of dictionnary of commands 
272     typedef std::map<std::string,CommandInfoType> CommandDictType;
273
274
275     /// Interprets a line 
276     void DoInterpretLine( const std::string& line ); //, bool &insideComment );
277
278     /// Reads a line from prompt
279     void GetLineFromPrompt( std::string& line );
280
281     /// Splits a line into words
282     void SplitLine ( const std::string& line,
283                      std::vector<std::string>& words );
284
285     /// Executes the right action depending on the command name
286     void InterpretCommand( const std::vector<std::string>& words,
287                            CommandInfoType& info );
288
289     /// Switch to the interpretation of a file
290     void SwitchToFile( const std::string& filename, bool source = false );
291
292    /// Switch to the interpretation of a stringstream
293     void SwitchToStream( std::stringstream* stream );
294
295     /// Closes the currently open file
296     void CloseCurrentFile();
297
298     /// Closes all open files
299     void CloseAllFiles();
300 */
301           
302     /// Resets all
303     virtual void commandReset();
304
305           
306     /// Displays help (entry point of any help)
307     virtual void commandHelp(const std::vector<std::string>& words);
308
309     /// Displays help on all the commands
310     void HelpCommands();
311
312           
313     /// Displays help on a particular command 
314     virtual void commandHelp( const std::string& command );
315
316     ///
317     virtual void commandGraph(const std::vector<std::string>& words);
318
319     ///
320     virtual void commandConfig() const; 
321     /// 
322     //    void Print(const std::string&);
323
324     virtual void commandIndex(const std::string& filename, 
325                const std::string& type = "Initials");
326
327 /*EED Borrame     
328     ///
329     void FindCommandsWithPrefix( char* buf,
330                                  int n,
331                                  std::vector<std::string>& commands );
332 */
333           
334     /// Creates and connects the piece of pipeline which defines a GUI 
335     /// for the box box. 
336     /// Define it as a complex box type with name instanceName+"Type"
337     /// The instance is called instanceName
338     /// and connected to the existing pipeline
339       virtual void commandNewGUI(const std::string& box,const std::string& instanceName);
340       virtual void commandDebug(const std::string& arg);
341           virtual void commandNew(std::string boxType, std::string boxName);
342           virtual void commandDelete(std::string boxName);
343           virtual void commandConnection(std::string nodeFrom, std::string outputLabel, std::string nodeTo, std::string inputLabel);
344           virtual void commandPackage(std::string packageName);
345           virtual void commandEndPackage();
346           virtual void commandDefine(std::string name, std::string pack, std::string scriptfilename);
347           virtual void commandEndDefine();
348           virtual void commandKind(std::string kind);
349           virtual void commandPrint(std::string value);
350           virtual void commandExec(std::string word);
351           virtual void commandInput(std::string name, std::string box, std::string input,std::string  help);
352           virtual void commandOutput(std::string name, std::string box, std::string output,std::string  help);
353           virtual void commandSet(std::string box, std::string input, std::string value);
354           virtual void commandAuthor(std::string author);
355           virtual void commandCategory(std::string categorytype);
356           virtual void commandDescription(std::string description);
357           virtual void commandClear();
358           virtual void commandInclude(std::string word, bool ok);
359           virtual void commandLoad(std::string packageName);
360           virtual void commandUnload(std::string packageName);
361           virtual void commandBreak();
362           virtual void commandQuit();
363           virtual void commandMessage();
364           virtual void commandMessage(std::string kind, std::string levelstr);
365           virtual void SetCurrentFileName(std::string fullPathScriptName); 
366
367           
368           /// Constructor
369           Interpreter(const std::string& cpp_file = "");
370           Interpreter(VirtualExec::Pointer);
371           
372   private:
373
374                   
375     void Init(VirtualExec::Pointer, const std::string& cpp_file);
376
377 /*EED  Borrame 
378     /// Opens the file fullPathScriptName 
379     /// includeScriptName is the name as given to the include command 
380     void LoadScript( std::string fullPathScriptName,
381                      std::string includeScriptName);
382 */  
383     //==================================================================
384     // ATTRIBUTES
385
386     /// The command executer used
387     bbtk::VirtualExec::Pointer mVirtualExecuter;
388     bbtk::Executer::WeakPointer mRealExecuter;
389
390 /*EED Borrame     
391     /// The user of  the interpreter (0 if none)
392     bbtk::InterpreterUser* mUser;
393     /// Vector of open files / buffers (can be stringstream)
394     std::vector<std::istream*> mFile;
395
396     /// Vector of names of open files with full path (as open)
397     std::vector<std::string> mFileName;
398
399     /// Vector of names of files which have been open 
400     /// before (and may closed then which are no more in mFileName)
401     /// with full path (as open)
402     std::vector<std::string> mFileNameHistory;
403
404     /// Vector of names of open files as given to the include command
405     std::vector<std::string> mIncludeFileName;
406
407     /// Stores the current line number in each open file
408     std::vector<int> mLine;
409
410     /// The dictionnary of commands
411     CommandDictType mCommandDict;
412
413     /// Are we in a command line context ?
414     bool mCommandLine;
415
416     /// Are we inside a commented-out zone ?
417     bool mInsideComment;
418     
419     /// The current interpreter status
420     ExitStatus mStatus;
421
422     /// The history of commands
423     std::deque< char* > mHistory;
424
425     int bufferNb;
426     
427     bool mThrow;
428
429     /// The break signal
430     //    BreakSignalType mBreakSignal;
431  
432  */
433
434   protected:
435           ~Interpreter();
436
437           /// 
438           virtual void CatchInterpreterException( const InterpreterException& e );
439           virtual void CatchBbtkException( const bbtk::Exception& e );
440           virtual void CatchStdException( const std::exception& e );
441           virtual void CatchUnknownException();
442           
443           
444   };
445   // EO class Interpreter
446
447
448
449 }
450 #endif