OpenASIP  2.0
SimpleScriptInterpreter.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file SimpleScriptInterpreter.cc
26  *
27  * Definition of SimpleScriptInterpreter class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
31  * @note rating: red
32  */
33 
34 #include <vector>
35 
37 #include "StringTools.hh"
38 #include "InterpreterContext.hh"
39 #include "LineReader.hh"
40 #include "Application.hh"
41 
42 using std::string;
43 using std::vector;
44 
45 /**
46  * Constructor.
47  */
49 }
50 
51 /**
52  * Destructor.
53  */
55 }
56 
57 /**
58  * Initializes the interpreter.
59  *
60  * @param context Context for interpreter.
61  * @param reader LineReader for interpreter.
62  */
63 void
65  int,
66  char*[],
67  InterpreterContext* context,
68  LineReader* reader) {
69 
70  context_ = context;
71  setLineReader(reader);
72 }
73 
74 /**
75  * Sets variable to interpreter.
76  *
77  * @param name The name of the variable.
78  * @param value The value of the variable.
79  * @exception NumberFormatException Cannot throw.
80  */
81 void
83  const std::string& name, const DataObject& value) {
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 }
92 
93 /**
94  * Returns the variable with the given name.
95  *
96  * If variable is not found, returns uninitialized data object.
97  *
98  * @param name The name of the variable.
99  * @return The data object that holds the value of the varible.
100  */
102 SimpleScriptInterpreter::variable(const std::string& name) {
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 }
110 
111 /**
112  * Interprets a given command line.
113  *
114  * @param commandLine The command line to interpreted.
115  * @return True if interpreting was successful, false otherwise.
116  */
117 bool
118 SimpleScriptInterpreter::interpret(const std::string& commandLine) {
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 }
169 
170 /**
171  * This function does nothing, because result is already stored
172  * in ScriptInterpreter.
173  *
174  * @exception Cannot throw.
175  */
176 void
178 
179 /**
180  * Returns an instance of InterpreteContext.
181  *
182  * @return InterpreterContext.
183  */
186  return *context_;
187 }
188 
189 /**
190  * Does nothing.
191  */
192 void
194 }
195 
196 /**
197  * Does nothing.
198  */
199 void
201  const CustomCommand&) {
202 }
SimpleScriptInterpreter::variable
virtual DataObject variable(const std::string &name)
Definition: SimpleScriptInterpreter.cc:102
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::~SimpleScriptInterpreter
virtual ~SimpleScriptInterpreter()
Definition: SimpleScriptInterpreter.cc:54
SimpleScriptInterpreter::context
virtual InterpreterContext & context() const
Definition: SimpleScriptInterpreter.cc:185
SimpleScriptInterpreter::initialize
virtual void initialize(int argc, char *argv[], InterpreterContext *context, LineReader *reader)
Definition: SimpleScriptInterpreter.cc:64
SimpleScriptInterpreter::addCustomCommandToInterpreter
virtual void addCustomCommandToInterpreter(const CustomCommand &command)
Definition: SimpleScriptInterpreter.cc:193
InterpreterContext.hh
CustomCommand::execute
virtual bool execute(const std::vector< DataObject > &arguments)=0
StringTools.hh
assert
#define assert(condition)
Definition: Application.hh:86
ScriptInterpreter::setLineReader
virtual void setLineReader(LineReader *reader)
Definition: ScriptInterpreter.cc:377
LineReader.hh
Application.hh
CustomCommand
Definition: CustomCommand.hh:54
InterpreterContext
Definition: InterpreterContext.hh:40
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
SimpleScriptInterpreter::removeCustomCommandFromInterpreter
virtual void removeCustomCommandFromInterpreter(const CustomCommand &command)
Definition: SimpleScriptInterpreter.cc:200
SimpleScriptInterpreter::setVariableToInterpreter
virtual void setVariableToInterpreter(const std::string &name, const DataObject &value)
Definition: SimpleScriptInterpreter.cc:82
SimpleScriptInterpreter.hh
LineReader
Definition: LineReader.hh:52
StringTools::chopString
static std::vector< TCEString > chopString(const std::string &source, const std::string &delimiters)
Definition: StringTools.cc:181
SimpleScriptInterpreter::SimpleScriptInterpreter
SimpleScriptInterpreter()
Definition: SimpleScriptInterpreter.cc:48
SimpleScriptInterpreter::interpret
virtual bool interpret(const std::string &commandLine)
Definition: SimpleScriptInterpreter.cc:118
ScriptInterpreter::setError
virtual void setError(bool state)
Definition: ScriptInterpreter.cc:205
SimpleScriptInterpreter::setResultToInterpreter
virtual void setResultToInterpreter(const DataObject &value)
Definition: SimpleScriptInterpreter.cc:177
DataObject::setString
virtual void setString(std::string value)
Definition: DataObject.cc:130
ScriptInterpreter
Definition: ScriptInterpreter.hh:55