Negate floats as -0 - x and parenthesize ** under prefix operators
Why
The JS IR represents a negation as a subtraction from zero, and the printer printed every 0 - x as -x. That is wrong for a float subtraction: 0. -. x is 0 when x is 0., but -x is -0. A float negation is now a subtraction from -0., which equals -x for every x, and only that is printed as - x. An int negation stays 0 - x; it is printed as -x only as the operand of a bitwise operator, such as the | 0 of every int subtraction, which drops the sign of a zero. Prefix operators also printed their operand at the level of **, so a power had no parentheses there, which JavaScript rejects. This concerned a float negation, ~ on a bigint, typeof and await; the operands of all prefix operators now go through unary_operand. Where 0 - x is printed as -x, an x that prints with a leading minus is kept apart by a space, as found by prints_with_leading_minus; of the subtractions, only -0 - x prints with a leading minus now.
modifiedJS subtraction from zero
let fromLiteral = x => 0. -. xlet fromInt = x => Float.fromInt(0) -. xlet negate = x => -.xlet negateInt = (x: int) => -x
js
modifiedJS subtraction from a negative zero literal
let f = x => -0. -. xjs
modifiedJS power under a prefix operator
let negate = (a: float, b: float) => -.(a ** b)let bitNot = (a: bigint, b: bigint) => ~~~(a ** b)let typeOf = (a: float, b: float) => typeof(a ** b)let wait = async (a: float, b: float) => await Obj.magic(a ** b)
js
Review
- Why decide the int case by the enclosing operator? Inlining can put an int literal into a float subtraction, so a left operand
0does not tell an int from a float, but the sign of a zero never survives a bitwise operator. - Which checked-in outputs change? Only float_add_fold_test.mjs: a subtraction from a zero int such as
- -5now prints as0 - -5. Its case for a negative bigint, which this would turn into0 - -5nand a "Cannot mix BigInt" error, negates the bigint with-.instead.