libminizinc icon indicating copy to clipboard operation
libminizinc copied to clipboard

Can't use interger literal -9223372036854775808

Open CervEdin opened this issue 1 year ago • 4 comments

Seems like the parser can't parse integer literals greater than 9223372036854775807

echo 'type int64 = -9223372036854775808..9223372036854775807' | minizinc -
stdin:1.15-33:
type int64 = -9223372036854775808..9223372036854775807
              ^^^^^^^^^^^^^^^^^^^
Error: syntax error, unexpected invalid integer literal

CervEdin avatar Jul 12 '24 14:07 CervEdin

Internally, -9223372036854775808 seems to be fine:

int: a = -9223372036854775807 - 1;
int: b = 9223372036854775807;

output ["\(a) \(b)"];

The backend solvers have their own integer limits.

a1880 avatar Jul 13 '24 18:07 a1880

This is due to a quirk in the way integer literals and the - operator are parsed. Like in other languages, -9223372036854775808 is parsed as the negation operator - followed by the integer literal 9223372036854775808. Since the literal is outside of the range of valid integers, you get the invalid integer literal error. This is not easy to fix in the current architecture, since it would require changing the internal representation of integers used in the parser. The only workaround is to calculate the value (like in @a1880's comment):

type int64 = -9223372036854775807-1..9223372036854775807;

We should probably add suitably defined constants to the library.

guidotack avatar Jul 17 '24 03:07 guidotack

This is due to a quirk in the way integer literals and the - operator are parsed. Like in other languages, -9223372036854775808 is parsed as the negation operator - followed by the integer literal 9223372036854775808. Since the literal is outside of the range of valid integers, you get the invalid integer literal error. This is not easy to fix in the current architecture, since it would require changing the internal representation of integers used in the parser. The only workaround is to calculate the value (like in @a1880's comment):

type int64 = -9223372036854775807-1..9223372036854775807;

We should probably add suitably defined constants to the library.

I suspected as much, FWIW this is not something I need or rely on, I came across this by accident and I think the workaround is fine in case I ever need it

CervEdin avatar Jul 18 '24 11:07 CervEdin

Turns out the fix wasn't too difficult after all (although it's a bit ugly).

guidotack avatar Jul 22 '24 01:07 guidotack