Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

only girls sleep at this time :P

(started new implementation of ConsoleExecutor parser, but it's still buggy)

File size: 11.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
45        this->originalCommand_ = command;
46        this->command_ = command;
47        this->originalCommandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0');
48        this->commandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0');
49
50        this->additionalParameter_ = "";
51
52        this->bEvaluatedParams_ = false;
53
54        this->listOfPossibleIdentifiers_.clear();
55        this->listOfPossibleFunctions_.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        COUT(4) << "CE_execute: " << this->originalCommand_ << "\n";
82
83        unsigned int startindex = this->getStartindex();
84        if (this->originalCommandTokens_.size() > startindex)
85            return this->function_->parse(removeSlashes(this->originalCommandTokens_.subSet(startindex).join() + this->getAdditionalParameter()));
86        else
87            return this->function_->parse(removeSlashes(this->additionalParameter_));
88    }
89
90    std::string CommandEvaluation::complete() const
91    {
92        switch (this->state_)
93        {
94            case CS_Uninitialized:
95            case CS_Empty:
96            case CS_ShortcutOrIdentifier:
97                {
98                    std::list<std::pair<const std::string*, const std::string*> > temp;
99                    if (this->state_ == CS_Empty)
100                    {
101                        temp.insert(temp.end(), this->listOfPossibleFunctions_.begin(), this->listOfPossibleFunctions_.end());
102                        temp.insert(temp.end(), this->listOfPossibleIdentifiers_.begin(), this->listOfPossibleIdentifiers_.end());
103                    }
104                    return (CommandEvaluation::getCommonBegin(temp));
105                }
106                break;
107            case CS_Shortcut_Params:
108                if (this->function_)
109                {
110                    if (this->commandTokens_.size() > 1)
111                        return (this->function_->getName() + " " + this->commandTokens_.subSet(1, this->commandTokens_.size() - 1).join() + " " + CommandEvaluation::getCommonBegin(this->listOfPossibleArguments_));
112                    else
113                        return (this->function_->getName() + " ");
114                }
115                break;
116            case CS_Shortcut_Finished:
117                if (this->function_)
118                {
119                    if (this->commandTokens_.size() > 1)
120                        return (this->function_->getName() + " " + this->originalCommandTokens_.subSet(1, this->originalCommandTokens_.size() - 1).join());
121                    else
122                        return (this->function_->getName());
123                }
124                break;
125            case CS_Function:
126                if (this->functionclass_)
127                    return (this->functionclass_->getName() + " " + CommandEvaluation::getCommonBegin(this->listOfPossibleFunctions_));
128                break;
129            case CS_Function_Params:
130                if (this->functionclass_ && this->function_)
131                {
132                    if (this->commandTokens_.size() > 2)
133                        return (this->functionclass_->getName() + " " + this->function_->getName() + " " + this->commandTokens_.subSet(2, this->commandTokens_.size() - 1).join() + " " + CommandEvaluation::getCommonBegin(this->listOfPossibleArguments_));
134                    else
135                        return (this->functionclass_->getName() + " " + this->function_->getName() + " ");
136                }
137                break;
138            case CS_Function_Finished:
139                if (this->functionclass_ && this->function_)
140                {
141                    if (this->commandTokens_.size() > 2)
142                        return (this->functionclass_->getName() + " " + this->function_->getName() + " " + this->originalCommandTokens_.subSet(2, this->originalCommandTokens_.size() - 1).join());
143                    else
144                        return (this->functionclass_->getName() + " " + this->function_->getName());
145                }
146               break;
147            case CS_Error:
148                break;
149        }
150
151        return this->originalCommand_;
152    }
153
154    std::string CommandEvaluation::hint() const
155    {
156        switch (this->state_)
157        {
158            case CS_Uninitialized:
159                break;
160            case CS_Empty:
161            case CS_ShortcutOrIdentifier:
162                return (CommandEvaluation::dump(this->listOfPossibleFunctions_) + "\n" + CommandEvaluation::dump(this->listOfPossibleIdentifiers_));
163                break;
164            case CS_Function:
165                return CommandEvaluation::dump(this->listOfPossibleFunctions_);
166                break;
167            case CS_Shortcut_Params:
168            case CS_Function_Params:
169                return CommandEvaluation::dump(this->listOfPossibleArguments_);
170            case CS_Shortcut_Finished:
171            case CS_Function_Finished:
172                if (this->function_)
173                    return CommandEvaluation::dump(this->function_);
174                break;
175            case CS_Error:
176                return this->errorMessage_;
177                break;
178        }
179
180        return "";
181    }
182
183    void CommandEvaluation::evaluateParams()
184    {
185        this->bEvaluatedParams_ = false;
186
187        for (unsigned int i = 0; i < MAX_FUNCTOR_ARGUMENTS; i++)
188            this->param_[i] = MT_null;
189
190        if (!this->isValid())
191            return;
192
193        unsigned int startindex = this->getStartindex();
194
195        if (this->originalCommandTokens_.size() <= startindex)
196        {
197            if (this->function_->evaluate(this->getAdditionalParameter(), this->param_, " "))
198                this->bEvaluatedParams_ = true;
199        }
200        else if (this->originalCommandTokens_.size() > startindex)
201        {
202            if (this->function_->evaluate(this->originalCommandTokens_.subSet(startindex).join() + this->getAdditionalParameter(), this->param_, " "))
203                this->bEvaluatedParams_ = true;
204        }
205    }
206
207    void CommandEvaluation::setEvaluatedParameter(unsigned int index, MultiTypeMath param)
208    {
209        if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
210            this->param_[index] = param;
211    }
212
213    MultiTypeMath CommandEvaluation::getEvaluatedParameter(unsigned int index) const
214    {
215        if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
216            return this->param_[index];
217
218        return MT_null;
219    }
220
221    bool CommandEvaluation::hasReturnvalue() const
222    {
223        if (this->function_)
224            return this->function_->hasReturnvalue();
225
226        return MT_null;
227    }
228
229    MultiTypeMath CommandEvaluation::getReturnvalue() const
230    {
231        if (this->function_)
232            return this->function_->getReturnvalue();
233
234        return MultiTypeMath();
235    }
236
237
238    unsigned int CommandEvaluation::getStartindex() const
239    {
240        if (this->state_ == CS_Shortcut_Params || this->state_ == CS_Shortcut_Finished)
241            return 1;
242        else if (this->state_ == CS_Function || this->state_ == CS_Function_Params || this->state_ == CS_Function_Finished)
243            return 2;
244        else
245            return 0;
246    }
247
248    std::string CommandEvaluation::getCommonBegin(const std::list<std::pair<const std::string*, const std::string*> >& list)
249    {
250        if (list.size() == 0)
251        {
252            return "";
253        }
254        else if (list.size() == 1)
255        {
256            return ((*(*list.begin()).first) + " ");
257        }
258        else
259        {
260            std::string output = "";
261            for (unsigned int i = 0; true; i++)
262            {
263                char temp = 0;
264                for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it)
265                {
266                    if ((*(*it).first).size() > i)
267                    {
268                        if (it == list.begin())
269                        {
270                            temp = (*(*it).first)[i];
271                        }
272                        else
273                        {
274                            if (temp != (*(*it).first)[i])
275                                return output;
276                        }
277                    }
278                    else
279                    {
280                        return output;
281                    }
282                }
283                output += temp;
284            }
285            return output;
286        }
287    }
288
289    std::string CommandEvaluation::dump(const std::list<std::pair<const std::string*, const std::string*> >& list)
290    {
291        std::string output = "";
292        for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it)
293        {
294            if (it != list.begin())
295                output += " ";
296
297            output += *(*it).second;
298        }
299        return output;
300    }
301
302    std::string CommandEvaluation::dump(const ConsoleCommand* command)
303    {
304        std::string output = "";
305        for (unsigned int i = 0; i < command->getParamCount(); i++)
306        {
307            if (i != 0)
308                output += " ";
309
310            if (command->defaultValueSet(i))
311                output += "[";
312            else
313                output += "{";
314
315            output += command->getTypenameParam(i);
316
317            if (command->defaultValueSet(i))
318                output += "=" + command->getDefaultValue(i).toString() + "]";
319            else
320                output += "}";
321        }
322        return output;
323    }
324}
Note: See TracBrowser for help on using the repository browser.