OpenASIP 2.2
Loading...
Searching...
No Matches
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
42using std::string;
43using std::vector;
44
45/**
46 * Constructor.
47 */
50
51/**
52 * Destructor.
53 */
56
57/**
58 * Initializes the interpreter.
59 *
60 * @param context Context for interpreter.
61 * @param reader LineReader for interpreter.
62 */
63void
65 int,
66 char*[],
67 InterpreterContext* context,
68 LineReader* reader) {
69
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 */
81void
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 */
102SimpleScriptInterpreter::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 */
117bool
118SimpleScriptInterpreter::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) {
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}
169
170/**
171 * This function does nothing, because result is already stored
172 * in ScriptInterpreter.
173 *
174 * @exception Cannot throw.
175 */
176void
178
179/**
180 * Returns an instance of InterpreteContext.
181 *
182 * @return InterpreterContext.
183 */
186 return *context_;
187}
188
189/**
190 * Does nothing.
191 */
192void
195
196/**
197 * Does nothing.
198 */
199void
#define assert(condition)
virtual bool execute(const std::vector< DataObject > &arguments)=0
virtual std::string stringValue() const
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 setLineReader(LineReader *reader)
virtual void setError(bool state)
virtual void setResult(DataObject *result)
virtual void initialize(int argc, char *argv[], InterpreterContext *context, LineReader *reader)
virtual void addCustomCommandToInterpreter(const CustomCommand &command)
virtual bool interpret(const std::string &commandLine)
virtual void removeCustomCommandFromInterpreter(const CustomCommand &command)
InterpreterContext * context_
Context for interpreter.
virtual InterpreterContext & context() const
virtual DataObject variable(const std::string &name)
VariableMap variables_
Holds all the variables given to interpreter.
virtual void setVariableToInterpreter(const std::string &name, const DataObject &value)
virtual void setResultToInterpreter(const DataObject &value)
static std::string trim(const std::string &source)
static std::vector< TCEString > chopString(const std::string &source, const std::string &delimiters)