Example 4: Format

NOTE

Use of structured block format. This program formats a mvBASIC program to display block structuring by indenting lines.

 

*---- Definitions

10   SP = 6        ;  * left margin column number

   ID = 3          ; * number of spaces to indent

*---- Initialization

   SPX = SP

   LINE.NO = 0

*---- Input file name and program name

20   PRINT

   PRINT

   PRINT 'DATA/BASIC FILE NAME     - ':;

   INPUT FILE

   IF FILE = '' THEN STOP

   OPEN '', FILE ELSE PRINT 'CANT OPEN FILE -':

      FILE;  GOTO 20

30   PRINT 'DATA/BASIC PROGRAM NAME - ':;

    INPUT NAME

   IF NAME = '' THEN GOTO 20

   NEWITEM = ''

   READ ITEM FROM NAME ELSE

      PRINT 'CANNOT FIND THAT PROGRAM'

      GOTO 30

   END

*---- Get new line, if none - then done

100   LINE.NO = LINE.NO + 1

   LINE = EXTRACT(ITEM,LINE.NO,0,0)

   IF LINE = '' THEN

      WRITE NEWITEM ON NAME

      PRINT;  PRINT;  PRINT '--DONE--'; GOTO 10

   END

   LABEL = ''

*---- Strip off leading/trailing spaces

200   IF LINE[1,1] = ' ' THEN LINE = LINE[2,32767];

      GOTO 200

210      IF LINE[LEN(LINE),1] = ' ' THEN

      LINE = LINE[1,LEN(LINE)-1];

      GOTO 210

   END

*---- Look for a comment ('*', '!', OR 'REM')

   IF LINE[1,1] = '*' OR LINE[1,1] = ’!’ OR LINE[1,3] =

      β€™REM’ THEN GOTO 1500

*---- Look for 'for'

   IF LINE[1,4]='FOR ' AND INDEX(LINE,'NEXT ',1)>0

      THEN       GOTO 2000

   IF LINE[1,4]='FOR ' AND INDEX(LINE,'NEXT ',1)=0

      THEN GOTO 1000

*---- Look for 'end'

   IF LINE = 'END' THEN GOTO 1100

   IF LINE[1,4] = 'END ' THEN

      IF LINE[LEN(LINE)-4,5] = ' ELSE' THEN GOTO

            1200

   END

*---- Look for 'next'

   IF LINE[1,5] = 'NEXT ' THEN GOTO 1100

*---- Extract leading numeric label

   IF LINE[1,1] MATCHES '1N' THEN

      L = 2

300      IF LINE[L,1] MATCHES '1N' THEN L=L+1; GOTO

         300

      LABEL = LINE[1,L-1]

      LINE = LINE[L,32767]

      GOTO 200

   END

*---- Look for line ending in ' ELSE' OR ' THEN' ('IF' OR 'READ')

   X = LINE[LEN(LINE)-4,5]

   IF X = ' THEN' THEN GOTO 1000

   IF X = ' ELSE' THEN GOTO 1000

*---- This is just another line, therefore no change

   GOTO 2000

*---- Indent on subsequent lines

1000   SP = SP - ID

   GOTO 2000

*---- Outdent on this and subsequent lines

1100   SP = SP - ID

*---- Outdent this line only

1200   SPX = SPX - ID

   GOTO 2000

*---- Print with no indentation

1500   SPX = 0

*---- Write new line

2000   NEW.LINE = LABEL :  STR(' ', SPX-LEN(LABEL)) :

      LINE

   PRINT NEW.LINE

   NEWITEM =

      REPLACE(NEWITEM,LINE.NO,0,0,NEW.LINE)

   SPX = SP

   GOTO 100

END

See Also

Appendix C: mvBASIC Program Examples

General Coding Techniques

Example 1: Triples

Example 2: Guess

Example 3: INV-INQ

Example 5: Lot-update

Example 6: Communications

Program