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