Recent

Author Topic: An Idea for a helpful utility?  (Read 624 times)

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 689
An Idea for a helpful utility?
« on: November 09, 2025, 07:19:35 pm »
The code in fpcsrc is written to support a broad variety of use cases and conditions, and that makes it widely useful across many different needs and platforms. But it also makes it hard to read and study for a casual programmer like me.  I would like to see a utility that inputs a unit of source code from fpcsrc  and specializes it to a particular use case by removing all of the irrelevant {$If...} {$Else...} {$Endif...} clauses, with the result placed in a new file.  The purpose of this would be simply to make the source code more accessable/readable for study. 

Here is an example of what I mean.  This code if from fpcsrc:

Code: Pascal  [Select][+][-]
  1. {$IFNDEF FPC_DOTTEDUNITS}
  2. unit fpjson;
  3. {$ENDIF FPC_DOTTEDUNITS}
  4.  
  5. {$i fcl-json.inc}
  6. {$modeswitch advancedrecords}
  7.  
  8. interface
  9.  
  10. {$IFDEF FPC_DOTTEDUNITS}
  11. uses
  12.   {$IFNDEF PAS2JS}
  13.   System.Variants,
  14.   {$ENDIF}
  15.   {$IFDEF PAS2JS}
  16.   JS, System.RtlConsts, System.Types,
  17.   {$ENDIF}
  18.   System.SysUtils,
  19.   System.Classes,
  20.   System.Contnrs;
  21. {$ELSE FPC_DOTTEDUNITS}
  22. uses
  23.   {$IFNDEF PAS2JS}
  24.   variants,
  25.   {$ENDIF}
  26.   {$IFDEF PAS2JS}
  27.   JS, RTLConsts, Types,
  28.   {$ENDIF}
  29.   SysUtils,
  30.   classes,
  31.   contnrs;
  32. {$ENDIF FPC_DOTTEDUNITS}
  33.  
  34. type
  35.   TJSONtype = (jtUnknown, jtNumber, jtString, jtBoolean, jtNull, jtArray, jtObject);
  36.   TJSONInstanceType = (
  37.     jitUnknown,
  38.     jitNumberInteger,
  39.     {$IFNDEF PAS2JS}
  40.     jitNumberInt64,
  41.     jitNumberQWord,
  42.     {$ELSE}
  43.     jitNumberNativeInt,
  44.     {$ENDIF}
  45.     jitNumberFloat,
  46.     jitString,
  47.     jitBoolean,
  48.     jitNull,
  49.     jitArray,
  50.     jitObject);
  51.   TJSONFloat = Double;
  52.   TJSONStringType = {$IFNDEF PAS2JS}UTF8String{$else}string{$ENDIF};
  53.   TJSONUnicodeStringType = Unicodestring;
  54.   {$IFNDEF PAS2JS}
  55.   TJSONCharType = AnsiChar;
  56.   PJSONCharType = ^TJSONCharType;
  57.   TJSONVariant = variant;
  58.   TFPJSStream = TMemoryStream;
  59.   TJSONLargeInt = Int64;
  60.   {$else}
  61.   TJSONCharType = Char;
  62.   TJSONVariant = jsvalue;
  63.   TFPJSStream = TJSArray;
  64.   TJSONLargeInt = NativeInt;
  65.   {$ENDIF}
  66.   TFormatOption = (foSingleLineArray,   // Array without CR/LF : all on one line
  67.                    foSingleLineObject,  // Object without CR/LF : all on one line
  68.                    foDoNotQuoteMembers, // Do not quote object member names.
  69.                    foUseTabchar,        // Use tab characters instead of spaces.
  70.                    foSkipWhiteSpace,    // Do not use whitespace at all
  71.                    foSkipWhiteSpaceOnlyLeading,   //  When foSkipWhiteSpace is active, skip whitespace for object members only before :
  72.                    foForceLF,            // On Windows, use this to force use of LF instead of CR/LF
  73.                    foFormatFloat         // Format floats using floattostr
  74.                    );
  75.   TFormatOptions = set of TFormatOption;  

This is what it looks like when it has been "specialized" to not dotted units and not PAS2JS:
Code: Pascal  [Select][+][-]
  1.  
  2. unit fpjson;
  3.  
  4. {$i fcl-json.inc}
  5. {$modeswitch advancedrecords}
  6.  
  7. interface
  8. uses
  9.   variants,
  10.   SysUtils,
  11.   classes,
  12.   contnrs;
  13.  
  14. type
  15.   TJSONtype = (jtUnknown, jtNumber, jtString, jtBoolean, jtNull, jtArray, jtObject);
  16.   TJSONInstanceType = (
  17.     jitUnknown,
  18.     jitNumberInteger,
  19.     jitNumberInt64,
  20.     jitNumberQWord,
  21.     jitNumberFloat,
  22.     jitString,
  23.     jitBoolean,
  24.     jitNull,
  25.     jitArray,
  26.     jitObject);
  27.   TJSONFloat = Double;
  28.   TJSONStringType = UTF8String;
  29.   TJSONUnicodeStringType = Unicodestring;
  30.   TJSONCharType = AnsiChar;
  31.   PJSONCharType = ^TJSONCharType;
  32.   TJSONVariant = variant;
  33.   TFPJSStream = TMemoryStream;
  34.   TJSONLargeInt = Int64;
  35.   TFormatOption = (foSingleLineArray,   // Array without CR/LF : all on one line
  36.                    foSingleLineObject,  // Object without CR/LF : all on one line
  37.                    foDoNotQuoteMembers, // Do not quote object member names.
  38.                    foUseTabchar,        // Use tab characters instead of spaces.
  39.                    foSkipWhiteSpace,    // Do not use whitespace at all
  40.                    foSkipWhiteSpaceOnlyLeading,   //  When foSkipWhiteSpace is active, skip whitespace for object members only before :
  41.                    foForceLF,            // On Windows, use this to force use of LF instead of CR/LF
  42.                    foFormatFloat         // Format floats using floattostr
  43.                    );
  44.   TFormatOptions = set of TFormatOption;    



For someone like me, the specialized case without the compiler conditionals is much easier to study in an effort to  understand the code (and fpcsrc is always an excellent source of code to study).  I'd try to write a program to do this myself, but don't have the skill to do a decent job of it.

creaothceann

  • Full Member
  • ***
  • Posts: 223
Re: An Idea for a helpful utility?
« Reply #1 on: November 09, 2025, 09:02:10 pm »
iirc Lazarus can show the inactive parts of a file in a different style.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 689
Re: An Idea for a helpful utility?
« Reply #2 on: November 09, 2025, 10:47:59 pm »
date=1762718530]
iirc Lazarus can show the inactive parts of a file in a different style.
[/quote]

Can anyone tell me how to do that?  If the style is different and consistent enough, I could probably write a program to just delete everything in that style from a file and put the result in a new one.

Thaddy

  • Hero Member
  • *****
  • Posts: 18520
  • Here stood a man who saw the Elbe and jumped it.
Re: An Idea for a helpful utility?
« Reply #3 on: November 10, 2025, 08:27:38 am »
You mean this?
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. begin
  3.   {$ifdef cpuI386}
  4.   writeln('Intel/amd 32 bit');
  5.   {$endif}
  6.   {$ifdef cpux64}
  7.   writeln('Intel/amd 32 bit' );
  8.   {$endif}
  9. end.
Depending on the cpu, one of the two lines will be greyed in the Lazarus editor to show that particular code will not be compiled.
There are some preprocessors available that can do that for you: removing code from sources that will not be compiled based on a (set of) defines. For Delphi, I used DIPP in the past, but I also wrote one to parse KOL a long time ago.
I assume there are many more, e.g. based on Castalia or maybe fcl-passsrc.
Nowadays I leave everything in, though.

But that the IDE shows it, means it contains a define parser, so maybe you can lift that out.
If you do so, always make a backup, because it can and will make mistakes.

Delphi dipp is here: https://www.yunqa.de/delphi/apps/dipp/index

An alternative that can probably translated to FPC is ppp:
https://clootie.narod.ru/delphi/download_utils.html#ppp and comes with sourcecode. (The original is by Barry Kelly, a.k.a. the last known good compiler engineer at Embarcadero)

Can' t find my own parser back -Well, I can but it is on a BigFoot drive and I have no ide interface anymore - , but that is based on Castalia (ultimately TmwParser that Lazarus uses a descendant from)

All of these, except mine, are maintained.
« Last Edit: November 10, 2025, 09:21:25 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 689
Re: An Idea for a helpful utility?
« Reply #4 on: November 10, 2025, 04:33:20 pm »
You mean this?
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. ...
  3. [/quote]
  4.  
  5. Thanks -- both of those preprocessors do the kind of thing I had in mind -- and a whole lot more.  I will take a look at the ppp source but suspect that it's more than I can handle.  It would be great to have something like them for FPC/Lazarus.
  6.  
  7. I wish I could read code from the fpcsrc and navigate around all the conditionals and defines, but I just can't do it anymore -- I get confused enough just trying to peel the class hierarchies.
  8.  
  9. Maybe an "AI" could help nowadays.

 

TinyPortal © 2005-2018