Alex Rivera | Logout

Is there any kind of "expression class" (C++)

Asked 2009-06-10T21:27:39.440
21

I am creating a game that lets the player enter input, changes some states, then checks if a "goal value" is true (obviously this description is muchly simplified), and I want to be able to have that goal value be anything from if the players life is below a certain value to if the amount of enemies remaining is equal to zero. Is there any "expression class" that can hold a simple "value1 compare-operator value2" and check it? ie:

expression goal(x = 4);

if not, does anybody have any suggestions as to how I could develop an expression class?

EDIT: another (more towards what I am trying to achieve) example:

game.init(){ expression goal = FileRead(goalfile); }
game.checkstate(){ if(goal) exit(1); } //exit 1 is the games win state

//another more specific eg.:
class level1 { public: expression goal(total_enemies == 0); };
class level2 { public: expression goal(player.x == goal.x && player.y == goal.y); };
Edit
Report

4 Answers

28

Dynamic expressions

If you want to receive a string from the user and built an expression from that, maybe the C++ Mathematical Expression Library fits your bill?

template<typename T>
void trig_function()
{
   std::string expression_string = "clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)";
   T x;
   exprtk::symbol_table<T> symbol_table;
   symbol_table.add_variable("x",x);
   symbol_table.add_constants();

   exprtk::expression<T> expression;
   expression.register_symbol_table(symbol_table);

   exprtk::parser<T> parser;
   parser.compile(expression_string,expression);

   for (x = T(-5.0); x <= T(+5.0); x += 0.001)
   {
      T y = expression.value();
      printf("%19.15f\t%19.15f\n",x,y);
   }
}

There are also the possibility embed a scripting language, such as Lua or Python, which will give you (even) more power. This is something to consider if you're writing a game, since you'll likely want to script large parts of it.

If you're using Qt, you can use QtScript (Javascript-ish) to run expressions that read (static or dynamic) properties from your QObject-derived objects.

Using one of the above keeps you from having to write your own parser, AST and evaluator, however for a small set of operators it shouldn't be too hard to hack together something if you use Boost.Spirit or some other decent parsing library.

Static expressions

For selecting between a set of predefined expressions (i.e. known at compile time), you should store the expression in a polymo

answered 2009-06-10T21:37:09.873
1

There is no standard way to compile expressions during runtime. You'll have to do it some other way.

You might consider using a scripting language, like Lua or Python, and embed it in your C++. That would allow your players the ability to program to the extent you want them to.

answered 2009-06-10T22:00:49.267
0

There's no standard way to do it in C++. One solution is to write your own parser.

Another solution, that I'd recommend: embed a Lua interpreter in your program. Lua is a simple yet powerful programming language, that also has an extremely lightweight (<300kB) and easy-to-use interpreter. Read an introductory article here: http://www.ibm.com/developerworks/linux/library/l-embed-lua/index.html

Having Lua embedded in your game has a number of nice side advantages:

  • you can use it as a powerful configuration language for your game
  • with Lua, you can easily create a command-line interactive environment, which is nice for testing and experiements. For example, you will be able to change the game engine parameters and see the effect immediately, without recompiling. That's especially convenient for "researchy" projects or game programming.
answered 2009-06-10T22:15:59.827
-3

There don't seem to be all that many recognised expression evaluation libraries for C++. I wrote my own for CSVfix which you can persuse by looking at the a_expr.h and a_expr.cpp files in the ALib library that forms part of the CSVfix source. The evaluator has not a great deal to recommend itself, except that it does the job and is (IMHO) fairly simple to understand.

Unfurtunately, there is currently no public documentation for the evaluator, and by my own rule of thumb, that which is not documented cannot be reused. However, the unit tests show how it can be used, and the source may give you some ideas on implementing your own evaluator, should the fancy take you.

answered 2009-06-10T21:55:50.803

Your Answer