REM
REM Analyze redcode directory... 2/22/09 WTN
REM
REM Prompt for directory of .red files then prints total counts
REM for each instruction, modifier, A and B address modes and
REM percentage of A and B data that's +/- warrior length.
REM For the purpose of determining optimum weighting parameters
REM when evolving. The tricky part of this is separating the
REM A and B data fields, can't just count the strings. To avoid
REM distorting the A/B data ratios and DAT counts from use "spacers"
REM in some human-written code, 0's in DAT statements aren't counted
REM in the number totals and DAT 0,0 is totally ignored.
REM
REM Note - warrior files must have base names that are
REM 8 characters or less with a .RED extension. This is dos...
REM
nins = 19: nmod = 7: nadr = 8
DATA "DAT","MOV","ADD","SUB","MUL","DIV","MOD","JMP","JMZ","JMN"
DATA "DJN","SPL","SLT","CMP","SEQ","SNE","NOP","LDP","STP"
DATA ".I ",".F ",".X ",".A ",".B ",".AB ",".BA "
DATA "#","$","@","*","<",">","{","}"
DIM ins$(nins), mod$(nmod), adr$(nadr)
DIM ic(19), mc(7), aac(8), bac(8)
adsc = 0: adbc = 0: bdsc = 0: bdbc = 0
FOR i = 1 TO nins: READ ins$(i): NEXT i
FOR i = 1 TO nmod: READ mod$(i): NEXT i
FOR i = 1 TO nadr: READ adr$(i): NEXT i
PRINT "Redcode Analyzer"
PRINT "Enter small number max : ";
LINE INPUT a$: snmax = VAL(a$)
IF snmax = 0 THEN SYSTEM
PRINT "Enter redcode directory: ";
LINE INPUT dir$
dir$ = LTRIM$(RTRIM$(dir$))
IF dir$ = "" THEN SYSTEM
SHELL "DIR /B " + dir$ + "\*.RED > $RCFILES.TMP"
OPEN "$RCFILES.TMP" FOR INPUT AS #1
WHILE NOT EOF(1)
LINE INPUT #1, rcname$
PRINT "Processing file "; rcname$
OPEN dir$ + "\" + rcname$ FOR INPUT AS #2
WHILE NOT EOF(2)
LINE INPUT #2, rcline$
rcline$ = LTRIM$(UCASE$(rcline$))
IF LEFT$(rcline$, 1) <> ";" THEN
p = INSTR(rcline$, ";")
IF p > 0 THEN rcline$ = LEFT$(rcline$, p - 1)
p = INSTR(rcline$, ",")
IF p > 8 AND p < LEN(rcline$) THEN
aline$ = LEFT$(rcline$, p - 1)
bline$ = RIGHT$(rcline$, p + 1)
FOR i = 1 TO nins
p = INSTR(aline$, ins$(i))
IF p > 0 THEN ii = i: ic(i) = ic(i) + 1
NEXT i
FOR i = 1 TO nmod
p = INSTR(aline$, mod$(i))
IF p > 0 THEN im = i: mc(i) = mc(i) + 1
NEXT i
FOR i = 1 TO nadr
p = INSTR(aline$, adr$(i))
IF p > 0 THEN
iaa = i: aac(i) = aac(i) + 1
aline$ = MID$(aline$, p + 1)
END IF
NEXT i
adata = VAL(aline$)
dat00 = 0
IF adata = 0 AND ins$(ii) = "DAT" THEN
dat00 = dat00 + 1
ELSE
IF ABS(adata) > snmax THEN adbc = adbc + 1 ELSE adsc = adsc + 1
END IF
FOR i = 1 TO nadr
p = INSTR(bline$, adr$(i))
IF p > 0 THEN
iab = i: bac(i) = bac(i) + 1
bline$ = MID$(bline$, p + 1)
END IF
NEXT i
bdata = ABS(VAL(bline$))
IF bdata = 0 AND ins$(ii) = "DAT" THEN
dat00 = dat00 + 1
ELSE
IF bdata > snmax THEN bdbc = bdbc + 1 ELSE bdsc = bdsc + 1
END IF
IF dat00 = 2 THEN
ic(ii) = ic(ii) - 1
mc(im) = mc(im) - 1
aac(iaa) = aac(iaa) - 1
bac(iab) = bac(iab) - 1
END IF
END IF
END IF
WEND
CLOSE #2
WEND
CLOSE #1
KILL "$RCFILES.TMP"
PRINT "Enter filename for report: ";
LINE INPUT report$
report$ = LTRIM$(RTRIM$(report$))
IF report$ = "" THEN report$ = "CON"
OPEN report$ FOR OUTPUT AS #3
PRINT #3, "Totals..."
FOR i = 1 TO nins
PRINT #3, ins$(i); ":"; ic(i)
NEXT i
FOR i = 1 TO nmod
PRINT #3, LEFT$(mod$(i) + " ", 3); ":"; mc(i)
NEXT i
PRINT #3, "A Field..."
FOR i = 1 TO nadr
PRINT #3, adr$(i); " :"; aac(i)
NEXT i
PRINT #3, "Small # :"; adsc
PRINT #3, "Big # :"; adbc
PRINT #3, "B Field..."
FOR i = 1 TO nadr
PRINT #3, adr$(i); " :"; bac(i)
NEXT i
PRINT #3, "Small # :"; bdsc
PRINT #3, "Big # :"; bdbc
CLOSE #3
SYSTEM