bmx-ng / bcc

A next-generation bcc parser for BlitzMax
zlib License
33 stars 12 forks source link

BCC allows to access multi-dim arrays with less/more dimensions #649

Open GWRon opened 10 months ago

GWRon commented 10 months ago

@MidimasterSoft wrote at discord:

There was a question on SyntaxBomb about arrays. During the answer I found out, that this wrong code line does not raise an error message in BlitzMax NG DEBUG and RELEASE mode:


Global Test:Int[1, 1, 2]

' now call the array with one dimension too small, but no error message: Test[0,0]=4

Print Test[0,0,0] + " " + Test[0,0,1]


I asked for the output of vanilla:

Building untitled1 Compiling:test.bmx Compile Error: Incorrect number of array dimensions [C:/code/test.bmx;4;1] Build Error: failed to compile C:/code/test.bmx Process complete



Maybe bcc could spit out a similar error (maybe even with "given vs required")
GWRon commented 10 months ago

The generated code indicates that the C code uses "1 dimensional arrays" and simply adds up the passed indices of the multi-dimensional access to a single index.

Local int_array:Int[1,1,2]
int_array[0,0] = 4
int_array[1,0,0] = 4

becomes

        BBARRAY bbt_int_array=bbArrayNew("i", 3, 1, 1, 2);
        BBUINT* bbt_=((BBARRAY)bbt_int_array)->scales + 1;
        ((BBINT*)BBARRAYDATA(bbt_int_array,1))[(*(bbt_)) * 0U + 0U]=4;
        BBUINT* bbt_2=((BBARRAY)bbt_int_array)->scales + 1;
        ((BBINT*)BBARRAYDATA(bbt_int_array,1))[(*(bbt_2)) * 1U + (*(bbt_2+1)) * 0U + 0U]=4;

Does not help much though -- regarding the BCC checks, but explains why it is "working" (as long as you just leave out dimensions / aka setting their index indirectly to "0")