GithubHelp home page GithubHelp logo

cornwarecjp / brainfuck-compiler-interpreter Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 21 KB

An optimizing Brainfuck compiler/interpreter

License: GNU General Public License v3.0

Python 100.00%
brainfuck brainfuck-interpreter brainfuck-compiler brainfuck-parser brainfuck-transpiler

brainfuck-compiler-interpreter's Introduction

Brainfuck-interpreter-compiler

An optimizing Brainfuck compiler/interpreter.

Usage

bf.py program.bf
bf.py --debug program.bf
bf.py --compile program.bf program.c

Without options: run program.bf, interpreted

--debug: run program.bf, interpreted; after program termination, print a memory dump.

--compile: compile program.bf to C-code; save this in program.c.

Note that, in interpreted mode, the current implementation reads all input in one go, before the start of the program; this does not enable any kind of interactivity. A small change in the code might change this behavior.

Memory elements are one-byte (8-bit) values; their values wrap when incrementing/decrementing. All memory elements have initial value zero. The memory pointer is wrapping: when moving past one end of the memory, it ends up at the opposite end of the memory.

Limits

bf.py observes limits on the following:

  • program size
  • memory size
  • number of instructions executed before program termination

These limits can all be changed at will by editing bf.py.

Program size is the size of the .bf program file, including any non-Brainfuck characters.

Memory size is the number of elements in the memory made available to the Brainfuck program.

The number of instructions is limited to prevent infinite loops in use cases where those are unwanted. The limitation is placed on the number of optimized instructions, which may be lower than the number of instructions in the original Brainfuck code

Optimizations

The following optimizations are applied:

  • Consecutive increments or decrements of memory values or of the memory pointer are merged together into a single addition/subtraction
  • Target positions of jump instructions are pre-computed, so that, on execution, there is no need to walk through the code to find the proper position to jump to.
  • The very common pattern in Brainfuck, where one memory location is decremented in a loop until it is zero, and zero or more other memory locations are incremented/decremented in the same loop, is merged into a single instruction. This optimizes, for instance, common implementations of set-to-zero, move-value, copy-value, add, subtract and multiply-with-constant.

brainfuck-compiler-interpreter's People

Contributors

cornwarecjp avatar

Watchers

 avatar  avatar

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.