[Oberon] Runtime array in Oberon-07

Ivan Denisov d.ivan.krsk at gmail.com
Tue Oct 18 17:07:48 CEST 2022


Hi


I made some list realization of dynamic arrays.

MODULE Dyn;

CONST size = 100;

TYPE
     Array* = POINTER TO ArrayDesk;
     ArrayDesk = RECORD a: ARRAY size OF REAL; next: Array END;

PROCEDURE New* (): Array;
VAR a: Array;
BEGIN
  NEW(a);
  RETURN a
END New;

PROCEDURE Put* (a: Array; pos: INTEGER; val: REAL);
VAR tmp: Array; i: INTEGER;
BEGIN
     tmp := a;
  FOR i := 1 TO pos DIV size DO
   IF tmp.next = NIL THEN NEW(tmp.next) END;
   tmp := tmp.next
  END;
  tmp.a[pos MOD size] := val
END Put;

PROCEDURE Get* (a: Array; pos: INTEGER): REAL;
VAR tmp: Array; i: INTEGER;
BEGIN
  tmp := a;
  FOR i := 1 TO pos DIV size DO
   IF tmp.next = NIL THEN NEW(tmp.next) END;
   tmp := tmp.next
  END;
  RETURN tmp.a[pos MOD size]
END Get;

END Dyn.


Then you can use


MODULE Init;

IMPORT Log, Dyn;

VAR a: Dyn.Array;

PROCEDURE Do;
BEGIN
  a := Dyn.New();
  Dyn.Put(a, 1500, 3.14);
  Log.Real(Dyn.Get(a, 1500)); Log.Ln
END Do;

BEGIN
  Do
END Init.


https://online.oberon.org/model/311


Kind regards,

Ivan Denisov



18.10.2022 21:37, Deadmarshal пишет:
> Hi,
>
> I've read that in Oberon-07 we can't have a pointer to an array, and I 
> was wondering how we can set the size of an array at runtime. I want 
> to do something similar to the below code snippet:
>
> MODULE Test;
> (*Oberon-2 compatible*)
> IMPORT In,Out;
>
> VAR
>   PA:POINTER TO ARRAY OF INTEGER;
>   N,Input,I:INTEGER;
>
> BEGIN
>   Out.String("Size of array: "); Out.Ln;
>   In.Int(N);
>   NEW(PA,N);
>   Out.String("Enter Items: "); Out.Ln;
>   FOR I := 0 TO N-1 DO
>     In.Int(Input);
>     PA[I] := Input;
>   END;
>   FOR I := 0 TO N-1 DO
>     Out.Int(PA[I],0);
>     Out.Char(' ');
>   END;
>   Out.Ln;
> END Test.
>
>
> --
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> https://lists.inf.ethz.ch/mailman/listinfo/oberon


More information about the Oberon mailing list