Recent

Author Topic: Code Macros with arguments  (Read 4562 times)

spuds

  • New Member
  • *
  • Posts: 16
Code Macros with arguments
« on: March 26, 2018, 05:17:34 pm »
I have hundreds of formulas defined in my application. In order to simplify the formulas I would like to define a code
macro to expand them before compiling. Here is one example of a formula:

formula := 'Eval(''' + IntToStr(Base + 5) + ''') * 2122.00';

I would like to  say in code

formula := @(Base + 5) * 2122.00

Then  have the macro substitute   'Eval(''' + IntToStr(Base + 5) + ''')   where ever  @(Base+5) appears in code

It would appear that I need to define a macro that takes arguments much like a #define in the C programming language. Is this doable in free pascal and if not are there alternative solutions.


« Last Edit: March 26, 2018, 05:19:55 pm by spuds »

Thaddy

  • Hero Member
  • *****
  • Posts: 19249
  • Glad to be alive.
Re: Code Macros with arguments
« Reply #1 on: March 26, 2018, 05:31:06 pm »
https://www.freepascal.org/docs-html/prog/progse5.html

Note parameterized macro's are not (yet) supported so its use can not be fully compared to C macro's.
I do not think your case can be solved with the current macro support.
objects are fine constructs. You can even initialize them with constructors.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1285
Re: Code Macros with arguments
« Reply #2 on: March 26, 2018, 06:20:14 pm »
Maybe using an Expression Parser:
http://wiki.freepascal.org/How_To_Use_TFPExpressionParser
How to solve string formula?
http://forum.lazarus.freepascal.org/index.php?topic=34321.0

Maybe using Variants (and macros) in FPC, or Symbolic QuickEvaluate, or any scripting language that can write to a console (Lua, PascalScript, PHP, JavaScript, etc):
New FreePascal Language feature
http://forum.lazarus.freepascal.org/index.php?topic=35372.0

Thaddy

  • Hero Member
  • *****
  • Posts: 19249
  • Glad to be alive.
Re: Code Macros with arguments
« Reply #3 on: March 26, 2018, 06:27:52 pm »
@valdir.marcos
Freepascal is not a scripting language.
C style macro's expand to native code.

But your suggestion is an option if speed is not important.
objects are fine constructs. You can even initialize them with constructors.

spuds

  • New Member
  • *
  • Posts: 16
Re: Code Macros with arguments
« Reply #4 on: March 26, 2018, 06:28:18 pm »
From doing further research I found that there is a macro processor called M4 that is open source and runs under all unix-like operating systems. It seems to have all the capabilities required to solve my needs. If anyone has a need for a macro pre-processor that supports parameters M4 can be used as an alternative to the lazarus macro pre-processor.


Thaddy

  • Hero Member
  • *****
  • Posts: 19249
  • Glad to be alive.
Re: Code Macros with arguments
« Reply #5 on: March 26, 2018, 06:30:42 pm »
That's shell macro vs language macro. The former is already supported by Freepascal. No need for m4. The latter is simply scripting, not native code.
objects are fine constructs. You can even initialize them with constructors.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1285
Re: Code Macros with arguments
« Reply #6 on: March 26, 2018, 07:03:02 pm »
@valdir.marcos
Freepascal is not a scripting language.
Just curious. Where did I say the contrary?

C style macro's expand to native code.
I and all old programmers know that. Just curious. Where did I demonstrate that I don't know that?

But your suggestion is an option if speed is not important.
Yes, I was trying to suggest alternatives to @spuds.
First, the problem needs a solution, then you improve that to the fastest possible solution.

 

TinyPortal © 2005-2018