Auto Command

Purpose

Automatic collection and declaration of undeclared variables as global variables

Syntax

Auto

Description

While Auto was implemented to allow the conversion of GFABASIC16 source code (LST) files which do not contain explicit variable declarations (which are required in GFABASIC32) - this is its intended use - it can also be used to audit variable usage within a program where, after changes have been made, variables that are declared either as local or global may no longer be used (see Remarks).

Auto is designed to be placed at the top of a program listing: when the compiler encounters the command, it performs an additional sweep of the following code to detect any undeclared variables, which it adds to the line containing the Auto command. Note: On each compile, whether using Shift-F5 (test compile) or F5 by itself (Run), any text (including comments) after the Auto command will be deleted in preparation for the list of undeclared variables.

Once the list of undeclared variables have been discovered, the Auto command should be replaced with either Global, Dim or Local, whichever is pertinent; otherwise, the compiler will perform the additional sweep every time the program is run and refresh the variable list on each occasion.

Before using the Auto command, it must be understood that it is very simple in its operation and has many limitations - it was, after all, purely designed to update GFABASIC16 listings, which did not have complex data variable types. Hence:

Example

Auto

a% = 1

a$(0) = 1

b = 33.5

---> becomes after Shift+F5 --->

Auto a$(), a%, b As Double

a% = 1

a$(0) = 1

b = 33.5

Note that the a$() array is undefined and will result in an 'Array Bounds Exceeded' error.

Remarks

As mentioned above, a second use of Auto is auditing variable usage within a program where, after changes, certain variables may no longer be necessary. This will only work properly for those variables that the command recognises and it will not correctly recognise types which have been declared using As rather than by use of a postfix. However, despite these limitations, it can still be a very useful auditing tool.

In the example below, the sub routine has been optimised and much of the old code has been removed meaning that certain variables are no longer used:

 

Function Test(f%)

Local a$, c%, e As Boolean

e = (f% < 200)

Return Iif(e, "True", "False")

EndFunction

To audit the variable in the function, you comment the Local declaration line and insert Auto below, then press Shift+F5, which gives you the following listing:

 

Function Test(f%)

°Local a$, c%, e As Boolean

Auto e As Double

e = (f% < 200)

Return Iif(e, "True", "False")

EndFunction

As can be seen, Auto has found only the e variable as the a$ and c% variables are no longer needed - it should also be noted that it has erroneously labelled it as a Double, which does not matter as this can be easily corrected. Hence, to complete the audit, all that is necessary is to change Auto to Local and Double to Boolean and you have the fully optimised routine as shown below:

 

Function Test(f%)

Local e As Boolean

e = (f% < 200)

Return Iif(e, "True", "False")

EndFunction

This may seem unnecessary for the short function above, but, in long, complex procedures with many variables, this is a far quicker and easier way to optimise variable declaration than to do it manually.

See Also

Sub, Procedure, Function, Global, Dim

{Created by Sjouke Hamstra; Last updated: 28/09/2023 by James Gaite; Other Contributors: Jean-Marie Melanson}