! optimizer
! BASIC! program to automatically merge and optimize INCLUDE files 
! from development .bas file into one production file
! 
!
!
! Note: Only merges INCLUDEs of main .bas file, not nested ones.
! Limitations:
!  double exclamation comment sections are not taken out
!  function names in original .bas files should not begin with  _ underscores 
! function names inside quotes are converted too
!       
! be careful not too overwrite your original file or 
! other important .bas files!
!
!

! optimizer library

FN.DEF grab$(f$)
 TEXT.OPEN r,h,f$
 s$=""
 WHILE !eof

  TEXT.READLN h,buf$
  TEXT.EOF h,eof
  IF !eof THEN s$+=buf$+"\n"
 REPEAT
 FN.RTN s$
FN.END



FN.DEF hx$(x)
 LET a$="1234567890abcdefghijklmnopqrstuvwxyz"
 LET z=LEN(a$)

 IF x>z*z 
  POPUP "too many functions!"
  EXIT
 ENDIF
 LET d1$=MID$(a$,MOD(x,z)+1,1)
 LET x=FLOOR(x/z)
 IF x
  LET d2$=MID$(a$,MOD(x,z)+1,1)
 ENDIF
 FN.RTN d2$+d1$
FN.END

FN.DEF optfn$(p$)

 LIST.CREATE s, funcr

 ! scan for function names
 DO 

  a=IS_IN("\n",p$,olda+1)
  IF !a THEN D_U.BREAK
  a$=MID$(p$,olda,a-olda+1)
  olda=a

  LET a$=TRIM$(a$)
  IF STARTS_WITH("fn.def ",LOWER$(a$)) 
   fcount++
   LET a$=nocasereplace$(a$,"fn.def ","")
   LET i=IS_IN("(",a$)
   LET fname$=LOWER$(MID$(a$,1,i-1))

   IF LEN(fname$)>3
    LIST.SIZE funcr,siz
    IF siz>1
     LET j=0
     DO 
      j++
      LIST.GET funcr,j,l$
     UNTIL LEN(fname$)>=LEN(l$) | j>=siz
     LIST.INSERT funcr,j,fname$
    ELSE
     LIST.ADD funcr,fname$
    ENDIF
    !    PRINT fname$
   ENDIF 
  ENDIF
 UNTIL done


 prog$=p$
 LET oldl=LEN(prog$)
 ! replace function names 
 LIST.SIZE funcr, size
 FOR i = 1 TO size
  LIST.GET funcr, i, oldf$
  LET newf$="_"+HX$(i)
  IF IS_IN("$",oldf$) THEN newf$+="$"
  prog$=nocasereplace$(prog$, oldf$+"(", newf$+"(")
  prog$=nocasereplace$(prog$, oldf$+" (", newf$+"(")

  PRINT "replaced "+oldf$
 NEXT 
 LET newl=LEN(prog$)


 PRINT INT$(100*(oldl-newl)/newl)+"% saved!"

 FN.RTN prog$
FN.END

FN.DEF stripws$(b$)

 SPLIT bb$[],b$,"\n"
 ARRAY.LENGTH z,bb$[]
 FOR i=1 TO z
  bb$[I]=TRIM$(bb$[I])
  IF STARTS_WITH("!!",bb$[i])
   skip=!skip
   F_N.CONTINUE
  ENDIF
  IF skip THEN F_N.CONTINUE

  IF STARTS_WITH("!",bb$[i])| LEN(bb$[i])<2 | STARTS_WITH("%",bb$[i])
   F_N.CONTINUE
  ENDIF
  x$+=bb$[i]+"\n"
 NEXT 

 FN.RTN x$
FN.END


FN.DEF opt$(f$)
 ! pre: f$ is .bas filename
 ! post: returns optimized program string

 TEXT.OPEN r,fn,f$
 q$=CHR$(34)
 DO
  TEXT.READLN FN, a$
  IF a$="EOF" THEN D_U.BREAK
  LET a$=TRIM$(a$)
  LET hasinc=( STARTS_WITH("INCLUDE",A$) | STARTS_WITH("include",a$))
  LET hasbas= (IS_IN(".BAS",A$) | IS_IN(".bas",a$))
  IF hasinc & hasbas & !STARTS_WITH("!",a$)
   temp$=a$
   LET A$=REPLACE$(A$,"INCLUDE ","")
   LET A$=REPLACE$(A$,"include ","")
   LET A$=REPLACE$(A$,Q$,"")
   LET a$=TRIM$(a$)
   LET a$="../source/"+A$
   PRINT a$+"...merging"
   FILE.EXISTS e,a$
   IF e
    i$=grab$(A$)
    a$=i$
   ELSE
    PRINT "WARNING: "+a$+" not found!"
    a$=temp$
   ENDIF
  ELSEIF STARTS_WITH("! ",A$) | STARTS_WITH("REM ",A$) | STARTS_WITH("rem ",A$) | LEN(a$)<2
   a$=""


  ENDIF
  o$+=a$+"\n"


 UNTIL A$="EOF"
 TEXT.CLOSE fn
 ! PRINT o$
 st=CLOCK()
 o$=optfn$(o$)
 POPUP STR$((CLOCK()-st)/1000)+" secs"
 cr$=CHR$(13)
 lf$=CHR$(10)

 ! o$=replace$(o$, cr$+lf$, lf$)
 o$=stripws$(o$)

 FN.RTN o$


