The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
formula.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2016 by David White <dave.net>
3  Part of the Silver Tree Project
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by or later.
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY.
9 
10  See the COPYING file for more details.
11 */
12 
13 #ifndef FORMULA_HPP_INCLUDED
14 #define FORMULA_HPP_INCLUDED
15 
16 #include "formula/debugger_fwd.hpp"
17 #include "formula/formula_fwd.hpp"
18 #include "formula/tokenizer.hpp"
19 #include "formula/variant.hpp"
20 
21 namespace game_logic
22 {
23 
24 class formula_callable;
25 class formula_expression;
28 
29 class formula {
30 public:
32  const formula_callable& variables, formula_debugger *fdb = nullptr,
33  variant default_res=variant(0)) {
34  if(f) {
35  return f->evaluate(variables, fdb);
36  } else {
37  return default_res;
38  }
39  }
40 
41  variant evaluate(const formula_callable& variables, formula_debugger *fdb = nullptr) const
42  {
43  if (fdb!=nullptr) {
44  return evaluate_formula_callback(*fdb,*this,variables);
45  } else {
46  return execute(variables,fdb);
47  }
48  }
49 
50  variant evaluate(formula_debugger *fdb = nullptr) const
51  {
52  if (fdb!=nullptr) {
53  return evaluate_formula_callback(*fdb,*this);
54  } else {
55  return execute(fdb);
56  }
57  }
58 
60  explicit formula(const std::string& str, function_symbol_table* symbols=nullptr);
61  explicit formula(const formula_tokenizer::token* i1, const formula_tokenizer::token* i2, function_symbol_table* symbols=nullptr);
62  ~formula();
63  const std::string& str() const { return str_; }
64 
65  static const char*const id_chars;
66 private:
67  variant execute(const formula_callable& variables, formula_debugger *fdb = nullptr) const;
68  variant execute(formula_debugger *fdb) const;
69  formula() : expr_(), str_()
70  {}
71  expression_ptr expr_;
75  friend class formula_debugger;
76 };
77 
78 struct formula_error : public game::error
79 {
81  : error()
82  , type()
83  , formula()
84  , filename()
85  , line(0)
86  {}
87 
89  const std::string& file, int line);
90 
91  ~formula_error() throw() {}
92 
96  int line;
97 };
98 
99 }
100 
101 #endif
std::string str_
Definition: formula.hpp:72
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
Formula AI debugger, forward.
variant evaluate_formula_callback(formula_debugger &fdb, const formula &f, const formula_callable &variables)
static const char *const id_chars
Definition: formula.hpp:65
static formula_ptr create_optional_formula(const std::string &str, function_symbol_table *symbols=nullptr)
Definition: formula.cpp:1210
function_symbol_table * symbols_
Definition: formula.hpp:73
static variant evaluate(const const_formula_ptr &f, const formula_callable &variables, formula_debugger *fdb=nullptr, variant default_res=variant(0))
Definition: formula.hpp:31
boost::shared_ptr< formula_expression > expression_ptr
Definition: formula.hpp:26
variant execute(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: formula.cpp:1369
variant evaluate(formula_debugger *fdb=nullptr) const
Definition: formula.hpp:50
const std::string & str() const
Definition: formula.hpp:63
variant evaluate(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: formula.hpp:41
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:27
expression_ptr expr_
Definition: formula.hpp:71
GLsizei const GLcharARB ** string
Definition: glew.h:4503
GLclampf f
Definition: glew.h:3024