Code Monkey home page Code Monkey logo

brainfuck's Introduction

Brainf*ck

This is a simple Brainf*ck interpreter in C which can detect and locate runtime errors.

The Language

Brainf*ck is a programming language created in 1993 by Urban Müller. It utilises a simple machine model, which consists of:

  • A one-dimentional array of 30000 byte cells initialised to 0;
  • A data pointer which points to the first element at the beginning of program execution;
  • A program;
  • An instruction pointer;
  • An input stream (stdin);
  • An output stream (stdout).

The eight single-character commands of the language are listed below. All the other characters are ignored. The commands in the program are executed sequentially unless a [ or ] command is encountered.

Command Description
> Increments the data pointer (to point to the next cell to the right).
< Decrements the data pointer (to point to the next cell to the left).
+ Increments the byte at the data pointer.
- Decrements the byte at the data pointer.
. Outputs the byte at the data pointer as an ASCII character.
, Accepts an ASCII character as one byte of input, storing its value in the byte at the data pointer.
[ If the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jumps it forward to the command after the matching ] command.
] If the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jumps it back to the command after the matching [ command.

See Wikipedia for a more detailed explanation.

To Build

Building with GNU Make

Building the interpreter requires make, clang, and git. To satisfy these requirements, you may need to install them using your package manager first. For instance:

$ sudo apt install build-essential clang git  # for Debian, Ubuntu, and related
$ sudo dnf install make clang git             # for Fedora, Red Hat, and related

Then the interpreter can be built with GNU Make:

$ git clone https://github.com/ZhongRuoyu/Brainfuck.git
$ cd Brainfuck
$ make

The last make command generates an executable named bf in the directory.

Building Manually

On platforms like Windows where make is not well supported, the interpreter can still be built manually. To do so, git clone this repository or download a zipped archive. Open a terminal in its root directory and build the executable using all the source files in /src, with include path /include. For instance, using clang on Windows with PowerShell:

clang src/*.c -I include -std=c11 -O2 -o bf.exe

To Use

To use the interpreter, simply supply the path to the Brainf*ck program as an argument. For instance, running the following...

$ bf test/hello.b

... executes the program at test/hello.b, and generates the following output:

Hello World!

The interpreter has the capability to detect and locate runtime errors. For instance, interpreting the following program...

        (pointer initially at location 0)
+++++   increment the value at cell 0 to 5
<       try decrementing the pointer
>       increment the pointer
[-]     reset the cell to 0

... generates the following error:

At Line 3, Column 1: error: decrementing pointer to inaccessible cell.
    <       try decrementing the pointer
    ^

Run bf --help to view the detailed help message on using the interpreter.

To execute the interpreter, you may need to key in its path as well.

See Also

A code golf version of the interpreter, based on commit a25de5a, is available here in ZhongRuoyu/CodeGolf.

Licence

Copyright (c) 2021 Zhong Ruoyu.

Unless otherwise stated, the files in this repository are licensed under the MIT License.

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.