FN.END



!

FN.DEF nocasereplace$(s$,f$, r$)
 LET start=1
 LET zf=LEN(f$)
 LET zr=LEN(r$)
 LET ff$=LOWER$(f$)
 LET ss$=LOWER$(s$)
 DO 
  LET i=IS_IN(ff$,ss$,start)

  IF !i THEN D_U.BREAK
  news$+=MID$(s$,start,i-start)+r$
  LET start=i+zf
 UNTIL !i
 news$+=MID$(s$,start,LEN(s$)-start+1)
 FN.RTN news$

FN.END

FN.DEF cask(m$)
 DIALOG.MESSAGE,m$,c,"yes","no"
 IF c=1 THEN FN.RTN 1
 FN.RTN 0
FN.END

FN.DEF writeln(f$, msg$) 
 TEXT.OPEN w, fh, f$
 TEXT.WRITELN fh, msg$
 TEXT.CLOSE fh 
FN.END

FN.DEF readln$(f$) 
 FILE.EXISTS isoldfile, f$ 
 IF isoldfile
  TEXT.OPEN r,fh3,f$
  TEXT.READLN fh3, a$
 ELSE
  LET A$=""
 ENDIF
 FN.RTN a$
FN.END

FN.DEF choosefile$(path$)
!!
This program can be used to
explore the file system on an
Android device. 

Any filename with a (d) after
it is a directory. Tap on that
entry to open that directory.

Tap on the top entry, "..", to
move up one directory level.

Press the BACK key to exit.

This program will list all the
way up to the file system root.

The initial directory shown is
"/sdcard/rfo-basic/data/"

!!

 ! Lists a directory. Gets and
 ! returns the user's selection

 dloop:

 ! Get the listing from
 ! the path directory
 ! and sort it

 ARRAY.DELETE d1$[]
 FILE.DIR path$, d1$[]
 ARRAY.LENGTH length, d1$[]

 ! Copy the file list
 ! adding the top ",,"
 ! entry

 ARRAY.DELETE d2$[]
 DIM d2$[length+1]
 d2$[1] = ".."
 FOR i = 1 TO length
  d2$[i + 1] = d1$[i]
 NEXT i

 ! Present the list
 ! and get the user's
 ! choice
 s=0
 LIST.CREATE s,l
 ARRAY.LENGTH z,d2$[]
 FOR i=1 TO z
  LIST.ADD l,d2$[i]
 NEXT
 !asklist$(l,"select file",&s)
 SELECT s, d2$[], ""
 IF s = 0 THEN END

 ! If top entry, "..", not
 ! selected then append
 ! the selected directory
 ! name to the path

 IF s>1
  n = IS_IN("(d)", d2$[s])
  IF n = 0
   FN.RTN path$+d2$[s]
   END
  ENDIF
  dname$ = LEFT$(d2$[s],n-1)
  path$=path$+dname$+"/"
  GOTO dloop
 ENDIF

 ! If s = 1 then must back
 ! one level

 ! if at start path then
 ! back up one level

 IF path$ = ""
  path$ = "../"
  GOTO dloop
 ENDIF

 ! Not at start path
 ! split the path by
 ! the "/" chars

 ARRAY.DELETE p$[]
 SPLIT p$[], path$, "/"
 ARRAY.LENGTH length, p$[]

 ! If the last entry is
 ! ".." then add "../"
 ! to back up one level

 IF p$[length] = ".."
  path$ = path$ + "../"
  GOTO dloop
 ENDIF

 ! Last entry is not ".."
 ! so must delete the
 ! last directory from
 ! the path

 ! If only one entry
 ! then path is back
 ! to the base path

 IF length = 1
  path$ = ""
  GOTO dloop
 ENDIF

 ! Reconstruct path without
 ! the last directory

 path$ = ""
 FOR i = 1 TO length - 1
  path$ = path$ + p$[i] + "/"
 NEXT i

 GOTO dloop
FN.END

FN.DEF ISOLD(f$) 
 FILE.EXISTS isoldfile, f$ 
 FN.RTN ISOLDFILE 
FN.END

FN.DEF pickfile$()
 ok=2
 f$=readln$("lastmerge.txt")
 IF f$<>""
  DIALOG.MESSAGE , "use "+f$,ok,"yes","no"
 ENDIF
 IF ok=2

  F$=ChooseFile$("../../rfo-basic/source/")  
  writeln("lastmerge.txt",f$)
 ENDIF


 IF !ISOLD(f$) 
  PRINT F$+" not existing.."
  EXIT
 ENDIF
 FN.RTN f$
FN.END


FN.DEF main()
 f$=pickfile$()
 PRINT f$

 o$=opt$(f$)
 f$=REPLACE$(f$,".bas","_opt.bas")
 f$=REPLACE$(f$,".BAS","_opt.BAS")
 PRINT "writing.."+f$
 writeln(f$,o$)
 POPUP "done!"
 PRINT "tap to exit"
 DO
  PAUSE 100
 UNTIL t
FN.END

main()
EXIT

ONCONSOLETOUCH:
 t=1
CONSOLETOUCH.RESUME
