Recent

Author Topic: [solved] macro for code ?  (Read 3113 times)

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1422
[solved] macro for code ?
« on: April 22, 2025, 08:00:30 am »
im not sure how to do in pascal.. is there a way to substitute
Code: Pascal  [Select][+][-]
  1. FOR X:= LOW(AR_CONTROLS) TO HIGH(AR_CONTROLS) DO
with something shorter that references it such as ar_loop or something. like i do with resource strings keys ?
« Last Edit: April 23, 2025, 11:32:39 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. 💁🏻‍♀️

Khrys

  • Full Member
  • ***
  • Posts: 229
Re: macro for code ?
« Reply #1 on: April 22, 2025, 08:07:03 am »
Code: Pascal  [Select][+][-]
  1. {$macro on}
  2.  
  3. {$define AR_LOOP := FOR X := LOW(AR_CONTROLS) TO HIGH(AR_CONTROLS) DO}
  4.  
  5. AR_LOOP BEGIN
  6.   // ...
  7. END;

I'd strongly prefer a  for..in  loop, though.

Thaddy

  • Hero Member
  • *****
  • Posts: 16982
  • Ceterum censeo Trump esse delendam
Re: macro for code ?
« Reply #2 on: April 22, 2025, 09:07:02 am »
Joanna, you are probably missing parameterized macro's, something like
Code: Pascal  [Select][+][-]
  1. THELOOP(AR_CONTROLS);
which would iterate over any array. That is not possible in Freepascal, but what is possible is to use for in do which is a shorter notation..
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1422
Re: macro for code ?
« Reply #3 on: April 22, 2025, 11:37:25 am »
Thanks for the answers,
Also I’m curious what the scope of the macro is. Is it accessible from other files? Where is it declared usually?
✨ 🙋🏻‍♀️ 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. 💁🏻‍♀️

Thaddy

  • Hero Member
  • *****
  • Posts: 16982
  • Ceterum censeo Trump esse delendam
Re: macro for code ?
« Reply #4 on: April 22, 2025, 02:18:47 pm »
Macros are unit local afaik. It requires an inc file to use it in all units that want to use them in. In FreePascal macro's are by no means like something like C macro's, but depending on how you look at that it may actually be a good thing, given that C style macros are untyped and that is not very Pascallish.
That does not mean the feature is not useful, it means it is only very basic. A bit like your capslock problem... :P
(I have a DisJoannafy tool)
« Last Edit: April 22, 2025, 02:22:37 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1422
Re: macro for code ?
« Reply #5 on: April 23, 2025, 12:46:01 am »
Another question, do for in loops ever iterate backwards or are they only one way?
✨ 🙋🏻‍♀️ 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. 💁🏻‍♀️

Fibonacci

  • Hero Member
  • *****
  • Posts: 717
  • Internal Error Hunter
Re: macro for code ?
« Reply #6 on: April 23, 2025, 01:09:32 am »
Another question, do for in loops ever iterate backwards or are they only one way?

Use tricks

Code: Pascal  [Select][+][-]
  1. {$modeswitch typehelpers}
  2. {$modeswitch anonymousfunctions}
  3. {$modeswitch functionreferences}
  4.  
  5. type
  6.   TSomeTypeArray = Array of String;
  7.  
  8.   THelperCallback = reference to procedure(s: String);
  9.  
  10.   THelper = type helper for TSomeTypeArray
  11.     procedure forEach(cb: THelperCallback; reversed: Boolean=false);
  12.   end;
  13.  
  14. procedure THelper.forEach(cb: THelperCallback; reversed: Boolean=false);
  15. var
  16.   i: Integer;
  17. begin
  18.   if not reversed then
  19.     for i := 0 to high(Self) do cb(Self[i])
  20.   else
  21.     for i := high(Self) downto 0 do cb(Self[i])
  22. end;
  23.  
  24. var
  25.   a: TSomeTypeArray;
  26.  
  27. begin
  28.   a := ['first', 'second', 'third'];
  29.  
  30.   // forward
  31.   writeln('Forward:');
  32.   a.forEach(procedure(s: String)
  33.   begin
  34.     writeln(' * s = ', s);
  35.   end);
  36.  
  37.   writeln;
  38.  
  39.   // backward
  40.   writeln('Backward:');
  41.   a.forEach(procedure(s: String)
  42.   begin
  43.     writeln(' * s = ', s);
  44.   end, True{reversed});
  45.  
  46.   readln;
  47. end.

Code: [Select]
Forward:
 * s = first
 * s = second
 * s = third

Backward:
 * s = third
 * s = second
 * s = first
« Last Edit: April 23, 2025, 01:12:39 am by Fibonacci »

ASerge

  • Hero Member
  • *****
  • Posts: 2411
