OpenASIP 2.2
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
ScriptInterpreter Class Referenceabstract

#include <ScriptInterpreter.hh>

Inheritance diagram for ScriptInterpreter:
Inheritance graph
Collaboration diagram for ScriptInterpreter:
Collaboration graph

Public Member Functions

 ScriptInterpreter ()
 
virtual ~ScriptInterpreter ()
 
virtual void addCustomCommand (CustomCommand *command)
 
virtual void removeCustomCommand (const std::string &commandName)
 
virtual CustomCommandcustomCommand (const std::string &commandName)
 
virtual void setResult (DataObject *result)
 
virtual void setResult (const std::string &result)
 
virtual void setResult (int result)
 
virtual void setResult (double result)
 
virtual std::string result ()
 
virtual void setError (bool state)
 
virtual void setError (std::string errorMessage)
 
virtual bool error () const
 
virtual void setVariable (const std::string &interpreterVariableName, const std::string &value)
 
virtual void setVariable (const std::string &interpreterVariableName, int value)
 
virtual std::string variableStringValue (const std::string &interpreterVariableName)
 
virtual int variableIntegerValue (const std::string &interpreterVariableName)
 
virtual bool processScriptFile (const std::string &scriptFileName)
 
virtual void finalize ()
 
virtual void setLineReader (LineReader *reader)
 
virtual LineReaderlineReader () const
 
std::vector< std::string > customCommandsSortedByName ()
 
virtual void initialize (int argc, char *argv[], InterpreterContext *context, LineReader *reader)=0
 
virtual void setVariableToInterpreter (const std::string &name, const DataObject &value)=0
 
virtual DataObject variable (const std::string &name)=0
 
virtual bool interpret (const std::string &commandLine)=0
 
virtual void setResultToInterpreter (const DataObject &value)=0
 
virtual InterpreterContextcontext () const =0
 

Protected Member Functions

virtual void addCustomCommandToInterpreter (const CustomCommand &command)=0
 
virtual void removeCustomCommandFromInterpreter (const CustomCommand &command)=0
 

Private Types

typedef std::map< std::string, CustomCommand * >::iterator MapIter
 Iterator for map.
 
typedef std::map< std::string, CustomCommand * >::value_type ValType
 val_type for map.
 

Private Member Functions

 ScriptInterpreter (const ScriptInterpreter &)
 Copying not allowed.
 
ScriptInterpreteroperator= (const ScriptInterpreter &)
 Assignment not allowed.
 

Private Attributes

std::map< std::string, CustomCommand * > commands_
 Map containing all custom commands for interpreter.
 
DataObjectresult_
 The latest interpreter result.
 
bool errorState_
 Indicates whether we are in error state.
 
bool finalized_
 True if Interpreter is finalized.
 
LineReaderreader_
 LineReader for interpreter.
 

Detailed Description

Abstract base class for all interpreters.

Interpreter processes its input, takes actions according to it and returns to wait more input.

Definition at line 55 of file ScriptInterpreter.hh.

Member Typedef Documentation

◆ MapIter

typedef std::map<std::string,CustomCommand*>::iterator ScriptInterpreter::MapIter
private

Iterator for map.

Definition at line 117 of file ScriptInterpreter.hh.

◆ ValType

typedef std::map<std::string,CustomCommand*>::value_type ScriptInterpreter::ValType
private

val_type for map.

Definition at line 119 of file ScriptInterpreter.hh.

Constructor & Destructor Documentation

◆ ScriptInterpreter() [1/2]

ScriptInterpreter::ScriptInterpreter ( )

Constructor.

Definition at line 50 of file ScriptInterpreter.cc.

50 :
51 result_(NULL), errorState_(false), finalized_(false) {
52}
bool errorState_
Indicates whether we are in error state.
DataObject * result_
The latest interpreter result.
bool finalized_
True if Interpreter is finalized.

◆ ~ScriptInterpreter()

ScriptInterpreter::~ScriptInterpreter ( )
virtual

Destructor.

Definition at line 57 of file ScriptInterpreter.cc.

