Author Topic: Macros and Metalanguage : Peeks and Pokes  (Read 359 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3751
    • Oxygen Basic
Macros and Metalanguage : Peeks and Pokes
« 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:

Code: [Select]
'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

Brian Alvarez

  • Newbie
  • *
  • Posts: 45
    • PluriBASIC
Re: Macros and Metalanguage : Peeks and Pokes
« Reply #1 on: May 03, 2018, 04:43:04 AM »

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.

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3751
    • Oxygen Basic
Re: Macros and Metalanguage : Peeks and Pokes
« Reply #2 on: May 03, 2018, 05:09:04 AM »
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.

Mike Lobanovsky

  • Admin Support Member
  • *****
  • Posts: 1857
Re: Macros and Metalanguage : Peeks and Pokes
« Reply #3 on: May 03, 2018, 11:19:41 AM »
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?
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, 2 x nVidia GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3751
    • Oxygen Basic
Re: Macros and Metalanguage : Peeks and Pokes
« Reply #4 on: May 03, 2018, 02:10:28 PM »
Aha! You can use a macro to select the right function. Extreme polymorphism :)

Mike Lobanovsky

  • Admin Support Member
  • *****
  • Posts: 1857
Re: Macros and Metalanguage : Peeks and Pokes
« Reply #5 on: May 03, 2018, 02:49:58 PM »
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?
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, 2 x nVidia GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3751
    • Oxygen Basic
Re: Macros and Metalanguage : Peeks and Pokes
« Reply #6 on: May 03, 2018, 05:50:14 PM »
The macrotemps can be listed in any order, and the spacer is cosmetic.

You could split the macro header. For instance:
Code: [Select]
macro mac any* (
  r,        'return
  x,y,z,    'params
  xx,yy,zz  'temps
){...}