Page 1 of 1

Indentation editor

Posted: Wed Dec 30, 2015 8:13 pm
by smbstarv
Though automatic indentation may soon be available in the sB editor, I tried to program one myself.
Part i selects a file ; this should be a syntactically correct sB file.
Part ii rewrites this file as an indented file of the same name extended with ".ind.txt"

Code: Select all

         
REM ****** indentation editor for smartBasic, by smbstarv; 30-12-2015
'
'g'    part i  selects a FILE. This FILE must be a syntactically correct sB program
         
'r'    part ii rewrites that file with indented lines, as a file with same name and postfix ".ind.txt"
         
'g'  part i : SELECTION of FILE
         DIM dn$(100)
         DIM fns$(100)
         OPTION BASE 0
         SET OUTPUT FONT SIZE 15
     IL0:
         TEXT CLEAR
         PRINT
         PRINT "             INDENTATION EDITOR"
         PRINT
         n$="/"
     IL1:
         DIR n$ SET
         DIR n$ LIST DIRS dn$,dna
         DIR n$ LIST FILES fns$,fna
         FOR i=1 TO dna
                 PRINT i,dn$(i-1),,"(folder)"
                 IF (i)%10 =0 THEN
                         PRINT "more......"
                         INPUT "select...":x$
                         TEXT CLEAR
                         KEUZE=VAL(x$)
                         IF KEUZE>0 THEN GOTO IL3
                         IF KEUZE<0 THEN GOTO IL0
                 END IF
         NEXT i
         FOR j=1 TO fna
                 PRINT j+i-1,fns$(j-1)
                 IF (i-1+j)%10=0 THEN
                         PRINT "more......"
                         INPUT "select...":x$
                     1234 TEXT CLEAR
                         KEUZE=VAL(x$)
                         IF KEUZE>0 THEN GOTO IL3
                         IF KEUZE<0 THEN GOTO IL0
                 END IF
         NEXT j
         PRINT
         PRINT "Select... "
         INPUT "Select.....":KEUZE
     IL3:
         IF KEUZE=99 THEN STOP
         IF KEUZE<1 OR KEUZE>(dna+fna) THEN
                 TEXT CLEAR
                 GOTO IL0
         END IF
         IF KEUZE <= dna THEN
                 n$=n$&"/"&dn$(KEUZE-1)
                 TEXT CLEAR
                 GOTO IL1
         END IF
         IF KEUZE>dna THEN
                 TEXT CLEAR
                 fname$=fns$(KEUZE-1-dna)
             IL5:
                 PRINT "              FILE :"; fname$
                 fnameout$=fname$&".ind.txt"
                 FILE fnameout$ DELETE
                 PRINT "Wait: ..";
         END IF
         
'r'  part ii : WRITE indented sB FILE TO new FILE WITH same NAME WITH ".ind.txt" postfix
         
         OPTION BASE 1
         dent1=10               'initiele indentatie
         dent2=10
         dt=8                        ' extra indentatie
         temp=0
         id$=""                     ' gelezen identifier
         sptr=0                   ' pointer naar symbool in een regel
         regel$="REM*******"&fnameout$&" is automatically indented ******"
         
         DO
                 GOSUB progress
                 previd$=LOWSTR$(id$)
                 GOSUB identifier
                 id$=LOWSTR$(id$)
                 IF previd$="end" THEN id$="end"&id$
                 IF CHR$(delim)=":" THEN temp=-4
                 IF VAL(id$)>0 AND previd$="" THEN temp=-4
                 IF id$="then" AND (delim=0 OR delim=33 OR delim=39) THEN dent2=dent1+dt
                 IF id$="do" OR id$="while" OR id$="for" OR id$="def" THEN dent2=dent1+dt
                 IF id$="endif" OR id$="until" OR id$="endwhile" OR id$="next" OR id$="enddef" THEN
                         dent1-=dt
                         dent2=dent1
                 END IF
                 IF id$="else" AND (delim=0 OR delim=33 OR delim=39) THEN
                         dent2=dent1
                         dent1-=dt
                 END IF
                 IF id$="def" THEN dent2=dent1+dt
                 IF id$="rem" THEN
                         dent1=0
                         sptr=0
                 END IF
         UNTIL FILE_END (fname$)
         GOSUB printregel
         GOTO klaar
