Page 1 of 1

MacroIf

Posted: Wed Feb 08, 2006 12:52 am
by FloHimself
See Subject.

Re: MacroIf

Posted: Wed Feb 08, 2006 3:25 am
by PB
I don't get it? Macros are definitions... what you're asking is like asking
for DimIf... care to explain what I'm obviously misunderstanding? :)

Posted: Wed Feb 08, 2006 9:41 am
by FloHimself

Code: Select all

Macro MidU(a, b, c=-1) 
  MacroIf c = -1 
    Mid(a, b, Len(a) - b + 1) 
  MacroElse 
    Mid(a, b, c) 
  EndMacroIf 
EndMacro

String.s = "Hallo du da" 

Debug MidU(String, 4, 3) 
Debug MidU(String, 4)
Ever played with the fasm macros? They seem more powerful then asm
itself! :P

Posted: Wed Feb 08, 2006 9:55 am
by Dare2
I like this.

I thought you meant something else when first I saw your post (if macro defined ..)

So .. the compiler actually builds two (or more) sets of code and tests to see which to use at runtime? Nifty. It would makes some things Macro-able that currently are not. As per your example.l

Posted: Wed Feb 08, 2006 10:00 am
by Flype

Code: Select all

Macro MidU(a, b, c=-1)
  If (c=-1)
    a = Mid(a, b, Len(a) - b + 1)
  Else
    a = Mid(a, b, c)
  EndIf
EndMacro

String.s = "Hallo du da"

MidU(String, 4) : Debug String
MidU(String, 4, 3) : Debug String
maybe you can just do it this way. or using CompilerIf statement.

Posted: Wed Feb 08, 2006 10:06 am
by El_Choni
I think he means at compile time. If the third argument is not supplied, use one definition, else use the other.

Posted: Wed Feb 08, 2006 10:09 am
by Dare2
That would be smarter. :)


Edit:

Code: Select all

MacroIf c = #PB_Undefined

Posted: Wed Feb 08, 2006 10:22 am
by FloHimself
El_Choni wrote:I think he means at compile time.
Exact! Otherwise I could just write a more elegant Procedure.

Posted: Wed Feb 08, 2006 10:24 am
by FloHimself
Dare2 wrote:That would be smarter. :)


Edit:

Code: Select all

MacroIf c = #PB_Undefined

Code: Select all

CompilerIf PB = #PB_Undefined
:P

Posted: Wed Feb 08, 2006 10:26 am
by Dare2
:D

Posted: Wed Feb 08, 2006 10:52 am
by helpy
FloHimself wrote:

Code: Select all

Macro MidU(a, b, c=-1) 
  MacroIf c = -1 
    Mid(a, b, Len(a) - b + 1) 
  MacroElse 
    Mid(a, b, c) 
  EndMacroIf 
EndMacro

String.s = "Hallo du da" 

Debug MidU(String, 4, 3) 
Debug MidU(String, 4)
Ever played with the fasm macros? They seem more powerful then asm
itself! :P
In Deinem Beispiel würde das ja funktionieren, und der Compiler müsste nur einen der beiden Aufrufe in den Code einsetzen. Wenn nun aber der dritte Parameter eine Variable ist, dann müsste der Compiler im Code eine If/Else-Abfrage einsetzen ... und in diesem Fall würde folgendes ausreichen:

[edit]OK (have to translate it):
In your example this would work and the compiler would replace the code by one of the MID-calls. But if the macro is "called" with a variable (and not with a constant, than the compiler has to insert an if-else ... and for this the following macro would be ok too:
[/edit]

Code: Select all

Macro MidU(a, b, c=-1) 
  If c = -1 
    Mid(a, b, Len(a) - b + 1) 
  Else 
    Mid(a, b, c) 
  EndIf 
EndMacro
Tja, Deine Variante wäre einfach etwas mehr optimiert!

[edit]Translation:
Yes, your version would result in more optimized code!

cu, helpy

Posted: Wed Feb 08, 2006 3:32 pm
by FloHimself
helpy wrote:But if the macro is "called" with a variable (and not with a constant, than the compiler has to insert an if-else ... and for this the following macro would be ok too:
Nope. If the macro is called with only 2 parameters the macro should
evaluate to :

Code: Select all

    Mid(a, b, Len(a) - b + 1)
If you call it with 3 parameters, whether with a constant or variable, it
should evaluate to:

Code: Select all

    Mid(a, b, c)
There is no need to have a If-Else.

Posted: Wed Feb 08, 2006 8:24 pm
by helpy
@FloHimself

You are right!

I was thinking the wrong way ... "what if the third parameter is a variable, which would be -1" ... wrong question! because the third parameter must not have the value -1 in any case.

cu, helpy

Posted: Wed Feb 08, 2006 9:41 pm
by Straker
So is this how we would handle native function overloading?

Posted: Wed Feb 08, 2006 11:07 pm
by Dräc
@FloHimself : +1 :)