[Implemented] XIncludeFile with the main source file

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
CSHW89
User
User
Posts: 30
Joined: Thu Sep 09, 2010 2:47 pm

[Implemented] XIncludeFile with the main source file

Post by CSHW89 »

Hi people,
It would be nice if 'XIncludeFile' doesn't try to include the main source file.
Here's what I mean:

File ClassA.pb:

Code: Select all

XIncludeFile "ClassB.pb"

Interface ClassA
EndInterface

Procedure ClassA_AddB(*this.ClassA, *add)
  ;...
EndProcedure
File ClassB.pb:

Code: Select all

XIncludeFile "ClassA.pb"

Interface ClassB
EndInterface

Procedure ClassB_AddA(*this.ClassB, *add)
  ;...
EndProcedure
If I compile "ClassA.pb", the compiler tries to include "ClassA.pb", too.

Thanks
Kevin
Image Image Image
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: XIncludeFile with the main source file

Post by citystate »

so...
you'd like Purebasic to account for bad programming? :P
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: XIncludeFile with the main source file

Post by kenmo »

Although it's not really a bug, it shouldn't be too difficult for the team to "fix"...

Assuming that XIncludeFile keeps some sort of list of which files have already been included, why not add the main source file to the list at the very beginning of compilation?

(But normally your code should have some sort of hierarchy... included sub-files shouldn't be trying to re-include your main file!)
CSHW89
User
User
Posts: 30
Joined: Thu Sep 09, 2010 2:47 pm

Re: XIncludeFile with the main source file

Post by CSHW89 »

kenmo wrote:Although it's not really a bug, it shouldn't be too difficult for the team to "fix"...

Assuming that XIncludeFile keeps some sort of list of which files have already been included, why not add the main source file to the list at the very beginning of compilation?
Thanks. Yes, I hope.
kenmo wrote:(But normally your code should have some sort of hierarchy... included sub-files shouldn't be trying to re-include your main file!)
Recursive data structures, e.g. Nondeterministic Finite Automata:

NFA.pb:

Code: Select all

XIncludeFile "NFAState.pb"

Structure NFA
  List state.NFAState()
EndStructure
NFAState.pb:

Code: Select all

XIncludeFile "NFATrans.pb"

Structure NFAState
  List trans.NFATrans()
EndStructure
NFATrans.pb:

Code: Select all

XIncludeFile "NFA.pb"

Structure NFATrans
  *value
  *goal.NFAState
EndStructure
Image Image Image
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: XIncludeFile with the main source file

Post by Demivec »

CSHW89 wrote:Recursive data structures, e.g. Nondeterministic Finite Automata:
Your example with NFA.pb ,NFAState.pb, and NFATrans.pb, would never be compilable. Each of the files requires all of the other files (and itself) to be included before continuing. It is a clear example for the need of better programming structure because the improvements to XIncludeFile still wouldn't make it possible.

PureBasic is a one-pass compiler, not a recursive one. :wink:


I know you didn't ask for it but I might as well suggest it anyway. Until XIncludeFile is improved as you suggested you can define constants that can be tested with CompileIf's.

It would look like this:

Code: Select all

;//NFA.pb:
CompilerIf Defined(include_NFA, #PB_Constant)
CompilerElse
  #include_NFA = 1
  
  XIncludeFile "NFAState.pb"
  
  Structure NFA
    List state.NFAState()
  EndStructure
CompilerEndIf 

Code: Select all

;//NFAState.pb:
CompilerIf Defined(include_NFAState, #PB_Constant)
CompilerElse
  #include_NFAState = 1
  
  XIncludeFile "NFATrans.pb"
  
  Structure NFAState
    List trans.NFATrans()
  EndStructure
CompilerEndIf

Code: Select all

;//NFATrans.pb:
CompilerIf Defined(include_NFATrans, #PB_Constant)
CompilerElse
  #include_NFATrans = 1
  
  XIncludeFile "NFA.pb"  
  
  Structure NFATrans
    *value
    *goal.NFAState
  EndStructure
CompilerEndIf
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: XIncludeFile with the main source file

Post by kenmo »

Yeah, Demivec's way is the way it's usually done in C, etc. (#ifdef)

PS. You can use Defined(include_NFA, #PB_Constant) = 0 instead of an immediate CompilerElse, it looks a tiny bit nicer.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: XIncludeFile with the main source file

Post by Demivec »

kenmo wrote:PS. You can use Defined(include_NFA, #PB_Constant) = 0 instead of an immediate CompilerElse, it looks a tiny bit nicer.
That would be nicer. I considered using CompilerIf Not Defined(include_NFA, #PB_Constant), but realized that the Not operator hasn't yet been implemented for that purpose. I had overlooked simply comparing the results with zero. Thanks for pointing it out.
Post Reply