'
     PRINTREGEL:
         FILE fname$&".ind.txt" PRINT tab (dent1+temp); regel$
         dent1=dent2
         temp=0
         RETURN
'
     LEESSYMB:          'leest een symbool
     ls: IF sptr=0 THEN
                 GOSUB printregel
             ls2:
                 IF FILE_END(fname$) THEN GOTO klaar ELSE FILE fname$ READLINE regel$
                 regel$=TRIM$(regel$)
                 previd$=""
                 rlnr+=1
                 IF ASC(regel$)=ASC("''")  OR ASC(regel$)=ASC("{") THEN
                         FILE fname$&".ind.txt" PRINT regel$
                         GOTO ls2
                 END IF
                 rl=LEN(regel$)
         END IF
         sptr+=1
     ns:
         symb=ASC(regel$,sptr)
         symb2=ASC(regel$,sptr+1)
         IF sptr>rl THEN  sptr=0
'
         IF commblock=0 AND symb=47 AND symb2=42 THEN
                 commblock=1
                 RETURN
         END IF                   'commentaarblok begin
         IF symb=42 AND symb2=47 THEN
                 commblock=0
                 RETURN
         END IF                                                         ' commentaarblok einde
         IF instring=0 AND symb=39 THEN
                 commline=1
                 RETURN
         END IF                                                            '  commentaarregel tot EOL of
         IF symb=0 THEN
                 commline=0
                 RETURN
         END IF                                                                     '  bij of  EOL einde commentaarregel
         
         IF symb=33 AND NOT instring AND NOT commblock THEN     'split multicommand-line
                 rg1$=LEFT$(regel$,sptr-1)
                 rg2$=MID$(regel$,sptr+1)
                 regel$=rg1$
                 GOSUB printregel
                 regel$=rg2$
                 sptr=1
         END IF
         
         IF instring=0 AND symb=34 THEN
                 instring=1
                 RETURN
         END IF                                                              '  begin string
         IF instring=1 AND symb=34 AND symb2=34 THEN
                 instring=1
                 RETURN
         END IF                                ' double quote in string
         IF instring=1 AND symb=34 AND symb2<>34 THEN
                 instring=0
                 RETURN
         END IF                               ' einde string
         RETURN    ' ls
'
     IDENTIFIER:
         GOSUB leessymb
         IF commblock OR commline THEN GOTO identifier
         IF instring THEN GOTO identifier
         IF iscyfer(symb) THEN GOTO numlab
         IF isletter(symb)=0 THEN GOTO identifier
         id$=CHR$(symb)
     id1:
         GOSUB leessymb
         IF idsymb(symb) THEN
                 id$=id$&CHR$(symb)
                 GOTO id1
         ELSE
                 WHILE symb=32 AND symb2=32
                         GOSUB leessymb
                 END WHILE
                 IF symb=32 THEN delim=symb2  ELSE delim=symb
         END IF
         RETURN    'identifier
     numlab:
         id$=CHR$(symb)
     nl1:
         GOSUB leessymb
         IF iscyfer(symb) THEN
                 id$=id$&CHR$(symb)
                 GOTO nl1
         ELSE
                 IF symb=32 OR symb=0 THEN
                         RETURN
                 ELSE
                         GOTO identifier
                 END IF
         END IF
'
         DEF idsymb(s)
                 IF s=ASC("$") THEN a=1 ELSE a=0
                 idsymb=OR(OR(isletter(s),iscyfer(s)),a)
         END DEF
         
         DEF ISLETTER(s)
                 IF  (s=>97 AND s=<122) OR (s=>65 AND  s=<90  ) OR s=95 OR s=46 THEN isletter=1 ELSE isletter=0    'letters, underscore of punt
         END DEF
         
         DEF ISCYFER(s)
                 IF s=>48 AND s=<57 THEN iscyfer=1 ELSE iscyfer=0
         END DEF
         
     progress:
         IF TIME()-t >1 THEN
                 PRINT ".";
                 t=TIME()
         END IF
         RETURN
'
     klaar:
         PRINT "DONE"
         END
         

