Post by tsh73 on Dec 17, 2023 18:23:04 GMT
It started as code formatter/comment stripper for FORTRAN code
(back then in 2007 for a textbook I needed many simple programs formatted the same,
and they had most unhelpful commenst like "A=A+1 !Adds 1 to A")
then I ajusted it to JB
then got rid of FORTRAN part ;)
It is full of ad-hock things,
formats code to my liking,
(
inserts some emply lines - because I wants some things separated,
does not remove any empty lines - because author likely meant something then putting extra lines
)
and is by no means extensively tested
(thought I dusted it off and fixed some things that did not worked on on Enzo code)
But it still would not process "next:next", so for this I changed source code.
For now it set
so comment are left in place.
I use it to impose a structure I could read on someone else's program.
And it really helps, run it over something big / or unformatted and see.
It happens it could help to pick some nesting errors
(and even one dangling "select case" line which really should be reported by JB).
It asks for BAS file, then processes it, writing to mainwin and to OUT.BAS
(so OUT.BAS is ovewritten every time)
If you just set file name (commented out, line 31
)
then it will not ask and run along same file
(useful if you fix / rerun / fix again)
(back then in 2007 for a textbook I needed many simple programs formatted the same,
and they had most unhelpful commenst like "A=A+1 !Adds 1 to A")
then I ajusted it to JB
then got rid of FORTRAN part ;)
It is full of ad-hock things,
formats code to my liking,
(
inserts some emply lines - because I wants some things separated,
does not remove any empty lines - because author likely meant something then putting extra lines
)
and is by no means extensively tested
(thought I dusted it off and fixed some things that did not worked on on Enzo code)
But it still would not process "next:next", so for this I changed source code.
For now it set
stripComments = 0
so comment are left in place.
I use it to impose a structure I could read on someone else's program.
And it really helps, run it over something big / or unformatted and see.
It happens it could help to pick some nesting errors
(and even one dangling "select case" line which really should be reported by JB).
It asks for BAS file, then processes it, writing to mainwin and to OUT.BAS
(so OUT.BAS is ovewritten every time)
If you just set file name (commented out, line 31
'fName$ = "deComment06.bas" 'this very program
)
then it will not ask and run along same file
(useful if you fix / rerun / fix again)
'comment stripper / formatter
'tsh73, as far as 2007 till Dec 2023
'Looks useful if not complete!
'v04 - some fixes
' (:) is token delim as well
' parallel write to "out.bas" (for files too big to copy from a mainwin)
'v05
' ?? somehow "end if" etc should match to unindent.
'v06
' THEN after continuation line
global commentSign$
global stringDelims$
global tokenDelims$
lang$ = "Basic"
select case lang$
case "Basic"
commentSign$ = "'" 'Basic
stringDelims$ = chr$(34)
tokenDelims$ = " :"
indentThings$ = "FOR|SELECT|DO|WHILE"
unindentThings$ = "END IF|END SELECT|END SUB|END FUNCTION|NEXT|LOOP|WEND"
elseThings$ = "ELSE|CASE"
subThings$ = "SUB|FUNCTION"
case else
print "Unsupported language - ";lang$
end
end select
'fName$ = "deComment06.bas" 'this very program
if fName$ ="" then
filedialog "Open BAS program", "*.bas", fName$
if fName$="" then
notice "No file chosen!"
end
end if
end if
stripComments = 1
stripComments = 0 'Leave the comments. Comment out and it'll remove it
IndentSize = 4 'LB\JB default
lNum = 0
oNum = 0
Indent = 1
open fName$ for input as #1
open "out.bas" for output as #2
contLine$=""
while eof(#1)=0
line input #1, aLine$
lNum = lNum + 1
'print lNum, aLine$
commPos = GetCommPos(aLine$)
bLine$ = aLine$
if commPos <> 0 then
bLine$ = left$(aLine$, commPos-1)
end if
'print lNum, bLine$
cLine$ = trim$(bLine$)
'line continuation
if right$(cLine$,1)= "_" then
contLine$=contLine$+ left$(upper$(cLine$), len(cLine$)-1)
'and clears at end of main loop
end if
dLine$ = trim$(aLine$) 'with comments preserved
'print lNum, cLine$
if stripComments then
out$=cLine$
else
out$=dLine$
end if
' if out$<>"" then 'strips empty lines
if 1 then
'get standalone JB labels be preceded by empty line
isStandAloneLabel = (left$(cLine$,1) = "[" and right$(cLine$,1) = "]")
if isStandAloneLabel then
oNum = oNum + 1
print , ""
print #2, ""
end if
'get sub/function be preceded by epmpty line
' isSubThingStart = isContainsOneOf(upper$(cLine$),subThings$, "|" )<>0 _
'thing above needed for things like Real Function f(x). (fortran) But it breaks for GOSUB (Basic).
isSubThingStart = isStartsWithOneOf(upper$(cLine$),subThings$, "|" )<>0 _
and isContainsOneOf(upper$(cLine$),"END|EXIT|DECLARE", "|" )=0
if isSubThingStart then
oNum = oNum + 1
print , ""
print #2, ""
end if
oNum = oNum + 1
'decrease indent - before printing
if isStartsWithOneOf(upper$(cLine$), unindentThings$, "|") then
Indent = Indent - 1
end if
'"else" is special - like temp -1 just for this line
if isStartsWithOneOf(upper$(cLine$), elseThings$, "|") then
elseIndent = 1
else
elseIndent = 0
end if
if isStandAloneLabel then 'labels go without an indent
'print lNum, out$
print out$
print #2, out$
else
'print lNum, space$(IndentSize*(Indent-elseIndent));out$
print space$(IndentSize*(Indent-elseIndent));out$
print #2, space$(IndentSize*(Indent-elseIndent));out$
end if
'for JB - no good if it has no space left for labels
if Indent < 1 then Indent = 1
'but increasing after
if isStartsWithOneOf(upper$(cLine$), indentThings$, "|") _
or isSubThingStart _
or (isStartsWith(contLine$+upper$(cLine$), "IF") and isEndsWith(contLine$+upper$(cLine$), "THEN")) then
Indent = Indent + 1
end if
end if
if right$(cLine$,1)<> "_" then 'after possible use
contLine$=""
end if
wend
print "----------------------------------------"
print "Source file: ";fName$
print lNum, "lines total ", oNum, "lines output"
print "Copied to OUT.BAS as well"
close #2
close #1
input "Press ENTER to end";dummy$
'------------------------------------------------
function GetCommPos(aLine$)
startPos = 1
in = 0
do while 1
aPos = instr( aLine$, commentSign$, startPos) 'actually should check for in-string constants thing
if aPos = 0 then 'no comments
exit do
end if
'or we should check if we are inside a string. If there is odd number of stringDelims$ to the left then indeed we are
for i = startPos to aPos - 1
if instr(stringDelims$, mid$(aLine$, i,1))<>0 then
in = 1-in 'flip it
end if
next
if in then 'indeed thos position is inside a string!
startPos = aPos+1 'go look farther
else
exit do
end if
loop
GetCommPos = aPos
end function
'------------------------------------------------
function isContainsOneOf(BigStr$, toFinds$, delim$)
i = 1
do while word$(toFinds$, i, delim$) <>""
aWord$ = word$(toFinds$, i, delim$)
if instr(" "+BigStr$+" ", " "+aWord$+" ") then
isContainsOneOf = 1
exit function
end if
i = i+1
loop
isContainsOneOf = 0 'no find
end function
'------------------------------------------------
function isStartsWithOneOf(BigStr$, toFinds$, delim$)
i = 1
do while word$(toFinds$, i, delim$) <>""
aWord$ = word$(toFinds$, i, delim$)
if isStartsWith(BigStr$, aWord$) then
isStartsWithOneOf = 1
exit function
end if
i = i+1
loop
isStartsWithOneOf = 0 'no find
end function
'------------------------------------------------
function isStartsWith(BigStr$, toFind$)
mLen = len(toFind$)
'isStartsWith = (mid$(BigStr$,1,mLen) = mid$(toFind$,1,mLen))
'now problem is that elseThing should not be considered ELSE
nextLetter$ = mid$(BigStr$,mLen+1,1)
isNextDelimiter = (nextLetter$ ="") or (instr(tokenDelims$,nextLetter$)<>0)
isStartsWith = (left$(BigStr$,mLen) = toFind$) and isNextDelimiter
'special case with END <spaces> STUFF
if instr(toFind$, "END ")=1 then 'going to skip extra spaces
if instr(BigStr$, "END ")=1 then
wrd2$=word$(toFind$,2) 'IF, SUB etc
'find that, after beginning part
str2$=trim$(mid$(BigStr$,len("END ")+1))
if instr(str2$, wrd2$)=1 then isStartsWith = 1
end if
end if
end function
'------------------------------------------------
function isEndsWith(BigStr$, toFind$)
mLen = len(toFind$)
isEndsWith = (right$(BigStr$,mLen) = toFind$)
end function