Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Monsieur OUXX on Wed 25/07/2007 15:17:38

Title: Bug in array's index evaluation? (SOLVED)
Post by: Monsieur OUXX on Wed 25/07/2007 15:17:38
The following code gives a wrong result :
[EDIT]According to Ashen, this specific code works, but my code (more complicated) doesn't[/EDIT]

#define MACRO1  1
#define MACRO2  -1000

...
int tab[10];
int i=1005;

int x = tab[i+MACRO1 MACRO2];


i+MACRO1 MACRO2 should be evaluated as "i+1 -1000", so it should be equal to 1005+1-1000 = 6.
Instead, it seems to be evaluated as "i+1+1000" and it is equal to 1005+1+1000 = 2006

Or maybe I made a mistake? Maybe "-" is forbidden in macros? But then how does it come that "i+MACRO1 MACRO2" still manages to be evaluated (since it would then give "1005+1   1000")?


I'd be tempted to say that this bug is similar to the one preventing functions to be evaluated when used as the index of an array.  ( example : tab[MyFunction()];
Title: Re: bug in array's index evaluation or in macros ?
Post by: strazer on Wed 25/07/2007 15:33:00
Try

int x = tab[(i+MACRO1) MACRO2];

I think it's because of right-to-left evaluation.
Title: Re: bug in array's index evaluation or in macros ?
Post by: Ashen on Wed 25/07/2007 15:41:12
It seems to compile OK for me even without the brackets, and returns tab[6].
Where are they defined?
Title: Re: bug in array's index evaluation or in macros ?
Post by: Monsieur OUXX on Wed 25/07/2007 16:06:17
Quote from: strazer on Wed 25/07/2007 15:33:00
I think it's because of right-to-left evaluation.

You are absolutely right, thank you very much strazer!

int x = tab[(i+MACRO1) MACRO2]; works

int x = tab[(i MACRO2) + MACRO1];   also works and is more consistent with the meaning of my macros.

Just for curiosity : I wonder how the "right to left" evaluation impacts this expression, since "(i+MACRO1) MACRO2" is equivalent to "MACRO2 THEN (i+MACRO1)", which is equivalent to "MACRO2 THEN MACRO1 THEN i"... So "(i+MACRO1) MACRO2" looks like it should give exactly the same result as "i+MACRO1  MACRO2"

Edit:

Quote from: Ashen on Wed 25/07/2007 15:41:12
It seems to compile OK for me even without the brackets

The example I gave was a simple one, where I removed all unuseful info, so it may work; but there still was an evaluation problem that strazer solved. Thank you anyway for having tried.

Quotewhere are they defined?

MACRO2 is defined in the header of the module.
MACRO1 is defined in the body of the module.
Title: Re: bug in array's index evaluation or in macros ?
Post by: strazer on Wed 25/07/2007 16:16:39
Glad I could help. :)

Quote from: Monsieur OUXX on Wed 25/07/2007 16:06:17
int x = tab[(i MACRO2) + MACRO1];   also works and is more consistent with the meaning of my macros.

Right, better put the +'s in there to be on the safe side (as +- equals -).

Quote from: Monsieur OUXX on Wed 25/07/2007 16:06:17So "(i+MACRO1) MACRO2" looks like it should give exactly the same result as "i+MACRO1  MACRO2"

I'll let someone else answer this since I'd surely get it wrong... ;)
Title: Re: Bug in array's index evaluation? (SOLVED)
Post by: Pumaman on Sat 28/07/2007 21:54:15
Yes, there's a "Right-to-left evaluation" checkbox that you can set on the General SEttings pane to set this.

If you're writing a module, I'd recommend you check the LRPRECEDENCE macro (or just use parenthesis to remove all ambiguity) in your code.