davidgiven / cowgol

A self-hosted Ada-inspired programming language for very small systems.
BSD 2-Clause "Simplified" License
247 stars 20 forks source link

Dimension declarations in 2D arrays reversed? #104

Open ibara opened 3 years ago

ibara commented 3 years ago

Hello.

Here are two programs that ought to be equivalent, one in C and the other in Cowgol: https://github.com/ibara/cowgol-utilities/blob/main/2darray.c https://github.com/ibara/cowgol-utilities/blob/main/2darray.cow

However, they are not equivalent. The output from the C version is what I would expect but that is not the output from the Cowgol version. To get the expected output from the Cowgol version, swap the dimension index declaration on line 6. That is, put XMAX before YMAX in the Cowgol version, even though all other accesses are in the form [y][x].

This is a bit unexpected, so bringing it up in case it is not intentional.

Discovered when writing a Game of Life implementation in Cowgol.

davidgiven commented 3 years ago

Oh, interesting. That's not actually supposed to work! I've just had else on another forum complain about only having single-dimension arrays, too...

I will look into this; it's clearly almost working so it would be good to regularise. Thanks for the test case.

dumblob commented 1 year ago

Did not check the source but dare to ask anyway - is this already supported?

shattered commented 5 months ago

sizeof for 2d arrays also returns unexpected values (bytesof is ok):

var mshorts: uint16[5][2] := { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10} };

sizeof returns 2

davidgiven commented 5 months ago

Given the structure of the initialiser, I would expect sizeof in that case to return 2 --- there are two elements in the outermost array.

What's happening here is that clearly [] is associative in the wrong direction. Which is a shame as it's much easier to parse this way round.