On 2010-12-17, at 5:13 PM, Maxime Chevalier-Boisvert wrote:
Marc, would it be easy to make it so that negative constants in the AST (eg: -1) show up as negative number constants, instead of showing up as the negation operator applied to a positive constant? It's posing me a minor issue at the IIR level.
I would think the constant folder would easily take care of that. JavaScript (like many languages) does not have negative constants. The parser should not make such a transformation because we might use the parser for other things (for example program analysis where we care that "-" is actually an executed operation).
Here's an interesting related issue. In a programming language with fixed precision integers, and signed integer constants, how can the most negative integer be parsed? For example on a 32 bit machine, the most negative integer is -2147483648. But that integer can't be generated as -(2147483648) because 2147483648 does not fit in a signed 32 bit integer. The trick is to accumulate the number as a negative number, i.e. for each digit d, the computation is n = n*10 - d. So we get:
n = 0 n = -2 n = -21 n = -214 n = -2147 n = -21474 n = -214748 n = -2147483 n = -21474836 n = -214748364 n = -2147483648
So the number always fits in a 32 bit signed integer. To share the code for the positive and negative cases, the negative value is always computed, and if the sign was *not* "-" then the negation is computed (an exception is raised if the number was -2147483648, because the number +2147483648 does not fit in a 32 bit signed integer).
This trick can be used in other contexts where a greater range of integers is required.
Marc