Page 1 of 1

[Implemented] Macros would be nice

Posted: Tue Oct 01, 2002 10:53 am
by BackupUser
Restored from previous forum. Originally posted by PB.

I was looking through an old programming reference manual and one of
the "commands" was Macro name ... EndMacro. Anything in those keywords
became the macro name, and whenever you used that name later in your
code, the macro was inserted by the compiler. So basically it lets
you reduce your source size and also save typing.

For example, I use the line MessageBeep_(#MB_ICONASTERISK) a lot in my
own apps. I could declare this as a "macro" with the following, and at
compile time the compiler would replace !chime with the actual code.
(Obviously the leading exclamation point would have to be something
else, because that's already used for inline ASM, isn't it?).

Note: Don't confuse macros with procedures -- the two things are very
different and are used to achieve different goals... macros are more like
a highly streamlined version of the IncludeFile command, but without
requiring external files and longer typing. :)

Code: Select all

Macro chime
  MessageBeep_(#MB_ICONASTERISK)
EndMacro
;
!chime ; Compiler uses MessageBeep_(#MB_ICONASTERISK) here.
PB - Registered PureBasic Coder

Posted: Tue Oct 01, 2002 2:23 pm
by BackupUser
Restored from previous forum. Originally posted by fred.

Ok, Macro/EndMacro is simple enough. I think it will be like a procedure because you can pass parameters to a macro which are replaced by the compiler. To call it, do it like a Procedure

Macro MyMSG(Arg)
MessageRequester("INFO", Arg, 0)
EndMacro

MyMSG("Hello")

What do you think ?

Fred - AlphaSND

Posted: Tue Oct 01, 2002 4:23 pm
by BackupUser
Restored from previous forum. Originally posted by Rings.

i agree, could be very useful :)
only in debugger its strange(same as include files)

Its a long way to the top if you wanna .....CodeGuru

Posted: Tue Oct 01, 2002 4:53 pm
by BackupUser
Restored from previous forum. Originally posted by Danilo.

Like i wished some time ago, Fred.
You already said it will be implemented for v4.0 :)

I also wished MACROs to be varARGs, remember ??

Code: Select all

MACRO msgbox(arg1)  MessageBox_(0,arg1,"",0)
ENDMACRO

MACRO msgbox(arg1,arg2)
  MessageBox_(0,arg2,arg1,0)
ENDMACRO

msgbox("Error, please try again!")

msgbox("INFO","This works just great!")
Thanks!

cya,
...Danilo

(registered PureBasic user)

Posted: Tue Oct 01, 2002 6:11 pm
by BackupUser
Restored from previous forum. Originally posted by Berikco.
Originally posted by fredMacro MyMSG(Arg)
MessageRequester("INFO", Arg, 0)
EndMacro
Yes, could be very usefull.
And faster than procedure, no jump...

Regards,

Berikco

http://www.benny.zeb.be

Posted: Tue Oct 01, 2002 8:36 pm
by BackupUser
Restored from previous forum. Originally posted by PB.

Code: Select all

Macro MyMSG(Arg)
  MessageRequester("INFO", Arg, 0)
EndMacro
MyMSG("Hello")
Yep, perfect; remembering that the compiler would internally (ie. not to
the source!) change MyMSG("Hello") to MessageRequester("INFO","Hello",0)
during compilation.

This will make coding so much faster -- and even make my own autocomplete
feature of PBToolBox obsolete!


PB - Registered PureBasic Coder

Posted: Tue Oct 01, 2002 9:59 pm
by BackupUser
Restored from previous forum. Originally posted by tinman.
Originally posted by Danilo

Like i wished some time ago, Fred.
You already said it will be implemented for v4.0 :)

I also wished MACROs to be varARGs, remember ??
It might be useful to have some special macro symbols too, such as the number of parameters and an automatically updating symbol (Fred, you must know this from assemblers, sometimes .@ IIRC).

Oh, and could we please have a CompilerError command if we are going to have macros (like Blitz2's CERR command)?


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.30)

Posted: Tue Oct 01, 2002 10:59 pm
by BackupUser
Restored from previous forum. Originally posted by waffle.

actually, wouln't this be better implementaed as an "inline" function.
then use a compliler switch to decide how the code is handled.

