Brainfuck is a minimized programming language. It has only 8 keywords. It was designed first in 1993 by Urban Muller. [Here] gives more details on wiki. Brainfuck is often referred to as ‘BF’. It is based on ‘Tuning Complete’. However, as state-of-the-art computers do not have infinite storage space, the various implementations assume a fixed size of array e.g. 30000, as the initial storage.
Apart from 8 basic commands, BF also consists of an input data stream, an array of elements which are initialized as zeros, a pointer which points to the first element of the array at the beginning and an output data stream.
The 8 commands can be described as below.
Converting to C/C++, it can be described as follows.
The BF interpreter is not difficult to write; any learns can write a simple interpreter within a short time. However, the BF program is hard to comprehend. For example, the following BF program outputs the ‘Hello World!’ (and a new line).
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+. +++++++..+++.>++.<<+++++++++++++++.>.+++.------. --------.>+.>.
Well, since BF only has 8 keyword character, anything else would be just ignored by the interpreter. The following is a more-easy-to-understand version of the above line of BF code with comments.
The BF is capable of doing anything in theory. Every basic program consists of input and output, which are already supported in Brainfuck. The PHP Online Brainfuck Interpreter can be found in [here].
The Python-version of BF interpreter can be found at [here]
#!/usr/bin/env python # https://helloacm.com # Brainfuck Interpreter from __future__ import print_function def bf(src, left, right, data, idx): """ brainfuck interpreter src: source string left: start index right: ending index data: input data string idx: start-index of input data string """ if len(src) == 0: return if left < 0: left = 0 if left >= len(src): left = len(src) - 1 if right < 0: right = 0 if right >= len(src): right = len(src) - 1 # tuning machine has infinite array size # increase or decrease here accordingly arr = [0] * 30000 ptr = 0 i = left while i <= right: s = src[i] if s == '>': ptr += 1 # wrap if out of range if ptr >= len(arr): ptr = 0 elif s == '<': ptr -= 1 # wrap if out of range if ptr < 0: ptr = len(arr) - 1 elif s == '+': arr[ptr] += 1 elif s == '-': arr[ptr] -= 1 elif s == '.': print(chr(arr[ptr]), end="") elif s == ',': if idx >= 0 and idx < len(data): arr[ptr] = ord(data[idx]) idx += 1 else: arr[ptr] = 0 # out of input elif s =='[': if arr[ptr] == 0: loop = 1 while loop > 0: i += 1 c = src[i] if c == '[': loop += 1 elif c == ']': loop -= 1 elif s == ']': loop = 1 while loop > 0: i -= 1 c = src[i] if c == '[': loop -= 1 elif c == ']': loop += 1 i -= 1 i += 1 if __name__ == "__main__": src = raw_input() bf(src, 0, len(src) - 1, "", 0)
A C# Console Application (using .NET framework 4) that implements the Brainfuck programming language is given here.
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: Return in Try-Finally for Python/Java/Delphi/C#
Next Post: Hello World Program in LOGO