PureBasic Forward Declare Builder

Share your advanced PureBasic knowledge/code with the community.
USCode
Addict
Addict
Posts: 923
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

PureBasic Forward Declare Builder

Post by USCode »

-- PureBasic Forward Declare Builder --

PureBasic has the 'Declare' keyword because, as noted in the documentation, sometimes a procedure needs to call another procedure which isn't declared before its definition.
http://www.purebasic.com/documentation/ ... dures.html
Unfortunately if you want to order your procedures in your source code in any way that makes most sense to you, such as alphabetically or perhaps just sequentially as you create them, you have to Declare them first. Then any changes you make to a Procedure declaration must be repeated for the Declare declaration as well, a duplication of effort.

To address this, I wrote a simple program I call the 'PureBasic Forward Declare Builder'. This application simply reads your source code file and automatically outputs a separate file of Declare statements for every Procedure. You can then simply put an IncludeFile of that Declare file at the top of your source file. This allows you to declare your Procedures in any order that you prefer. Thanks to the excellent integration of external tools capabilities built into the PB IDE, the building of this Declares file can be automatic.

Setup steps:

1) Create an executable of the Forward Declare Builder code below and save the executable wherever you prefer.
2) In the PB IDE, go to Tools > Configure Tools ... and choose 'New'
3) Configure the Edit Tool Settings as follows:

Commandline: The location and name of your Forward Declare Builder executable from step 1. For example, C:\PBTools\PB_FDB.exe
Arguments: %FILE
Name: Forward Declare Builder
Event to trigger the tool: Before Compile/Run (or whichever you prefer but unfortunately 'Sourcecode saved' doesn't appear to work at this time)
Wait until tool quits - checked
Run hidden - checked
Enable tool on a per-source basis - checked (optional but recommended - if you only want to build the Forward Declares file for specific source files, see step 6)

4) Hit OK. Ensure the new Forward Declare Builder tool is checked in the Configure Tools window.
5) At the top of your application source code file add the IncludeFile with suffix "_Declares.pbi". For example, if your source code file is "YourSourceFile.pb" then IncludeFile "YourSourceFile_Declares.pbi" (the Forward Declares file will be created in the same directory as your source code file, so if necessary enter the appropriate path)
6) In the PB IDE, go to Compiler > Compiler options ... tab Compile/Run, check the Forward Declare Builder tool in "Execute tools".

You're done! Now every time you Compile/Run your source file the Forward Declare Builder will quickly run first and build a fresh updated Declare include file.
You will need to repeat steps 5 and 6 for each source file.

Optionally, you can just run the Forward Declare Builder code manually without all the above setup and it will simply prompt you for your source code file and generate the declares file.

Limitations:
- Assumes entire procedure declaration is on 1 line.
- Doesn't currently work with "Runtime" procedure declarations.

Please let me know if you have any questions. Thanks! :mrgreen:

PureBasic Forward Declare Builder:

Code: Select all

;
; PureBasic Forward Declare Builder
; Author: USCode
;
; September 23, 2010 - Original version
; September 10, 2013 - Updated for PB IDE Tools integration
;

EnableExplicit

Define.s sLine, sFile, sSource 

If CountProgramParameters()
  
  sSource = ProgramParameter(0)
  
Else
  
  sSource= OpenFileRequester("PB source file", "", "PureBasic (*.pb,*.pbi)|*.pb;*.pbi|All files (*.*)|*.*", 0)
  
EndIf

If FileSize(sSource) > 0
  
  If ReadFile(0, sSource)
    
    sFile = Left(sSource, Len(sSource) - Len(GetExtensionPart(sSource)) - 1) + "_Declares.pbi"
    
    If CreateFile(1, sFile)
      
      While Not Eof(0)
    
        sLine = Trim(ReadString(0))
        
        If Left(sLine,9) = "Procedure" And Left(sLine,15) <> "ProcedureReturn"
          
          WriteStringN(1, "Declare" + Right(sLine, Len(sLine) - 9))
        
        EndIf
      
      Wend
    
      CloseFile(1)
    
    EndIf
    
      CloseFile(0)
  
  EndIf
    
EndIf 

End
Last edited by USCode on Sat Nov 23, 2013 9:36 pm, edited 5 times in total.
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: PureBasic Forward Declare Builder

Post by idle »

good idea thanks
Windows 11, Manjaro, Raspberry Pi OS
Image
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: PureBasic Forward Declare Builder

Post by uwekel »

Thx for sharing!
Take care about the Runtime keyword which can be in front of Procedure.
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
USCode
Addict
Addict
Posts: 923
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

Re: PureBasic Forward Declare Builder

Post by USCode »

uwekel wrote:Thx for sharing!
Take care about the Runtime keyword which can be in front of Procedure.
I haven't used or even looked into the new Runtime library yet ... when declaring a Procedure as Runtime does it have the same declaration-before-using requirement as regular Procedures?
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: PureBasic Forward Declare Builder

Post by Tenaja »

Maybe a RegEx would be the way to go for finding the start of the procedures...
Post Reply