Code Formatter tool?

Miscellaneous Forums/General Discussion/Code Formatter tool?

Here's some old B2D code...

dropped$ = Lower$(Replace$(CommandLine$(), Chr$(34), ""))

If FileType(dropped$) <> 2 Then RuntimeError "'" + dropped$ + "' is Not a directory"

dir = ReadDir(dropped$)

bb_file = 0
c = 0
op$ = "*/+-=><"

file$ = NextFile$(dir)

While file$ <> ""
	indent = 0
	if_flag = 0
	case_flag = 0
	select_flag = 0
	out$ = ""
		
	If Lower$(Right$(file$, 3)) = ".bb"
		bb_file = bb_file + 1
		
		in = ReadFile(dropped$ + "" + file$)
		If (Not in) Then RuntimeError "Couldn't open '" + file$ + "'"
		
		out$ = Replace(out$, "End If", "EndIf")
		
		While (Not Eof(in))
			lin$ = Trim$(ReadLine$(in)) + Chr$(13) + Chr$(10)
			l$ = Left$(lin$, 1)

			Select l$
				Case ""
									
				Case ";"
										
				Default
					Select command$(lin$)
						Case "If"
							If Instr(lin$, " Then ") = 0
								out$ = out$ + String$(Chr$(9), indent) + lin$ 
								indent = indent + 1
								if_flag = if_flag + 1
							EndIf

						Case "For", "Repeat", "While", "Function", "Type"
							out$ = out$ + String$(Chr$(9), indent) + lin$ 
							indent = indent + 1
							
						Case "Select"
							out$ = out$ + String$(Chr$(9), indent) + lin$ 
							indent = indent + 1
							select_flag = 1

						Case "Case"
							If (case_flag = 0) Or (select_flag > 0)
								out$ = out$ + String$(Chr$(9), indent) + lin$
								indent = indent + 1
								case_flag = case_flag + 1
								select_flag = 0
							Else
								out$ = out$ + String$(Chr$(9), indent - 1) + lin$
							EndIf
						
						Case "Default"
							If case_flag
								out$ = out$ + String$(Chr$(9), indent - 1) + lin$
							EndIf

						Case "Else", "ElseIf"
							If if_flag > 0
								out$ = out$ + String$(Chr$(9), indent - 1) + lin$
							EndIf
							
						Case "EndIf"
							If if_flag > 0
								if_flag = if_flag - 1
								If indent > 0 Then indent = indent - 1
								out$ = out$ + String$(Chr$(9), indent) + lin$
							EndIf
							
						Case "Next", "Until", "Wend", "End"
							If command$(lin$, 5) = "Select"
								If case_flag > 0
									case_flag = case_flag - 1
									If indent > 0 Then indent = indent - 1
								EndIf
								
								select_flag = 0
							EndIf
								
							If indent > 0 Then indent = indent - 1
							out$ = out$ + String$(Chr$(9), indent) + lin$
						
						Default
							out$ = out$ + String$(Chr$(9), indent) + lin$
					End Select
			End Select
		Wend		
		
		CloseFile in
		
		out$ = Replace(out$, ",", ", ")
		For c=1 To Len(op$)
			out$ = Replace(out$, Mid$(op$, c, 1), " " + Mid$(op$, c, 1) + " ")
		Next
		
		For c=10 To 2 Step -1
			out$ = Replace(out$, String$(" ", c), " ")
		Next
		
		out$ = Replace(out$, ">  =", ">=")
		out$ = Replace(out$, "<  =", "<=")
		out$ = Replace(out$, "<  >", "<>")
		
		outfile = WriteFile(dropped$ + "" + file$ + ".txt")
		
		For c=1 To Len(out$)
			WriteByte outfile, Asc(Mid$(out$, c, 1))
		Next
		
		CloseFile outfile
			
		Print "Processed : '" + file$ + "'"
	EndIf	
	
	file$ = NextFile$(dir)
Wend

CloseDir(dir)

Print
Print "Total Files : " + bb_file

WaitKey()

End

Function command$(in$, start=1)
	Local c, out$, ch
	
	For c=start To Len(in$)
		ch = Asc(Mid$(in$, c, 1))
		If ch > 31
			If ch = 32   
				Return out$
			Else
				out$ = out$ + Chr$(ch)
			EndIf
		EndIf
	Next
	
	Return out$
End Function


...Should be easy enough to convert to/for BMax.