Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/CommandEvaluation.cc @ 1424

Last change on this file since 1424 was 1424, checked in by landauf, 16 years ago

finally got a good approach for the CommandExecutor parser. more to come.

File size: 8.6 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "CommandEvaluation.h"
30#include "ConsoleCommand.h"
31#include "Debug.h"
32
33namespace orxonox
34{
35    CommandEvaluation::CommandEvaluation()
36    {
37        this->initialize("");
38        this->state_ = CS_Uninitialized;
39    }
40
41    void CommandEvaluation::initialize(const std::string& command)
42    {
43        this->bNewCommand_ = true;
44        this->bCommandChanged_ = false;
45        this->originalCommand_ = command;
46        this->command_ = command;
47        this->commandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0');
48
49        this->additionalParameter_ = "";
50
51        this->bEvaluatedParams_ = false;
52
53        this->listOfPossibleIdentifiers_.clear();
54        this->listOfPossibleFunctions_.clear();
55        this->listOfPossibleArguments_.clear();
56
57        this->functionclass_ = 0;
58        this->function_ = 0;
59
60        this->errorMessage_ = "";
61        this->state_ = CS_Empty;
62    }
63
64    bool CommandEvaluation::isValid() const
65    {
66        return (this->function_);
67    }
68
69    bool CommandEvaluation::execute() const
70    {
71        if (!this->isValid())
72            return false;
73
74        if (this->bEvaluatedParams_ && this->function_)
75        {
76            COUT(4) << "CE_execute (evaluation): " << this->function_->getName() << " " << this->param_[0] << " " << this->param_[1] << " " << this->param_[2] << " " << this->param_[3] << " " << this->param_[4] << std::endl;
77            (*this->function_)(this->param_[0], this->param_[1], this->param_[2], this->param_[3], this->param_[4]);
78            return true;
79        }
80
81        if (!this->bCommandChanged_)
82        {
83            COUT(4) << "CE_execute: " << this->command_ << "\n";
84
85            unsigned int startindex = this->getStartindex();
86            if (this->commandTokens_.size() > startindex)
87                return this->function_->parse(removeSlashes(this->commandTokens_.subSet(startindex).join() + this->getAdditionalParameter()));
88            else
89                return this->function_->parse(removeSlashes(this->additionalParameter_));
90        }
91
92        return false;
93    }
94
95    std::string CommandEvaluation::complete()
96    {
97        if (!this->bNewCommand_)
98        {
99std::cout << "ASDF" << std::endl;
100            switch (this->state_)
101            {
102                case CS_Uninitialized:
103                    break;
104                case CS_Empty:
105                    break;
106                case CS_ShortcutOrIdentifier:
107                    if (this->function_)
108                        return CommandExecutor::complete(this->function_->getName() + " ");
109                    else if (this->functionclass_)
110                        return CommandExecutor::complete(this->functionclass_->getName() + " ");
111                    break;
112                case CS_Function:
113                    if (this->function_)
114                        return CommandExecutor::complete(this->functionclass_->getName() + " " + this->function_->getName() + " ");
115                    break;
116                case CS_Params:
117                    break;
118                case CS_Finished:
119                    break;
120                case CS_Error:
121                    break;
122            }
123        }
124        this->bNewCommand_ = false;
125        return this->command_;
126    }
127
128    std::string CommandEvaluation::hint() const
129    {
130        switch (this->state_)
131        {
132            case CS_Uninitialized:
133                break;
134            case CS_Empty:
135            case CS_ShortcutOrIdentifier:
136                if (this->listOfPossibleFunctions_.size() == 0)
137                    return CommandEvaluation::dump(this->listOfPossibleIdentifiers_);
138                else if (this->listOfPossibleIdentifiers_.size() == 0)
139                    return CommandEvaluation::dump(this->listOfPossibleFunctions_);
140                else
141                    return (CommandEvaluation::dump(this->listOfPossibleFunctions_) + "\n" + CommandEvaluation::dump(this->listOfPossibleIdentifiers_));
142                break;
143            case CS_Function:
144                return CommandEvaluation::dump(this->listOfPossibleFunctions_);
145                break;
146            case CS_Params:
147                if (this->listOfPossibleArguments_.size() > 0)
148                    return CommandEvaluation::dump(this->listOfPossibleArguments_);
149                else
150                    return CommandEvaluation::dump(this->function_);
151            case CS_Finished:
152                if (this->function_)
153                    return CommandEvaluation::dump(this->function_);
154                break;
155            case CS_Error:
156                return this->errorMessage_;
157                break;
158        }
159
160        return "";
161    }
162
163    void CommandEvaluation::evaluateParams()
164    {
165        this->bEvaluatedParams_ = false;
166
167        for (unsigned int i = 0; i < MAX_FUNCTOR_ARGUMENTS; i++)
168            this->param_[i] = MT_null;
169
170        if (!this->isValid())
171            return;
172
173        unsigned int startindex = this->getStartindex();
174
175        if (this->commandTokens_.size() <= startindex)
176        {
177            if (this->function_->evaluate(this->getAdditionalParameter(), this->param_, " "))
178                this->bEvaluatedParams_ = true;
179        }
180        else if (this->commandTokens_.size() > startindex)
181        {
182            if (this->function_->evaluate(this->commandTokens_.subSet(startindex).join() + this->getAdditionalParameter(), this->param_, " "))
183                this->bEvaluatedParams_ = true;
184        }
185    }
186
187    void CommandEvaluation::setEvaluatedParameter(unsigned int index, MultiTypeMath param)
188    {
189        if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
190            this->param_[index] = param;
191    }
192
193    MultiTypeMath CommandEvaluation::getEvaluatedParameter(unsigned int index) const
194    {
195        if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
196            return this->param_[index];
197
198        return MT_null;
199    }
200
201    bool CommandEvaluation::hasReturnvalue() const
202    {
203        if (this->function_)
204            return this->function_->hasReturnvalue();
205
206        return MT_null;
207    }
208
209    MultiTypeMath CommandEvaluation::getReturnvalue() const
210    {
211        if (this->function_)
212            return this->function_->getReturnvalue();
213
214        return MultiTypeMath();
215    }
216
217
218    unsigned int CommandEvaluation::getStartindex() const
219    {
220        if (this->functionclass_ && this->function_)
221            return 2;
222        else if (this->function_)
223            return 1;
224        else
225            return 0;
226    }
227
228    std::string CommandEvaluation::dump(const std::list<std::pair<const std::string*, const std::string*> >& list)
229    {
230        std::string output = "";
231        for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it)
232        {
233            if (it != list.begin())
234                output += " ";
235
236            output += *(*it).second;
237        }
238        return output;
239    }
240
241    std::string CommandEvaluation::dump(const ConsoleCommand* command)
242    {
243        std::string output = command->getName() + ": ";
244        for (unsigned int i = 0; i < command->getParamCount(); i++)
245        {
246            if (i != 0)
247                output += " ";
248
249            if (command->defaultValueSet(i))
250                output += "[";
251            else
252                output += "{";
253
254            output += command->getTypenameParam(i);
255
256            if (command->defaultValueSet(i))
257                output += "=" + command->getDefaultValue(i).toString() + "]";
258            else
259                output += "}";
260        }
261        return output;
262    }
263}
Note: See TracBrowser for help on using the repository browser.