Recent

Author Topic: Keywords in Pascal  (Read 650 times)

Zoran

  • Hero Member
  • *****
  • Posts: 1882
    • http://wiki.lazarus.freepascal.org/User:Zoran
Keywords in Pascal
« on: October 27, 2024, 05:17:34 pm »
I would like to discuss something with the community.
  • See the reserved words list in "User’s Guide"
    The list contains: break, continue, exit, false, true
    As discussed in the forum recently, these are not reserved words, as they can be redefined.
  • See the Reserved Words chapter in "Reference Guide".
    There they are divided to:
    • TP reserved words
    • Reserved words added in Object Pascal
    • The list of "modifiers", which are described something like non-reserved keywords -- "break", "continue" and "exit" are listed there, but not "false" and "true".
Before making a documentation bug report about "User's Guide" listing, I thought it might be worth to talk about it with the community.
I thought it might be better to keep the whole list there, but renaming it to "keywords", and then divide the list in two sections -- reserved and non-reserved.

So, what do you think, should "User's Guide" distinguish keywords from reserved words? Should "Reference Guide" include "false" and "true" in "modifiers"?

440bx

  • Hero Member
  • *****
  • Posts: 4740
Re: Keywords in Pascal
« Reply #1 on: October 27, 2024, 06:29:00 pm »
So, what do you think, should "User's Guide" distinguish keywords from reserved words? Should "Reference Guide" include "false" and "true" in "modifiers"?
I think the "User's guide" and the "Reference guide" should make it very clear that there are language "words" that, while not reserved words, should be considered as if they were because redefining them can cause all kinds of problems.

Essentially, I am in favor of making a list of "keywords" that have the characteristic mentioned above.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8035
Re: Keywords in Pascal
« Reply #2 on: October 27, 2024, 06:47:25 pm »
I think the "User's guide" and the "Reference guide" should make it very clear that there are language "words" that, while not reserved words, should be considered as if they were because redefining them can cause all kinds of problems.

Essentially, I am in favor of making a list of "keywords" that have the characteristic mentioned above.

I'm inclined to agree, but I think the correct term is "reserved words": "keywords" sounds more like BASIC.

At that point the important question is whether reserved words could be tabulated against compiler modes, which hopefully would cover cases where in one mode it could be redefined while in other modes it could not.

And regrettably, we have to consider the variant terminology implicit in full ISO compatibility...

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

Zoran

  • Hero Member
  • *****
  • Posts: 1882
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Keywords in Pascal
« Reply #3 on: October 27, 2024, 07:41:51 pm »
...
I'm inclined to agree, but I think the correct term is "reserved words": "keywords" sounds more like BASIC.
...

What do you mean? The whole point is that there are words which are special in Pascal, but not all of them are "reserved" (some can be redefined, eg. Break, Continue, Exit, False, True).
They cannot be named reserved words, this term remains reserved for the subset of these words which cannot be redefined.

TRon

  • Hero Member
  • *****
  • Posts: 3643
Re: Keywords in Pascal
« Reply #4 on: October 27, 2024, 07:55:26 pm »
So, what do you think, should "User's Guide" distinguish keywords from reserved words? Should "Reference Guide" include "false" and "true" in "modifiers"?
imho there should at least be a clear mention that they exist and distinguish them from "normal" reserved words (no matter how they are named/called).

Currently it is more a hit or miss when you are not familiar and can indeed cause all sort of issues (which imo should also be mentioned clearly).

Thank you for giving this attention.
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8035
Re: Keywords in Pascal
« Reply #5 on: October 27, 2024, 08:44:20 pm »
What do you mean? The whole point is that there are words which are special in Pascal, but not all of them are "reserved" (some can be redefined, eg. Break, Continue, Exit, False, True).
They cannot be named reserved words, this term remains reserved for the subset of these words which cannot be redefined.

Sorry, I was incomplete. "Reserved words" which cannot be redefined and are basically part of the dialect defined by the current mode, vs "predefined".

However there's a massive problem with the predefineds: there's obviously some in the System unit which are unavoidable, (False and True as specific examples since Boolean is a Pascal mandatory type), vs e.g. BeginThread which is not part of any standardised dialect.

But if we assume that String is a reserved word and predefined type, and that the SysUtils unit is almost always imported, how do we deal with the postfix operators that are added whether the programmer intends (or realises) it or not i.e. string.ToInteger and so on?

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

ASerge

  • Hero Member
  • *****
  • Posts: 2337
Re: Keywords in Pascal
« Reply #6 on: October 27, 2024, 09:29:52 pm »
So, what do you think, should "User's Guide" distinguish keywords from reserved words?
Yes
Quote
Should "Reference Guide" include "false" and "true" in "modifiers"?
No. The values False and True are only predefined identifiers, and should be excluded from the User's Guide. The System unit contains many such words.

Zoran

  • Hero Member
  • *****
  • Posts: 1882
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Keywords in Pascal
« Reply #7 on: October 27, 2024, 09:37:29 pm »

Sorry, I was incomplete. "Reserved words" which cannot be redefined and are basically part of the dialect defined by the current mode, vs "predefined".

Okay. I believe we'll all agree that these (and only these) are "Reserved words". The fact is the full list of these depends on compiler mode.

