[Implemented] #PB_Label

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

[Implemented] #PB_Label

Post by Hroudtwolf »

Hi,

A little wish again...
An #PB_Label option for Defined would be very helpful.


Best regards

Wolf
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

I Second it :!:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Its not possible as labels can be used before they are declared, and PB is a one-pass compiler:

Code: Select all

CompilerIf Defined(mylabel, #PB_Label) ; how do we know there will be a "mylabel:" later ?
CompilerEndIf

; ...

mylabel: 
quidquid Latine dictum sit altum videtur
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I thought I understood the one-pass compiler concept until I realized one day that while I can't do this:

Code: Select all

DoIt()

Procedure DoIt()
  ; do something
EndProcedure
without having the procedure declared first, which makes perfect sense, why is it that I can do this:

Code: Select all

CatchImage(0, ?label)

; lots of lines of code

DataSection
  label: Includebinary "image.bmp"
EndDataSection
when, due to the one-pass operation, the compiler seemingly shouldn't know where ?label is?

There must be something I'm missing, as this has puzzled me for quite a long time.
BERESHEIT
Fred
Administrator
Administrator
Posts: 18161
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Actually, "?" operand only work with a label, so we "pre-declare" the label (and check for real declaration at the very end of the program). That's a little exception, that's true.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

For procedures, the compiler has to know the parameters and returntype
to generate the right code and show correct error messages.

For labels all that isn't needed, thats why it is possible to simply assume the label exists
and fire an error at the very end if it isn't found.

[edit] must type faster... :twisted:
quidquid Latine dictum sit altum videtur
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

It's a great pity. :(
But, thanks for the explanations.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

I believed that labels just was treated as simple long type variables (pointers) internally.
Maybe if there was like that, things would be easier. :?:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

ehh! look at this:

Code: Select all

If Defined(var,#PB_Variable)
  Debug "yes":Else:Debug "nope"
EndIf
var.l
nope, but

Code: Select all

var.l
If Defined(var,#PB_Variable)
  Debug "yes":Else:Debug "nope"
EndIf
yes.
So:

Code: Select all

If Defined(var,#PB_Label)
  Debug "yes":Else:Debug "nope"
EndIf
var:
should be nope, but:

Code: Select all

var:
If Defined(var,#PB_Label)
  Debug "yes":Else:Debug "nope"
EndIf
should be yes. Respected the one compilation pass :wink:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

That would be very inconsistent:

Code: Select all

x = ?mylabel ; perfectly valid

Debug Defined(mylabel, #PB_Label) ; returns 0, even though mylabel is accessible !?

mylabel:
As i said, labels are quite different from other things in PB.
Since a label can be accessed before it is defined, Defined() should also reflect this,
but this isn't possible.
quidquid Latine dictum sit altum videtur
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

But... if compiler knows what is ?mylabel , why some like this couldn't work?

Code: Select all

x = ?mylabel ; perfectly valid

Debug Defined(?mylabel, #PB_Label)
mylabel:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

read the full post please. Fred and I explained it... :roll:
quidquid Latine dictum sit altum videtur
User avatar
pcfreak
User
User
Posts: 75
Joined: Sat May 22, 2004 1:38 am

Post by pcfreak »

wouldn't it be possible to let the fasm compiler handle this, like converting the Defined() command for this case to asm macro instructions

!if defined label
debug "label defined"
!else
debug "label undefined"
!end if

well.. would be the 'dirty' way but possible i think.. don't know atm how pb defines labels intern and can't check 'cause i'm at uni atm :p
Post Reply