OpenASIP 2.2
Loading...
Searching...
No Matches
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.
 
TclInterpreteroperator= (const TclInterpreter &)
 Assignment not allowed.
 

Private Attributes

InterpreterContextcontext_
 Context for interpreter.
 
Tcl_Interp * interpreter_
 Interpreter instance.
 

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 :
51}
Tcl_Interp * interpreter_
Interpreter instance.
InterpreterContext * context_
Context for interpreter.

◆ ~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}
std::string name() const
static char * stringToCharPtr(const std::string &source)
static int customCommandRedirector(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])

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}
#define assert(condition)
virtual bool execute(const std::vector< DataObject > &arguments)=0
static DataObject tclObjToDataObject(Tcl_Obj *object)

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);
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}
static std::string toString(const T &source)
virtual void setVariable(const std::string &interpreterVariableName, const std::string &value)
virtual void setLineReader(LineReader *reader)
virtual InterpreterContext & context() const

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}
virtual std::string readLine(std::string prompt="")=0
virtual LineReader * lineReader() const
virtual void setError(bool state)
virtual void setResult(DataObject *result)
static std::string trim(const std::string &source)

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

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}
static Tcl_Obj * dataObjectToTclObj(const DataObject &object)

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}
virtual std::string stringValue() const

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}
virtual void setString(std::string value)

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: