new 'debug()' formula function

This commit is contained in:
Iurii Chernyi 2009-08-18 09:50:48 +00:00
parent e3dcb3794a
commit bdfbd91a42
6 changed files with 75 additions and 8 deletions

View File

@ -1,6 +1,7 @@
Version 1.7.3+svn:
* AI:
* Formula AI debugger (uses -new-widgets)
* New 'debug()' formula function
* Graphics:
* New portrait for the Ancient Wose.
* Language and i18n:

View File

@ -123,8 +123,8 @@ std::string formula_ai::evaluate(const std::string& formula_str)
game_logic::map_formula_callable callable(this);
callable.add_ref();
formula_debugger fdb;
const variant v = f.evaluate(callable,&fdb);
//formula_debugger fdb;
const variant v = f.evaluate(callable,NULL);
outcome_positions_.clear();

View File

@ -140,15 +140,13 @@ public:
virtual std::string str() const
{
std::stringstream s;
s << "{map_expression:(";
s << " [";
for(std::vector<expression_ptr>::const_iterator i = items_.begin(); ( i != items_.end() ) && ( i+1 != items_.end() ) ; i+=2) {
s << "[";
s << (*i)->str();
s << "] -> [";
s << " -> ";
s << (*(i+1))->str();
s << "]";
}
s << ")";
s << " ]";
return s.str();
}
private:

View File

@ -17,6 +17,7 @@
//#include "foreach.hpp"
#include "callable_objects.hpp"
#include "formula_debugger.hpp"
#include "formula_function.hpp"
#include "game_display.hpp"
#include "log.hpp"
@ -50,6 +51,35 @@ std::string function_expression::str() const
namespace {
class debug_function : public function_expression {
public:
explicit debug_function(const args_list& args)
: function_expression("debug",args, 0, 1)
{}
private:
variant execute(const formula_callable& variables, formula_debugger *fdb) const {
boost::shared_ptr<formula_debugger> fdbp;
bool need_wrapper = false;
if (fdb==NULL) {
fdbp = boost::shared_ptr<formula_debugger>(new formula_debugger());
fdb = &*fdbp;
need_wrapper = true;
}
if (args().size()==1) {
if (!need_wrapper) {
return args()[0]->evaluate(variables,fdb);
} else {
return wrapper_formula(args()[0]).evaluate(variables,fdb);
}
} else {
return wrapper_formula().evaluate(variables,fdb);
}
}
};
class dir_function : public function_expression {
public:
explicit dir_function(const args_list& args)
@ -956,6 +986,7 @@ functions_map& get_functions_map() {
if(functions_table.empty()) {
#define FUNCTION(name) functions_table[#name] = new function_creator<name##_function>();
FUNCTION(debug);
FUNCTION(dir);
FUNCTION(if);
FUNCTION(switch);

View File

@ -141,6 +141,43 @@ expression_ptr create_function(const std::string& fn,
const function_symbol_table* symbols);
std::vector<std::string> builtin_function_names();
class wrapper_formula : public formula_expression {
public:
wrapper_formula()
: arg_()
{
}
wrapper_formula(expression_ptr arg)
: arg_(arg)
{
}
virtual ~wrapper_formula()
{
}
virtual std::string str() const
{
if (arg_) {
return arg_->str();
} else {
return "";
}
}
private:
virtual variant execute(const formula_callable& variables, formula_debugger *fdb = NULL) const
{
if (arg_) {
return arg_->evaluate(variables,fdb);
} else {
return variant();
}
}
expression_ptr arg_;
};
}
#endif

View File

@ -67,7 +67,7 @@ void tformula_debugger::pre_show(CVideo& /*video*/, twindow& window)
if (!i.evaluated() ) {
execution_text << "#<span color=\"green\">" << i.counter() <<"</span>: \"<span color=\"green\">"<< i.name() << "</span>\": '" << i.str() << "' " << std::endl;
} else {
execution_text << "#<span color=\"yellow\">" << i.counter() <<"</span>: \"<span color=\"yellow\">"<< i.name() << "</span>\": '" << i.str() << "' = " << "<span color=\"red\">"<< i.value().to_debug_string(NULL,true) <<"</span>" << std::endl;
execution_text << "#<span color=\"yellow\">" << i.counter() <<"</span>: \"<span color=\"yellow\">"<< i.name() << "</span>\": '" << i.str() << "' = " << "<span color=\"red\">"<< i.value().to_debug_string(NULL,false) <<"</span>" << std::endl;
}
}