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

#include <SimpleScriptInterpreter.hh>

Inheritance diagram for SimpleScriptInterpreter:
Inheritance graph
Collaboration diagram for SimpleScriptInterpreter:
Collaboration graph

Public Member Functions

 SimpleScriptInterpreter ()
 
virtual ~SimpleScriptInterpreter ()
 
virtual void initialize (int argc, char *argv[], InterpreterContext *context, LineReader *reader)
 
virtual void setVariableToInterpreter (const std::string &name, const DataObject &value)
 
virtual DataObject variable (const std::string &name)
 
virtual bool interpret (const std::string &commandLine)
 
virtual void setResultToInterpreter (const DataObject &value)
 
virtual InterpreterContextcontext () const
 
- Public Member Functions inherited from ScriptInterpreter
 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 ()
 

Protected Member Functions

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

Private Types

typedef std::map< std::string, std::string > VariableMap
 Map for variables.
 

Private Member Functions

 SimpleScriptInterpreter (const SimpleScriptInterpreter &)
 Copying not allowed.
 
SimpleScriptInterpreteroperator= (const ScriptInterpreter &)
 Assignment not allowed.
 

Private Attributes

VariableMap variables_
 Holds all the variables given to interpreter.
 
InterpreterContextcontext_
 Context for interpreter.
 

Detailed Description

Interpreter which uses only custom commands.

Custom commands are user defined commands.

Definition at line 49 of file SimpleScriptInterpreter.hh.

Member Typedef Documentation

◆ VariableMap

typedef std::map<std::string, std::string> SimpleScriptInterpreter::VariableMap
private

Map for variables.

Definition at line 75 of file SimpleScriptInterpreter.hh.

Constructor & Destructor Documentation

◆ SimpleScriptInterpreter() [1/2]

SimpleScriptInterpreter::SimpleScriptInterpreter ( )

Constructor.

Definition at line 48 of file SimpleScriptInterpreter.cc.

◆ ~SimpleScriptInterpreter()

SimpleScriptInterpreter::~SimpleScriptInterpreter ( )
virtual

Destructor.

Definition at line 54 of file SimpleScriptInterpreter.cc.

54 {
55}

◆ SimpleScriptInterpreter() [2/2]

SimpleScriptInterpreter::SimpleScriptInterpreter ( const SimpleScriptInterpreter )
private

Copying not allowed.

Member Function Documentation

◆ addCustomCommandToInterpreter()

void SimpleScriptInterpreter::addCustomCommandToInterpreter ( const CustomCommand command)
protectedvirtual

Does nothing.

Implements ScriptInterpreter.

Definition at line 193 of file SimpleScriptInterpreter.cc.

193 {
194}

◆ context()

InterpreterContext & SimpleScriptInterpreter::context ( ) const
virtual

Returns an instance of InterpreteContext.

Returns
InterpreterContext.

Implements ScriptInterpreter.

Definition at line 185 of file SimpleScriptInterpreter.cc.

185 {
186 return *context_;
187}
InterpreterContext * context_
Context for interpreter.

References context_.

Referenced by CmdTrigger::execute(), CmdReset::execute(), CmdQuit::execute(), CmdOutput::execute(), CmdRegister::execute(), CmdMem::execute(), CmdAdvanceClock::execute(), CmdOutput::helpText(), initialize(), main(), and OsalInterpreter::operation().

◆ initialize()

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

Initializes the interpreter.

Parameters
contextContext for interpreter.
readerLineReader for interpreter.

Implements ScriptInterpreter.

Definition at line 64 of file SimpleScriptInterpreter.cc.

68 {
69
71 setLineReader(reader);
72}
virtual void setLineReader(LineReader *reader)
virtual InterpreterContext & context() const

References context(), context_, and ScriptInterpreter::setLineReader().

Referenced by main().

Here is the call graph for this function:

◆ interpret()

bool SimpleScriptInterpreter::interpret ( const std::string &  commandLine)
virtual

Interprets a given command line.

Parameters
commandLineThe command line to interpreted.
Returns
True if interpreting was successful, false otherwise.

Implements ScriptInterpreter.

Definition at line 118 of file SimpleScriptInterpreter.cc.