57 {
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}
static void writeToErrorLog(const std::string fileName, const int lineNumber, const std::string functionName, const std::string message, const int neededVerbosity=0)
static void deleteAllValues(MapType &aMap)
std::map< std::string, CustomCommand * > commands_
Map containing all custom commands for interpreter.

References commands_, MapTools::deleteAllValues(), finalized_, result_, and Application::writeToErrorLog().

Here is the call graph for this function:

◆ ScriptInterpreter() [2/2]

ScriptInterpreter::ScriptInterpreter ( const ScriptInterpreter )
private

Copying not allowed.

Member Function Documentation

◆ addCustomCommand()

void ScriptInterpreter::addCustomCommand ( CustomCommand command)
virtual

Adds a custom command to interpreter.

Parameters
commandThe command to be added.

Definition at line 78 of file ScriptInterpreter.cc.

78 {
79 command->setInterpreter(this);
80 commands_.insert(ValType(command->name(), command));
82}
std::string name() const
void setInterpreter(ScriptInterpreter *si)
virtual void addCustomCommandToInterpreter(const CustomCommand &command)=0
std::map< std::string, CustomCommand * >::value_type ValType
val_type for map.

References addCustomCommandToInterpreter(), commands_, CustomCommand::name(), and CustomCommand::setInterpreter().

Referenced by CompiledSimInterpreter::CompiledSimInterpreter(), main(), and SimulatorInterpreter::SimulatorInterpreter().

Here is the call graph for this function:

◆ addCustomCommandToInterpreter()

virtual void ScriptInterpreter::addCustomCommandToInterpreter ( const CustomCommand command)
protectedpure virtual

Implemented in SimpleScriptInterpreter, and TclInterpreter.

Referenced by addCustomCommand().

◆ context()

virtual InterpreterContext & ScriptInterpreter::context ( ) const
pure virtual

◆ customCommand()

CustomCommand * ScriptInterpreter::customCommand ( const std::string &  commandName)
virtual

Returns a custom command with a given name.

Returns NULL if CustomCommand is not found.

Parameters
commandNameThe name of the wanted command.
Returns
The pointer to a wanted custom command.

Definition at line 110 of file ScriptInterpreter.cc.

110 {
111
112 if (!MapTools::containsKey(commands_, commandName)) {
113 return NULL;
114 }
115
116 MapIter mi = commands_.find(commandName);
117 return (*mi).second;
118}
static bool containsKey(const MapType &aMap, const KeyType &aKey)
std::map< std::string, CustomCommand * >::iterator MapIter
Iterator for map.

References commands_, and MapTools::containsKey().

Referenced by CmdHelp::execute(), HelpCommand::execute(), and SimpleScriptInterpreter::interpret().

Here is the call graph for this function:

◆ customCommandsSortedByName()

vector< string > ScriptInterpreter::customCommandsSortedByName ( )

Returns a vector of alphabetically sorted names of CustomCommands.

Returns
CustomCommands sorted by name.

Definition at line 342 of file ScriptInterpreter.cc.

342 {
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}

References commands_.

Referenced by CmdHelp::execute(), and HelpCommand::execute().

◆ error()

bool ScriptInterpreter::error ( ) const
virtual

Returns true, if interpreter is in error state.

Returns
True, if interpreter is in error state.

Definition at line 229 of file ScriptInterpreter.cc.

229 {
230 return errorState_;
231}

References errorState_.

Referenced by ProximSimulationThread::Entry().

◆ finalize()

void ScriptInterpreter::finalize ( )
virtual

Removes all created CustomCommands from interpreter.

This is called before interpreter is deleted.

Definition at line 329 of file ScriptInterpreter.cc.

329 {
330 for (MapIter mi = commands_.begin(); mi != commands_.end(); mi++) {
332 }
333 finalized_ = true;
334}
virtual void removeCustomCommandFromInterpreter(const CustomCommand &command)=0

References commands_, finalized_, and removeCustomCommandFromInterpreter().

Referenced by main(), and SimulatorInterpreter::~SimulatorInterpreter().

Here is the call graph for this function:

◆ initialize()

