900 "TAPELEN" store ##First, read in the source # "" # 1 while # getline # dup -1 = if # pop # 0 # else # "\n" swap + + # 1 # end # end # strlen 1 swap substr swap pop # #bottom of stack now contains 1 string, the source code # strlen "len" store # "code" store argc 1 = ! if "Please give the brainfuck file as a command-line argument." print lf exit end 0 argvget "r" fopen "filehandle" store "" filehandle fgetc dup -1 = ! while + filehandle fgetc dup -1 = ! end pop strlen "len" store "code" store ##Then, create the tape TAPELEN dup 0 > while 0 swap 1 - dup 0 > end ##Finally, start executing @defun "execute" { #"start of 'execute': " print stackdump depth 1 + "depth" gstore #"trying to execute " print dup print lf "code" store 0 "i" store i len < while #"i = " print i print lf "code" swapoutvar i stridx "c" store "code" store #"c = " c + print stackdump c "#" = if stackdump end c "+" = if 1 + 256 % end c "-" = if 255 + 256 % end c "." = if dup chr print end c "," = if pop getc dup -1 = if pop 255 else ord 256 % end #"Input put on stack " print dup print lf end c ">" = if -1 roll end c "<" = if 1 roll end c "]" = if i "loopend" depth 1 - + gstore len "i" store # == break end c "[" = if i "loopstart" depth + store #"loopstart" print depth print " = " print "loopstart" depth + get print lf 0 "didloop" depth + store dup while 1 "didloop" depth + store "code" swapoutvar i 1 + len substr swap "code" store execute #stackdump "i = " print i print lf "loopstart" depth + get "i" store dup end "didloop" depth + get ! if 1 "searchdepth" store i 1 + "i" store "code" dup swapoutvar i len < searchdepth 0 > * while i stridx "c" store #"Searching index " print i print " (char " print c print ")" print lf c "[" = if searchdepth 1 + "searchdepth" store end c "]" = if searchdepth 1 - "searchdepth" store end i 1 + "i" store i len < searchdepth 0 > * end #stackdump swap store searchdepth 0 = ! if "No matching ']' found for '[' at index " print loopstart print lf exit end i 1 - "i" store else "loopend" depth + get 1 + "loopstart" depth + get + "i" store end #"after loop, i = " print i print lf end i 1 + "i" store i len < end depth 1 - "depth" gstore } 0 "depth" store "code" swapoutvar execute #kaboom depth 0 = ! if "The interpreter exited in depth " depth + "..." + print end