# Brainfuck Interpreter # by Martin del Rio # https://www.ldpl-lang.org/ # This example can also be used to prove the Turing-Completeness of LDPL! # Usage: ldpl brainfuck.ldpl # Created for LDPL 1.0 # # Important: # This example is outdated as it loads files without using the file statements # (load file, etc.). While it works, please check the ldpl documentation for # information on how to use these functions. DATA: argc is number # Argument count source-file is text # Brainfuck source code source-length is number # Length of Brainfuck Source File current-char is text # Current character in source ip is number # Instruction Pointer tape is number vector # Brainfuck Tape pointer is number # Brainfuck Tape Pointer temp-text is text # Auxiliar text variable i is number # Auxiliar iteration variable open-[]-found is number # Number of open []s found PROCEDURE: # Check if we received a filename get length of argv in argc if argc is less than 1 then display "No file to execute passed!" crlf "Usage: brainfuck-bin " crlf exit end if # Load brainfuck source file load file argv:0 in source-file get length of source-file in source-length display "Source loaded." crlf display "Source length: " source-length crlf # Execute while ip is less than source-length do get character at ip from source-file in current-char if current-char is equal to "+" then in tape:pointer solve 1 + tape:pointer else if current-char is equal to "-" then in tape:pointer solve tape:pointer - 1 else if current-char is equal to ">" then in pointer solve 1 + pointer else if current-char is equal to "<" then in pointer solve pointer - 1 else if current-char is equal to "." then get ascii character tape:pointer in temp-text display temp-text else if current-char is equal to "," then accept tape:pointer else if current-char is equal to "[" then if tape:pointer is equal to 0 then store 1 in open-[]-found while open-[]-found is greater than 0 do in ip solve 1 + ip get character at ip from source-file in temp-text if temp-text is equal to "[" then in open-[]-found solve 1 + open-[]-found else if temp-text is equal to "]" then in open-[]-found solve open-[]-found - 1 end if repeat end if else if current-char is equal to "]" then if tape:pointer is not equal to 0 then store 1 in open-[]-found while open-[]-found is greater than 0 do in ip solve ip - 1 get character at ip from source-file in temp-text if temp-text is equal to "]" then in open-[]-found solve 1 + open-[]-found else if temp-text is equal to "[" then in open-[]-found solve open-[]-found - 1 end if repeat end if end if # Otherwise character is ignored as per spec in ip solve 1 + ip repeat