virtual void ScriptInterpreter::initialize ( int  argc,
char *  argv[],
InterpreterContext context,
LineReader reader 
)
pure virtual

◆ interpret()

virtual bool ScriptInterpreter::interpret ( const std::string &  commandLine)
pure virtual

◆ lineReader()

LineReader * ScriptInterpreter::lineReader ( ) const
virtual

Returns the LineReader instance.

LineReader is needed by some CustomCommands. ScriptInterpreter may also need it.

Returns
LineReader instance.

Definition at line 367 of file ScriptInterpreter.cc.

367 {
368 return reader_;
369}
LineReader * reader_
LineReader for interpreter.

References reader_.

Referenced by SetHistorySave::execute(), SetHistoryFilename::execute(), SetHistorySize::execute(), TclInterpreter::interpret(), SimControlLanguageCommand::outputStream(), and SimulatorInterpreter::SimulatorInterpreter().

◆ operator=()

ScriptInterpreter & ScriptInterpreter::operator= ( const ScriptInterpreter )
private

Assignment not allowed.

◆ processScriptFile()

bool ScriptInterpreter::processScriptFile ( const std::string &  scriptFileName)
virtual

Opens a script file and processes it.

Parameters
scriptFileNameThe name of the script file.
Returns
True, if process is successful, false otherwise.
Exceptions
UnreachableStreamIf file cannot be opened.

Reimplemented in TclInterpreter.

Definition at line 303 of file ScriptInterpreter.cc.

303 {
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}
virtual bool interpret(const std::string &commandLine)=0

References interpret().

Here is the call graph for this function:

◆ removeCustomCommand()

void ScriptInterpreter::removeCustomCommand ( const std::string &  commandName)
virtual

Removes a custom command from interpreter.

Reserved memory for CustomCommand is freed.

Parameters
commandNameThe name of the command that is removed.

Definition at line 92 of file ScriptInterpreter.cc.

92 {
93 if (MapTools::containsKey(commands_, commandName)) {
94 MapIter mi = commands_.find(commandName);
96 delete (*mi).second;
97 commands_.erase(mi);
98 }
99}

References commands_, MapTools::containsKey(), and removeCustomCommandFromInterpreter().

Referenced by CompiledSimInterpreter::CompiledSimInterpreter().

Here is the call graph for this function:

◆ removeCustomCommandFromInterpreter()

virtual void ScriptInterpreter::removeCustomCommandFromInterpreter ( const CustomCommand command)
protectedpure virtual

Implemented in SimpleScriptInterpreter, and TclInterpreter.

Referenced by finalize(), and removeCustomCommand().

◆ result()

string ScriptInterpreter::result ( )
virtual

Returns the result of last executed command as a string.

Returns
Last result as string.
Exceptions
NumberFormatExceptionIf converting result to string fails.

Definition at line 191 of file ScriptInterpreter.cc.

191 {
192 if (result_ != NULL) {
193 return result_->stringValue();
194 } else {
195 return "";
196 }
197}
virtual std::string stringValue() const

References result_, and DataObject::stringValue().

Referenced by ProximSimulationThread::Entry(), Script::execute(), executeCommand(), SimpleScriptInterpreter::interpret(), SimulatorCLI::interpreteAndPrintResults(), Proxim::OnInit(), SimulatorCLI::run(), setError(), setResult(), setResult(), setResult(), setResult(), and DesignSpaceExplorer::simulate().

Here is the call graph for this function:

◆ setError() [1/2]

void ScriptInterpreter::setError ( bool  state)
virtual

◆ setError() [2/2]

void ScriptInterpreter::setError ( std::string  errorMessage)
virtual

Sets an error message and sets errors status of interpreter.

Parameters
errorMessageThe error message.

Definition at line 215 of file ScriptInterpreter.cc.

215 {
217 result->setString(errorMessage);
219 setError(true);
220}
virtual std::string result()
virtual void setError(bool state)
virtual void setResult(DataObject *result)

References result(), setError(), and setResult().

Here is the call graph for this function:

◆ setLineReader()

void ScriptInterpreter::setLineReader ( LineReader reader)
virtual

Sets the LineReader for the interpreter.

