Recent

Author Topic: Nested declarations inside advanced record  (Read 22995 times)

TRon

  • Hero Member
  • *****
  • Posts: 3995
Re: Nested declarations inside advanced record
« Reply #90 on: January 17, 2025, 12:08:53 am »
And what about this valid program, I don't understand what's going on here:

Let's try:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$h+}
  4.  
  5. procedure a();             // -> scope a
  6.   var c: integer = 1;
  7.  
  8.   procedure b();           // -> scope b
  9.   var c: integer = 2;
  10.   begin
  11.     writeln('scope b: ', c);
  12.   end;                     // <- scope b
  13.  
  14. begin                      // this scope a
  15.   writeln('scope a: ', c);
  16.   b();
  17.   writeln('scope a: ', c);
  18.   b();
  19. end;                       // <- scope a
  20.  
  21. begin                      // -> scope main
  22.   a();
  23.   readln;
  24. end.                       // <- scope main
  25.  

Code: Bash  [Select][+][-]
  1. scope a: 1
  2. scope b: 2
  3. scope a: 1
  4. scope b: 2
  5.  
I do not have to remember anything anymore thanks to total-recall.

jamie

  • Hero Member
  • *****
  • Posts: 6802
Re: Nested declarations inside advanced record
« Reply #91 on: January 17, 2025, 01:38:10 am »
Still at it I see. All it takes is one to destroy it for everyone else, be it this thread or other threads currently boiling.

 I've be kicking my heals lately if I should upgrade from D 11 to 12, not sure if the coast is worth it since that too seems to have some minor issues.
 But with his recent activity If I had to choose between two evals I think I'll upgrade.

  If there are individuals campaigning here to discourage the use of fpc/Lazarus, it's working.

 Jamie
The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Nested declarations inside advanced record
« Reply #92 on: January 17, 2025, 02:13:24 am »
@lainz,

@TRon provided you a good explanation.

