Oxygen Basic
Programming => Example Code => General => Topic started by: Charles Pegge on May 03, 2018, 04:19:53 AM
-
Due to O2's late expansion of macros, and late Metalanguage processing, the two can work together to produce highly flexible code, that supports many different types.
Using Peeks and Pokes as examples:
'2018-05-03 T 11:26:29
'peeks and pokes
'
macro peeks string(r,v,i,n)
===========================
'r returned string
'v variable being peeked
'i offest from variable's base address
'n number of bytes
r=nuls n
#if typecodeof(v)<=0 'not a variable
' ' '
#elseif typecodeof(v)<0x80 'primitive number
copy strptr(r),@v+i,n '
#elseif typecodeof(v)<0x100 'string
copy strptr(r),strptr(v)+i,n '
#else 'UDTs, Objects etc
copy strptr(r),@v+i,n '
#endif
end macro
'
macro pokes (v,i,p)
===================
'v variable being poked
'i offset from variable's base address
'p string containingg bytes to be poked
'
#if typecodeof(v)<=0 'not a variable
' '
#elseif typecodeof(v)<0x80 'primitive number
copy @v+i,strptr(p),len p '
#elseif typecodeof(v)<0x100 'string
copy strptr(v)+i,strptr(p),len p '
#else 'UDTs, Objects etc
copy @v+i,strptr(p),len p '
#endif
end macro
'
'
'TESTS
======
string p,s
s="0123456789"
p=peeks(s,2,4)+peeks(s,8,2)
print p
pokes(s,2,"-")
print s
-
Outstanding. I see that macros are enhanced at compile time to generate code depending on the situation. Thats cool! Will definately take advantage of this.
-
Yes, Brian.
It is all resolved at compile time. But late expansion is much more flexible than (preprocessor) early expansion. I commend it to all programming languages :)
I removed the #preprocess mode switch from o2, for this reason.
-
I believe heavy use of macros instead of true functions in one's code is going to overblow the resultant executables to unreasonable sizes.
Now suppose Brian prefers to implement a number of PB keywords, operators et al. as O2 macros that seem so flexible. Where would that lead to if the end user's script is of any decent size, say, a few dozens of thousands lines, which isn't uncommon in professional PB use cases?
-
Aha! You can use a macro to select the right function. Extreme polymorphism :)
-
I thought my FBSL used to be overly polymorphic (hence Freestyle). But O2 is then what I'd call total anarchy! :D
Now is the order in which the "macrotemps" follow in a macro declaration header significant? I mean, do they have to appear in the macro body in the exact same order? Also, are the spaces you're using to separate the macro params and "macrotemps" significant to the parser or optional?
-
The macrotemps can be listed in any order, and the spacer is cosmetic.
You could split the macro header. For instance:
macro mac any* (
r, 'return
x,y,z, 'params
xx,yy,zz 'temps
){...}