OpenASIP 2.2
Loading...
Searching...
No Matches
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
42using std::string;
43using std::ifstream;
44using std::vector;
45using 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 */
77void
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 */
91void
92ScriptInterpreter::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 */
110ScriptInterpreter::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 */
127void
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 */
143void
144ScriptInterpreter::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 */
159void
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 */
175void
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 */
190string
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 */
204void
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 */
214void
215ScriptInterpreter::setError(std::string errorMessage) {
217 result->setString(errorMessage);
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 */
228bool
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 */
239void
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 */
255void
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 */
272string
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 */
287int
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 */
302bool
303ScriptInterpreter::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 */
328void
330 for (MapIter mi = commands_.begin(); mi != commands_.end(); mi++) {
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 */
341vector<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 */
368 return reader_;
369}
370
371/**
372 * Sets the LineReader for the interpreter.
373 *
374 * @param reader The LineReader.
375 */
376void
find Finds info of the inner loops in the false
static void writeToErrorLog(const std::string fileName, const int lineNumber, const std::string functionName, const std::string message, const int neededVerbosity=0)
std::string name() const
void setInterpreter(ScriptInterpreter *si)
virtual void setDouble(double value)
virtual std::string stringValue() const
virtual void setString(std::string value)
virtual int integerValue() const
virtual void setInteger(int value)
static void deleteAllValues(MapType &aMap)
static bool containsKey(const MapType &aMap, const KeyType &aKey)
virtual LineReader * lineReader() const
bool errorState_
Indicates whether we are in error state.
virtual bool interpret(const std::string &commandLine)=0
virtual void setVariable(const std::string &interpreterVariableName, const std::string &value)
virtual bool processScriptFile(const std::string &scriptFileName)
virtual void setResultToInterpreter(const DataObject &value)=0
virtual int variableIntegerValue(const std::string &interpreterVariableName)
DataObject * result_
The latest interpreter result.
virtual void addCustomCommandToInterpreter(const CustomCommand &command)=0
std::map< std::string, CustomCommand * >::value_type ValType
val_type for map.
virtual void removeCustomCommandFromInterpreter(const CustomCommand &command)=0
virtual void removeCustomCommand(const std::string &commandName)
virtual void finalize()
virtual std::string result()
virtual CustomCommand * customCommand(const std::string &commandName)
virtual void setLineReader(LineReader *reader)
std::map< std::string, CustomCommand * >::iterator MapIter
Iterator for map.
virtual void setError(bool state)
virtual void setVariableToInterpreter(const std::string &name, const DataObject &value)=0
LineReader * reader_
LineReader for interpreter.
virtual std::string variableStringValue(const std::string &interpreterVariableName)
virtual void addCustomCommand(CustomCommand *command)
std::vector< std::string > customCommandsSortedByName()
virtual void setResult(DataObject *result)
std::map< std::string, CustomCommand * > commands_
Map containing all custom commands for interpreter.
virtual bool error() const
virtual DataObject variable(const std::string &name)=0
bool finalized_
True if Interpreter is finalized.