For some reason, the bracket code I implemented into a Brainfuck interpreter I'm working on isn't working correctly. Here's my code so far:
'Brainf**k Interpreter v1.0 'By HTV04 OPTION STRICT ACLS VAR VER$="1.0 (SB3)" '--Library-- DEF REPLACE$(S$,O$,N$) 'From YARN.LIB v1.2 VAR W$,I%,S% VAR L%=LEN(O$) VAR N%=LEN(N$) W$=S$+"" I%=INSTR(W$,O$) WHILE I%!=-1 W$=SUBST$(W$,I%,L%,N$) S%=I%+N% I%=INSTR(S%,W$,O$) WEND RETURN W$ END DEF COUNT(S$,U$) 'From YARN.LIB v1.2 VAR O%,I%,S% REPEAT I%=INSTR(S%,S$,U$) INC O%,I%!=-1 INC S%,LEN(U$) UNTIL I%==-1||S%>=LEN(S$) RETURN O% END DEF BFERROR S$,I BEEP 0 PRINT "" PRINT "Syntax error: "+S$ PRINT "Break at "+STR$(I+1) END '---- '--Loading Files-- IF PRGNAME$(3)!="-BFIOPTS.PRG" THEN LOAD "PRG3:-BFIOPTS.PRG",0 ENDIF EXEC 3 '-- '---- '--Constant Setup-- VAR OPTIMIZE=VAR("3:OPTIMIZE") VAR MEMSIZE=VAR("3:MEMSIZE") VAR BITWIDTH=VAR("3:BITWIDTH") VAR SIGNEDINT=VAR("3:SIGNEDINT") VAR SBOPTIMIZE=VAR("3:SBOPTIMIZE") VAR SBMEMOF=VAR("3:SBMEMOF") VAR SBCHR13=VAR("3:SBCHR13") VAR SBINPUT=VAR("3:SBINPUT") VAR SBINPUT$=VAR("3:SBINPUT$") VAR SBINPUTEOF=VAR("3:SBINPUTEOF") VAR SBWAIT=VAR("3:SBWAIT") VAR SBWAITCNT=VAR("3:SBWAITCNT") '---- '--Variable Setup-- VAR I=0 VAR I1=0 VAR I2=0 VAR A VAR FAILSAFE=0 VAR G$="" VAR BFCMDS$="+-><[].," VAR S$="" VAR BITMAX=0 VAR BITMIN=0 VAR P=0 VAR BC=0 VAR IN=0 '---- '--Array Setup-- DIM MEM[MEMSIZE] DIM BRACKET[0] '--Setup-- PRINT "Brainf**k Interpreter v"+VER$ PRINT "By HTV04" PRINT "" IF OPTIMIZE!=0 THEN IF OPTIMIZE!=1 THEN FAILSAFE=1 ENDIF ENDIF IF MEMSIZE<0 THEN FAILSAFE=1 ELSEIF MEMSIZE!=FLOOR(MEMSIZE) THEN FAILSAFE=1 ENDIF IF BITWIDTH<0 THEN FAILSAFE=1 ELSEIF BITWIDTH!=FLOOR(BITWIDTH) THEN FAILSAFE=1 ENDIF IF SIGNEDINT!=0 THEN IF SIGNEDINT!=1 THEN FAILSAFE=1 ENDIF ENDIF IF SBOPTIMIZE!=0 THEN IF SBOPTIMIZE!=1 THEN FAILSAFE=1 ENDIF ENDIF IF SBMEMOF!=0 THEN IF SBMEMOF!=1 THEN FAILSAFE=1 ENDIF ENDIF IF SBCHR13!=0 THEN IF SBCHR13!=1 THEN FAILSAFE=1 ENDIF ENDIF IF SBINPUT!=0 THEN IF SBINPUT!=1 THEN FAILSAFE=1 ENDIF ENDIF IF SBINPUTEOF!=0 THEN IF SBINPUTEOF!=1 THEN IF SBINPUTEOF!=2 THEN FAILSAFE=1 ENDIF ENDIF ENDIF IF SBWAIT!=0 THEN IF SBWAIT!=1 THEN FAILSAFE=1 ENDIF ENDIF IF FAILSAFE==1 THEN PRINT "ERROR: One or more options are set incorrectly:" IF OPTIMIZE!=0 THEN IF OPTIMIZE!=1 THEN PRINT "- OPTIMIZE" ENDIF ENDIF IF MEMSIZE<0 THEN PRINT "- MEMSIZE" ELSEIF MEMSIZE!=FLOOR(MEMSIZE) THEN PRINT "- MEMSIZE" ENDIF IF BITWIDTH!=0 THEN IF BITWIDTH!=1 THEN IF BITWIDTH!=2 THEN PRINT "- BITWIDTH" ENDIF ENDIF ENDIF IF SIGNEDINT!=0 THEN IF SIGNEDINT!=1 THEN PRINT "- SIGNEDINT" ENDIF ENDIF PRINT "" IF SBOPTIMIZE!=0 THEN IF SBOPTIMIZE!=1 THEN PRINT "- SBOPTIMIZE" ENDIF ENDIF IF SBMEMOF!=0 THEN IF SBMEMOF!=1 THEN PRINT "- SBMEMOF" ENDIF ENDIF IF SBCHR13!=0 THEN IF SBCHR13!=1 THEN PRINT "- SBCHR13" ENDIF ENDIF IF SBINPUT!=0 THEN IF SBINPUT!=1 THEN PRINT "- SBINPUT" ENDIF ENDIF IF SBINPUTEOF!=0 THEN IF SBINPUTEOF!=1 THEN IF SBINPUTEOF!=2 THEN PRINT "- SBINPUTEOF" ENDIF ENDIF ENDIF IF SBWAIT!=0 THEN IF SBWAIT!=1 THEN PRINT "- SBWAIT" ENDIF ENDIF PRINT "" PRINT "Please set the values for these options correctly." END ENDIF IF SIGNEDINT==0 THEN BITMAX=POW(2,BITWIDTH)-1 BITMIN=0 ENDIF IF SIGNEDINT==1 THEN BITMAX=POW(2,BITWIDTH-1)-1 BITMIN=POW(2,BITWIDTH)*(-1) ENDIF '--Code Minification-- PRINT "Reading... "; FOR I=1 TO PRGSIZE(1) PRGEDIT 1,I G$=PRGGET$() FOR I1=0 TO LEN(G$)-1 FOR I2=0 TO LEN(BFCMDS$)-1 IF G$[I1]==BFCMDS$[I2] THEN S$=S$+BFCMDS$[I2] ENDIF NEXT NEXT NEXT PRINT "Done." '---- '--Code Optimization-- IF OPTIMIZE==1 THEN PRINT "Optimizing... "; REPEAT S$=REPLACE$(S$,"+-","") UNTIL COUNT(S$,"+-")==0 REPEAT S$=REPLACE$(S$,"-+","") UNTIL COUNT(S$,"-+")==0 PRINT "Done." ENDIF '---- '--Interpreter Optimization-- IF SBOPTIMIZE==1 THEN S$=REPLACE$(S$,"[-]","A") S$=REPLACE$(S$,"[+]","A") ENDIF '---- '--Interpreter-- CLS PRINT ""; FOR I=0 TO LEN(S$)-1 IF S$[I]=="+" THEN INC MEM[P] IF MEM[P]>BITMAX THEN MEM[P]=BITMIN ENDIF ENDIF IF S$[I]=="-" THEN DEC MEM[P] IF MEM[P]<BITMIN THEN MEM[P]=BITMAX ENDIF ENDIF IF S$[I]==">" THEN INC P IF P==MEMSIZE THEN P=0 ENDIF ENDIF IF S$[I]=="<" THEN DEC P IF P<0 THEN P=MEMSIZE-1 ENDIF ENDIF IF S$[I]=="[" THEN IF MEM[P]==0 THEN FOR I=I+1 TO LEN(S$)-1 IF S$[I]=="[" THEN INC BC ENDIF IF S$[I]=="]" THEN IF BC>0 THEN DEC BC ELSE BREAK ENDIF ENDIF NEXT IF I==LEN(S$)-1 THEN BFERROR "missing ]",I END ENDIF ELSE PUSH BRACKET,I ENDIF ENDIF IF S$[I]=="]" THEN IF LEN(BRACKET)>0 THEN IF MEM[P]!=0 THEN I=BRACKET[LEN(BRACKET)-1] ELSE A=POP(BRACKET) ENDIF ELSE BFERROR "missing [",I END ENDIF ENDIF IF S$[I]=="." THEN IF MEM[P]!=13 THEN PRINT CHR$(MEM[P]); ENDIF ENDIF IF S$[I]=="A" THEN MEM[P]=0 ENDIF NEXTAny idea on what I'm doing wrong?