MacroIf

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

MacroIf

Post by FloHimself »

See Subject.
My favorite numbers: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: MacroIf

Post 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? :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post 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
My favorite numbers: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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
@}--`--,-- A rose by any other name ..
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

I think he means at compile time. If the third argument is not supplied, use one definition, else use the other.
El_Choni
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

That would be smarter. :)


Edit:

Code: Select all

MacroIf c = #PB_Undefined
@}--`--,-- A rose by any other name ..
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post by FloHimself »

El_Choni wrote:I think he means at compile time.
Exact! Otherwise I could just write a more elegant Procedure.
My favorite numbers: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post by FloHimself »

Dare2 wrote:That would be smarter. :)


Edit:

Code: Select all

MacroIf c = #PB_Undefined

Code: Select all

CompilerIf PB = #PB_Undefined
:P
My favorite numbers: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

:D
@}--`--,-- A rose by any other name ..
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post 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
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post 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.
My favorite numbers: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post 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
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Post by Straker »

So is this how we would handle native function overloading?
Dräc
Enthusiast
Enthusiast
Posts: 150
Joined: Sat Oct 09, 2004 12:10 am
Location: Toulouse (France)
Contact:

Post by Dräc »

@FloHimself : +1 :)
Post Reply