REM
REM Brainfuck interpreter - http://en.wikipedia.org/wiki/Brainfuck
REM
REM Created on 17/4/2009 - PvE
REM Revised November 2009.
REM Revised December 2009.
REM
REM Get the separate arguments
SPLIT ARGUMENT$ BY " " TO arg$ SIZE dim
IF dim < 2 THEN
PRINT "Usage: bf <file>"
END
ENDIF
REM Determine size
filesize = FILELEN(arg$[1])
REM Get the contents
OPEN arg$[1] FOR READING AS bf
REM claim memory
txt = MEMORY(filesize)
REM Read file into memory
GETBYTE txt FROM bf SIZE filesize
CLOSE FILE bf
REM Initialize work memory
mem = MEMORY(30000)
REM This is The Pointer pointing to memory
thepointer = 0
REM This is the cursor pointing in the current program
cursor = 0
REM Start interpreting program
WHILE cursor < filesize DO
command = PEEK(txt + cursor)
SELECT command
CASE 62
thepointer = thepointer + 1
CASE 60
thepointer = thepointer - 1
CASE 43
POKE mem + thepointer, PEEK(mem + thepointer) + 1
CASE 45
POKE mem + thepointer, PEEK(mem + thepointer) - 1
CASE 46
PRINT CHR$(PEEK(mem + thepointer));
CASE 44
key = GETKEY
POKE mem + thepointer, key
CASE 91
jmp = 1
IF ISFALSE(PEEK(mem + thepointer)) THEN
REPEAT
cursor = cursor + 1
IF PEEK(txt + cursor) EQ 91 THEN
jmp = jmp + 1
ELIF PEEK(txt + cursor) EQ 93 THEN
jmp = jmp - 1
END IF
UNTIL PEEK(txt + cursor) EQ 93 AND ISFALSE(jmp)
END IF
CASE 93
jmp = 1
IF ISTRUE(PEEK(mem + thepointer)) THEN
REPEAT
cursor = cursor - 1
IF PEEK(txt + cursor) EQ 93 THEN
jmp = jmp + 1
ELIF PEEK(txt + cursor) EQ 91 THEN
jmp = jmp - 1
END IF
UNTIL PEEK(txt + cursor) EQ 91 AND ISFALSE(jmp)
END IF
END SELECT
cursor = cursor + 1
WEND