Recent

Author Topic: Procedure variables and scope in virtual pascal  (Read 1751 times)

bryce

  • Guest
Procedure variables and scope in virtual pascal
« on: November 01, 2019, 10:43:50 am »
Hi, having some trouble with procedural variables and their scope.

Heres some example code:
Code: Pascal  [Select][+][-]
  1. PROGRAM Test;
  2. TYPE
  3.    t_Action = PROCEDURE;
  4.  
  5. PROCEDURE Menu (Action : t_Action);
  6.    BEGIN
  7.       Action;
  8.    END;
  9.  
  10. PROCEDURE CallMenu;
  11.    VAR
  12.       Value : LONGINT;
  13.       Action : t_Action;
  14.    
  15.    PROCEDURE DoSomething;
  16.       BEGIN
  17.          WRITE('Value: ',Value);
  18.       END;
  19.  
  20.    BEGIN
  21.       Value := 1;
  22.       @Action := @DoSomething;
  23.       Menu(Action);
  24.    END;
  25.  
  26. BEGIN
  27.    CallMenu;
  28.    READLN;
  29. END.


I would expect the output to be 1, but instead its a random value, as if I have not initialized Value. How do I initialize value in a way the nested procedure can still see it.


I have tried looking in the sources for a solution, the only thing I could find was some assembly code that 'preserves local variables', but I had no idea how it worked or if it was even relevant to what I want to do.


Any help or resources would be greatly appreciated  :)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8317
Re: Procedure variables and scope in virtual pascal
« Reply #1 on: November 01, 2019, 11:30:48 am »
Does this relate to Virtual Pascal or Free Pascal (FPC)?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

bryce

  • Guest
Re: Procedure variables and scope in virtual pascal
« Reply #2 on: November 01, 2019, 11:34:16 am »
I am using virtual pascal, and have compiled this using virtual pascal, but it may work the same in free pascal.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8317
Re: Procedure variables and scope in virtual pascal
« Reply #3 on: November 01, 2019, 11:44:35 am »
This forum is specifically for Free Pascal and related projects.
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

bryce

  • Guest
Re: Procedure variables and scope in virtual pascal
« Reply #4 on: November 01, 2019, 11:48:03 am »
This forum is specifically for Free Pascal and related projects.

Free pascal is very similar to virtual pascal. As there are no resources available online for virtual pascal I was hoping my mistake could be identified or some guidance could be given.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12107
  • FPC developer.
Re: Procedure variables and scope in virtual pascal
« Reply #5 on: November 01, 2019, 11:59:42 am »
I made your example working for Free Pascal, if Virtual Pascal is simular enough you can port it to Virtual Pascal

Code: Pascal  [Select][+][-]
  1.  
  2.     PROGRAM Test;
  3. {$modeswitch nestedprocvars}  
  4. // docs see https://www.freepascal.org/docs-html/ref/refse17.html
  5.     TYPE
  6.        t_Action = PROCEDURE is nested;
  7.      
  8.     PROCEDURE Menu (Action : t_Action);
  9.        BEGIN
  10.           Action;
  11.        END;
  12.      
  13.     PROCEDURE CallMenu;
  14.        VAR
  15.           Value : LONGINT;
  16.           Action : t_Action;
  17.        
  18.        PROCEDURE DoSomething;
  19.           BEGIN
  20.              WRITE('Value: ',Value);
  21.           END;
  22.      
  23.        BEGIN
  24.           Value := 1;
  25.           Action := @DoSomething;
  26.           Menu(Action);
  27.        END;
  28.      
  29.     BEGIN
  30.        CallMenu;
  31.        READLN;
  32.     END.
  33.  
  34.  

P.s. afaik this is a feature that VP doesn't have, and it won't work. The fact that you had to add so many @'s was a dead give away that the type safety was violated, and thus unreliable.

The problem is that your procedure is nested, but you use a "global procedure" type declaration when declarating the procedural type (and VP has no other one to my best knowledge, no way to define it as nested) FPC does, the "is nested" in the example.

p.s.2 VP afaik still has a facebook group somewhere that provides support. I suggest you try there or switch to Free Pascal, as you probably should anyway.
« Last Edit: November 02, 2019, 02:21:36 pm by marcov »

 

TinyPortal © 2005-2018