Jump to content

Use of the Expression class


photo

Recommended Posts

Hi, 

 

I'm trying to use the Expression class but it seems the examples given in the documentation are off.

For example this does not compile ("can't find "run" function with 4 arguments in class Expression"): 

Expression e1 = new Expression("
	{
		int a,b,c,d;
		return a + b + c + d;
	}
","a,b,c,d");
if(e1.isCompiled()) {
	log.message("%s\n",typeinfo(e1.run(1,2,3,4)));
}

To give you some context, I'm trying to use this to run unit tests on our project. The Expression class seemed to be a good way of doing it since it's supposed to allow expressions written in strings to be processed at run time. This way we could easily display which test went wrong. It works for simple tests using static methods but I wish I could run the expressions with arguments. If it's not possible, do you know any better way to evaluate and display a boolean expression ?

Thanks !

 

Link to comment

Hi Jonathan,

 

Thanks for noticing, this example is obsolete. Constructor now hasn't argument list and run function didn't take arguments. Instead you must set arguments using Expression::setVariable(var_name, var_value). For example:

Expression e1 = new Expression("{ int a,b,c,d; return a + b + c + d; }");
e1.setVariable("a",1);
e1.setVariable("b",1);
e1.setVariable("c",1);
e1.setVariable("d",1);
log.message("%d\n",e1.run());
Link to comment

Hi Maxim, 

 

Ok thanks a lot, that solves it for me ! In the meantime I used a workaround to perform unit tests, based on the assert macro definition. For those who might be interested, it goes like this :

#define UNIT_TEST(EXP)	{ if(EXP) { } else { log.warning("[UT] NOT OK: '%s' in %s\n", #EXP, __FUNC__); } }
Link to comment
×
×
  • Create New...