]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreter.h
*** empty log message ***
[bbtk.git] / kernel / src / bbtkInterpreter.h
1 /*=========================================================================
2
3   Program:   bbtk
4   Module:    $RCSfile: bbtkInterpreter.h,v $ $
5   Language:  C++
6   Date:      $Date: 2008/02/12 12:55:16 $
7   Version:   $Revision: 1.11 $
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
41   class BBTK_EXPORT Interpreter
42   {
43
44   private:
45
46     /// The enumeration of command codes == Command name
47     typedef enum
48     {
49       cNew,
50       cDelete,
51       cConnect,
52       cExec,
53       cPackage,
54       cEndPackage,
55       cDefine,
56       cEndDefine,
57       cInput,
58       cOutput,
59       cSet,
60       cConfig,  // JPR
61       cReset,   // EED
62       cAuthor, 
63       cCategory, // JPR
64       cDescription,
65       cHelp,
66       cMessage,
67       cInclude,
68       cQuit,
69       cLoad,
70       cUnload,
71       cGraph,
72       cPrint,
73       cIndex,
74       cWorkspace // LG
75     } CommandCodeType;
76
77     /// The structure storing the informations on a command 
78     typedef struct 
79     {
80       std::string category;
81       int argmin, argmax;
82       CommandCodeType code;
83       std::string syntax;
84       std::string help;
85     } CommandInfoType;
86
87     /// The type of dictionnary of commands 
88     typedef std::map<std::string,CommandInfoType> CommandDictType;
89
90   public:
91     /// Constructor
92     Interpreter();
93
94     /// Destructor
95     ~Interpreter();
96
97    static Interpreter* mGlobalInterpreter;
98
99     /// Launches a command line interpreter (with a prompt)
100     void CommandLineInterpreter();
101
102     /// Sets the inputs of the workspace :
103     /// the map is passed as is to the Executer
104     void SetInputs(const std::map<std::string,std::string>& m)
105     { mExecuter->SetInputs(m); }
106
107     /// Puts the executer in "no exec" mode,
108     /// which creates but does not execute pipelines.
109     void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
110
111     ///
112     //typedef Executer::DialogModeType DialogModeType;
113     typedef VirtualExec::DialogModeType DialogModeType;
114
115     void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
116
117     /// Runs the interpretation of a file
118     void InterpretFile( const std::string& filename, 
119                         bool use_configuration_file=true);
120
121     /// Interprets a line (either from a file or typed interactively)
122     void InterpretLine( const std::string& line, bool &insideComment );
123
124     /// Reads a line from prompt
125     void GetLineFromPrompt( std::string& line );
126
127     /// Splits a line into words
128     void SplitLine ( const std::string& line,
129                      std::vector<std::string>& words );
130
131     /// Executes the right action depending on the command name
132     void InterpretCommand( const std::vector<std::string>& words,
133                            CommandInfoType& info );
134
135     /// Switch to the interpretation of a file
136     void SwitchToFile( const std::string& filename, 
137                        bool use_configuration_file=true);
138
139     /// Closes the currently open file
140     void CloseCurrentFile();
141
142     /// Closes all open files
143     void CloseAllFiles();
144
145     /// Displays help (entry point of any help)
146     void Help(const std::vector<std::string>& words);
147
148     /// Displays help on all the commands
149     void HelpCommands();
150
151     /// Displays help on a particular command 
152     void HelpCommand( const std::string& command );
153
154     ///
155     void Graph(const std::vector<std::string>& words);
156
157     ///
158     void Config() const; 
159     /// 
160     void Print(const std::string&);
161
162     void Index(const std::string& filename, 
163                const std::string& type = "Initials");
164     ///
165     void FindCommandsWithPrefix( char* buf,
166                                  int n,
167                                  std::vector<std::string>& commands );
168
169     /// Sets the bool that indicates wether we are in command line context
170     void SetCommandLine(bool v = true) { mCommandLine = v; }
171
172
173   private:
174
175     /// Opens the file fullPathScriptName 
176     /// includeScriptName is the name as given to the include command 
177     void LoadScript( std::string fullPathScriptName,
178                      std::string includeScriptName);
179
180   private:
181
182     //==================================================================
183     // ATTRIBUTES
184
185     /// The factory
186     //    bbtk::Factory* mFactory;
187
188     /// The command executer
189     bbtk::VirtualExec* mExecuter;
190
191     /// Vector of open files 
192     std::vector<std::ifstream*> mFile;
193
194     /// Vector of names of open files with full path (as open)
195     std::vector<std::string> mFileName;
196
197     /// Vector of names of open files as given to the include command
198     std::vector<std::string> mIncludeFileName;
199
200     /// Stores the current line number in each open file
201     std::vector<int> mLine;
202
203     /// The dictionnary of commands
204     CommandDictType mCommandDict;
205
206     /// Are we in a command line context ?
207     bool mCommandLine;
208
209     /// The history of commands
210     std::deque< char* > mHistory;
211
212   };
213
214
215   // The "Quit" exception
216   struct QuitException : public bbtk::Exception
217   {
218     QuitException() : bbtk::Exception("","","") {}
219   };
220
221 }
222 #endif