Bug - Fixed Error in parsing negative numbers

philmasterplus

Active member
Tested with r8651. A number preceded by a negative sign causes an error when it is followed by any operator. I was able to bypass this issue by wrapping an additional pair of parentheses around the offending negative number.

PHP:
int a = -12 / 7;    //Error: Expected ;, found / (negativetest.ash, line 1)
int b = (-12 / 7);  //Error: Expected ), found / (negativetest.ash, line 2)
int c = (-12) / 7;  //OK
int g = (- 12) / 7; //OK
int d = -(12 / 7);  //OK
int e = - 12;       //OK
int f = - 12 + 1;   //Expected ;, found + (negativetest.ash, line 7)

Now the question is...will it be
1) fixed, because...perfection!
2) not fixed, because low priority/easy to bypass/etc?
If the answer is (2), I could add it to the "Tips & Tricks & Workarounds" page on the mafia wiki.

p.s. I appreciate how the devs have created a full-blown programming language and interpreter for such a (relatively) small group of people. How long did it took to make it release-worthy in the first place?
 
Ahh. Good. It seems like the trouble I was having the other day actually was a problem then and not just me :)

This code throws the above mentioned error:
Code:
void main(int want)
{
   int have = my_meat();
   int closet = my_closet_meat();
   int take = (want - have);
   int put = -1* take;
}

While this works:
Code:
void main(int want)
{
   int have = my_meat();
   int closet = my_closet_meat();
   int take = (want - have);
   int put = take * -1;
}

A bit odd if you ask me :)
 
As I posted in http://kolmafia.us/showthread.php?4990-Rounding-error&p=35776&viewfull=1#post35776 and you posted in post 2 there, if you don't make it VERY aware, it will assume you know what you're talking about. If you don't put your initial negative number into paranthesis or something to make it an int first, it's an operation... it's not negative one, the number, it's minus one, the operation. Problem is, minus one... from what? Normally, you provide it with a starting point. want-have (take) in one example above, (-12) in the initial post here... Otherwise it's going based on difference to the starting point... which probably isn't the math operation you're expecting to see.
 
But it has nothing to make the operation on in the second case either, yet that works. It's that little inconsitency that annoys me :)
It's not that it's hard to work around (well now that I know of the problem), it is just annoying.

Also, a = (-12)*3 and a = -(12)*3 works, but not int a = -12*3. I can see the first and the third one from the examples above but the second one feels very odd.
 
In the case of the second one, a number inside parenthesis, with a number outside, is multiplied. Don't look at -(12) as -12*3, look at it as -1 * 12 * 3.
 
In the case of the second one, a number inside parenthesis, with a number outside, is multiplied. Don't look at -(12) as -12*3, look at it as -1 * 12 * 3.

Yes, but -1 * 12 isn't working so why should -1 * 12 *3 work?
Sorry if I'm a bit confused over what I think math shoudl do versus what it is programmed to do :)
 
By making it -(12) * 3, you're actually making it 12 * -1 * 3... the numbers in parenthesis are all calculated first, then the items that modify them, and finally the rest of the equation... I shouldn't have written it in the improper order in post 5. Sorry. :)
 
No, no, no. -12/7 is not -1*12/7, according to mafia's parser. It's -12 END-OF-EXPRESSION ERROR * 12 / 7, because
1) a - followed immediately by an integer is parsed as a (constant) negative number ad
2) the parsing method for expressions (Parser.parseExpression()) doesn't expect the expression to continue after a negative number. It expects a negative number to always be the complete expression.
3) this "works" if it is enclosed in something that accepts a complete expression, like when it's the final RHS of an expression, or in parentheses. It might possibly work even in other RHS situations.
4) Even when it works, it might break precedence ( A + -B * C ) will evaluate as ( ( A + -B ) * C ) instead of ( A + ( -B * C ) )
 
Tested with r8651. A number preceded by a negative sign causes an error when it is followed by any operator. I was able to bypass this issue by wrapping an additional pair of parentheses around the offending negative number.
Revision 8653 should fix this.

p.s. I appreciate how the devs have created a full-blown programming language and interpreter for such a (relatively) small group of people. How long did it took to make it release-worthy in the first place?
Xylpher initially wrote ASH as a project for an Undergraduate compiler class, I believe. It took him a month or so to provide a usable but fragile language.

I spent about a month applying Software Engineering to make the code robust and reliable.
Hola refactored all the source and added things like relay overrides.
I continued to add things like maps and records.
Jason added sort and various things.

It's obviously still not done, considering that you just found a bug,...
 
Back
Top