Author Topic: Macro parameter question  (Read 863 times)

0 Members and 1 Guest are viewing this topic.

Brian Alvarez

  • Hero Member
  • *****
  • Posts: 588
    • PluriBASIC
Macro parameter question
« on: August 20, 2019, 09:04:08 AM »

 Suppose i have many different but very similar modules i would like to generate on the fly rather than
coding them manually each. How would i generate module names on the fly?

 For example lets say i want to name modules with 3 main parts... sufix, datatype and numbering. How would i do it?
This is somewhat i tried last week and failed (untested pseudocode written from memory):

Code: [Select]
#def nameGen %1_%2_%3

macro generateModule(suf,dtt,num)
     function nameGen(suf,dtt,num)(dtt p1) as dtt
          #if typeof(dtt) = string
               ' string code...
          #endif
          #if typeof(dtt) = long
               ' long code...
          #endif
     end function
end macro

 I recall i was getting something like a parameter issue. Maybe the statement thought i was naming my function nameGen and was expecting a datatype for suf.
I also tried:

Code: [Select]
#def nameGen function %1_%2_%3

macro generateModule(suf,dtt,num)
     nameGen(suf,dtt,num)(dtt p1) as dtt
     .
     .
     .

 To no avail... is there something else i can try? What i need is to be able to call my macro like this:

Code: [Select]
generateModule(TCP,string,210)
And have a function like this generated:

Code: [Select]
     function TCP_string_210(string p1) as string
               ' string code...
     end function


Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4330
    • Oxygen Basic
Re: Macro parameter question
« Reply #1 on: August 20, 2019, 12:03:20 PM »
Hi Brian,

Some insider information :)

Code: [Select]
/*
'o2 typecodes hexadecimal
'
  sbyte 1,
  short 2,
  long 4,
  byte 21,
  ubyte 21,
  word 22,
  dword 24,
  quad 48,
  qword 58,
  single 64,
  double 68,
  extended 6A,
  char A1,
  wchar A2,
  zstring A1,
  wzstring A2,
  bstring C1,
  wbstring C2,
  string E1,
  wstring E2,

  ranges for primitives:

  signed ints   01..1f
  unsigned ints 21..3f
  floats        61..7f
  char          A1..BF
  bstrings      C1..DF
  strings       E1..FF

  classes and UDTs:

  0x200 ...
*/

#def nameGen %1_%2_%3

macro generateModule(suf,dtt,num)
     function nameGen(suf,dtt,num)(dtt p1) as dtt
          #if typecodeof(dtt) >= 0x200
               '
          #elseif typecodeof(dtt) > 0xa0
               ' string code...
          #elseif typecodeof(dtt) > 0x60
               ' float code...
          #elseif typecodeof(dtt) >  0
               ' long code...
          #else
               ' unidentified
          #endif
     end function
end macro

Brian Alvarez

  • Hero Member
  • *****
  • Posts: 588
    • PluriBASIC
Re: Macro parameter question
« Reply #2 on: August 20, 2019, 12:42:46 PM »
Ahh good to know those constants. They will be very useful! :)

However i am getting:

Code: [Select]
ERROR: ERROR: undefined type: function
WORD: tcp
IN: namegen
LINE: 393
FILE: "main source

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4330
    • Oxygen Basic
Re: Macro parameter question
« Reply #3 on: August 20, 2019, 07:50:36 PM »
I think this is what you need:
Code: [Select]
'                  suf dtt num
'------------------------------
def generateModule '%1_%2_%3   
    function %1_%2_%3(%2 p1) as %2
          #if typecodeof(%2) >= 0x200
               ' udt code
          #elseif typecodeof(%2) > 0xa0
               ' string code...
          #elseif typecodeof(%2) > 0x60
               ' float code...
          #elseif typecodeof(%2) >  0
               ' int code...
               return p1*3
          #else
               ' unidentified
          #endif
     end function
end def

generateModule x int 3
print x_int_3(7) '21

Brian Alvarez

  • Hero Member
  • *****
  • Posts: 588
    • PluriBASIC
Re: Macro parameter question
« Reply #4 on: August 21, 2019, 10:46:47 AM »
Perfect! thanks Charles!

Brian Alvarez

  • Hero Member
  • *****
  • Posts: 588
    • PluriBASIC
Re: Macro parameter question
« Reply #5 on: August 21, 2019, 03:57:09 PM »
It works pretty good. Just one minor issue, this works fine:

Code: [Select]
def support_strptr   
    function %1s(byval int n) as sys {return strptr(v%1[vidx[n]])}           

end def

while this:

Code: [Select]
def support_strptr   
    function %1s(byval int n) as sys {return strptr(v%1[vidx[n]])}           
end def

Raises this error:

Code: [Select]
ERROR: ERROR: reserved word
WORD: function
LINE: 853
FILE: "main source

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4330
    • Oxygen Basic
Re: Macro parameter question
« Reply #6 on: August 22, 2019, 02:00:44 AM »
Hi Brian,

It may be due to undefined dependencies. This works:

Code: [Select]
int vidx
string vx[100]
def support_strptr   
    function %1s(byval int n) as sys {return strptr(v%1[vidx[n]])}           
end def
#recordof support_strptr
support_strptr x

and this instantiation:

Code: [Select]
int vidx={1,2,3}
string vx[100]
int i
for i=1 to 100 : vx[i]="" : next
vx[2]="ok"
def support_strptr   
    function %1s(byval int n) as sys {return strptr(v%1[vidx[n]])}           
end def
#recordof support_strptr
support_strptr x
print xs(2) " " (string) xs(2)
« Last Edit: August 22, 2019, 02:09:17 AM by Charles Pegge »