Basic Program Formatter
by Graham Crow

 Introduction
 ------------

Many Basic programs are 'packed' by eliminating all inessential spaces and by including as many statements as possible (separated by colons) on each line. While this saves space and may also reduce execution time, it does not make for legibility! BForm allows you to reverse this process and save the modified program under a different name.


 Using BForm
 -----------

From the Desktop double-click on the application !BForm to install it on the icon bar. Click Select on the icon bar icon to open the window.

Drag a Basic File from a Viewer onto the window or the icon bar. Details of the current file appear in the top section of the window.

In section 2 choose to split lines and/or insert spaces.

In section 3, click on 'OK' to begin processing, and observe progress being reported.

Save the modified program by dragging the Basic icon to a Filer window. Note that a default name (adding a -F suffix to the Basic filename) is supplied.

To finish, close the window or (finally) click menu on the icon bar menu, and choose 'Quit'.

 
 Additional User Notes
 ---------------------

BForm is multi-tasking, so you can perform other Desktop tasks while processing takes place. You can pause processing by pressing Escape, after which you may elect to continue or abort.

The modified program is automatically renumbered in increments of one using a modified form of the utility presented by Alan Wrigley in Risc User 3:8. Any referenced lines (GOTO, RESTORE) are preserved during renumbering.

Any machine code (starting when the first non-space byte of a line or statement is [ and ending when it is ] ) is left untouched by BForm. This means that you can temporarily protect sections of the program if you wish by enclosing them in square brackets. 
 

 Program Notes
 -------------
 
The strategy is to read the target program from disc and to create a modified version of it in memory using the space in WimpSlot. Each line is read byte by byte and copied to memory, inserting spaces and splitting lines as appropriate. Finally, the program in memory is renumbered and saved to disc.

The space is checked before attempting to write a byte to memory and if necessary WimpSlot is extended. If there is no room to extend then a fatal error message is generated. (DEFFNspaceOK).

DEFPROCloadfile() handles the dragging of a Basic file onto the application. When the user clicks 'OK' PROCstartprocess is called. Since the application is multi-tasking, reason code zero in the polling loop is used to call PROCdoline which processes a single line of the target program. The first four bytes are processed. If the second byte is &FF the end of the program has been reached and PROCendprocess is called. Otherwise the program advances to the first non-space byte.

The program must decide two things: whether to split a line at a colon, and whether to insert a space after the current byte. These operations are governed by the following four PROCedures:

 DEFPROCdoline
 -------------

There are some circumstances in which a line must be left intact, so the first non-space byte is checked to see if it is REM * DATA IF or ON. If it is any of these, the flag nosplit% is set to TRUE. Similarly, if it is [ then mc% is set to TRUE because machine code has to be left untouched. If the byte is a token the flag token% is set. Finally, a repeated call is made to PROCrestofline until the end of line marker &D is reached, when the pointers are reset and the length byte updated from count%.

 DEFPROCrestofline
 -----------------

Essentially, this routine gets another byte from disc and if appropriate calls PROCspace to insert an extra space. It then puts the byte into memory before re-setting the flags token%, number%, varend% and quotes%. If the byte is a colon then a call is made to PROCsplitline.

If the byte is a colon the next byte is checked. IF that is a REM then nosplit% is set (ie don't split lines with REMs tacked on). IF it's open/close square brackets then the flag mc% is set/unset. If mc% is TRUE then all bytes other than colons are simply transferred from disc to memory.

The procedure has to take special account of the following: 
 o &8D followed by 3 encoded 'GOTO' bytes
 o TO+"P" - treated as a token for TOP
 o & preceding hex numbers
 o quotes

 DEFPROCsplitline
 ----------------

This procedure creates four new start-of-line bytes, with the line number being the previous line plus one. The length byte is temporarily set to zero and its address stored in ad% for updating when the end of line marker is reached. Any spaces before or after the colon are removed.

 DEFPROCspace
 ------------

This routine inserts a space where appropriate following a token, a number, a variable or quotes. For example, a token followed by another token normally causes a space to be inserted (e.g. RECTANGLEFILL becomes RECTANGLE FILL).


 RISC User 1993