Re: Indentation editor

Posted: Thu Dec 31, 2015 9:34 am
by smbstarv
A small correction added ....
..

Code: Select all

     
REM ****** indentation editor for smartBasic, by smbstarv; 30-12-2015, corr 31-12-2015
'
'g'    part i  selects a FILE. This FILE must be a syntactically correct sB program
         
'r'    part ii rewrites that file with indented lines, as a file with same name and postfix ".ind.txt"
         
'g'  part i : SELECTION of FILE
         DIM dn$(100)
         DIM fns$(100)
         OPTION BASE 0
         SET OUTPUT FONT SIZE 15
     IL0:
         TEXT CLEAR
         PRINT
         PRINT "             INDENTATION EDITOR"
         PRINT
         n$="/"
     IL1:
         DIR n$ SET
         DIR n$ LIST DIRS dn$,dna
         DIR n$ LIST FILES fns$,fna
         FOR i=1 TO dna
                 PRINT i,dn$(i-1),,"(folder)"
                 IF (i)%10 =0 THEN
                         PRINT "more......"
                         INPUT "select...":x$
                         TEXT CLEAR
                         KEUZE=VAL(x$)
                         IF KEUZE>0 THEN GOTO IL3
                         IF KEUZE<0 THEN GOTO IL0
                 END IF
         NEXT i
         FOR j=1 TO fna
                 PRINT j+i-1,fns$(j-1)
                 IF (i-1+j)%10=0 THEN
                         PRINT "more......"
                         INPUT "select...":x$
                     1234 TEXT CLEAR
                         KEUZE=VAL(x$)
                         IF KEUZE>0 THEN GOTO IL3
                         IF KEUZE<0 THEN GOTO IL0
                 END IF
         NEXT j
         PRINT
         PRINT "Select... "
         INPUT "Select.....":KEUZE
     IL3:
         IF KEUZE=99 THEN STOP
         IF KEUZE<1 OR KEUZE>(dna+fna) THEN
                 TEXT CLEAR
                 GOTO IL0
         END IF
         IF KEUZE <= dna THEN
                 n$=n$&"/"&dn$(KEUZE-1)
                 TEXT CLEAR
                 GOTO IL1
         END IF
         IF KEUZE>dna THEN
                 TEXT CLEAR
                 fname$=fns$(KEUZE-1-dna)
             IL5:
                 PRINT "              FILE :"; fname$
                 fnameout$=fname$&".ind.txt"
                 FILE fnameout$ DELETE
                 PRINT "Wait: ..";
         END IF
         
'r'  part ii : WRITE indented sB FILE TO new FILE WITH same NAME WITH ".ind.txt" postfix
         
         OPTION BASE 1
         dent1=10               'initiele indentatie
         dent2=10
         dt=8                        ' extra indentatie
         temp=0
         id$=""                     ' gelezen identifier
         sptr=0                   ' pointer naar symbool in een regel
         regel$="REM*******"&fnameout$&" is automatically indented ******"
         
         DO
                 GOSUB progress
                 previd$=LOWSTR$(id$)
                 GOSUB identifier
                 id$=LOWSTR$(id$)
                 IF previd$="end" THEN id$="end"&id$
                 IF CHR$(delim)=":" THEN temp=-4
                 IF VAL(id$)>0 AND previd$="" THEN temp=-4
                 IF id$="then" AND (delim=0 OR delim=33 OR delim=39) THEN dent2=dent1+dt
                 IF id$="do" OR id$="while" OR id$="for" OR id$="def" THEN dent2=dent1+dt
                 IF id$="endif" OR id$="until" OR id$="endwhile" OR id$="next" OR id$="enddef" THEN
                         dent1-=dt
                         dent2=dent1
                 END IF
                 IF id$="else" AND (delim=0 OR delim=33 OR delim=39) THEN
                         dent2=dent1
                         dent1-=dt
                 END IF
                 IF id$="def" THEN dent2=dent1+dt
                 IF id$="rem" THEN
                         dent1=0
                         sptr=0
                 END IF
         UNTIL FILE_END (fname$)
         GOSUB printregel
         GOTO klaar