I want to offer one derived from the code you posted that is more complete.  Once you understand @TRon's explanation, tackle this one:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$h+}
  2.  
  3. program TRonScopes;
  4.  
  5. procedure a(ParameterA : integer);        // -> scope a
  6.          { ^ the procedure block and its corresponding scope start at the     }
  7.          {   open parenthesis.  This is important for 2 reasons: 1. it causes }
  8.          {   ParameterA NOT to be available outside of procedure "a" and 2.   }
  9.          {   it causes the parameter to be in the same scope as all other     }
  10.          {   definitions contained in the procedure block.                    }
  11.  
  12.   { ParameterA, "c", "k", "b" and "v" are all in the same scope which is what }
  13.   { allows procedure "a" to use them without qualifier.                       }
  14.  
  15.   var
  16.     c: integer = 1;
  17.  
  18.   const
  19.     k = 100;
  20.  
  21.  
  22.   { ------------------------------------------------------------------------- }
  23.   { nested procedures                                                         }
  24.  
  25.   procedure b(ParameterB : integer);      // -> scope b
  26.            { ^ start of procedure block and scope (nested in scope of "a")    }
  27.   var
  28.     d: integer = k;     { "k" from the outer scope is visible                 }
  29.  
  30.     c: integer = 2;
  31.                 { ^ "c" become visible after the ";", not before              }
  32.  
  33.     { note that "x" and "z" below are not visible yet even though they are in }
  34.     { the same scope (defined by "b"'s procedure block)                       }
  35.  
  36.   const
  37.     x = 3; { x is visible after the ";" but not before                        }
  38.     z = x; { this is ok because "x" is visible                                }
  39.  
  40.     { the definition below is erroneous and the compiler knows that because   }
  41.     { "y" has not been fully defined by the time it is used.                  }
  42.  
  43.     // y = y;  { uncomment to see the error "identifier not found"            }
  44.  
  45.   begin     { scopes "a" and "b" are visible                                  }
  46.  
  47.     d := d;                                // gets rid of "not used" hint
  48.  
  49.     writeln('scope b: ', ParameterA);      // ParameterA = 7
  50.     writeln('scope b: ', ParameterB);      // ParameterB = 3 and 33
  51.     writeln('scope b: ', c);               // c = 2
  52.   end;      { end of scope b                                                  }
  53.  
  54.   { "v" is in the same scope as the other identifiers defined in procedure "a"}
  55.   { but, because of _where_ it is defined, it is not visible to procedure "b" }
  56.   { IOW, scope <> visibility                                                  }
  57.   var
  58.     v : integer = 2;  { procedure "b" cannot see (thus not use) this variable }
  59.  
  60.   { end of nested procedures                                                  }
  61.   { ------------------------------------------------------------------------- }
  62.  
  63. begin       { scope "a" is visible ("b" is not)                               }
  64.  
  65.   writeln('scope a: ', c);                 // c = 1
  66.   b(3);
  67.   writeln('scope a: ', c);                 // c = 1
  68.   b(33);
  69.  
  70.   { procedure "b" is in the scope of procedure "a" BUT the ParameterB is NOT  }
  71.  
  72.   { uncomment the following to see "identifier not found" which proves that   }
  73.   { the parameter is in a different scope than the procedure name.            }
  74.  
  75.   // ParameterB := 1;
  76. end;                                       // <- scope a
  77.  
  78.  
  79. begin                                      // -> scope main/TRonScopes
  80.   a(7);
  81.   readln;
  82. end.                                       // <- scope main/TRonScopes
  83.  
This one includes parameters and identifier visibility.  It's just an "extension" of the code you presented.




But with his recent activity If I had to choose between two evals I think I'll upgrade.
Let them know you disapprove of how they implement the "with" statement, no doubt they'll "correct" that for you...

Happy upgrades to you!
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

lainz

  • Hero Member
  • *****
  • Posts: 4689
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Nested declarations inside advanced record
« Reply #93 on: January 17, 2025, 03:21:30 am »
Thanks. So is the same as in Javascript with the scopes.
@440bx why there's v and y? Seems that you wanted to type v in both places or something that I don't understand...

Edit: now I understand y = y is not possible. Done! The same if it was v = v in that scope...
« Last Edit: January 17, 2025, 03:23:58 am by lainz »

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Nested declarations inside advanced record
« Reply #94 on: January 17, 2025, 04:15:30 am »
Edit: now I understand y = y is not possible. Done! The same if it was v = v in that scope...
You got it.


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

Warfley

  • Hero Member
  • *****
  • Posts: 1865
Re: Nested declarations inside advanced record
« Reply #95 on: January 18, 2025, 12:27:44 am »
read some books!
You brought up a book and are now contradicting what is written black on white without any room for interpretation in the very same book you brought up.

I think I don't need book advice from you :)

And you know whats better than any list of books on compiler construction? Working on actual real world compilers. I worked with the sources of clang, fpc and a some compilers for domain specific languages used in the automitive industry. I worked with compilers used by millions and deployed in code for billion dollar projects.
You bring up some educational visualizations for a reference compiler that no one ever used for any productive code.

Books are great to learn the basics, but if you never go out and work on real world code, you can never go beyond a very shallow understanding.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1325
Re: Nested declarations inside advanced record
« Reply #96 on: January 18, 2025, 01:03:01 am »
Still at it I see. All it takes is one to destroy it for everyone else, be it this thread or other threads currently boiling.
I've be kicking my heals lately if I should upgrade from D 11 to 12, not sure if the coast is worth it since that too seems to have some minor issues.
 But with his recent activity If I had to choose between two evals I think I'll upgrade.
 If there are individuals campaigning here to discourage the use of fpc/Lazarus, it's working.
 Jamie
Speaking of which I recently came across this thread in the mailing lists. https://www.mail-archive.com/fpc-pascal@lists.freepascal.org/msg57220.html
The discussion led to some bad ideas like “let’s change lazarus to be as bad as other projects that are popular ”. Finally nickolay pointed out that fpc and Lazarus are better than other languages he has used. So what is the problem then? {Apart from pascal usage being suppressed by wealthy people who hate it, which we can do nothing about}

Many people in our community are antisocial to put it mildly.. Why can’t programming be fun like it used to be? I learned so much just from chatting about pascal and other topics with Jamie and other people too. It’s like all the pascal programmers I used to socialize with have disappeared and the majority are just not too active forum accounts.

Then there are angry threads like this one. Is this the closest that we get to fun around here?

I’ll never forget the time long ago when a thread discussion which was quite pleasant and had many participants was suddenly locked upon the whim of a moderator who decided it was off topic. At least that thread was a positive experience that didn’t make the community look bad. I can’t say the same for many threads I’ve seen since...

I’ll never get over my disappointment about the lively discussion that was locked and lost opportunity to make friends and learn new things that will never happen again because apparently this type of thread is the best we can do now. We have become spectators of a fight rather than accomplishing something useful for making people want to use fpc.

Our community is not making using Fpc/Lazarus seem fun, it often seems More like angry frustrated people getting mad at each other. Who would want to be part of that?

« Last Edit: January 18, 2025, 01:27:53 am by Joanna from IRC »
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Nested declarations inside advanced record
« Reply #97 on: January 18, 2025, 02:13:29 am »
I think I don't need book advice from you :)
You need a lot more than advice.  As I said, educate yourself, hopefully after you gain some basic  knowledge you'll stop spreading your ignorance.  Start with PBH's On Pascal Compilers, you really need to get a grasp of the basics.

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

PascalDragon

  • Hero Member
  • *****
  • Posts: 5870
  • Compiler Developer
Re: Nested declarations inside advanced record
« Reply #98 on: January 18, 2025, 04:57:51 pm »
I’ll never forget the time long ago when a thread discussion which was quite pleasant and had many participants was suddenly locked upon the whim of a moderator who decided it was off topic. At least that thread was a positive experience that didn’t make the community look bad. I can’t say the same for many threads I’ve seen since...

If a thread is about XYZ and then starts to be about ABC then that drifting off must be stopped, because when someone searches for XYZ only to have to wade through pages of ABC then that person isn't helped. More so if after pages of ABC XYZ isn't even solved yet.

I’ll never get over my disappointment about the lively discussion that was locked and lost opportunity to make friends and learn new things that will never happen again because apparently this type of thread is the best we can do now. We have become spectators of a fight rather than accomplishing something useful for making people want to use fpc.

That's how forums are supposed to operate. If you don't like that, well, though luck.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1325
Re: Nested declarations inside advanced record
« Reply #99 on: January 20, 2025, 12:29:42 am »
@pascaldragon
It’s possible to move a conversation that has gone off topic to a new thread instead of ruining it. I’ve seen it done before so I know it can be done.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

PascalDragon

  • Hero Member
  • *****
  • Posts: 5870
  • Compiler Developer
Re: Nested declarations inside advanced record
« Reply #100 on: January 20, 2025, 10:33:14 pm »
@pascaldragon
It’s possible to move a conversation that has gone off topic to a new thread instead of ruining it. I’ve seen it done before so I know it can be done.

It's possible, yes, but it's up to the moderator to decide how they want to handle that.

 

TinyPortal © 2005-2018