However there's a massive problem with the predefineds: there's obviously some in the System unit which are unavoidable, (False and True as specific examples since Boolean is a Pascal mandatory type), vs e.g. BeginThread which is not part of any standardised dialect.

I surely wouldn't include every identifier defined in System unit (or objpas which is also implicitly imported in some compiler modes). There are words which are part of the language syntax itself, such as class visibility specifiers (private, public, ...), routine or method modifiers (override, static, abstract, stdcall, ...), fundamental type names (boolean, char, ...), flow control statements (break, continue, exit), etc.

These can be used as identifiers in some places, but not always. For example you can not declare class member variable named private (not even if you write var explicitely above it), but you can declare a local variable private in a function's var section.
Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. type
  11.   TSomeClass = class(TObject)
  12.   public
  13.     //private: Boolean; // this does not compile
  14.  
  15.     //var protected: Boolean; // neither does this
  16.   end;
  17.  
  18. implementation
  19.  
  20. procedure SomeProcedure;
  21. var
  22.   private: Boolean;
  23.   Boolean: Char;
  24.   protected: System.Boolean; // Boolean is redeclared above, now you must use System prefix.
  25.   Char: SmallInt;
  26.   Break: Word;
  27. begin
  28.   //
  29. end;
  30.  
  31. end.
  32.  

But if we assume that String is a reserved word and predefined type, and that the SysUtils unit is almost always imported, how do we deal with the postfix operators that are added whether the programmer intends (or realises) it or not i.e. string.ToInteger and so on?

MarkMLl

I surely wouldn't include these identifiers from type helpers declared in sysutils. What is "special" about them, except they are declared in a rtl unit?

Zoran

  • Hero Member
  • *****
  • Posts: 1882
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Keywords in Pascal
« Reply #8 on: October 27, 2024, 09:48:33 pm »
So, what do you think, should "User's Guide" distinguish keywords from reserved words?
Yes
Quote
Should "Reference Guide" include "false" and "true" in "modifiers"?
No. The values False and True are only predefined identifiers, and should be excluded from the User's Guide. The System unit contains many such words.

Okay. I still feel these "special"... You might have a point there, as these can be declared normally:
Code: Pascal  [Select][+][-]
  1. const
  2.   False = Boolean(0);
  3.   True = Boolean(1);
  4.  

But I'd say they are special because the fundamental language constructs (such as "if statement") use these to control flow. The Pascal language specification could not describe control flow without them, right?
« Last Edit: October 27, 2024, 09:52:12 pm by Zoran »

ASerge

  • Hero Member
  • *****
  • Posts: 2337
Re: Keywords in Pascal
« Reply #9 on: October 27, 2024, 09:55:25 pm »
If add False and True, then need also Pointer, Integer, etc.

440bx

  • Hero Member
  • *****
  • Posts: 4740
Re: Keywords in Pascal
« Reply #10 on: October 27, 2024, 10:06:18 pm »
The real problem at this point:

where do you draw the line ?  What makes a "keyword" a "keyword" ?



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

MarkMLl

  • Hero Member
  • *****
  • Posts: 8035
Re: Keywords in Pascal
« Reply #11 on: October 27, 2024, 10:08:57 pm »
The real problem at this point:

where do you draw the line ?  What makes a "keyword" a "keyword" ?

I'd suggest: that it's defined in the ppc binary rather than as a result of an import.

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

VisualLab

  • Hero Member
  • *****
  • Posts: 575
Re: Keywords in Pascal
« Reply #12 on: October 27, 2024, 10:41:34 pm »
So, what do you think, should "User's Guide" distinguish keywords from reserved words? Should "Reference Guide" include "false" and "true" in "modifiers"?
I think the "User's guide" and the "Reference guide" should make it very clear that there are language "words" that, while not reserved words, should be considered as if they were because redefining them can cause all kinds of problems.

Essentially, I am in favor of making a list of "keywords" that have the characteristic mentioned above.

I think similarly. With division into 2 groups: keywords and special words (e.g. false, true).

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1226
Re: Keywords in Pascal
« Reply #13 on: October 28, 2024, 12:12:03 am »
Yes, I’m aware that reserved words are part of the original pascal language definition and all that....

In my opinion all words that do something should Not be Reusable. How is anyone to know what every word that does something is? Expecting everyone to read and memorize a long list of keywords that shouldn’t be used is not reasonable.

Reusing the name obliterates all access to the original functionality within that scope. It’s error prone and not robust.I’ve unintentionally reused keywords before, not realizing that they were keywords. This should not be possible. The only workaround that I can think of is using autocomplete to check if the desired identifier name is already being used for something else. This should not be necessary and would only work if using the unit which references the particular keyword at that moment in time.
Suppose that another unit uses both the unit with original keyword and the unit with the redefined keyword? It starts to get ambiguous. Whatever unit is declared first decides it or it has to be referenced unitname.keyword .. what a mess..

Key words should not work for anything besides what they are intended for. Existing code containing misused keywords can be fixed. There are plenty of identifier names possible without needing to reuse keywords.
« Last Edit: October 28, 2024, 02:24:04 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. 💁🏻‍♀️

 

TinyPortal © 2005-2018