Recent

Author Topic: code macros in pascal code?  (Read 13196 times)

lt.col.blair

  • New Member
  • *
  • Posts: 15
code macros in pascal code?
« on: November 25, 2010, 10:22:59 am »
Hi,

several years ago I used "code macros" in 8051-assembler language. I wonder (and hope) if something similar is possible in Lazarus / FreePascal?

Since I haven't seen anythin like it since then, I'll try to make clear what I'm talking about in an example:

Let's say I have a group of function to read some value:
ReadInt
ReadBool
ReadStr
ReadObject ...
All these functions use exactly the same code - maybe something like this:

function ReadInteger(InVal: Integer): Integer
var
  TmpVal: Integer
begin
  TmpVal := GetIt(InVal);
  Result := DoSomeMore(TmpVal);
end;

The only difference between the functions is the variable type for TmpVal and the functions result type.

What I'm dreaming of is a macro language that enables me to do the following:

{$define macro MyMacro(Param1, ParamN)}
function Read{Param1}(InVal: {ParamN}): {ParamN}
var
  TmpVal: {ParamN}
begin
  TmpVal := GetIt(InVal);
  Result := DoSomeMore(TmpVal);
end;
{$endmacro}

{$UseMacro MyMacro(Int, Integer)}
{$UseMacro MyMacro(Str, String)}
{$UseMacro MyMacro(Bool, Boolean)}
{$UseMacro MyMacro(Object, TObject)}

I hope this is understandable.
Is anything like that possible in or planned for FreePascal or Lazarus?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: code macros in pascal code?
« Reply #1 on: November 25, 2010, 03:57:07 pm »
We actually have simple macro, but it's not enabled by default. Use {$macro on} to enable it, and {$define macroname:=macrovalue} to write it. You can put that function in an include file and define the macro just before the inclusion, e.g.:
Code: [Select]
{$define Param:=Integer}
{$I thefunctions.inc}
{$define Param:=Char}
{$I thefunctions.inc}

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9874
  • Debugger - SynEdit - and more
    • wiki
Re: code macros in pascal code?
« Reply #2 on: November 25, 2010, 04:04:44 pm »
there are also generics, but that is OO only, not for plain procedures

jhvhs

  • New Member
  • *
  • Posts: 18
Re: code macros in pascal code?
« Reply #3 on: November 25, 2010, 05:55:33 pm »
(slightly off) Hm... Seems that delphi were behind fpc on generics. The object pascal implementation of the idea was introduced in fpc 2.2.0 in 2007, while Delphi had this implemented (and unicode support, by the way...) somewhere in 2008 in its first release by embarcadero.

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: code macros in pascal code?
« Reply #4 on: November 25, 2010, 06:10:26 pm »
In any case, I believe generics feature is the right answer to the problem stated in the original post. How to conveniently implement it is another question...
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

lt.col.blair

  • New Member
  • *
  • Posts: 15
Re: code macros in pascal code?
« Reply #5 on: November 29, 2010, 09:34:35 am »
Hi,
thanks for your hints, but I can't get it to work.
I tried (without parameters) this:

a) in Main.pas
Code: [Select]
{$macro on}

function TMain.myfunc1(val: string): Integer;
{$I macro1.inc}
end;

function TMain.myfunc2(val: string): string;
{$I macro1.inc}
end;

b) in macro1.inc:

Code: [Select]
{$define mymacro:=var Tmp: Integer;begin Tmp := StrToInt(val); result := tmp;}
This gives me "Syntax error, "begin" expected but "end" found.
What am I doing wrong?
Thanks
« Last Edit: November 29, 2010, 10:02:59 am by lt.col.blair »

lt.col.blair

  • New Member
  • *
  • Posts: 15
Re: code macros in pascal code?
« Reply #6 on: November 29, 2010, 10:39:00 am »
Hi again,
Sorry, a little more creative trying did the trick :-[

For all, who might be interested I'll put a complete simple example here. It's stupid, but it shows the possibilities.

Code: [Select]
{$macro on}
{$define mymacro:= macrovar
var
Tmp: macrovar;
begin
Tmp := macroconvert(val);
  result := tmp;
end;
}
{$define macrovar:=Integer}
{$define macroconvert:=StrToInt}
function TMain.myfunc1(val: string): mymacro

{$define macrovar:=string}
{$define macroconvert:=}
function TMain.myfunc2(val: string): mymacro

{$define macrovar:=TDateTime}
{$define macroconvert:=StrToDate}
function TMain.myfunc3(val: string): mymacro

{$define macrovar:=Double}
{$define macroconvert:=StrToFloat}
function TMain.myfunc4(val: string): mymacro

After macro evaluation the code will be:
Code: [Select]
function TMain.myfunc1(val: string): Integer;
var
Tmp: Integer;
begin
Tmp := StrToInt(val);
  result := tmp;
end;

function TMain.myfunc2(val: string): string
var
Tmp: string;
begin
Tmp := (val);
  result := tmp;
end;

function TMain.myfunc3(val: string): TDateTime
var
Tmp: TDateTime;
begin
Tmp := StrToDate(val);
  result := tmp;
end;

function TMain.myfunc4(val: string): Double
var
Tmp: Double;
begin
Tmp := StrToFloat(val);
  result := tmp;
end;

I hope this will help anyone else, who wants to use these macros.
Again: thanks to those, who helped me on this.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: code macros in pascal code?
« Reply #7 on: December 01, 2010, 08:08:29 pm »
(slightly off) Hm... Seems that delphi were behind fpc on generics. The object pascal implementation of the idea was introduced in fpc 2.2.0 in 2007, while Delphi had this implemented (and unicode support, by the way...) somewhere in 2008 in its first release by embarcadero.

(Codegear had generics in D2007, but only in the .NET parts)

To answer the real question: one could also use an external macro processor like M4.

 

TinyPortal © 2005-2018