'
     PRINTREGEL:
         FILE fname$&".ind.txt" PRINT tab (dent1+temp); regel$
         dent1=dent2
         temp=0
         RETURN
'
     LEESSYMB:          'leest een symbool
     ls: IF sptr=0 THEN
                 GOSUB printregel
             ls2:
                 IF FILE_END(fname$) THEN GOTO klaar ELSE FILE fname$ READLINE regel$
                 regel$=TRIM$(regel$)
                 previd$=""
                 rlnr+=1
                 IF ASC(regel$)=ASC("''")  OR ASC(regel$)=ASC("{") THEN
                         FILE fname$&".ind.txt" PRINT regel$
                         GOTO ls2
                 END IF
                 rl=LEN(regel$)
         END IF
         sptr+=1
     ns:
         symb=ASC(regel$,sptr)
         symb2=ASC(regel$,sptr+1)
         IF sptr>rl THEN  sptr=0
'
         IF commblock=0 AND symb=47 AND symb2=42 THEN
                 commblock=1
                 RETURN
         END IF                   'commentaarblok begin
         IF symb=42 AND symb2=47 THEN
                 commblock=0
                 RETURN
         END IF                                                         ' commentaarblok einde
         IF instring=0 AND symb=39 THEN
                 commline=1
                 RETURN
         END IF                                                            '  commentaarregel tot EOL of
         IF symb=0 THEN
                 commline=0
                 RETURN
         END IF                                                                     '  bij of  EOL einde commentaarregel
         
         IF symb=33 AND NOT instring AND NOT commblock THEN     'split multicommand-line
                 rg1$=LEFT$(regel$,sptr-1)
                 rg2$=MID$(regel$,sptr+1)
                 regel$=rg1$
                 GOSUB printregel
                 regel$=rg2$
                 sptr=1
         END IF
         
         IF instring=0 AND symb=34 THEN
                 instring=1
                 RETURN
         END IF                                                              '  begin string
         IF instring=1 AND symb=34 AND symb2=34 THEN
                 instring=1
                 RETURN
         END IF                                ' double quote in string
         IF instring=1 AND symb=34 AND symb2<>34 THEN
                 instring=0
                 RETURN
         END IF                               ' einde string
         RETURN    ' ls
'
     IDENTIFIER:
         GOSUB leessymb
         IF commblock OR commline THEN GOTO identifier
         IF instring THEN GOTO identifier
         IF iscyfer(symb) THEN GOTO numlab
         IF isletter(symb)=0 THEN GOTO identifier
         id$=CHR$(symb)
     id1:
         GOSUB leessymb
         IF idsymb(symb) THEN
                 id$=id$&CHR$(symb)
                 GOTO id1
         ELSE
                 WHILE symb=32 AND symb2=32
                         GOSUB leessymb
                 END WHILE
                 IF symb=32 THEN delim=symb2  ELSE delim=symb
         END IF
         RETURN    'identifier
     numlab:
         id$=CHR$(symb)
     nl1:
         GOSUB leessymb
         IF iscyfer(symb) THEN
                 id$=id$&CHR$(symb)
                 GOTO nl1
         ELSE
                 IF symb=32 OR symb=0 THEN
                         RETURN
                 ELSE
                         GOTO identifier
                 END IF
         END IF
'
         DEF idsymb(s)
                 IF s=ASC("$") THEN a=1 ELSE a=0
                 idsymb=OR(OR(isletter(s),iscyfer(s)),a)
         END DEF
         
         DEF ISLETTER(s)
                 IF  (s=>97 AND s=<122) OR (s=>65 AND  s=<90  ) OR s=95 OR s=46 THEN isletter=1 ELSE isletter=0    'letters, underscore of punt
         END DEF
         
         DEF ISCYFER(s)
                 IF s=>48 AND s=<57 THEN iscyfer=1 ELSE iscyfer=0
         END DEF
         
     progress:
         IF TIME()-t >1 THEN
                 PRINT ".";
                 t=TIME()
         END IF
         RETURN
'
     klaar:
         GOSUB printregel
         PRINT "DONE"
         END
         
         

Re: Indentation editor

Posted: Thu Dec 31, 2015 4:39 pm
by rbytes
Nice job!