GithubHelp home page GithubHelp logo

brainfuck's Introduction

Build Status

Brainfuck is an esoteric programming language created in 1993 by Urban Müller, and notable for its extreme minimalism.1

The language consists of eight simple commands, which make it a nice language to implement when learning about compilers and interpreters. Which is exactly what this is.

Basic usage looks like...

python -m brainfuck -n examples/fibo.bf
>> 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89

Note: the example programs here can be found in the examples directory

stdin and stdout are the default i/o streams so you can do something like this.

python -m brainfuck examples/echo.bf < macbeth.txt >> shakespeare.txt

You can also import it as a python module.

>>> from io import StringIO
>>> import brainfuck
>>>
>>> out_stream = StringIO()
>>> with open('examples/hello.bf') as f:
...     brainfuck.eval(f.read(), output_stream=out_stream, optimize=True)
...
<State pointer:6 value:10>
>>> out_stream.getvalue()
'Hello World!\n'

You can use the optimize flag -o (or brainfunk.eval(..., optimize=True)) to perform some parsing optimizations. Since it adds additional parsing time only more complex programs will benefit from it.

Currently the following optimizations are performed with the -o flag:

  • Token compressions - Repeat tokens are compacted into one command +++ becomes +3
  • Reset loops - [-] and [+] which decrements/increment a memory value to 0 will simply set them to 0

Depending on your program this can significantly reduce runtime. Consider hanoi.bf which without optimization takes about 30min to run. With optimizations on it runs in about 2min.

Running the unit tests can be done from the project directory.

python -m unittest discover

brainfuck's People

Contributors

rsiemens avatar

brainfuck's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.