Commit dab80c81 by kollo

added new program basformat.bas

parent f49ddeba
#!/usr/bin/xbasic
' Format an X11-Basic Program such that (c) Markus Hoffmann 2016
' a) all unnecessary whitespaces are removes
' b) Commands and BASIC Keywords are in capital letters
' c) Structure formatting by Einrueckungen
'
' Uage: xbasic basformat.bas input.bas -o output.bas
'
' The default output file name is a.bas
'
'
i=1
outputfilename$="a.bas"
WHILE LEN(PARAM$(i))
IF LEFT$(PARAM$(i))="-"
IF PARAM$(i)="--help" OR PARAM$(i)="-h"
@intro
@using
ELSE IF PARAM$(i)="--version"
@intro
QUIT
ELSE IF PARAM$(i)="-o"
INC i
IF LEN(PARAM$(i))
outputfilename$=PARAM$(i)
ENDIF
ENDIF
ELSE
inputfile$=param$(i)
IF NOT EXIST(inputfile$)
PRINT "basformat: "+inputfile$+": file or path not found"
CLR inputfile$
ENDIF
ENDIF
INC i
WEND
IF LEN(inputfile$)
IF EXIST(outputfilename$)
PRINT "basformat: Outputfilename already exists: ";outputfilename$
ELSE
@convert
ENDIF
ELSE
PRINT "basformat: No input files"
ENDIF
QUIT
PROCEDURE intro
PRINT "X11-Basic source formatter and beautifier V.1.00 (c) Markus Hoffmann 2016"
VERSION
RETURN
PROCEDURE using
PRINT "Usage: basformat [options] file..."
PRINT "Options:"
PRINT " -h, --help Display this information"
PRINT " -o <file> Place the output into <file>"
RETURN
PROCEDURE convert
levelin$()=["WHILE","DO","REPEAT","IF","FUNCTION","ELSE","FOR","PROCEDURE"]
levelout$()=["WEND","LOOP","UNTIL","ENDIF","ENDFUNCTION","ELSE","NEXT","RETURN"]
OPEN "O",#2,outputfilename$
OPEN "I",#1,inputfile$
WHILE NOT EOF(#1)
LINEINPUT #1,t$
IF left$(t$)<>"'" and left$(t$)<>"#"
t$=trim$(t$)
ENDIF
IF len(t$)
IF left$(t$)="#" or left$(t$)="'"
@lineout(t$)
ELSE
SPLIT t$," ",TRUE,a$,t$
IF len(t$) and left$(t$)="="
a$=a$+t$
ENDIF
FOR i=0 to DIM?(levelin$())-1
IF upper$(a$)=levelin$(i)
levelflag=TRUE
BREAK
ENDIF
NEXT i
FOR i=0 to DIM?(levelout$())-1
IF upper$(a$)=levelout$(i)
DEC level
BREAK
ENDIF
NEXT i
IF upper$(a$)="RETURN" and len(t$)
INC level
ENDIF
IF len(t$)=0
IF INSTR(a$,"=") or left$(a$)="@" or left$(a$)="~" or right$(a$)=":"
@lineout(a$)
ELSE
@lineout(UPPER$(a$))
ENDIF
ELSE
IF instr(a$,"=")
@lineout(a$+" "+t$)
ELSE
@lineout(UPPER$(a$)+" "+t$)
ENDIF
ENDIF
IF levelflag
INC level
levelflag=0
ENDIF
ENDIF
lastlinewasempty=FALSE
ELSE
IF lastlinewasempty=FALSE
@lineout("")
lastlinewasempty=TRUE
ENDIF
ENDIF
IF level<0
PRINT "' ERROR: something is wrong..."
level=0
ENDIF
INC anzzeilen
WEND
CLOSE #1,#2
PRINT "' numlines=";anzzeilen
RETURN
PROCEDURE lineout(l$)
IF l$=""
PRINT #2
ELSE
PRINT #2,STRING$(level," ")+l$
ENDIF
RETURN
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment