' *******************************************
' *** AMOS Professional De-Coloniser V1.3 ***
' ***          By John.A.Kinsella         ***
' *******************************************

' *** This program seperates multi-statement lines into single statement 
'     lines, as well as removing REMs, and also converts IF...THEN 
'     commands into IF...END IF commands.  

Set Accessory 

INIT_SCREEN

' *** Make sure we are an accessory

If Prg Under<>1
   R=Dialog Box(AL$,2,"This program only work as an accessory!")
   MN_QUIT
End If 

' *** Do buffer check

Ask Editor Equ("AEdAsk_Free")
If Param<5000
   R=Dialog Box(AL$,2,"Please increse program buffer to continue (Try 20k Extra)")
   MN_QUIT
End If 

' *** Ask user to continue 

R=Dialog Box(AL$,2,"De-Colonise the current program, please confirm.")
If R=1
   MN_QUIT
End If 

' *** Question to remove REMs  

NOREM=Dialog Box(AL$,2,"Remove REM statements from program.")

' *** Open all procedures  

Call Editor Equ("AEd_UnfoldAll")

' *** Ask number of lines

Ask Editor Equ("AEdAsk_NumberOfLines")
NLINES=Param

' *** Goto top of text 

Call Editor Equ("AEd_TopOfText")

' *** De-Colonise program  

R=Dialog Box(AL$,0,"De-Colonising program (ESC Quits) ["+Str$(NLINES)+" Lines ]")

Set Input 10,-1
Open Out 2,"RAM:TEMP.ASC"

For N=1 To NLINES
   
   If Inkey$=Chr$(27)
      E$="De-Colonisation aborted."
      Exit 
   End If 
   
   Ask Editor Equ("AEdAsk_CurrentLine"),N
   LINE$=Param$
   
   L$=LINE$
   Gosub NOCOLON
   
   Call Editor Equ("AEd_Down")
   
Next N

If E$<>""
   R=Dialog Box(AL$,1,E$)
   MN_QUIT
End If 

Close 

' *** Erase previous program & merge De-Colonised one. 

R=Dialog Box(AL$,0,"Merging De-Colonised program, please wait...")

Call Editor Equ("AEd_HighlightAll")
Call Editor Equ("AEd_BlocCut")
Call Editor Equ("AEd_Forget")
Call Editor Equ("AEd_MergeAscii"),0,"RAM:TEMP.ASC"

' *** Print message. 

R=Dialog Box(AL$,1,"Now Re-Tokenise the program...")

' *** Quit !!! 

MN_QUIT

Procedure INIT_SCREEN
   
   Global AL$
   Restore ALT
   
   Repeat 
      Read A$
      AL$=AL$+A$
   Until A$=""
   
   Resource Screen Open 0,640,40,0
   Curs Off 
   Flash Off 
   Cls 0
   Screen Display 0,,80,,
   Wait Vbl 
   
   Paper 0
   Pen 1
   Pop Proc
   
   ' *** QUICK RUN DIALOG BOXES 
   
   ALT:
   Data " "
   Data "SIze   SW,SH;"
   Data "BAse   SWidth SX -2/,SHeight SY- 2/;"
   Data "IF     0VA 0=;"
   Data "["
   Data "BOx    0,0,1,SX,SY;"
   Data "POut   1VACX,16,1VA,0,7;"
   Data "BU 0,0,0,SX,SY,0,0,0;[][SM;]"
   Data "]"
   Data "IF     0VA 1=;"
   Data "["
   Data "BOx    0,0,1,SX,SY;"
   Data "POut   1VACX,8,1VA,0,7;"
   Data "BU 1,SX112-,SY16-,96,16,0,0,1;[LI 0,0,41BP3*+,SX;PR 4MECXBP+,1,4ME,7;][BR0;BQ;]"
   Data "KY 27,0;"
   Data "BU 0,0,0,SX,SY,0,0,0;[][SM;]"
   Data "RUn    0,3;"
   Data "]"
   Data "IF     0VA 2=;"
   Data "["
   Data "BOx    0,0,1,SX,SY;"
   Data "POut   1VACX,8,1VA,0,7;"
   Data "BU 1,16,SY16-,96,16,0,0,1;[LI 0,0,41BP3*+,SX;PR 4MECXBP+,1,4ME,7;][BR0;BQ;]"
   Data "KY 13,0;"
   Data "BU 2,SX112-,SY16-,96,16,0,0,1;[LI 0,0,41BP3*+,SX;PR 3MECXBP+,1,3ME,7;][BR0;BQ;]"
   Data "KY 27,0;"
   Data "BU 0,0,0,SX,SY,0,0,0;[][SM;]"
   Data "RUn    0,3;"
   Data "]"
   Data "EXit;"
   Data ""
   
End Proc

Procedure MN_QUIT
   
   Trap Dialog Close 
   Trap Screen Close 0
   
   Erase All 
   Close 
   
   Trap Kill "RAM:TEMP.ASC"
   
   End 
   
End Proc

NOCOLON:

' *** Remove spaces  

TL=1
TM$=" "

While TM$=" "
   TM$=Mid$(L$,TL,1)
   Inc TL
Wend 

Dec TL

L$=Mid$(L$,TL,Len(L$)-TL+1)

' *** This section seperates each command & converts IF...THENs  
'     into IF...END IFs, and also removes REMs if required.  

If(((Left$(L$,3)<>"Rem") and(Left$(L$,1)<>"'") and(NOREM=2)) or(NOREM=1))
   
   QU=0
   M$=""
   P=1
   SM=0
   
   While P<=Len(L$)
      
      If((Mid$(L$,P,2)=" :") or(Mid$(L$,P,4)="Then") or(Mid$(L$,P,4)="Else") or(Mid$(L$,P,7)="Else If")) and QU=0
         
         If Mid$(L$,P,4)="Then"
            TH=1
            P=P+2
         End If 
         
         If Mid$(L$,P,7)="Else If"
            Print #2,"Else ";
            SM=1
            P=P+2
         End If 
         
         If Mid$(L$,P,4)="Else"
            EL=1
            P=P+2
         End If 
         
         If SM=1
            Print #2,M$;
            SM=0
         Else 
            Print #2,M$
         End If 
         
         Inc P
         M$=""
         
         If EL=1
            Print #2,"Else"
            EL=0
         End If 
         
      Else 
         
         If QU=0
            
            If Mid$(L$,P,3)="Rem"
               M$=M$+Mid$(L$,P)
               P=Len(L$)
               Goto NXT
            End If 
            
         End If 
         
         If Mid$(L$,P,1)=Chr$(34)
            QU=1-QU
         End If 
         
         M$=M$+Mid$(L$,P,1)
         
      End If 
      
      Inc P
      
   Wend 
   
   NXT:
   
   If M$<>""
      Print #2,M$
   End If 
   
End If 

If TH=1
   Print #2,"End If"
   TH=0
End If 

Return 






