Oleg-N-Cher/OfrontPlus

Wrong translation of index for mixed-type arrays

Closed this issue · 5 comments

GameHunter reports:

MODULE Test;

TYPE
  Arr = ARRAY OF ARRAY 4 OF INTEGER;

PROCEDURE Ex ( VAR a: Arr );
  BEGIN
    a[0,1]:= -1
  END Ex;

END Test.
typedef
	INTEGER *Test_Arr[4];

static void Test_Ex (INTEGER *a, LONGINT a__len)
{
	a[0][1] = -1;
}

Ofront+ swears at the two-dimensional array index, which is passed as a pointer, i.e., a one-dimensional array. The type of mixed array is calculated incorrectly here, which is a mixture of Array and DynArr types, and, accordingly, indexing is incorrect because the array is partially dynamic, and the index must be calculated using Horner schema.

I propose this fix in the procedure OPV.design:

image

image

This bug is inherited from the original Ofront, also present in CPfront.

jtempl/ofront#46

In ofront (and probably also in Ofront+), I think, the problem is caused by having a named type (Arr) in the example. In DeclareObj the LOOP is terminated when a named type is found (...strobj^.name # ""). However, a named DynArr type must not terminate this loop.
I guess you have to add an AND condition, but without changing the rest of the IF condition, i.e. the parenthesis structure.
IF ((typ^.strobj # NIL) & (typ^.strobj^.name # "")) ...
to
IF ((typ^.strobj # NIL) & (typ^.strobj^.name # "") & (comp # DynArr)) ...

All right, if you think that's the right condition, then I agree.

The remark is not on the essence of your proposal, but on the form. Outside parentheses are unnecessary. And in the previous version of the condition:

IF ((typ^.strobj # NIL) & (typ^.strobj^.name # OPT.null)) OR (form = NoTyp) OR (comp = Record) THEN EXIT

these parentheses are also unnecessary, because the & operation takes precedence over OR.

The extra parentheses are not reqired indeed. The change should be tested very carefully. May be some extra debugging output would help to detect places where the change has an effect on existing sources.

Thank you, Josef!

I tested your code as best I could and decided to add it to the master branch. If any problems come up, I'll let you know.