Re: macro for code ?
« Reply #7 on: April 23, 2025, 02:55:08 am »
Another question, do for in loops ever iterate backwards or are they only one way?
By default, the for in loop runs in the forward order. But it can be customized.
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$IFDEF WINDOWS}
  3.   {$APPTYPE CONSOLE}
  4. {$ENDIF}
  5. {$LONGSTRINGS ON}
  6. {$MODESWITCH TYPEHELPERS}
  7.  
  8. type
  9.   TStringArray = array of string;
  10.  
  11.   TStringArrayReverseEnumerator = class(TObject)
  12.   strict private
  13.     FOwner: TStringArray;
  14.     FIndex: SizeInt;
  15.     function GetCurrent: string;
  16.   public
  17.     constructor Create(const AOwner: TStringArray);
  18.     function GetEnumerator: TStringArrayReverseEnumerator; inline;
  19.     function MoveNext: Boolean; inline;
  20.     property Current: string read GetCurrent;
  21.   end;
  22.  
  23.   TStringArrayHelper = type helper for TStringArray
  24.     function Reverse: TStringArrayReverseEnumerator;
  25.   end;
  26.  
  27.  
  28. function TStringArrayHelper.Reverse: TStringArrayReverseEnumerator;
  29. begin
  30.   Result := TStringArrayReverseEnumerator.Create(Self);
  31. end;
  32.  
  33. constructor TStringArrayReverseEnumerator.Create(const AOwner: TStringArray);
  34. begin
  35.   FOwner := AOwner;
  36.   FIndex := Length(FOwner);
  37. end;
  38.  
  39. function TStringArrayReverseEnumerator.GetCurrent: string;
  40. begin
  41.   Result := FOwner[FIndex];
  42. end;
  43.  
  44. function TStringArrayReverseEnumerator.GetEnumerator: TStringArrayReverseEnumerator;
  45. begin
  46.   Result := Self;
  47. end;
  48.  
  49. function TStringArrayReverseEnumerator.MoveNext: Boolean;
  50. begin
  51.   Result := FIndex > 0;
  52.   if Result then
  53.     Dec(FIndex);
  54. end;
  55.  
  56. var
  57.   A: TStringArray;
  58.   S: string;
  59. begin
  60.   A := ['first', 'second', 'third'];
  61.   Writeln('Forward:');
  62.   for S in A do
  63.     Writeln(' * s = ', S);
  64.   Writeln;
  65.   Writeln('Backward:');
  66.   for S in A.Reverse do
  67.     Writeln(' * s = ', S);
  68.   Readln;
  69. end.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1422
Re: macro for code ?
« Reply #8 on: April 23, 2025, 11:22:49 am »
Thanks that’s complicated  :D
Im not sure I think once heard that sets are Not really in any particular order.. I only use for in loops for Iterating sets usually.
✨ 🙋🏻‍♀️ 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. 💁🏻‍♀️

Thaddy

  • Hero Member
  • *****
  • Posts: 16982
  • Ceterum censeo Trump esse delendam
Re: [solved] macro for code ?
« Reply #9 on: April 23, 2025, 03:23:41 pm »
In set theory you are right, no particular order.
In Pascal implementations the default set construct is always in order, though.
But other sets, written in pascal, like in generics.collections need not be in order. Only the values need to be unique.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Zoran

  • Hero Member
  • *****
  • Posts: 1948
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: [solved] macro for code ?
« Reply #10 on: April 23, 2025, 05:45:01 pm »

In set theory you are right, no particular order.
In Pascal implementations the default set construct is always in order, though.

No, that's plain wrong! A Pascal set is not ordered.
Arrays are ordered, sets are not.
What you probably meant to say is that the underlying type for which the set is declared has to be an ordinal type.
But the set itself has no order. It only contains some element or not.

Let's take a look at set theory - you said they are not ordered, and Pascal sets are - let's take a set of integral numbers A={1, 2, 5}.
What can we say about this set?
We can say that number 2 belongs to it, we can say that number 3 doesn't belong to it. We can notice that its elements can be compared (1<2, 1<5, 2<5), as this is a set of integral numbers, but it has nothing to do with the set itself. The set itself is not ordered, it can be written {2, 5, 1} and this is the same set.
Now, Thaddy, what is different in Pascal sets?
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

TBMan

  • Full Member
  • ***
  • Posts: 135
Re: [solved] macro for code ?
« Reply #11 on: April 23, 2025, 06:06:34 pm »

In set theory you are right, no particular order.
In Pascal implementations the default set construct is always in order, though.

No, that's plain wrong! A Pascal set is not ordered.
Arrays are ordered, sets are not.
What you probably meant to say is that the underlying type for which the set is declared has to be an ordinal type.
But the set itself has no order. It only contains some element or not.

Let's take a look at set theory - you said they are not ordered, and Pascal sets are - let's take a set of integral numbers A={1, 2, 5}.
What can we say about this set?
We can say that number 2 belongs to it, we can say that number 3 doesn't belong to it. We can notice that its elements can be compared (1<2, 1<5, 2<5), as this is a set of integral numbers, but it has nothing to do with the set itself. The set itself is not ordered, it can be written {2, 5, 1} and this is the same set.
Now, Thaddy, what is different in Pascal sets?



What's the difference between "construct" and elements within the set?  I think Zoran and Thaddy are discussing  two different things.
« Last Edit: April 23, 2025, 08:42:20 pm by TBMan »

Zoran

  • Hero Member
  • *****
  • Posts: 1948
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: [solved] macro for code ?
« Reply #12 on: April 23, 2025, 11:04:56 pm »
What's the difference between "construct" and elements within the set?  I think Zoran and Thaddy are discussing  two different things.

You mean that "construct" there stands for "elements within the set"?
I'd say that in cited context "default set construct" should be understood like "set as a standard structured type in Pascal".


Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

TBMan

  • Full Member
  • ***
  • Posts: 135
Re: [solved] macro for code ?
« Reply #13 on: April 24, 2025, 12:22:24 am »
What's the difference between "construct" and elements within the set?  I think Zoran and Thaddy are discussing  two different things.

You mean that "construct" there stands for "elements within the set"?
I'd say that in cited context "default set construct" should be understood like "set as a standard structured type in Pascal".

Discuss it with your buddy Thaddy. I'm out.
« Last Edit: April 24, 2025, 12:43:13 am by TBMan »

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1422
Re: [solved] macro for code ?
« Reply #14 on: April 24, 2025, 11:38:42 am »
I wonder how it’s implemented though. ..
Has anyone experimented with adding numbers to a set in a particular order and then using
Code: Pascal  [Select][+][-]
  1.  for x in theSet do
Even if they come out in the same order they were put in it might be considered undefined behavior.
✨ 🙋🏻‍♀️ 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