If, like me, you have ever typed a whole load of code into a large program without having compiled it for a while, you will may be familiar with the 'could not close nest' error message. You get this message when your code structure is incorrect - for example, if you have an 'if' without a 'then' or 'endif', or if you have a 'for' without a 'next'.
Trying to find the error in a program several thousand lines long, especially when your indenting isn't as neat as it should be, can lose you hours (trust me!).
This code may be of some help - I have found it very useful for my own debugging. All you need to do is paste the full path of the .dba file you want to scan at the top of the code, and it will check the structure of your code, reporting any mismatched ifs/thens/endifs, whiles/endwhiles, repeats/untils, and fors/nexts.
It will also tell you the name and line number of mismatched for/next loops. The scanner will automatically disregard any parts of your code remmed out with the ` character, or the rem/remstart/remend commands.
If any clever sausage feels like improving the code in any way, please do.
set display mode 1024,768,32
file$="testfile.dba"
if file exist(file$)
open to read 1,file$
repeat
word$=""
inc linenumber
cls
print "Scanned lines: ",linenumber
`read the line of code
read string 1,string$
`split into words
length=len(string$)
for n=1 to length
character$=mid$(string$,n)
if character$="`" then goto nextline
if character$<>" "
word$=word$+character$
endif
if character$=" " or n=length
if lower$(word$)="rem" and skipwords=0 then goto nextline
if lower$(word$)="remstart" then skipwords=1
if lower$(word$)="remend" then skipwords=0
if lower$(word$)="if" and skipwords=0 then inc numifs
if lower$(word$)="then" and skipwords=0 then inc numthens
if lower$(word$)="endif" and skipwords=0 then inc numendifs
if lower$(word$)="repeat" and skipwords=0 then inc numrepeats
if lower$(word$)="until" and skipwords=0 then inc numuntils
if lower$(word$)="while" and skipwords=0 then inc numwhiles
if lower$(word$)="endwhile" and skipwords=0 then inc numendwhiles
if lower$(word$)="for" and skipwords=0
inc numfors
dim forword$(numfors)
dim onlinenumber(numfors)
onlinenumber(numfors)=linenumber
`find word after for
p=n+1
repeat
test$=mid$(string$,p)
if test$<>" " and test$<>"=" then forword$(numfors)=forword$(numfors)+test$
inc p
until test$=" " or test$="=" or p>length
endif
if lower$(word$)="next" and skipwords=0
inc numnexts
dim nextword$(numnexts)
`find word after next
p=n+1
repeat
test$=mid$(string$,p)
if test$<>" " and p<=length then nextword$(numnexts)=nextword$(numnexts)+test$
inc p
until test$=" " or p>length
endif
word$=""
endif
next n
nextline:
until file end(1)=1
close file 1
print "Number of Ifs: ",numifs
total=numthens+numendifs
print "Number of Thens and Endifs: ",total
print "Number of Repeats: ",numrepeats
print "Number of Untils: ",numuntils
print "Number of Whiles: ",numwhiles
print "Number of Endwhiles: ",numendwhiles
`display fors without nexts
revisednumfors=numfors
for n=1 to numfors
match=0
for p=1 to numnexts
if nextword$(p)=forword$(n) then match=1
next p
if match=0 and lower$(forword$(n))<>"files" and lower$(forword$(n))<>"fonts" then print "For ",forword$(n),"=... has no 'Next ",forword$(n),"' at line number ",onlinenumber(n)
if match=0 and lower$(forword$(n))="files" or lower$(forword$(n))="fonts" then dec revisednumfors
next n
print "Number of Fors: ",revisednumfors
print "Number of Nexts: ",numnexts
endif
wait key