mail[Wesnoth-commits] r24885 - in /trunk/src: formula.cpp formula_function.cpp formula_function.hpp formula_tokenizer.cpp formula_tokenizer.hpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by dave on March 20, 2008 - 03:22:
Author: dave
Date: Thu Mar 20 03:22:45 2008
New Revision: 24885

URL: http://svn.gna.org/viewcvs/wesnoth?rev=24885&view=rev
Log:
applied barbarianhero's 'functions' patch to formula system

Modified:
    trunk/src/formula.cpp
    trunk/src/formula_function.cpp
    trunk/src/formula_function.hpp
    trunk/src/formula_tokenizer.cpp
    trunk/src/formula_tokenizer.hpp

Modified: trunk/src/formula.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula.cpp?rev=24885&r1=24884&r2=24885&view=diff
==============================================================================
--- trunk/src/formula.cpp (original)
+++ trunk/src/formula.cpp Thu Mar 20 03:22:45 2008
@@ -66,6 +66,25 @@
 }
 
 namespace {
+
+class function_list_expression : public formula_expression {
+public:
+       explicit function_list_expression(const function_symbol_table 
*symbols)
+               : symbols_(symbols)
+       {}
+
+private:
+       variant execute(const formula_callable& variables) const {
+               std::vector<variant> res;
+               std::vector<std::string> function_names = 
symbols_->get_function_names();
+               for(int i = 0; i < function_names.size(); i++) {
+                       res.push_back(variant(function_names[i]));
+               }
+               return variant(&res);
+       }
+
+       const function_symbol_table* symbols_;
+};
 
 class list_expression : public formula_expression {
 public:
@@ -511,7 +530,11 @@
                                parse_args(i1+1,i2-1,&args,symbols);
                                return expression_ptr(new 
list_expression(args));
                } else if(i2 - i1 == 1) {
-                       if(i1->type == TOKEN_IDENTIFIER) {
+                       if(i1->type == TOKEN_KEYWORD) {
+                               if(std::string(i1->begin,i1->end) == 
"functions") {
+                                       return expression_ptr(new 
function_list_expression(symbols));
+                               }
+                       } else if(i1->type == TOKEN_IDENTIFIER) {
                                return expression_ptr(new 
identifier_expression(
                                                 
std::string(i1->begin,i1->end)));
                        } else if(i1->type == TOKEN_INTEGER) {

Modified: trunk/src/formula_function.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_function.cpp?rev=24885&r1=24884&r2=24885&view=diff
==============================================================================
--- trunk/src/formula_function.cpp (original)
+++ trunk/src/formula_function.cpp Thu Mar 20 03:22:45 2008
@@ -578,6 +578,15 @@
        }
 
        return expression_ptr();
+}
+
+std::vector<std::string> function_symbol_table::get_function_names() const
+{
+       std::vector<std::string> res;
+       for(std::map<std::string, formula_function>::const_iterator iter = 
custom_formulas_.begin(); iter != custom_formulas_.end(); iter++ ) {
+               res.push_back((*iter).first);
+       }
+       return res;
 }
 
 expression_ptr create_function(const std::string& fn,

Modified: trunk/src/formula_function.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_function.hpp?rev=24885&r1=24884&r2=24885&view=diff
==============================================================================
--- trunk/src/formula_function.hpp (original)
+++ trunk/src/formula_function.hpp Thu Mar 20 03:22:45 2008
@@ -101,6 +101,7 @@
        void add_formula_function(const std::string& name, const_formula_ptr 
formula, const_formula_ptr precondition, const std::vector<std::string>& 
args);
        virtual expression_ptr create_function(const std::string& fn,
                                                               const 
std::vector<expression_ptr>& args) const;
+       std::vector<std::string> get_function_names() const;
 };
 
 expression_ptr create_function(const std::string& fn,

Modified: trunk/src/formula_tokenizer.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_tokenizer.cpp?rev=24885&r1=24884&r2=24885&view=diff
==============================================================================
--- trunk/src/formula_tokenizer.cpp (original)
+++ trunk/src/formula_tokenizer.cpp Thu Mar 20 03:22:45 2008
@@ -32,6 +32,7 @@
 
 //create the array with list of possible tokens
 token_type token_types[] = { { 
regex("^(not\\b|and\\b|or\\b|where\\b|d(?=[^a-zA-Z])|\\*|\\+|\\-|\\^|%|/|<=|>=|<|>|!=|=|\\.)"),
 TOKEN_OPERATOR },
+                               { regex("functions"),   TOKEN_KEYWORD },
                                { regex("^'[^']*'"),    TOKEN_STRING_LITERAL 
},
                                { regex("^[a-zA-Z_]+"), TOKEN_IDENTIFIER },
                                { regex("^\\d+"),       TOKEN_INTEGER },
@@ -40,7 +41,7 @@
                                { regex("^\\["),        TOKEN_LSQUARE },
                                { regex("^\\]"),        TOKEN_RSQUARE },
                                { regex("^,"),          TOKEN_COMMA },
-                               { regex("^\\s+"),       TOKEN_WHITESPACE } 
+                               { regex("^\\s+"),       TOKEN_WHITESPACE }
 };
 
 }
@@ -79,10 +80,10 @@
                                                  TOKEN_WHITESPACE, 
TOKEN_LPARENS,
                                                  TOKEN_INTEGER, 
TOKEN_OPERATOR,
                                                  TOKEN_INTEGER, 
TOKEN_RPARENS,
-                                                 TOKEN_RPARENS,
+                                                 TOKEN_RPARENS, 
TOKEN_KEYWORD,
                              TOKEN_OPERATOR, TOKEN_INTEGER};
        std::string tokens[] = {"(", "abc", " ", "+", " ", "4", " ",
-                               "*", " ", "(", "5", "+", "3", ")", ")"};
+                               "*", " ", "(", "5", "+", "3", ")", ")", 
"functions"};
        for(int n = 0; n != sizeof(types)/sizeof(*types); ++n) {
                token t = get_token(i1,i2);
                assert(std::string(t.begin,t.end) == tokens[n]);

Modified: trunk/src/formula_tokenizer.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_tokenizer.hpp?rev=24885&r1=24884&r2=24885&view=diff
==============================================================================
--- trunk/src/formula_tokenizer.hpp (original)
+++ trunk/src/formula_tokenizer.hpp Thu Mar 20 03:22:45 2008
@@ -25,7 +25,7 @@
                          TOKEN_IDENTIFIER, TOKEN_INTEGER,
                   TOKEN_LPARENS, TOKEN_RPARENS,
                                  TOKEN_LSQUARE, TOKEN_RSQUARE, TOKEN_COMMA,
-                                 TOKEN_WHITESPACE };
+                                 TOKEN_WHITESPACE, TOKEN_KEYWORD };
 
 struct token {
        TOKEN_TYPE type;




Related Messages


Powered by MHonArc, Updated Thu Mar 20 03:41:10 2008