Parameters
readerThe LineReader.

Definition at line 377 of file ScriptInterpreter.cc.

377 {
378 reader_ = reader;
379}

References reader_.

Referenced by SimpleScriptInterpreter::initialize(), and TclInterpreter::initialize().

◆ setResult() [1/4]

void ScriptInterpreter::setResult ( const std::string &  result)
virtual

Stores the result of last executed command.

Sets result also to concrete interpreter.

Parameters
resultThe result.

Definition at line 144 of file ScriptInterpreter.cc.

144 {
145 if (result_ == NULL) {
146 result_ = new DataObject();
147 }
150}
virtual void setString(std::string value)
virtual void setResultToInterpreter(const DataObject &value)=0

References result(), result_, setResultToInterpreter(), and DataObject::setString().

Here is the call graph for this function:

◆ setResult() [2/4]

void ScriptInterpreter::setResult ( DataObject result)
virtual

Stores the result of last executed command.

Sets result also to concrete interpreter.

Parameters
resultThe result.

Definition at line 128 of file ScriptInterpreter.cc.

128 {
129 if (result_ != NULL) {
130 delete result_;
131 }
132 result_ = result;
134}

References result(), result_, and setResultToInterpreter().

Referenced by SimControlLanguageCommand::askConditionFromUser(), SimControlLanguageCommand::askExpressionFromUser(), CustomCommand::checkArgumentCount(), CustomCommand::checkDoubleArgument(), CustomCommand::checkIntegerArgument(), CustomCommand::checkPositiveIntegerArgument(), SimControlLanguageCommand::checkSimulationEnded(), SimControlLanguageCommand::checkSimulationInitialized(), SimControlLanguageCommand::checkSimulationNotAlreadyRunning(), SimControlLanguageCommand::checkSimulationStopped(), CustomCommand::checkUnsignedIntegerArgument(), ConditionScript::conditionOk(), CmdHelp::execute(), ConfCommand::execute(), HelpCommand::execute(), InfoRegistersCommand::execute(), InfoImmediatesCommand::execute(), InfoRegFilesCommand::execute(), InfoIunitsCommand::execute(), InfoBussesCommand::execute(), InfoPortsCommand::execute(), InfoSegmentsCommand::execute(), InfoFunitsCommand::execute(), InfoProcCommand::execute(), InfoStatsCommand::execute(), InfoProgramCommand::execute(), MachCommand::execute(), MemDumpCommand::execute(), MemWriteCommand::execute(), ProgCommand::execute(), SettingCommand::execute(), SymbolAddressCommand::execute(), CmdTrigger::execute(), CmdReset::execute(), CmdQuit::execute(), CmdOutput::execute(), CmdRegister::execute(), CmdMem::execute(), CmdAdvanceClock::execute(), SimpleScriptInterpreter::interpret(), TclInterpreter::interpret(), ExpressionScript::resultChanged(), setError(), and SimControlLanguageCommand::setErrorMessage().

Here is the call graph for this function:

◆ setResult() [3/4]

void ScriptInterpreter::setResult ( double  result)
virtual

Stores the result of last executed command.

Sets result also to concrete interpreter.

Parameters
resultThe result.

Definition at line 176 of file ScriptInterpreter.cc.

176 {
177 if (result_ == NULL) {
178 result_ = new DataObject();
179 }
182}
virtual void setDouble(double value)

References result(), result_, DataObject::setDouble(), and setResultToInterpreter().

Here is the call graph for this function:

◆ setResult() [4/4]

void ScriptInterpreter::setResult ( int  result)
virtual

Stores the result of last executed command.

Sets result also to concrete interpreter.

Parameters
resultThe result.

Definition at line 160 of file ScriptInterpreter.cc.

160 {
161 if (result_ == NULL) {
162 result_ = new DataObject();
163 }
166}
virtual void setInteger(int value)

References result(), result_, DataObject::setInteger(), and setResultToInterpreter().

Here is the call graph for this function:

◆ setResultToInterpreter()

virtual void ScriptInterpreter::setResultToInterpreter ( const DataObject value)
pure virtual

◆ setVariable() [1/2]

