$define question lazarus / 62-General ---------------------------------------------------------------------- 440bx · 2024-08-20 20:51 Hello, I would like to "create" a symbol name to be used by a compiler directive.  Consider the following:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     {$define OUTPUT_$i%CURRENTROUTINE%}     {$if defined(OUTPUT_$i%CURRENTROUTINE%)}      writeln('defined');    {$endif} 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. ---------------------------------------------------------------------- Remy Lebeau · 2024-08-20 22:28 Quote from: 440bx on August 20, 2024, 10:51:55 pmIs 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. ---------------------------------------------------------------------- 440bx · 2024-08-21 00:26 Quote from: Remy Lebeau on August 21, 2024, 12:28:06 amNo, there is not.  I suspected that but, figured I'd ask just in case. Thank you Remy. ---------------------------------------------------------------------- MarkMLl · 2024-08-21 06:54 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i (* If FPC version 3.2.0 or later is used, it is able to validate that error     *)(* messages correctly identify what function was being executed at the time:    *)(* this is important since it includes parsing rules where an error indicates   *)(* either something wrong in the input file or a flaw in the program. Versions  *)(* of FPC predating 2.2.4 lack the FPC_FULLVERSION predefined, so cannot fail   *)(* gracefully when they try to determine whether the CURRENTROUTINE expansion   *)(* is available. Other factors mandate that in practice FPC older than 2.6.0    *)(* will be unable to compile this source file without significant modification. *) {$undef HAS_CURRENTROUTINE     }{$if FPC_FULLVERSION >= 030200 }        (* Requires FPC 2.2.4 minimum         *){$define HAS_CURRENTROUTINE    }        (* Requires FPC 3.2.0 minimum           *){$assertions on                }        (* Make sure name checks are operative  *){$endif FPC_FULLVERSION        } ... (* Parse the PEC thumbnail section.*)function pec_thumbnailImageSubsection(): boolean; const  thisName= 'pec_thumbnailImageSubsection()'; var  backtrack: int64;  i: integer; begin{$ifdef HAS_CURRENTROUTINE }  Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +                                              {$I %CURRENTROUTINE%} + '()');{$endif HAS_CURRENTROUTINE }  result := false;  pushRule(thisName);  backtrack := FilePos(pesIn);  header;  if FilePos(pesIn) <> pecThumbnailByteOffset then begin    WriteLn('*** In ', peekRule(), ': thumbnail image isn''t contiguous with stitchlist');    WriteLn(Up + 'Backtrack ', popRule());    Seek(pesIn, backtrack);    exit  end;  for i := 0 to thumbnailColours do begin    WriteLn('Thumbnail colour: ', i);    ReadU8G(pesIn, pesOut, thumbnailWidth, thumbnailHeight, i)  end;  WriteLn(Up + 'OK ', popRule());  result := trueend { pec_thumbnailImageSubsection } ;  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 ---------------------------------------------------------------------- 440bx · 2024-08-21 11:02 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. ---------------------------------------------------------------------- Thaddy · 2024-08-21 11:35 The real problem is chars like $ and % in a define. That is alsking for trouble. Remove those and your define wil work. ---------------------------------------------------------------------- 440bx · 2024-08-21 11:43 Quote from: Thaddy on August 21, 2024, 01:35:33 pmRemove those and your define wil work. Post something that works _and_ does what I want it to do.  Thank you! ---------------------------------------------------------------------- Thaddy · 2024-08-21 12:32 You poor soul, Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$define OUTPUT_X}begin    {$if defined(OUTPUT_X)}      writeln('defined');    {$endif}end.Do not use macro code or assignment code like % and $. What is worse, Mark already explained that. ---------------------------------------------------------------------- MarkMLl · 2024-08-21 12:37 Quote from: Thaddy on August 21, 2024, 02:32:26 pmDo 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 ---------------------------------------------------------------------- 440bx · 2024-08-21 13:03 Thaddy, looks like you're having one of those days when you're starved for attention...