OpenASIP  2.0
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. More...
 

Private Member Functions

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

Private Attributes

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

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.

48  : ScriptInterpreter() {
49 }

◆ ~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

◆ 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 
70  context_ = context;
71  setLineReader(reader);
72 }

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) {
126  DataObject* result = new DataObject();
127  string msg = "Unknown command: " + commands[0];
128  result->setString(msg);
129  setResult(result);
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) {
144  DataObject* result = new DataObject();
145  result->setString(n.errorMessage());
146  setResult(result);
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
159  DataObject* result = new DataObject();
160  result->setString("");
161  setResult(result);
162  setError(false);
163  return true;
164  }
165 
166  assert(false);
167  return false;
168 }

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 }

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 variables_.

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:
SimpleScriptInterpreter::variables_
VariableMap variables_
Holds all the variables given to interpreter.
Definition: SimpleScriptInterpreter.hh:83
NumberFormatException
Definition: Exception.hh:421
ScriptInterpreter::result
virtual std::string result()
Definition: ScriptInterpreter.cc:191
SimpleScriptInterpreter::context_
InterpreterContext * context_
Context for interpreter.
Definition: SimpleScriptInterpreter.hh:85
DataObject
Definition: DataObject.hh:50
DataObject::stringValue
virtual std::string stringValue() const
Definition: DataObject.cc:344
SimpleScriptInterpreter::context
virtual InterpreterContext & context() const
Definition: SimpleScriptInterpreter.cc:185
CustomCommand::execute
virtual bool execute(const std::vector< DataObject > &arguments)=0
assert
#define assert(condition)
Definition: Application.hh:86
ScriptInterpreter::setLineReader
virtual void setLineReader(LineReader *reader)
Definition: ScriptInterpreter.cc:377
CustomCommand
Definition: CustomCommand.hh:54
ScriptInterpreter::ScriptInterpreter
ScriptInterpreter()
Definition: ScriptInterpreter.cc:50
ScriptInterpreter::customCommand
virtual CustomCommand * customCommand(const std::string &commandName)
Definition: ScriptInterpreter.cc:110
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
ScriptInterpreter::setResult
virtual void setResult(DataObject *result)
Definition: ScriptInterpreter.cc:128
StringTools::trim
static std::string trim(const std::string &source)
Definition: StringTools.cc:55
StringTools::chopString
static std::vector< TCEString > chopString(const std::string &source, const std::string &delimiters)
Definition: StringTools.cc:181
ScriptInterpreter::setError
virtual void setError(bool state)
Definition: ScriptInterpreter.cc:205
DataObject::setString
virtual void setString(std::string value)
Definition: DataObject.cc:130