Visual Brainfuck Interpreter

栏目: IT技术 · 发布时间: 4年前

内容简介:Brainfuck is an esoteric programming language, meaning that it's not very useful for any practical purposes, but it can be a great source of programming puzzles. It is an extremely minimalistic cellular automaton, and is meant to be similar to aSurprisingl

What is Brainfuck?

Brainfuck is an esoteric programming language, meaning that it's not very useful for any practical purposes, but it can be a great source of programming puzzles. It is an extremely minimalistic cellular automaton, and is meant to be similar to a Turing Machine . A brainfuck-executing machine consists only of a series of cells arranged linearly, each of which can store numerical values, and a pointer that can move between adjacent cells. This system can be programmed using combinations of only eight basic commands:

  • + increments the numerical value in the current cell.
  • - decrements the numerical value in the current cell.
  • > moves the pointer to the right.
  • < moves the pointer to the left.
  • , accepts input and stores it in the current cell.
  • . outputs the value of the current cell.
  • [ and ] are used to create loops. The code between two brackets is executed repeatedly until the currently selected cell contains a value of zero. More specifically, [ translates to 'if the current cell is zero, skip ahead to the corresponding ] , else proceed to next command' and ] translates to 'if the current cell is zero, proceed to next command, else jump back to the corresponding [ '.

Surprisingly, this language is Turing-complete, meaning that it can be used to implement any computable function (including any functions that can be implemented in Javascript, Python, or other programming language). However, Brainfuck has also been called a 'Turing tar-pit,' because implementations of more complex programs are extremely cumbersome and complex. Even implementing simple programs can be challenging puzzles.

There are multiple different ways of implementing Brainfuck. This interpreter supports two different versions of Brainfuck: numerical mode and ASCII mode. In numerical mode (the default), input consists of a series of comma-separated numbers, and cells can store any arbitrarily large positive integer value (but cannot go below zero). In ASCII mode, any ASCII string will be accepted as input and the characters are translated into numbers and entered into cells one-by-one, and numerical output is also translated back into ASCII before being displayed. Additionally, since there are only 256 ASCII characters, ASCII mode uses "byte cells" that can only contain integers between 0 and 255. Attempting to decrement 0 will produce an error in numerical mode, but it will "wrap around" to 255 in ASCII mode.

Here are a couple more conventions that this interpreter uses:

  • There are finitely many cells, and the number of cells must be specified before code is executed.
  • Attempting to move left from the leftmost cell or right from the rightmost cell results in an error.
  • The pointer starts on the leftmost cell.
  • If the program requests input and none is given, '0' is taken as the input.

Also, be wary of running your Brainfuck code with 'hide steps' turned on. If your code enters an infinite loop, it could temporarily crash the intepreter.

What the heck am I supposed to do with this?

Here are some simple, easy-to-deconstruct programs you can try in numerical mode:

  • ,. echoes back your input. (1 cell)
  • ,[-.] counts down to zero starting from the input number. (1 cell)
  • ,>,[-<+>]<. adds two inputs. (2 cells)
  • ,[->++<]>. returns double the input. (2 cells)

...and here are a few for ASCII mode:

  • ,[.,] echoes back your input. (1 cell)
  • ,[..,] echoes every character of your input twice. (1 cell)
  • ,[.,,] echoes every other character of your input. (1 cell)
  • ,[.,]++++++[->+++++<]>+++. adds an exclamation point to the end of your input. (2 cells)

Here are two more complicated examples. The code below (to be run in numerical mode with two cells) prints out the powers of two in increasing order until it either crashes or is aborted:

+[.[->++<]>[-<+>]<]

This code uses 13 cells in ASCII mode to print the text 'BRAINFUCK!'

>++++++++++[[>]+[<]>-]<+++++++++++[->+++<]>[>[++>]<-[<]>-]+++++[>>+++>>+>++>+>++++>>++<<<<<<<<<-]>>+>->++>++>->->+>->[-<]>[.>]

Want to try your hand at writing Brainfuck code? Here are four types of fun challenges you might want to try:

  • Pick a simple function and try to implement it with the shortest code possible.
    • Return the number 365. Or pick a random number between 100 and 1000 and write a program with as few commands as possible that returns that number. (numerical)
    • Print 'Hello World!' (ASCII)
    • Write a function that takes arbitrarily many inputs and returns the maximum value from among them. (numerical)
    • Given a string, remove all of the spaces from it and return the result. (ASCII)
  • Implement a simple function using only some small fixed number of cells.
    • Given a numerical input and using only three cells, count up to that input, returning all of the positive integers less than or equal to it. (numerical)
    • Calculate the square of a number using only three cells. (numerical)
    • Using four cells, create a function that accepts two numerical inputs and checks whether they are the same, returning 1 if they are equal and 0 otherwise. (numerical)
  • Build a function that isn't quite so simple - complex algorithms get ugly fast in Brainfuck.
    • Given a list of positive integers, return them in order from least to greatest. (numerical)
    • Given a string of mixed uppercase and lowercase alphabetical characters as input, convert them all to uppercase. (ASCII)
    • Return the binary digits of an input number. (numerical)
    • Given two base-ten numbers separated by spaces, return their sum (in base ten). (ASCII)
  • Given some pre-written Brainfuck code, try to figure out what it does and understand how it works.
    • >,[>,]<[.<] (ASCII, input string of upper/lowercase alphabetical characters, at least 11 cells)
    • ,+[[>+>+<<-]>>-[-<<+>>]<[->++<]>[-<+>]<<]>--[----<+>]<. (numerical, one input, at least 3 cells)

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Standards Creativity

Web Standards Creativity

Andy Budd、Dan Rubin、Jeff Croft、Cameron Adams、Ethan Marcotte、Andy Clarke、Ian Lloyd、Mark Boulton、Rob Weychert、Simon Collison、Derek Featherstone / friends of ED / March 19, 2007 / $49.99

Book Description * Be inspired by 10 web design lessons from 10 of the world's best web designers * Get creative with cutting-edge XHTML, CSS, and DOM scripting techniques * Learn breathtakin......一起来看看 《Web Standards Creativity》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具