OpenASIP  2.0
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
TclInterpreter Class Reference

#include <TclInterpreter.hh>

Inheritance diagram for TclInterpreter:
Inheritance graph
Collaboration diagram for TclInterpreter:
Collaboration graph

Public Member Functions

 TclInterpreter ()
 
virtual ~TclInterpreter ()
 
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 bool processScriptFile (const std::string &scriptFileName)
 
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 void finalize ()
 
virtual void setLineReader (LineReader *reader)
 
virtual LineReaderlineReader () const
 
std::vector< std::string > customCommandsSortedByName ()
 

Static Public Member Functions

static DataObject tclObjToDataObject (Tcl_Obj *object)
 
static Tcl_Obj * dataObjectToTclObj (const DataObject &object)
 
static int customCommandRedirector (ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
 

Protected Member Functions

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

Private Member Functions

 TclInterpreter (const TclInterpreter &)
 Copying not allowed. More...
 
TclInterpreteroperator= (const TclInterpreter &)
 Assignment not allowed. More...
 

Private Attributes

InterpreterContextcontext_
 Context for interpreter. More...
 
Tcl_Interp * interpreter_
 Interpreter instance. More...
 

Detailed Description

Tcl implementation of ScriptInterpreter.

Tcl (Tool command language) is a very simple programming language. In TCE it is used for command language for Simulator. (Maybe for some other application too.)

Is able to interpret all tcl commands and user defined CustomCommands.

Definition at line 52 of file TclInterpreter.hh.

Constructor & Destructor Documentation

◆ TclInterpreter() [1/2]

TclInterpreter::TclInterpreter ( )

Constructor.

Definition at line 49 of file TclInterpreter.cc.

49  :
50  ScriptInterpreter(), context_(NULL), interpreter_(NULL) {
51 }

◆ ~TclInterpreter()

TclInterpreter::~TclInterpreter ( )
virtual

Destructor.

Definition at line 56 of file TclInterpreter.cc.

56  {
57  Tcl_DeleteInterp(interpreter_);
58 }

References interpreter_.

◆ TclInterpreter() [2/2]

TclInterpreter::TclInterpreter ( const TclInterpreter )
private

Copying not allowed.

Member Function Documentation

◆ addCustomCommandToInterpreter()

void TclInterpreter::addCustomCommandToInterpreter ( const CustomCommand command)
protectedvirtual

Adds CustomCommand to interpreter.

Parameters
commandThe command to be added.

Implements ScriptInterpreter.

Definition at line 192 of file TclInterpreter.cc.

192  {
193  char* cName = StringTools::stringToCharPtr(command.name());
194  Tcl_CreateObjCommand(
195  interpreter_, cName, customCommandRedirector, (ClientData)(&command),
196  NULL);
197  delete[] cName;
198 }

References customCommandRedirector(), interpreter_, CustomCommand::name(), and StringTools::stringToCharPtr().

Here is the call graph for this function:

◆ context()

InterpreterContext & TclInterpreter::context ( ) const
virtual

Returns the context of the interpreter.

Returns
The context of the interpreter.

Implements ScriptInterpreter.

Definition at line 298 of file TclInterpreter.cc.

298  {
299  return *context_;
300 }

References context_.

Referenced by initialize(), and SimulatorInterpreter::SimulatorInterpreter().

◆ customCommandRedirector()

int TclInterpreter::customCommandRedirector ( ClientData  cd,
Tcl_Interp *  interp,
int  objc,
Tcl_Obj *CONST  objv[] 
)
static

This handler function is used to handle all CustomCommands.

ClientData field contains CustomCommand to be executed. Tcl_Objs are converted to DataObjects which are then passed to CustomCommand.

Parameters
cdClientData field.
interpNot used.
objcThe number of Tcl_Objs.
objvThe Tcl_Objs.

Definition at line 270 of file TclInterpreter.cc.

274  {
275 
276  vector<DataObject> args;
277 
278  for (int i = 0; i < objc; i++) {
280  args.push_back(dObj);
281  }
282 
283  CustomCommand* targetCommand = static_cast<CustomCommand*>(cd);
284  assert(cd != NULL);
285 
286  if (targetCommand->execute(args)) {
287  return TCL_OK;
288  }
289  return TCL_ERROR;
290 }

References assert, CustomCommand::execute(), and tclObjToDataObject().

Referenced by addCustomCommandToInterpreter().

Here is the call graph for this function:

◆ dataObjectToTclObj()

Tcl_Obj * TclInterpreter::dataObjectToTclObj ( const DataObject object)
static

Converts DataObject to Tcl_Obj.

Parameters
objectThe object to be converted.
Returns
Tcl_Obj converted from DataObject.
Exceptions
NumberFormatExceptionIf conversion fails.

Definition at line 235 of file TclInterpreter.cc.

235  {
236  char* cName = StringTools::stringToCharPtr(object.stringValue());
237  Tcl_Obj* obj = Tcl_NewStringObj(cName, -1);
238  delete[] cName;
239  return obj;
240 }

References StringTools::stringToCharPtr().

Referenced by setResultToInterpreter().

Here is the call graph for this function:

◆ initialize()

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

Initializes the interpreter.

Interpreter instance is created, as well as argc and argv variables are set in interpreter.

Parameters
argcThe number of command line arguments.
argvThe command line arguments.
contextThe context for interpreter.
readerLineReader for the interpreter.

Implements ScriptInterpreter.

Definition at line 72 of file TclInterpreter.cc.

76  {
77 
78  setLineReader(reader);
79  context_ = context;
80  interpreter_ = Tcl_CreateInterp();
81 
82  if (argc == 0 || argv == NULL) {
83  return;
84  }
85 
86  // argv is set to interpreter
87  char* args = Tcl_Merge(argc-1, (char **)argv+1);
88  char* argvStr = const_cast<char*>("argv");
89  Tcl_SetVar(interpreter_, argvStr, args, TCL_GLOBAL_ONLY);
90  ckfree(args);
91 
92  // argc is set to interpreter
93  string buffer = Conversion::toString(argc-1);
94  setVariable("argc", buffer);
95  setVariable("argv0", argv[0]);
96 }

References context(), context_, interpreter_, ScriptInterpreter::setLineReader(), ScriptInterpreter::setVariable(), and Conversion::toString().

Referenced by SimulatorInterpreter::SimulatorInterpreter().

Here is the call graph for this function:

◆ interpret()

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

Interprets one command line.

Parameters
commandLineThe line to be interpreted.
Returns
True if interpreting is successful, false otherwise.
Todo:
This does not work with nested loops correctly! It stops after it encouters the first line with only "}", thus it stops after the nested loop definition stops.

Implements ScriptInterpreter.

Definition at line 138 of file TclInterpreter.cc.

138  {
139 
140  LineReader* reader = lineReader();
141  StringTools::trim(commandLine);
142  string command = StringTools::trim(commandLine);
143  if (!command.empty() && command.substr(command.length() - 1, 1) == "{" &&
144  reader != NULL) {
145  // command line ended with '{' so let's wait for more input
146  /// @todo This does not work with nested loops correctly!
147  /// It stops after it encouters the first line with only "}",
148  /// thus it stops after the nested loop definition stops.
149  string line = "";
150  do {
151  line = StringTools::trim(reader->readLine(" "));
152  StringTools::trim(line);
153  command += "\n" + line;
154  } while (!line.empty() && line.substr(line.length() - 1, 1) != "}");
155  }
156 
157  char* cCommandLine = StringTools::stringToCharPtr(command);
158  int code = Tcl_Eval(interpreter_, cCommandLine);
159  delete[] cCommandLine;
160 
161  Tcl_Obj* object = Tcl_GetObjResult(interpreter_);
162  DataObject* dObject = new DataObject(tclObjToDataObject(object));
163  setResult(dObject);
164 
165  if (code == TCL_OK) {
166  setError(false);
167  return true;
168  } else {
169  setError(true);
170  return false;
171  }
172 }

References interpreter_, ScriptInterpreter::lineReader(), LineReader::readLine(), ScriptInterpreter::setError(), ScriptInterpreter::setResult(), StringTools::stringToCharPtr(), tclObjToDataObject(), and StringTools::trim().

Referenced by ProximSimulationThread::Entry(), SimulatorCLI::interpreteAndPrintResults(), Proxim::OnInit(), processScriptFile(), SimulatorCLI::run(), and DesignSpaceExplorer::simulate().

Here is the call graph for this function:

◆ operator=()

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

Assignment not allowed.

◆ processScriptFile()

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

Processes script file.

Parameters
scriptFileNameThe name of the script file.
Returns
True, if process is successful, false otherwise.
Exceptions
UnreachableStreamDon't throw it.

Reimplemented from ScriptInterpreter.

Definition at line 250 of file TclInterpreter.cc.

250  {
251  string command = "source " + scriptFileName;
252  if (!interpret(command)) {
253  return false;
254  }
255  return true;
256 }

References interpret().

Referenced by main(), and TTASimulatorCLI::TTASimulatorCLI().

Here is the call graph for this function:

◆ removeCustomCommandFromInterpreter()

void TclInterpreter::removeCustomCommandFromInterpreter ( const CustomCommand command)
protectedvirtual

Removes custom command from interpreter.

Parameters
commandThe command to be removed.

Implements ScriptInterpreter.

Definition at line 206 of file TclInterpreter.cc.

207  {
208 
209  char* cName = StringTools::stringToCharPtr(command.name());
210  Tcl_DeleteCommand(interpreter_, cName);
211  delete[] cName;
212 }

References interpreter_, CustomCommand::name(), and StringTools::stringToCharPtr().

Here is the call graph for this function:

◆ setResultToInterpreter()

void TclInterpreter::setResultToInterpreter ( const DataObject value)
virtual

Sets result to concrete interpreter.

Parameters
valueThe value to be set.
Exceptions
NumberFormatExceptionIf conversion to Tcl_Obj fails.

Implements ScriptInterpreter.

Definition at line 181 of file TclInterpreter.cc.

181  {
182  Tcl_Obj* object = dataObjectToTclObj(value);
183  Tcl_SetObjResult(interpreter_, object);
184 }

References dataObjectToTclObj(), and interpreter_.

Here is the call graph for this function:

◆ setVariableToInterpreter()

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

Sets a variable for interpreter.

Parameters
nameThe name of the variable.
valueThe value for a variable.
Exceptions
NumberFormatExceptionIf converting value to string fails.

Implements ScriptInterpreter.

Definition at line 106 of file TclInterpreter.cc.

107  {
108  char* cName = StringTools::stringToCharPtr(name);
109  char* cValue = StringTools::stringToCharPtr(value.stringValue());
110  Tcl_SetVar(interpreter_, cName, cValue, TCL_GLOBAL_ONLY);
111  delete[] cName;
112  delete[] cValue;
113 }

References interpreter_, StringTools::stringToCharPtr(), and DataObject::stringValue().

Here is the call graph for this function:

◆ tclObjToDataObject()

DataObject TclInterpreter::tclObjToDataObject ( Tcl_Obj *  object)
static

Converts Tcl_Obj to DataObject.

Parameters
objectThe object to be converted.
Returns
DataObject which was converted from Tcl_Obj.

Definition at line 221 of file TclInterpreter.cc.

221  {
222  DataObject dObj;
223  dObj.setString(Tcl_GetStringFromObj(object, NULL));
224  return dObj;
225 }

References DataObject::setString().

Referenced by customCommandRedirector(), and interpret().

Here is the call graph for this function:

◆ variable()

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

Returns the DataObject corresponding a certain variable.

Parameters
nameThe name of the variable.
Returns
The DataObject which holds the value of variable.

Implements ScriptInterpreter.

Definition at line 122 of file TclInterpreter.cc.

122  {
123  DataObject object;
124  char* cName = StringTools::stringToCharPtr(name);
125  string value = Tcl_GetVar(interpreter_, cName, TCL_GLOBAL_ONLY);
126  delete[] cName;
127  object.setString(value);
128  return object;
129 }

References interpreter_, and StringTools::stringToCharPtr().

Here is the call graph for this function:

Member Data Documentation

◆ context_

InterpreterContext* TclInterpreter::context_
private

Context for interpreter.

Definition at line 94 of file TclInterpreter.hh.

Referenced by context(), and initialize().

◆ interpreter_

Tcl_Interp* TclInterpreter::interpreter_
private

The documentation for this class was generated from the following files:
CustomCommand::name
std::string name() const
DataObject
Definition: DataObject.hh:50
DataObject::stringValue
virtual std::string stringValue() const
Definition: DataObject.cc:344
StringTools::stringToCharPtr
static char * stringToCharPtr(const std::string &source)
Definition: StringTools.cc:83
TclInterpreter::interpreter_
Tcl_Interp * interpreter_
Interpreter instance.
Definition: TclInterpreter.hh:96
Conversion::toString
static std::string toString(const T &source)
LineReader::readLine
virtual std::string readLine(std::string prompt="")=0
TclInterpreter::context_
InterpreterContext * context_
Context for interpreter.
Definition: TclInterpreter.hh:94
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
TclInterpreter::interpret
virtual bool interpret(const std::string &commandLine)
Definition: TclInterpreter.cc:138
ScriptInterpreter::ScriptInterpreter
ScriptInterpreter()
Definition: ScriptInterpreter.cc:50
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
TclInterpreter::context
virtual InterpreterContext & context() const
Definition: TclInterpreter.cc:298
LineReader
Definition: LineReader.hh:52
TclInterpreter::dataObjectToTclObj
static Tcl_Obj * dataObjectToTclObj(const DataObject &object)
Definition: TclInterpreter.cc:235
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
TclInterpreter::customCommandRedirector
static int customCommandRedirector(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
Definition: TclInterpreter.cc:270
ScriptInterpreter::lineReader
virtual LineReader * lineReader() const
Definition: ScriptInterpreter.cc:367
TclInterpreter::tclObjToDataObject
static DataObject tclObjToDataObject(Tcl_Obj *object)
Definition: TclInterpreter.cc:221