Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef FORMULA_HPP_INCLUDED
00015 #define FORMULA_HPP_INCLUDED
00016
00017 #include "formula_debugger_fwd.hpp"
00018 #include "formula_fwd.hpp"
00019 #include "formula_tokenizer.hpp"
00020 #include "variant.hpp"
00021
00022 namespace game_logic
00023 {
00024
00025 class formula_callable;
00026 class formula_expression;
00027 class function_symbol_table;
00028 typedef boost::shared_ptr<formula_expression> expression_ptr;
00029
00030 class formula {
00031 public:
00032 static variant evaluate(const const_formula_ptr& f,
00033 const formula_callable& variables, formula_debugger *fdb = NULL,
00034 variant default_res=variant(0)) {
00035 if(f) {
00036 return f->evaluate(variables, fdb);
00037 } else {
00038 return default_res;
00039 }
00040 }
00041
00042 variant evaluate(const formula_callable& variables, formula_debugger *fdb = NULL) const
00043 {
00044 if (fdb!=NULL) {
00045 return evaluate_formula_callback(*fdb,*this,variables);
00046 } else {
00047 return execute(variables,fdb);
00048 }
00049 }
00050
00051 variant evaluate(formula_debugger *fdb = NULL) const
00052 {
00053 if (fdb!=NULL) {
00054 return evaluate_formula_callback(*fdb,*this);
00055 } else {
00056 return execute(fdb);
00057 }
00058 }
00059
00060 static formula_ptr create_optional_formula(const std::string& str, function_symbol_table* symbols=NULL);
00061 explicit formula(const std::string& str, function_symbol_table* symbols=NULL);
00062 explicit formula(const formula_tokenizer::token* i1, const formula_tokenizer::token* i2, function_symbol_table* symbols=NULL);
00063 const std::string& str() const { return str_; }
00064
00065 private:
00066 variant execute(const formula_callable& variables, formula_debugger *fdb = NULL) const;
00067 variant execute(formula_debugger *fdb) const;
00068 formula() : expr_(), str_()
00069 {}
00070 expression_ptr expr_;
00071 std::string str_;
00072 friend class formula_debugger;
00073 };
00074
00075 struct formula_error : public game::error
00076 {
00077 formula_error()
00078 : error()
00079 , type()
00080 , formula()
00081 , filename()
00082 , line(0)
00083 {}
00084
00085 formula_error(const std::string& type, const std::string& formula,
00086 const std::string& file, int line);
00087
00088 ~formula_error() throw() {}
00089
00090 std::string type;
00091 std::string formula;
00092 std::string filename;
00093 int line;
00094 };
00095
00096 }
00097
00098 #endif