Oxygen Basic
Information => Development => Topic started by: Brian Alvarez 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):
#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:
#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:
generateModule(TCP,string,210)
And have a function like this generated:
function TCP_string_210(string p1) as string
' string code...
end function
-
Hi Brian,
Some insider information :)
/*
'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
-
Ahh good to know those constants. They will be very useful! :)
However i am getting:
ERROR: ERROR: undefined type: function
WORD: tcp
IN: namegen
LINE: 393
FILE: "main source
-
I think this is what you need:
' 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
-
Perfect! thanks Charles!
-
It works pretty good. Just one minor issue, this works fine:
def support_strptr
function %1s(byval int n) as sys {return strptr(v%1[vidx[n]])}
end def
while this:
def support_strptr
function %1s(byval int n) as sys {return strptr(v%1[vidx[n]])}
end def
Raises this error:
ERROR: ERROR: reserved word
WORD: function
LINE: 853
FILE: "main source
-
Hi Brian,
It may be due to undefined dependencies. This works:
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:
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)