Page 1 of 1

OpenVMS Pascal : Variable arguments passing

Posted: Mon Jan 17, 2022 4:54 pm
by garyrevell
Hi,

I've looked through the VSI Pascal reference documentation but can't see any mention/examples of variable argument passing. Now it could be that I've missed it or that it's not allowed BUT writeln(...) for example allows the passing of various arguments so wondered if there was a way to do it for user code?

Any pointers/ideas would be great.

Thanks

Gary

Re: OpenVMS Pascal : Variable arguments passing

Posted: Tue Jan 18, 2022 12:44 pm
by willemgrooters
Can you give a code example of what you like to achieve: variable number of arguments (and if so, how many), arguments that will be modified? (the latter is simple: specify the argument as 'var' :
Procedure (readonly, var to_be_modified)

Re: OpenVMS Pascal : Variable arguments passing

Posted: Tue Jan 18, 2022 1:28 pm
by jreagan
Look at the LIST attribute in chapter 10, and ARGUMENT_LIST_LENGTH/ARGUMENT in chapter 8. It isn't completely flexible with accepting multiple types so you can't do what WRITELN does. Things like WRITELN are built directly into the compiler. They are statements, not routine calls.

Re: OpenVMS Pascal : Variable arguments passing

Posted: Wed Jan 19, 2022 10:14 am
by arne_v
Example similar to what is in the manual:

Code: Select all

program va(input,output);

procedure demo(v : [LIST] integer);

var
   i : integer;

begin
   for i := 1 to argument_list_length(v) do begin
      write(' ',argument(v,i):1);
   end;
   writeln;
end;

begin
   demo(1);
   demo(1, 2);
   demo(1, 2, 3);
end.
Added in 9 hours 57 minutes 20 seconds:
If you want to do C style programming:

Code: Select all

program va2(input,output);

type
   pstring = varying[255] of char;
   integer_ptr = ^integer;
   pstring_ptr = ^pstring;

procedure demo(v : [LIST] pointer);

var
   p : pointer;
   ip : integer_ptr;
   sp : pstring_ptr;
   iv : integer;
   sv : pstring;
   i : integer;


begin
   for i := 1 to argument_list_length(v) do begin
      p := argument(v,i);
      if odd(i) then begin
         ip := p;
         iv := ip^;
         write(' ',iv:1);
      end else begin
         sp := p;
         sv := sp^;
         write(' ',sv);
      end;
   end;
   writeln;
end;

var
   one, three, five : [VOLATILE] integer;
   two, four : [VOLATILE] varying[255] of char;

begin
   one := 1;
   two := 'BB';
   three := 3;
   four := 'DDDD';
   five := 5;
   demo(address(one));
   demo(address(one), address(two));
   demo(address(one), address(two), address(three));
   demo(address(one), address(two), address(three), address(four));
   demo(address(one), address(two), address(three), address(four), address(five));
end.
Added in 54 minutes 27 seconds:
Or a little nicer:

Code: Select all

program va3(input,output);

type
   pstring = varying[255] of char;
   integer_ptr = ^integer;
   pstring_ptr = ^pstring;
   wrapped_typ = (integer_typ, pstring_typ);
   wrapper = record
                typ : wrapped_typ;
                adr : pointer;
             end;

function iwrap(p : pointer) : wrapper;

var
   res : wrapper;

begin
   res.typ := integer_typ;
   res.adr := p;
   iwrap := res;
end;

function swrap(p : pointer) : wrapper;

var
   res : wrapper;

begin
   res.typ := pstring_typ;
   res.adr := p;
   swrap := res;
end;

function iunwrap(w : wrapper) : integer;

var
   p : integer_ptr;

begin
   p := w.adr;
   iunwrap := p^;
end;

function sunwrap(w : wrapper) : pstring;

var
   p : pstring_ptr;

begin
   p := w.adr;
   sunwrap := p^;
end;

procedure demo(v : [LIST] wrapper);

var
   w : wrapper;
   i : integer;

begin
   for i := 1 to argument_list_length(v) do begin
      w := argument(v,i);
      case w.typ of
         integer_typ:
            begin
               write(' ',iunwrap(w):1);
            end;
         pstring_typ:
            begin
               write(' ',sunwrap(w));
            end;
      end;
   end;
   writeln;
end;

var
   one, three, five : [VOLATILE] integer;
   two, four : [VOLATILE] varying[255] of char;

begin
   one := 1;
   two := 'BB';
   three := 3;
   four := 'DDDD';
   five := 5;
   demo(iwrap(address(one)));
   demo(iwrap(address(one)), swrap(address(two)));
   demo(iwrap(address(one)), swrap(address(two)), iwrap(address(three)));
   demo(iwrap(address(one)), swrap(address(two)), iwrap(address(three)), swrap(address(four)));
   demo(iwrap(address(one)), swrap(address(two)), iwrap(address(three)), swrap(address(four)), iwrap(address(five)));
end.

Re: OpenVMS Pascal : Variable arguments passing

Posted: Mon Jan 24, 2022 10:00 am
by garyrevell
Thanks everyone, and especially Arne for the example program

Best regards

Gary