Page 1 of 1

Use of macro's in modules

Posted: Wed Aug 05, 2015 2:53 pm
by Geert
I"m getting some weird results when using macro's in modules.
When a macro defines a global variable, it doesn't seem to act like one.
Can somebody explain? Or is this a bug?

(Tested on MacOSX and Windows with PB5.24/5.31: same results.)

Code: Select all

DeclareModule TestGlobal
 Macro DefineGlobal()
  Global a=10
 EndMacro
 Global b=9
EndDeclareModule

Module TestGlobal
EndModule

UseModule TestGlobal

Procedure Test()
 ;Global a         ;Uncomment this and the results are as expected: a=10
 Debug "a="+Str(a) ;Why 0? I expect 10, because a is defined global.
 Debug "b="+Str(b)
 a=8
 b=7
EndProcedure

DefineGlobal()

Debug "a="+Str(a)
Debug "b="+Str(b)

Test()

Debug "a="+Str(a)
Debug "b="+Str(b)

;Debugger results:
; a=10
; b=9
; a=0    ??? why not 10?
; b=9
; a=10   ??? why not 8?
; b=7

Re: Use of macro's in modules

Posted: Wed Aug 05, 2015 2:56 pm
by Fred
This is not a macro issue, you are defining your global variable after your procedure definition, so it's not taken in account.

Code: Select all

DeclareModule TestGlobal
 Macro DefineGlobal()
  Global a=10
 EndMacro
 Global b=9
EndDeclareModule

Module TestGlobal
EndModule

UseModule TestGlobal

DefineGlobal()

Procedure Test()
 ;Global a         ;Uncomment this and the results are as expected: a=10
 Debug "a="+Str(a) ;Why 0? I expect 10, because a is defined global.
 Debug "b="+Str(b)
 a=8
 b=7
EndProcedure


Debug "a="+Str(a)
Debug "b="+Str(b)

Test()

Debug "a="+Str(a)
Debug "b="+Str(b)

;Debugger results:
; a=10
; b=9
; a=0    ??? why not 10?
; b=9
; a=10   ??? why not 8?
; b=7

Re: Use of macro's in modules

Posted: Wed Aug 05, 2015 4:00 pm
by infratec
Always remember:

PB is a single pass compiler.
So you need an 'order'.

Re: Use of macro's in modules

Posted: Wed Aug 05, 2015 4:28 pm
by Geert
Sometimes it's so obvious... Thanks!