/inline
would place the inline function's code at the calling point, just as a macro would, but with the switch off, its treated as just a regular function call to simplify debugging..
this makes it harder on fred, but makes testing easier on us and optimizing for speed would become just a /incline option.

Posted: Wed Oct 02, 2002 6:16 pm
by BackupUser
Restored from previous forum. Originally posted by tinman.
Originally posted by waffle

actually, wouln't this be better implementaed as an "inline" function.
then use a compliler switch to decide how the code is handled.
Inline functions and macros are different enough to mean that inline functions might be useful in some places, but you would not want a function in others.

For example, say you want to modify the value of 3 variables using a macro or inline function. The macro would look like this:

Macro foo(arg1, arg2, arg3)
x=arg1
y=arg2
z=arg3
EndMacro

But the function would need to behave like a function, so it would possibly look like this:

Procedure foo(arg1, arg2, arg3)
Shared x, y, z
x=arg1
y=arg2
z=arg3
EndProcedure

Oh, but then you cannot use this inline function if your x,y,z variables are local to a procedure.

Also:

Macro SOME_STRING_CONSTANT
"hell world"
EndMacro

You just can't do that with an inline function.

I don't say no to inline functions, but they are not a replcement for macros.


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.30)

Posted: Wed Oct 02, 2002 7:57 pm
by BackupUser
Restored from previous forum. Originally posted by fred.

True.

Fred - AlphaSND

Posted: Wed Oct 02, 2002 8:40 pm
by BackupUser
Restored from previous forum. Originally posted by waffle.

ok, you got me there :)
its just that debugging macros can be very hard,
really really hard if its a self-redefining macro (recursive call).
just thought that might be another way....

Posted: Mon Oct 07, 2002 12:05 pm
by BackupUser
Restored from previous forum. Originally posted by SoulTaker.


In C++ A Macro is creted by using the #define key word.

// Macro to define cursor lines
#define CURSOR(top, bottom) ((top) << 8) | bottom))

// Macro to get a random integer with a specified range
#define getrandom(min, max) \
((rand()%(int)(((max) + 1)-(min)))+ (min))


Un-defing a Macro

#define WIDTH 80
#define ADD( X, Y ) (X) + (Y)
.
.
.
#undef WIDTH
#undef ADD

What a Macro does is Add what ever is on the left when it see's the #define on the right.

So in PureBasic we could do something like this:

DefineMacro Sum / Ret.l = X.l + Y.l

Then in the PureBasic Code when it see's

Sum it will replace it with this

Ret.l = X.l + Y.l

Then we could undefine using UnDefineMacro

So we would need 2 new commands:

DefineMacro And UnDefineMacro.

The PureBasic Compiler would have to search the source code and every time it found a Macro it would have to replace it with the Macro code on the left.

It would also have to check to make sure that the Macro name was not a compiler reserved word or command.

As you can see you could build some powerfull Macro's.

P.S.
I do have the MSDN Library so if any of you need API stuff please let me know.


Abit BD7-II Raid P4 1.9 gHz 384 Ram Xtasy GFroce 3 TI 200 CD-RW DirectX 9.0 Beta 2 Sound Blaster Live! XP Pro, Registered PureBasic version 3.30 For Windows.

Posted: Mon Oct 07, 2002 12:18 pm
by BackupUser
Restored from previous forum. Originally posted by SoulTaker.

It would be like this Fred,

DefineMacro MyMSG("Hello") / MessageRequester("INFO","Hello",0)

Then when the compiler saw

MyMSG("Hello") it would replace it with the

MessageRequester("INFO","Hello",0)

We would need a center seperator to seperate the left side from the right, i used the backslash just for showing.

Prototype: DefineMacro MacroName / Replacement



Abit BD7-II Raid P4 1.9 gHz 384 Ram Xtasy GFroce 3 TI 200 CD-RW DirectX 9.0 Beta 2 Sound Blaster Live! XP Pro, Registered PureBasic version 3.30 For Windows.

Posted: Mon Oct 07, 2002 1:06 pm
by BackupUser
Restored from previous forum. Originally posted by El_Choni.

I really prefer the multilined version (like Procedure), and not the C one. Just my opinion.

Bye,

El_Choni