The Battle for Wesnoth  1.13.10+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) 2003 - 2017 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
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
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "formula/debugger_fwd.hpp"
18 #include "formula/formula_fwd.hpp"
19 #include "formula/tokenizer.hpp"
20 #include "formula/variant.hpp"
21 #include <memory>
22 
23 namespace wfl
24 {
25 class formula_expression;
26 class function_symbol_table;
27 
28 using expression_ptr = std::shared_ptr<formula_expression>;
29 
30 // Namespace alias for shorter typing
31 namespace tk = tokenizer;
32 
33 class formula
34 {
35 public:
36  formula(const std::string& str, function_symbol_table* symbols = nullptr);
37  formula(const tk::token* i1, const tk::token* i2, function_symbol_table* symbols = nullptr);
38 
39  static variant evaluate(
40  const const_formula_ptr& f,
41  const formula_callable& variables,
42  formula_debugger* fdb = nullptr,
43  variant default_res = variant(0))
44  {
45  if(f) {
46  return f->evaluate(variables, fdb);
47  } else {
48  return default_res;
49  }
50  }
51 
52  static formula_ptr create_optional_formula(const std::string& str, function_symbol_table* symbols = nullptr);
53 
54  variant evaluate(const formula_callable& variables, formula_debugger* fdb = nullptr) const
55  {
56  if(fdb != nullptr) {
57  return evaluate_formula_callback(*fdb, *this, variables);
58  } else {
59  return execute(variables, fdb);
60  }
61  }
62 
63  variant evaluate(formula_debugger* fdb = nullptr) const
64  {
65  if(fdb != nullptr) {
66  return evaluate_formula_callback(*fdb,*this);
67  } else {
68  return execute(fdb);
69  }
70  }
71 
72  const std::string& str() const { return str_; }
73 
74  static const char* const id_chars;
75 
76 private:
77  variant execute(const formula_callable& variables, formula_debugger* fdb = nullptr) const;
78  variant execute(formula_debugger* fdb) const;
79 
82  // Can't be a unique_ptr because function_symbol_table is an incomplete type,
83  // and the header it's declared in depends on this one.
84  const std::shared_ptr<function_symbol_table> managed_symbols_;
86 
87  friend class formula_debugger;
88 };
89 
90 struct formula_error : public game::error
91 {
93  : error()
94  , type()
95  , formula()
96  , filename()
97  , line(0)
98  {}
99 
101  const std::string& file, int line);
102 
103  ~formula_error() NOEXCEPT {}
104 
108  int line;
109 };
110 
111 } // namespace wfl
std::vector< char_t > string
static variant evaluate(const const_formula_ptr &f, const formula_callable &variables, formula_debugger *fdb=nullptr, variant default_res=variant(0))
Definition: formula.hpp:39
std::string formula
Definition: formula.hpp:106
Abstract baseclass for the tokenizer.
Definition: tokenizer.hpp:57
std::string filename
Definition: formula.hpp:107
const std::string & str() const
Definition: formula.hpp:72
Formula AI debugger, forward.
std::shared_ptr< formula_expression > expression_ptr
Definition: formula.hpp:28
std::string str_
Definition: formula.hpp:81
static formula_ptr create_optional_formula(const std::string &str, function_symbol_table *symbols=nullptr)
Definition: formula.cpp:222
function_symbol_table * symbols_
Definition: formula.hpp:85
~formula_error() NOEXCEPT
Definition: formula.hpp:103
std::string type
Definition: formula.hpp:105
variant evaluate_formula_callback(formula_debugger &fdb, const formula &f, const formula_callable &variables)
variant execute(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: formula.cpp:231
variant evaluate(formula_debugger *fdb=nullptr) const
Definition: formula.hpp:63
const std::shared_ptr< function_symbol_table > managed_symbols_
Definition: formula.hpp:84
std::shared_ptr< const formula > const_formula_ptr
Definition: formula_fwd.hpp:23
formula(const std::string &str, function_symbol_table *symbols=nullptr)
Definition: formula.cpp:68
static const char *const id_chars
Definition: formula.hpp:74
expression_ptr expr_
Definition: formula.hpp:80
#define f
Definition: contexts.hpp:42
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
variant evaluate(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: formula.hpp:54
std::shared_ptr< formula > formula_ptr
Definition: formula_fwd.hpp:21