Recent

Author Topic: How many levels of procedure nesting does FreePascal support?  (Read 622 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1077
    • HowTos Considered Harmful?
How many levels of procedure nesting does FreePascal support?
« on: September 14, 2024, 12:55:48 pm »

Code: Pascal  [Select][+][-]
  1. procedure A;
  2.  
  3.   procedure B;
  4.  
  5.     procedure C;
  6.     begin
  7.        ;
  8.     end;
  9.  
  10.   begin
  11.     ;
  12.   end;
  13.  
  14. begin
  15.   ;
  16. end;


Is going to C and deeper something available that I've failed to notice, or does nesting only go from A -> B?

I could do with something like this to help manage runtime settings in component hierachies.
Lazarus 3.0/FPC 3.2.2

dseligo

  • Hero Member
  • *****
  • Posts: 1361
Re: How many levels of procedure nesting does FreePascal support?
« Reply #1 on: September 14, 2024, 01:21:10 pm »
Is going to C and deeper something available that I've failed to notice, or does nesting only go from A -> B?

It sure goes deeper than 3 levels. I think it is limited by stack space. So, if your target is embedded (such as AVR microcontroller) then you have to be careful, but probably on some computer with large memory you don't have to worry about nesting procedures and functions.

Kays

  • Hero Member
  • *****
  • Posts: 604
  • Whasup!?
    • KaiBurghardt.de
Re: How many levels of procedure nesting does FreePascal support?
« Reply #2 on: September 14, 2024, 01:43:15 pm »
maxnesting is 32. This is the maximum lexical nesting. It is, as far as I know, not raised if routines are eliminated by inline or {$optimization autoInline}. Besides, 32 indentation levels make your code quite difficult to read, so I hope your code is generated, not written by hand.
« Last Edit: September 14, 2024, 02:20:03 pm by Kays »
Yours Sincerely
Kai Burghardt

VisualLab

  • Sr. Member
  • ****
  • Posts: 447
Re: How many levels of procedure nesting does FreePascal support?
« Reply #3 on: September 14, 2024, 02:15:16 pm »
Three levels is still "tolerable". But analyzing (or even writing) code above 4 levels is masochism. After all, such code is "chaff". I'll probably only enter it in the "obfuscated code" contest to show that Pascal can beat C in this respect. I hope that in the FCL and LCL libraries there are no nestings above 2 (or at least I haven't come across such cases so far, but I haven't looked through everything in detail).

440bx

  • Hero Member
  • *****
  • Posts: 4531
Re: How many levels of procedure nesting does FreePascal support?
« Reply #4 on: September 14, 2024, 05:34:41 pm »
Nothing wrong with 4 levels of nesting.

Pascal compilers that correctly implement expression parsing have, IIRC, 5 levels (one level for each operator precedence level.)  See the P4 and P5 Pascal compilers among others.

It seems programmers who usually show some concern (or at least desire) to properly localize data seem unable to apply the locality concept to code.

Go figure!. 

However, there is a potential problem with nesting that gets worse as the nesting gets deeper and that is the time it takes to access variables at the higher levels.  It should also be noted that is a problem that is usually very easy to avoid.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Bart

  • Hero Member
  • *****
  • Posts: 5356
    • Bart en Mariska's Webstek
Re: How many levels of procedure nesting does FreePascal support?
« Reply #5 on: September 14, 2024, 07:48:50 pm »
maxnesting is 32.
Actually you cannot nest more than 31 levels:
I could not resist and made such a beast.
Code: [Select]
C:\Users\Bart\LazarusProjecten>fpc nestedprocs.pp
Free Pascal Compiler version 3.2.2 [2021/05/15] for i386
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling nestedprocs.pp
nestedprocs.pp(33,63) Error: function nesting > 31

The procedure in question however only has 30 nested procedures.
I guess that the first level of nesting is the fact that the procedure is nested in the program?

Bart

vfclists

  • Hero Member
  • *****
  • Posts: 1077
    • HowTos Considered Harmful?
Re: How many levels of procedure nesting does FreePascal support?
« Reply #6 on: September 15, 2024, 09:30:56 am »
Is there a syntax to call the procedures in a hierarchical manner, such as A.B.C.D() or does each procedure have to be called globally?

What do I need to do to call the procedures in such a manner? Do I have to create a class or helpers or something along those lines?
Lazarus 3.0/FPC 3.2.2

vfclists

  • Hero Member
  • *****
  • Posts: 1077
    • HowTos Considered Harmful?
Re: How many levels of procedure nesting does FreePascal support?
« Reply #7 on: September 15, 2024, 09:32:05 am »
maxnesting is 32.
Actually you cannot nest more than 31 levels:
I could not resist and made such a beast.
Code: [Select]
C:\Users\Bart\LazarusProjecten>fpc nestedprocs.pp
Free Pascal Compiler version 3.2.2 [2021/05/15] for i386
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling nestedprocs.pp
nestedprocs.pp(33,63) Error: function nesting > 31

The procedure in question however only has 30 nested procedures.
I guess that the first level of nesting is the fact that the procedure is nested in the program?

Bart


Quote
The procedure in question however only has 30 nested procedures.
I guess that the first level of nesting is the fact that the procedure is nested in the program?


That's an interesting considerration
Lazarus 3.0/FPC 3.2.2

alpine

  • Hero Member
  • *****
  • Posts: 1263
Re: How many levels of procedure nesting does FreePascal support?
« Reply #8 on: September 15, 2024, 01:13:19 pm »
Is there a syntax to call the procedures in a hierarchical manner, such as A.B.C.D() or does each procedure have to be called globally?

What do I need to do to call the procedures in such a manner? Do I have to create a class or helpers or something along those lines?
You can't.
Each subsequent procedure must be called from the body of the previous one, otherwise there will be no stack frame in which to execute.
And secondly, it is simply invisible from the outside.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 15641
  • Censorship about opinions does not belong here.
Re: How many levels of procedure nesting does FreePascal support?
« Reply #9 on: September 15, 2024, 01:32:16 pm »
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. procedure level1;
  3.   procedure level2;
  4.     procedure level3;
  5.       procedure level4;
  6.       begin
  7.         WriteLn('Name is: ', {$I %CURRENTROUTINE%});
  8.       end;
  9.     begin
  10.       WriteLn('Name is: ', {$I %CURRENTROUTINE%});
  11.       level4;
  12.     end;
  13.   begin
  14.     WriteLn('Name is: ', {$I %CURRENTROUTINE%});
  15.     level3;
  16.   end;
  17. begin
  18.   WriteLn('Name is: ', {$I %CURRENTROUTINE%});
  19.   level2;
  20. end;
  21.  
  22. begin
  23.   level1;
  24. end.
Since this depends on local procedures the order is guaranteed, as are eye sore and eventually stack problems.
« Last Edit: September 15, 2024, 01:47:11 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018