void ScriptInterpreter::setVariable ( const std::string &  interpreterVariableName,
const std::string &  value 
)
virtual

Sets value for a variable.

Parameters
interpreterVariableNameThe name of the variable.
valueThe value for a variable.

Definition at line 240 of file ScriptInterpreter.cc.

242 {
243
244 DataObject object;
245 object.setString(value);
246 setVariableToInterpreter(interpreterVariableName, object);
247}
virtual void setVariableToInterpreter(const std::string &name, const DataObject &value)=0

References DataObject::setString(), and setVariableToInterpreter().

Referenced by TclInterpreter::initialize().

Here is the call graph for this function:

◆ setVariable() [2/2]

void ScriptInterpreter::setVariable ( const std::string &  interpreterVariableName,
int  value 
)
virtual

Sets the value for interpreter variable.

Parameters
interpreterVariableNameThe name of the variable.
valueThe value for the variable.

Definition at line 256 of file ScriptInterpreter.cc.

258 {
259
260 DataObject object;
261 object.setInteger(value);
262 setVariableToInterpreter(interpreterVariableName, object);
263}

References DataObject::setInteger(), and setVariableToInterpreter().

Here is the call graph for this function:

◆ setVariableToInterpreter()

virtual void ScriptInterpreter::setVariableToInterpreter ( const std::string &  name,
const DataObject value 
)
pure virtual

Implemented in SimpleScriptInterpreter, and TclInterpreter.

Referenced by setVariable(), and setVariable().

◆ variable()

virtual DataObject ScriptInterpreter::variable ( const std::string &  name)
pure virtual

◆ variableIntegerValue()

int ScriptInterpreter::variableIntegerValue ( const std::string &  interpreterVariableName)
virtual

Returns the value of the integer variable.

Parameters
interpreterVariableNameThe name of the variable.
Returns
The value of the variable.
Exceptions
NumberFormatExceptionIf converting variable to integer fails.

Definition at line 288 of file ScriptInterpreter.cc.

289 {
290 DataObject var = variable(interpreterVariableName);
291 int value = var.integerValue();
292 return value;
293}
virtual int integerValue() const
virtual DataObject variable(const std::string &name)=0

References DataObject::integerValue(), and variable().

Here is the call graph for this function:

◆ variableStringValue()

string ScriptInterpreter::variableStringValue ( const std::string &  interpreterVariableName)
virtual

Returns the value of the string variable.

Parameters
interpreterVariableNameThe name of the variable.
Returns
The value of the variable.
Exceptions
NumberFormatExceptionIf converting variable to string fails.

Definition at line 273 of file ScriptInterpreter.cc.

274 {
275 DataObject var = variable(interpreterVariableName);
276 string value = var.stringValue();
277 return value;
278}

References DataObject::stringValue(), and variable().

Here is the call graph for this function:

Member Data Documentation

◆ commands_

std::map<std::string, CustomCommand*> ScriptInterpreter::commands_
private

Map containing all custom commands for interpreter.

Definition at line 127 of file ScriptInterpreter.hh.

Referenced by addCustomCommand(), customCommand(), customCommandsSortedByName(), finalize(), removeCustomCommand(), and ~ScriptInterpreter().

◆ errorState_

bool ScriptInterpreter::errorState_
private

Indicates whether we are in error state.

Definition at line 131 of file ScriptInterpreter.hh.

Referenced by error(), and setError().

◆ finalized_

bool ScriptInterpreter::finalized_
private

True if Interpreter is finalized.

Definition at line 133 of file ScriptInterpreter.hh.

Referenced by finalize(), and ~ScriptInterpreter().

◆ reader_

LineReader* ScriptInterpreter::reader_
private

LineReader for interpreter.

Definition at line 135 of file ScriptInterpreter.hh.

Referenced by lineReader(), and setLineReader().

◆ result_

DataObject* ScriptInterpreter::result_
private

The latest interpreter result.

Definition at line 129 of file ScriptInterpreter.hh.

Referenced by result(), setResult(), setResult(), setResult(), setResult(), and ~ScriptInterpreter().


The documentation for this class was generated from the following files: