Recent

Author Topic: $define question  (Read 1415 times)

440bx

  • Hero Member
  • *****
  • Posts: 4565
$define question
« on: August 20, 2024, 10:51:55 pm »
Hello,

I would like to "create" a symbol name to be used by a compiler directive.  Consider the following:
Code: Pascal  [Select][+][-]
  1.     {$define OUTPUT_$i%CURRENTROUTINE%}
  2.  
  3.     {$if defined(OUTPUT_$i%CURRENTROUTINE%)}
  4.       writeln('defined');
  5.     {$endif}
  6.  
if those statements are in a function named "FunctionA" then the first {$define ... } would define a symbol "OUTPUT_FUNCTIONA" which would later be tested to conditionally compile some code (in this example a writeln.)

I know the above doesn't work because I tried it.

The question is: Is there a way of accomplishing this ? and, if the answer is yes, an example of how will be greatly appreciated.

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

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1408
    • Lebeau Software
Re: $define question
« Reply #1 on: August 21, 2024, 12:28:06 am »
Is there a way of accomplishing this ?

No, there is not.  This would require setting up some kind of preprocessing step that parses and modifies your source code before compiling it.  There is nothing built-in to FPC/Lazarus to handle this for you.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

440bx

  • Hero Member
  • *****
  • Posts: 4565
Re: $define question
« Reply #2 on: August 21, 2024, 02:26:42 am »
No, there is not. 
I suspected that but, figured I'd ask just in case.

Thank you Remy.
(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: 7713
Re: $define question
« Reply #3 on: August 21, 2024, 08:54:58 am »
I think that somebody has in the past demonstrated something roughly similar using nested defines, but the detail escapes me.

However as a workaround I've done this sort of thing:

Code: Pascal  [Select][+][-]
  1. (* If FPC version 3.2.0 or later is used, it is able to validate that error     *)
  2. (* messages correctly identify what function was being executed at the time:    *)
  3. (* this is important since it includes parsing rules where an error indicates   *)
  4. (* either something wrong in the input file or a flaw in the program. Versions  *)
  5. (* of FPC predating 2.2.4 lack the FPC_FULLVERSION predefined, so cannot fail   *)
  6. (* gracefully when they try to determine whether the CURRENTROUTINE expansion   *)
  7. (* is available. Other factors mandate that in practice FPC older than 2.6.0    *)
  8. (* will be unable to compile this source file without significant modification. *)
  9.  
  10. {$undef HAS_CURRENTROUTINE     }
  11. {$if FPC_FULLVERSION >= 030200 }        (* Requires FPC 2.2.4 minimum           *)
  12. {$define HAS_CURRENTROUTINE    }        (* Requires FPC 3.2.0 minimum           *)
  13. {$assertions on                }        (* Make sure name checks are operative  *)
  14. {$endif FPC_FULLVERSION        }
  15.  
  16. ...
  17.  
  18. (* Parse the PEC thumbnail section.
  19. *)
  20. function pec_thumbnailImageSubsection(): boolean;
  21.  
  22. const
  23.   thisName= 'pec_thumbnailImageSubsection()';
  24.  
  25. var
  26.   backtrack: int64;
  27.   i: integer;
  28.  
  29. begin
  30. {$ifdef HAS_CURRENTROUTINE }
  31.   Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
  32.                                                 {$I %CURRENTROUTINE%} + '()');
  33. {$endif HAS_CURRENTROUTINE }
  34.   result := false;
  35.   pushRule(thisName);
  36.   backtrack := FilePos(pesIn);
  37.   header;
  38.   if FilePos(pesIn) <> pecThumbnailByteOffset then begin
  39.     WriteLn('*** In ', peekRule(), ': thumbnail image isn''t contiguous with stitchlist');
  40.     WriteLn(Up + 'Backtrack ', popRule());
  41.     Seek(pesIn, backtrack);
  42.     exit
  43.   end;
  44.   for i := 0 to thumbnailColours do begin
  45.     WriteLn('Thumbnail colour: ', i);
  46.     ReadU8G(pesIn, pesOut, thumbnailWidth, thumbnailHeight, i)
  47.   end;
  48.   WriteLn(Up + 'OK ', popRule());
  49.   result := true
  50. end { pec_thumbnailImageSubsection } ;
  51.  

It would obviously be possible to also put in a $define next to the const (i.e. where inconsistencies would easily be spotted by eye), and as long as a recent version of the compiler was being used quite a few possible problems can be handled like that.

I've also got a recollection of using something a bit more complex for related error reporting, but finding it might take me a few minutes.

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

440bx

  • Hero Member
  • *****
  • Posts: 4565
Re: $define question
« Reply #4 on: August 21, 2024, 01:02:40 pm »
Thank you Mark, I appreciate the effort.

The problem is the complexity of the solution.  Its complexity is detrimental to its ease of understanding, therefore maintenance.

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

Thaddy

  • Hero Member
  • *****
  • Posts: 15735
  • Censorship about opinions does not belong here.
Re: $define question
« Reply #5 on: August 21, 2024, 01:35:33 pm »
The real problem is chars like $ and % in a define. That is alsking for trouble.

Remove those and your define wil work.
If I smell bad code it usually is bad code and that includes my own code.

440bx

  • Hero Member
  • *****
  • Posts: 4565
Re: $define question
« Reply #6 on: August 21, 2024, 01:43:24 pm »
Remove those and your define wil work.
Post something that works _and_ does what I want it to do.  Thank you!
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 15735
  • Censorship about opinions does not belong here.
Re: $define question
« Reply #7 on: August 21, 2024, 02:32:26 pm »
You poor soul,
Code: Pascal  [Select][+][-]
  1. {$define OUTPUT_X}
  2. begin
  3.     {$if defined(OUTPUT_X)}
  4.       writeln('defined');
  5.     {$endif}
  6. end.
Do not use macro code or assignment code like % and $.
What is worse, Mark already explained that.
 
If I smell bad code it usually is bad code and that includes my own code.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7713
Re: $define question
« Reply #8 on: August 21, 2024, 02:37:57 pm »
Do not use macro code or assignment code like % and $.
What is worse, Mark already explained that.

Thaddy, /DON'T/ drag me into this.

I said that at best one had to use a fairly crude workaround (although I'd point out that most of the verbosity was an initial check of the compiler's capabilities).

I most definitely did not say "DON'T USE", and would be one of the first to say that FPC's inability to handle macro parameters and minimal concatenation etc. is extremely unfortunate.

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

440bx

  • Hero Member
  • *****
  • Posts: 4565
Re: $define question
« Reply #9 on: August 21, 2024, 03:03:45 pm »
Thaddy, looks like you're having one of those days when you're starved for attention... 
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018