B-Lang-org / bsc

Bluespec Compiler (BSC)
Other
939 stars 143 forks source link

PPrint instance for infix operators incorrect #571

Open mieszko opened 1 year ago

mieszko commented 1 year ago

Pretty-printing BH code that has infix operators at different precedences is bugged.

Example input:

package Foo where

foo :: Integer
foo = 1 + 2 * 3 + 4

bar :: Integer
bar = 1 + (2 * 3) + 4

baz :: Integer
baz = (1 + 2) * 3 + 4

qux :: Integer
qux = 1 + 2 * (3 + 4)

Compiling with -dparsed yields:

package Foo where {
foo :: Integer;
foo =  1 + 2 * 3 + 4;;

bar :: Integer;
bar =  1 + 2 * 3 + 4;;

baz :: Integer;
baz =  1 + 2 * 3 + 4;;

qux :: Integer;
qux =  1 + 2 * 3 + 4;
}

which loses all the precedence information from the parens, so if it's fed back to bsc it produces different semantics.

This is related to #568 (although that is a separate CSyntax cons); they should probably be fixed together.

RyanGlScott commented 8 months ago

The same issue also affects BSV. Given this code:

package Foo;

Integer foo;
foo = 1 + 2 * 3 + 4;

Integer bar;
bar = 1 + (2 * 3) + 4;

Integer baz;
baz = (1 + 2) * 3 + 4;

Integer qux;
qux = 1 + 2 * (3 + 4);

endpackage

Compiling this with -dparsed yields:

package Foo;
Integer foo;
foo = 1 + 2 * 3 + 4;

Integer bar;
bar = 1 + 2 * 3 + 4;

Integer baz;
baz = 1 + 2 * 3 + 4;

Integer qux;
qux = 1 + 2 * 3 + 4;

endpackage: Foo