Forum > General

FPC Compiler Directives Macros... Tips?

(1/3) > >>

trn76:
Hi... I was wondering after reading the FPC documentation on compiler directives, if anyone knew some good tips and tricks regarding macros?
I have some units with TONS (seriously... I doubt (no, make that know!) that I am able to actually measure the weight of bits! ..hehe ) of consts following a pattern of a macro.
...Also, defining stuff with macros could make typing errors harder to accomplish.

example:

--- Code: ---const
  regV3AD       = $13;    // pattern here is "+1" on each const
  regV3SR       = $14;
  regFcLo       = $15;
  regFcHi       = $16;

  kCtrTest      = $08;    // pattern here is "shr" on each const
  kCtrRing      = $04;
  kCtrSync      = $02;
  kCtrGate      = $01;

  // I would love to be able to do something like:

  regV3AD       = {$MACRO PATTERN1};
  regV3SR       = {$MACRO PATTERN1};
  regFcLo       = {$MACRO PATTERN1};
  regFcHi       = {$MACRO PATTERN1};
 
  kCtrTest      = {$MACRO PATTERN2};
  kCtrRing      = {$MACRO PATTERN2};
  kCtrSync      = {$MACRO PATTERN2};
  kCtrGate      = {$MACRO PATTERN2};

  // yeah, you get the point... no, I won't use enums here! ;)


--- End code ---

If anyone also have some other good tips on macros, I would love to know...

Thanks

marcov:
Macros are not parametrisable in FreePascal and related dialects. So if you want that you need to use an external preprocessor like M4.

Leledumbo:

--- Quote from: trn76 on November 15, 2014, 01:12:56 am ---...Also, defining stuff with macros could make typing errors harder to accomplish.

--- End quote ---
And reading errors easier to accomplish. Who would know the value of, say, regFcHi? What if you accidentally change the order someday? Or insert something else in the middle? This is one of many known abuse of macros, which is why FPC doesn't support macros as deep as languages without proper modular compilation support.

--- Quote from: marcov on November 15, 2014, 03:02:53 pm ---Macros are not parametrisable in FreePascal and related dialects. So if you want that you need to use an external preprocessor like M4.

--- End quote ---
I don't think he asked for parameterised macros. It's more like a macro with a state, so after each invocation of the macro, it's state is modified. As he has stated, the first macro change its state by +1-ing its current value, while the second one shr-ing it. Initial value? No idea.

trn76:
Thanks both of you for the replies, atleast I learned a bit more on how macros work in FPC.


--- Quote from: Leledumbo on November 15, 2014, 05:43:38 pm ---I don't think he asked for parameterised macros. It's more like a macro with a state, so after each invocation of the macro, it's state is modified. As he has stated, the first macro change its state by +1-ing its current value, while the second one shr-ing it. Initial value? No idea.

--- End quote ---
Yeah, I was thinking of a state macro with an initialized macro variable, but FPC does not have macro variables as I just learned.

Leledumbo:

--- Quote from: trn76 on November 15, 2014, 09:35:28 pm ---Thanks both of you for the replies, atleast I learned a bit more on how macros work in FPC.


--- Quote from: Leledumbo on November 15, 2014, 05:43:38 pm ---I don't think he asked for parameterised macros. It's more like a macro with a state, so after each invocation of the macro, it's state is modified. As he has stated, the first macro change its state by +1-ing its current value, while the second one shr-ing it. Initial value? No idea.

--- End quote ---
Yeah, I was thinking of a state macro with an initialized macro variable, but FPC does not have macro variables as I just learned.

--- End quote ---
Yes, FPC's preprocessor (I don't think this is the right term, since we don't really have true preprocessor that runs before compilation) is just not designed to handle such a complex task. Not even the overly sophisticated C's preprocessor can handle it, I guess.

Navigation

[0] Message Index

[#] Next page

Go to full version