' Erzeugt die COMMAND.DAT fr den Compiler
' (c)Copyright 2002 by Sebastian Steinhauer

$STACK 1024*4
$LIB ALL OFF
$ERROR ALL OFF
$COM 1
$SOUND 1
$COMPILE EXE

%dINTEGER	= 0
%dSTRING	= 1

TYPE typCOMMAND
	'CmdString	AS STRING*32	'32 Byte CmdString
        CmdRetval	AS INTEGER	'Rckgabewert
        Param(16)	AS INTEGER	'Parameter an die Funktion
        Need(16)	AS INTEGER	'Welche Funktionen werden gebraucht?
        CodeLen		AS INTEGER	'Wie lang ist der ASM-Code
END TYPE

DIM txtfile(1024) AS SHARED STRING	'Speicher fr die Dateinamen
DIM txtcommand(4096) AS SHARED STRING	'Speicher fr die Kommandos
DIM txtfiles AS SHARED INTEGER
DIM txtcommands AS SHARED INTEGER

CLS
FindFiles

Preprocessor
FindCommands
doCommands
del_temp

PRINT
PRINT USING "Found ### commands in ## .TXT files.";txtcommands,txtfiles

'Bearbeitet die Befehle
SUB doCommands
        PRINT "Include commands:"
        OPEN "COMMAND.DAT" FOR OUTPUT AS #2

        PRINT #2,MKI$(txtcommands);

	FOR i%=1 TO txtfiles
        	PRINT "  Processing "+txtfile(i%)+"TXT";
                OPEN "LIB\"+txtfile(i%)+"$$$" FOR INPUT AS #1
                	doCommand
                CLOSE #1
                PRINT "  ok."
        NEXT i%
END SUB

'Arbeitet ein Kommando ab
SUB doCommand
	DIM C AS typCOMMAND

	curCmd%=-1 : curParam%=0 : curNeed%=0
        WHILE NOT EOF(1)
                INPUT #1,Cmd$
                SELECT CASE UCASE$(Cmd$)
                CASE "COMMAND"
                        IF curCmd%<>-1 THEN
                                C.Param(curParam%)=-1
                                C.Need(curNeed%)=-1
                                C.CodeLen=LEN(ASMcode$)
                                PRINT #2,MKI$(LEN(txtcommand(curCmd%)));
                                PRINT #2,txtcommand(curCmd%);
                                PRINT #2,C;
                                PRINT #2,ASMcode$;
                        END IF

                        INPUT #1,Cmd$,DatTyp$
                        curCmd%=FindCmd(Cmd$)
                        ASMcode$="":curParam%=0:curNeed%=0
                        C.CmdRetval=0
                        'PRINT
                        'PRINT "-> ";curCmd%,Cmd$
                CASE "PARAMS"
                        INPUT #1, Cmd$
                        WHILE Cmd$<>"NULL"
                        	'PRINT Cmd$
                                C.Param(curParam%)=GetDataType(Cmd$)
                                INCR curParam%
                                INPUT #1,Cmd$
                        WEND
                CASE "NEEDS"
                	INPUT #1, Cmd$
                        WHILE Cmd$<>"NULL"
                                C.Need(curNeed%)=FindCmd(Cmd$)
                                INCR curNeed%
                                INPUT #1,Cmd$
                        WEND
                CASE "BEGIN_ASM"
                        WHILE NOT EOF(1)
                        	LINE INPUT #1, Cmd$
                                IF Cmd$<>"END_ASM" THEN
                                        ASMcode$=ASMcode$+Cmd$+CHR$(13,10)
                                ELSE
                                	EXIT LOOP
                                END IF
                        WEND
                END SELECT
                IF INKEY$=CHR$(27) THEN END
        WEND

        IF curCmd%<>-1 THEN
		C.Param(curParam%)=-1
        	C.Need(curNeed%)=-1
                C.CodeLen=LEN(ASMcode$)
                PRINT #2,MKI$(LEN(txtcommand(curCmd%)));txtcommand(curCmd%);
        	PRINT #2,C;
        	PRINT #2,ASMcode$;
                'PRINT txtcommand(curCmd%)
        END IF
END SUB

'Sucht ein Kommando im Speicher
FUNCTION FindCmd(Cmd$)
        FOR i%=1 TO txtcommands
        	IF Cmd$=txtcommand(i%) THEN
                	FUNCTION=i%
                        EXIT FUNCTION
                END IF
        NEXT i%
END FUNCTION

'Welcher Datentyp?
FUNCTION GetDataType(Cmd$) AS INTEGER
	IF Cmd$="INTEGER" THEN
		FUNCTION=0
        ELSE
        	FUNCTION=1
        END IF
END FUNCTION

'Preprocessor. Lscht Kommentare und Leerzeilen
SUB Preprocessor
        PRINT "Preprocessing all .TXT files...";
	FOR i%=1 TO txtfiles
                OPEN "LIB\"+txtfile(i%)+"TXT" FOR INPUT AS #1
                OPEN "LIB\"+txtfile(i%)+"$$$" FOR OUTPUT AS #2
                        WHILE NOT EOF(1)
                                LINE INPUT #1, Buf$
                                x%=INSTR(Buf$,";")
                                IF x% > 0 THEN Buf$=LEFT$(Buf$,x%-1)
                                Buf$=TRIM$(Buf$)
                                IF Buf$<>"" THEN PRINT #2,Buf$
                        WEND
                CLOSE #1,#2
        NEXT i%
        PRINT "ok."
END SUB

'Lscht die TEMP-Dateien
SUB del_temp
	FOR i%=1 TO txtfiles
        	KILL "LIB\"+txtfile(i%)+"$$$"
        NEXT i%
END SUB

'Katalogisiert die Kommandos
SUB FindCommands
	PRINT "Find commands:"
	FOR i%=1 TO txtfiles
        	PRINT "  Processing "+txtfile(i%)+"TXT";
                old%=txtcommands
                OPEN "LIB\"+txtfile(i%)+"$$$" FOR INPUT AS #1
                	FindCommand
                CLOSE #1
                PRINT (txtcommands-old%);" commands found."
        NEXT i%
END SUB

'Sucht Kommandos in einer Datei raus
SUB FindCommand
        WHILE NOT EOF(1)
                INPUT #1, Cmd$
                IF UCASE$(Cmd$)="COMMAND" THEN
                	INPUT #1,Cmd$
                        INCR txtcommands
                        txtcommand(txtcommands)=Cmd$
                END IF
        WEND
END SUB

'Sucht die ganzen .TXT Dateien zusammen :)
SUB FindFiles
        File$=DIR$("LIB\*.TXT")
        WHILE File$<>""
                INCR txtfiles
                txtfile(txtfiles)=LEFT$(File$,LEN(File$)-3)
                File$=DIR$
        WEND
END SUB