118 {
119
120 string line = StringTools::trim(commandLine);
121 if (commandLine != "") {
122 vector<TCEString> commands = StringTools::chopString(commandLine, " ");
123
124 CustomCommand* custCommand = customCommand(commands[0]);
125 if (custCommand == NULL) {
127 string msg = "Unknown command: " + commands[0];
128 result->setString(msg);
130 return false;
131 }
132
133 vector<DataObject> args;
134 for (unsigned int i = 0; i < commands.size(); i++) {
135 DataObject obj;
136 obj.setString(commands[i]);
137 args.push_back(obj);
138 }
139
140 bool res;
141 try {
142 res = custCommand->execute(args);
143 } catch (const NumberFormatException& n) {
145 result->setString(n.errorMessage());
147 res = false;
148 }
149
150 if (res) {
151 setError(false);
152 return true;
153 } else {
154 setError(true);
155 return false;
156 }
157 } else {
158 // command line was empty
160 result->setString("");
162 setError(false);
163 return true;
164 }
165
166 assert(false);
167 return false;
168}
#define assert(condition)
virtual bool execute(const std::vector< DataObject > &arguments)=0
virtual void setString(std::string value)
std::string errorMessage() const
Definition Exception.cc:123
virtual std::string result()
virtual CustomCommand * customCommand(const std::string &commandName)
virtual void setError(bool state)
virtual void setResult(DataObject *result)
static std::string trim(const std::string &source)
static std::vector< TCEString > chopString(const std::string &source, const std::string &delimiters)

References assert, StringTools::chopString(), ScriptInterpreter::customCommand(), Exception::errorMessage(), CustomCommand::execute(), ScriptInterpreter::result(), ScriptInterpreter::setError(), ScriptInterpreter::setResult(), DataObject::setString(), and StringTools::trim().

Referenced by executeCommand(), and main().

Here is the call graph for this function:

◆ operator=()

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

Assignment not allowed.

◆ removeCustomCommandFromInterpreter()

void SimpleScriptInterpreter::removeCustomCommandFromInterpreter ( const CustomCommand command)
protectedvirtual

Does nothing.

Implements ScriptInterpreter.

Definition at line 200 of file SimpleScriptInterpreter.cc.

201 {
202}

◆ setResultToInterpreter()

void SimpleScriptInterpreter::setResultToInterpreter ( const DataObject value)
virtual

This function does nothing, because result is already stored in ScriptInterpreter.

Exceptions
Cannotthrow.

Implements ScriptInterpreter.

Definition at line 177 of file SimpleScriptInterpreter.cc.

177{}

◆ setVariableToInterpreter()

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

Sets variable to interpreter.

Parameters
nameThe name of the variable.
valueThe value of the variable.
Exceptions
NumberFormatExceptionCannot throw.

Implements ScriptInterpreter.

Definition at line 82 of file SimpleScriptInterpreter.cc.

83 {
84 VariableMap::iterator iter = variables_.find(name);
85
86 if (iter == variables_.end()) {
87 variables_[name] = value.stringValue();
88 } else {
89 (*iter).second = value.stringValue();
90 }
91}
virtual std::string stringValue() const
VariableMap variables_
Holds all the variables given to interpreter.

References DataObject::stringValue(), and variables_.

Here is the call graph for this function:

◆ variable()

DataObject SimpleScriptInterpreter::variable ( const std::string &  name)
virtual

Returns the variable with the given name.

If variable is not found, returns uninitialized data object.

Parameters
nameThe name of the variable.
Returns
The data object that holds the value of the varible.

Implements ScriptInterpreter.

Definition at line 102 of file SimpleScriptInterpreter.cc.

102 {
103 DataObject object;
104 VariableMap::iterator iter = variables_.find(name);
105 if (iter != variables_.end()) {
106 object.setString((*iter).second);
107 }
108 return object;
109}

References DataObject::setString(), and variables_.

Here is the call graph for this function:

Member Data Documentation

◆ context_

InterpreterContext* SimpleScriptInterpreter::context_
private

Context for interpreter.

Definition at line 85 of file SimpleScriptInterpreter.hh.

Referenced by context(), and initialize().

◆ variables_

VariableMap SimpleScriptInterpreter::variables_
private

Holds all the variables given to interpreter.

Definition at line 83 of file SimpleScriptInterpreter.hh.

Referenced by setVariableToInterpreter(), and variable().


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