OpenASIP  2.0
ScriptInterpreter.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file ScriptInterpreter.cc
26  *
27  * Definition of ScriptInterpreter class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @note reviewed 27 May 2004 by pj, jn, vpj, ll
31  * @note rating: yellow
32  */
33 
34 #include <fstream>
35 #include <algorithm>
36 
37 #include "ScriptInterpreter.hh"
38 #include "MapTools.hh"
39 #include "Exception.hh"
40 #include "Application.hh"
41 
42 using std::string;
43 using std::ifstream;
44 using std::vector;
45 using std::stable_sort;
46 
47 /**
48  * Constructor.
49  */
51  result_(NULL), errorState_(false), finalized_(false) {
52 }
53 
54 /**
55  * Destructor.
56  */
58 
59  if (!finalized_) {
60  string method = "ScriptInterpreter::~ScriptInterpreter()";
61  string message = "Interpreter not finalized.";
62  Application::writeToErrorLog(__FILE__, __LINE__, method, message);
63  }
64 
66 
67  if (result_ != NULL) {
68  delete result_;
69  }
70 }
71 
72 /**
73  * Adds a custom command to interpreter.
74  *
75  * @param command The command to be added.
76  */
77 void
79  command->setInterpreter(this);
80  commands_.insert(ValType(command->name(), command));
82 }
83 
84 /**
85  * Removes a custom command from interpreter.
86  *
87  * Reserved memory for CustomCommand is freed.
88  *
89  * @param commandName The name of the command that is removed.
90  */
91 void
92 ScriptInterpreter::removeCustomCommand(const std::string& commandName) {
93  if (MapTools::containsKey(commands_, commandName)) {
94  MapIter mi = commands_.find(commandName);
96  delete (*mi).second;
97  commands_.erase(mi);
98  }
99 }
100 
101 /**
102  * Returns a custom command with a given name.
103  *
104  * Returns NULL if CustomCommand is not found.
105  *
106  * @param commandName The name of the wanted command.
107  * @return The pointer to a wanted custom command.
108  */
110 ScriptInterpreter::customCommand(const std::string& commandName) {
111 
112  if (!MapTools::containsKey(commands_, commandName)) {
113  return NULL;
114  }
115 
116  MapIter mi = commands_.find(commandName);
117  return (*mi).second;
118 }
119 
120 /**
121  * Stores the result of last executed command.
122  *
123  * Sets result also to concrete interpreter.
124  *
125  * @param result The result.
126  */
127 void
129  if (result_ != NULL) {
130  delete result_;
131  }
132  result_ = result;
134 }
135 
136 /**
137  * Stores the result of last executed command.
138  *
139  * Sets result also to concrete interpreter.
140  *
141  * @param result The result.
142  */
143 void
144 ScriptInterpreter::setResult(const std::string& result) {
145  if (result_ == NULL) {
146  result_ = new DataObject();
147  }
150 }
151 
152 /**
153  * Stores the result of last executed command.
154  *
155  * Sets result also to concrete interpreter.
156  *
157  * @param result The result.
158  */
159 void
161  if (result_ == NULL) {
162  result_ = new DataObject();
163  }
166 }
167 
168 /**
169  * Stores the result of last executed command.
170  *
171  * Sets result also to concrete interpreter.
172  *
173  * @param result The result.
174  */
175 void
177  if (result_ == NULL) {
178  result_ = new DataObject();
179  }
182 }
183 
184 /**
185  * Returns the result of last executed command as a string.
186  *
187  * @return Last result as string.
188  * @exception NumberFormatException If converting result to string fails.
189  */
190 string
192  if (result_ != NULL) {
193  return result_->stringValue();
194  } else {
195  return "";
196  }
197 }
198 
199 /**
200  * Sets or unsets the error state of interpreter.
201  *
202  * @param state The error state flag.
203  */
204 void
206  errorState_ = state;
207 }
208 
209 /**
210  * Sets an error message and sets errors status of interpreter.
211  *
212  * @param errorMessage The error message.
213  */
214 void
215 ScriptInterpreter::setError(std::string errorMessage) {
216  DataObject* result = new DataObject();
217  result->setString(errorMessage);
218  setResult(result);
219  setError(true);
220 }
221 
222 
223 /**
224  * Returns true, if interpreter is in error state.
225  *
226  * @return True, if interpreter is in error state.
227  */
228 bool
230  return errorState_;
231 }
232 
233 /**
234  * Sets value for a variable.
235  *
236  * @param interpreterVariableName The name of the variable.
237  * @param value The value for a variable.
238  */
239 void
241  const std::string& interpreterVariableName,
242  const std::string& value) {
243 
244  DataObject object;
245  object.setString(value);
246  setVariableToInterpreter(interpreterVariableName, object);
247 }
248 
249 /**
250  * Sets the value for interpreter variable.
251  *
252  * @param interpreterVariableName The name of the variable.
253  * @param value The value for the variable.
254  */
255 void
257  const std::string& interpreterVariableName,
258  int value) {
259 
260  DataObject object;
261  object.setInteger(value);
262  setVariableToInterpreter(interpreterVariableName, object);
263 }
264 
265 /**
266  * Returns the value of the string variable.
267  *
268  * @param interpreterVariableName The name of the variable.
269  * @return The value of the variable.
270  * @exception NumberFormatException If converting variable to string fails.
271  */
272 string
274  const std::string& interpreterVariableName) {
275  DataObject var = variable(interpreterVariableName);
276  string value = var.stringValue();
277  return value;
278 }
279 
280 /**
281  * Returns the value of the integer variable.
282  *
283  * @param interpreterVariableName The name of the variable.
284  * @return The value of the variable.
285  * @exception NumberFormatException If converting variable to integer fails.
286  */
287 int
289  const std::string& interpreterVariableName) {
290  DataObject var = variable(interpreterVariableName);
291  int value = var.integerValue();
292  return value;
293 }
294 
295 /**
296  * Opens a script file and processes it.
297  *
298  * @param scriptFileName The name of the script file.
299  * @return True, if process is successful, false otherwise.
300  * @exception UnreachableStream If file cannot be opened.
301  */
302 bool
303 ScriptInterpreter::processScriptFile(const std::string& scriptFileName) {
304  ifstream fileStream(scriptFileName.c_str());
305 
306  if (fileStream.bad()) {
307  string method = "ScriptInterpreter::processScriptFile";
308  string message = "Cannot open file " + scriptFileName;
309  throw UnreachableStream(__FILE__, __LINE__, method, message);
310  }
311 
312  string line;
313  while (getline(fileStream, line)) {
314  if (!interpret(line)) {
315  fileStream.close();
316  return false;
317  }
318  }
319  fileStream.close();
320  return true;
321 }
322 
323 /**
324  * Removes all created CustomCommands from interpreter.
325  *
326  * This is called before interpreter is deleted.
327  */
328 void
330  for (MapIter mi = commands_.begin(); mi != commands_.end(); mi++) {
331  removeCustomCommandFromInterpreter(*(*mi).second);
332  }
333  finalized_ = true;
334 }
335 
336 /**
337  * Returns a vector of alphabetically sorted names of CustomCommands.
338  *
339  * @return CustomCommands sorted by name.
340  */
341 vector<string>
343 
344  vector<string> names;
345 
346  MapIter it = commands_.begin();
347  while (it != commands_.end()) {
348  names.push_back((*it).second->name());
349  it++;
350  }
351 
352  vector<string>::iterator first = names.begin();
353  vector<string>::iterator last = names.end();
354  stable_sort(first , last);
355  return names;
356 }
357 
358 /**
359  * Returns the LineReader instance.
360  *
361  * LineReader is needed by some CustomCommands. ScriptInterpreter may also
362  * need it.
363  *
364  * @return LineReader instance.
365  */
366 LineReader*
368  return reader_;
369 }
370 
371 /**
372  * Sets the LineReader for the interpreter.
373  *
374  * @param reader The LineReader.
375  */
376 void
378  reader_ = reader;
379 }
ScriptInterpreter::finalize
virtual void finalize()
Definition: ScriptInterpreter.cc:329
CustomCommand::name
std::string name() const
UnreachableStream
Definition: Exception.hh:171
ScriptInterpreter::result
virtual std::string result()
Definition: ScriptInterpreter.cc:191
Exception.hh
DataObject::setDouble
virtual void setDouble(double value)
Definition: DataObject.cc:145
DataObject
Definition: DataObject.hh:50
DataObject::stringValue
virtual std::string stringValue() const
Definition: DataObject.cc:344
Application::writeToErrorLog
static void writeToErrorLog(const std::string fileName, const int lineNumber, const std::string functionName, const std::string message, const int neededVerbosity=0)
Definition: Application.cc:224
ScriptInterpreter::variable
virtual DataObject variable(const std::string &name)=0
MapTools.hh
CustomCommand::setInterpreter
void setInterpreter(ScriptInterpreter *si)
DataObject::setInteger
virtual void setInteger(int value)
Definition: DataObject.cc:115
ScriptInterpreter::variableIntegerValue
virtual int variableIntegerValue(const std::string &interpreterVariableName)
Definition: ScriptInterpreter.cc:288
ScriptInterpreter::commands_
std::map< std::string, CustomCommand * > commands_
Map containing all custom commands for interpreter.
Definition: ScriptInterpreter.hh:127
MapTools::deleteAllValues
static void deleteAllValues(MapType &aMap)
ScriptInterpreter::reader_
LineReader * reader_
LineReader for interpreter.
Definition: ScriptInterpreter.hh:135
ScriptInterpreter::setVariableToInterpreter
virtual void setVariableToInterpreter(const std::string &name, const DataObject &value)=0
ScriptInterpreter::errorState_
bool errorState_
Indicates whether we are in error state.
Definition: ScriptInterpreter.hh:131
ScriptInterpreter::result_
DataObject * result_
The latest interpreter result.
Definition: ScriptInterpreter.hh:129
ScriptInterpreter::addCustomCommandToInterpreter
virtual void addCustomCommandToInterpreter(const CustomCommand &command)=0
DataObject::integerValue
virtual int integerValue() const
Definition: DataObject.cc:204
ScriptInterpreter::~ScriptInterpreter
virtual ~ScriptInterpreter()
Definition: ScriptInterpreter.cc:57
ScriptInterpreter::setLineReader
virtual void setLineReader(LineReader *reader)
Definition: ScriptInterpreter.cc:377
ScriptInterpreter::processScriptFile
virtual bool processScriptFile(const std::string &scriptFileName)
Definition: ScriptInterpreter.cc:303
ScriptInterpreter.hh
ScriptInterpreter::ValType
std::map< std::string, CustomCommand * >::value_type ValType
val_type for map.
Definition: ScriptInterpreter.hh:119
ScriptInterpreter::customCommandsSortedByName
std::vector< std::string > customCommandsSortedByName()
Definition: ScriptInterpreter.cc:342
ScriptInterpreter::removeCustomCommand
virtual void removeCustomCommand(const std::string &commandName)
Definition: ScriptInterpreter.cc:92
ScriptInterpreter::error
virtual bool error() const
Definition: ScriptInterpreter.cc:229
Application.hh
CustomCommand
Definition: CustomCommand.hh:54
ScriptInterpreter::ScriptInterpreter
ScriptInterpreter()
Definition: ScriptInterpreter.cc:50
ScriptInterpreter::removeCustomCommandFromInterpreter
virtual void removeCustomCommandFromInterpreter(const CustomCommand &command)=0
ScriptInterpreter::interpret
virtual bool interpret(const std::string &commandLine)=0
ScriptInterpreter::customCommand
virtual CustomCommand * customCommand(const std::string &commandName)
Definition: ScriptInterpreter.cc:110
ScriptInterpreter::setResult
virtual void setResult(DataObject *result)
Definition: ScriptInterpreter.cc:128
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
LineReader
Definition: LineReader.hh:52
ScriptInterpreter::MapIter
std::map< std::string, CustomCommand * >::iterator MapIter
Iterator for map.
Definition: ScriptInterpreter.hh:117
ScriptInterpreter::setVariable
virtual void setVariable(const std::string &interpreterVariableName, const std::string &value)
Definition: ScriptInterpreter.cc:240
ScriptInterpreter::setError
virtual void setError(bool state)
Definition: ScriptInterpreter.cc:205
DataObject::setString
virtual void setString(std::string value)
Definition: DataObject.cc:130
ScriptInterpreter::variableStringValue
virtual std::string variableStringValue(const std::string &interpreterVariableName)
Definition: ScriptInterpreter.cc:273
ScriptInterpreter::lineReader
virtual LineReader * lineReader() const
Definition: ScriptInterpreter.cc:367
ScriptInterpreter::setResultToInterpreter
virtual void setResultToInterpreter(const DataObject &value)=0
ScriptInterpreter::addCustomCommand
virtual void addCustomCommand(CustomCommand *command)
Definition: ScriptInterpreter.cc:78
ScriptInterpreter::finalized_
bool finalized_
True if Interpreter is finalized.
Definition: ScriptInterpreter.hh:133