Coder Social home page Coder Social logo

brainfuck's Introduction

brainfuck Build Status

Brainfuck interpreter written in C.

Usage

brainfuck [-veh] file...
-e --eval	run code directly
-v --version	show version information
-h --help	show a help message.

The interactive console can be accessed by passing no arguments.

We also provide a C api:

#include <stdio.h>
#include <stdlib.h>
#include <brainfuck.h>
    
int main() {
	BrainfuckState *state = brainfuck_state();
	BrainfuckExecutionContext *context = brainfuck_context(BRAINFUCK_TAPE_SIZE);
	BrainfuckInstruction *instruction = brainfuck_parse_string(",+++++.");
 	brainfuck_add(state, instruction);
 	brainfuck_execute(state->root, context);
	brainfuck_destroy_context(context);
 	brainfuck_destroy_state(state);
	return EXIT_SUCCESS;
}

Examples

The examples/ directory contains a large amount of brainfuck example programs. We have tried to attribute the original authors of these programs where possible.

Getting the source

Download the source code by running the following code in your command prompt:

$ git clone https://github.com/fabianishere/brainfuck.git

or simply grab a copy of the source code as a Zip file.

Building

Create the build directory.

$ mkdir build
$ cd build

Brainfuck requires CMake and a C compiler (e.g. Clang or GCC) in order to run. It also depends on libedit, which is available in the main repositories of most Linux distributions (e.g. as libedit-dev on Debian/Ubuntu) and comes with the macOS XCode command line tools. Then, simply create the Makefiles:

$ cmake ..

and finally, build it using the building system you chose (e.g. Make):

$ make

After the build has been finished, you may install the binaries to your local system (see CMAKE_INSTALL_PREFIX for information about the install prefix):

$ make install

Alternatively, you may run the interpreter directly without installation, for instance:

$ ./brainfuck ../examples/hello.bf

License

The code is released under the Apache License version 2.0. See LICENSE.txt.

Contributors

Fabian Mastenbroek https://github.com/fabianishere
aliclubb https://github.com/aliclubb
diekmann https://github.com/diekmann
SevenBits https://github.com/SevenBits
Alex Burka https://github.com/durka
outis https://github.com/outis
rien333 https://github.com/rien333
boweiliu https://github.com/boweiliu
Rotartsi https://github.com/ROTARTSI82
Saket Upadhyay https://github.com/Saket-Upadhyay
outis https://github.com/outis
Jalmari91 https://github.com/Jalmari91
Alok Singh https://github.com/alok
Lasse Damsgaard Skaalum https://github.com/humleflue

brainfuck's People

Contributors

aliclubb avatar alok avatar boweiliu avatar durka avatar fabianishere avatar jalmari91 avatar maxrdz avatar outis avatar rien333 avatar rotartsi82 avatar saket-upadhyay avatar sevenbits avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

brainfuck's Issues

Add Continuous Integration

We should be able to build on some Travis machines to check whether the master still builds before merging a pull request.

Unexpected "error: tape memory out of bounds (overrun)"

I've run into some issue when playing with bf using this interpreter. Below is the simplest test case I was able to make, that show the problem:

>
><<
$ echo ">\n><<" | brainfuck
error: tape memory out of bounds (overrun)
exceeded the tape size of 30000 cells
$ echo $?
1

If we add single space between > and < then problem disappears:

>
> <<
$ echo ">\n> <<" | brainfuck
$ echo $?
0

I get a fatal error when I try to run 'make'

I get the following error message:

[ 33%] Built target brainfuck
Scanning dependencies of target brainfuck-cli
make[2]: Warning: File 'CMakeFiles/brainfuck-cli.dir/depend.make' has modification time 0.77 s in the future
[ 50%] Building C object CMakeFiles/brainfuck-cli.dir/src/main.c.o
/mnt/c/GitRepos/brainfuck/src/main.c:23: warning: "BRAINFUCK_EDITLINE_LIB" redefined
   23 |  #define BRAINFUCK_EDITLINE_LIB
      |
<command-line>: note: this is the location of the previous definition
/mnt/c/GitRepos/brainfuck/src/main.c:24:11: fatal error: editline/readline.h: No such file or directory
   24 |  #include <editline/readline.h>
      |           ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/brainfuck-cli.dir/build.make:63: CMakeFiles/brainfuck-cli.dir/src/main.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:126: CMakeFiles/brainfuck-cli.dir/all] Error 2
make: *** [Makefile:141: all] Error 2

I am following the readme guide. And I get the error both in on MinGW on windows and in WSL.
If I'm the one doing something wrong, then please tell me what.

Thanks in advance.

Bad Merge

Code is currently not compilable due to debris left behind by diff3(1)

For example:

 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef BRAINFUCK_H
#define BRAINFUCK_H

<<<<<<< HEAD
#define BRAINFUCK_VERSION "2.3"
=======
#define BRAINFUCK_VERSION "2.4"
>>>>>>> f3fdf57cc86182aacc89de87f2d5f7827dbf6d0f
#define BRAINFUCK_CELL_TYPE int
#define BRAINFUCK_TAPE_SIZE 30000

#define BRAINFUCK_TOKEN_PLUS '+'
#define BRAINFUCK_TOKEN_MINUS '-'
#define BRAINFUCK_TOKEN_PREVIOUS '<'
#define BRAINFUCK_TOKEN_NEXT '>'
#define BRAINFUCK_TOKEN_OUTPUT '.'

Does not build with clang

make all CC=cc CCFLAGS=
Building
mkdir -p bin
cc  -o /private/tmp/brainfuck-9gd5/brainfuck-0.1.2/bin/brainfuck /private/tmp/brainfuck-9gd5/brainfuck-0.1.2/src/*
clang: error: cannot specify -o when generating multiple output files
make: *** [all] Error 1

See: Homebrew/legacy-homebrew#19542

Interpreter always ignores first character of brainfuck

Regardless of whether you are running a program from file or using direct input, the first character is always ignored. i.e. The ASCII value for '!' is 32, so theoretically the following program should output the character '!'
++++++++++++++++++++++++++++++++.
But instead outputs the character with the ASCII value of 31, even though '+' is written 32 times. Adding an extra '+' prints out an '!', but this is incorrect behaviour.

libedit/history.h include error

So , I got the interpreter working but just letting you know about this bug.

my software specs:

 OS: Arch Linux
 KDE Plasma Version: 5.10.5
 KDE Frameworks Version: 5.37.0
 Qt Version: 5.9.1
 Kernel Version: 4.12.8-2-ARCH
 OS Type: 64-bit
 ------------------------------------------------------------
  COMPILER
 ------------------------------------------------------------
 gcc (GCC) 7.1.1 20170630
 Copyright (C) 2017 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 --------------
  LIBEDIT
 --------------

 libedit 20170329_3.1-1 [installed]

When I compiled it shot with a include error ,

fatal error: editline/history.h: No such file or directory
   #include <editline/history.h>
            ^~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/brainfuck.dir/build.make:63: CMakeFiles/brainfuck.dir/src/main.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/brainfuck.dir/all] Error 2
make: *** [Makefile:130: all] Error 2

So what I did was comment this line in src/main.c

#ifdef BRAINFUCK_EDITLINE_LIB
        #include <editline/readline.h>
/*      
 * This line caused the compile error , even if it is not mac
 * I think , We don't need to include editline/history.h when
 * We already included editline/readline.h
        #ifndef __APPLE__ 
                #include <editline/history.h>  
        #endif
*/
#endif

After editing this , I compiled successfully!

is this intensional?......
just reporting!

Cmake error when trying to build

This is the error message I get:
~/Github/brainfuck/build$
cmake ..
CMake Error at /home/.../.local/lib/python3.10/site-packages/cmake/data/share/cmake-3.26/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find editline (missing: EDITLINE_INCLUDE_DIR EDITLINE_LIBRARY)
Call Stack (most recent call first):
/home/stefggl/.local/lib/python3.10/site-packages/cmake/data/share/cmake-3.26/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
cmake/Findeditline.cmake:31 (find_package_handle_standard_args)
CMakeLists.txt:52 (find_package)

-- Configuring incomplete, errors occurred!

cmake is version 3.26.4
Ubuntu 22.04 machine

Build fails due to editline

[ 33%] Built target brainfuck
[ 50%] Building C object CMakeFiles/brainfuck-cli.dir/src/main.c.o
/home/romario/super-ia-project/bfinterpreter2/src/main.c:23:11: fatal error: editline/readline.h: No such file or directory
#include <editline/readline.h>
^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/brainfuck-cli.dir/build.make:62: recipe for target 'CMakeFiles/brainfuck-cli.dir/src/main.c.o' failed
make[2]: *** [CMakeFiles/brainfuck-cli.dir/src/main.c.o] Error 1
CMakeFiles/Makefile2:904: recipe for target 'CMakeFiles/brainfuck-cli.dir/all' failed
make[1]: *** [CMakeFiles/brainfuck-cli.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2

Failed at cmake ..

Faced this issue while installing brainfuck in ubuntu x86_64 at this step

cmake ..

Output :

Could NOT find Doxygen (missing:  DOXYGEN_EXECUTABLE) 
CMake Error at CMakeLists.txt:29 (message):
  Doxygen is needed to build the documentation.

Support piped input

It would be nice if piped content was supported:

echo '.+[.+] prints out all ASCII characters' | brainfuck -c
# Other example: concatenate two brainfuck programs and execute them
cat file1 file2 | brainfuck -c
# Other example: minify brainfuck code using brnfckr and pass the result to `brainfuck` directly
brnfckr '.+[.+] prints out all ASCII characters' | brainfuck -c

Currently, this gives the following error:

error: no code specified!

error on make; editline/readline.h: no such file or directory

Hello. When I try to make the program, an error shows that editline/readline/h does not exist.

[ 40%] Built target libbrainfuck_static [ 60%] Building C object CMakeFiles/brainfuck.dir/src/main.c.o /media/sebastian/UUI/sml/Coding/BF/brainfuck/src/main.c:24:11: fatal error: editline/readline.h: No such file or directory #include <editline/readline.h> ^~~~~~~~~~~~~~~~~~~~~ compilation terminated. CMakeFiles/brainfuck.dir/build.make:62: recipe for target 'CMakeFiles/brainfuck.dir/src/main.c.o' failed make[2]: *** [CMakeFiles/brainfuck.dir/src/main.c.o] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/brainfuck.dir/all' failed make[1]: *** [CMakeFiles/brainfuck.dir/all] Error 2

I have libedit in a directory in my home directory, and I have the dependencies required for that as well. Am I doing something wrong?

Have a look at this

This is not an issue.
I admire this project quite much, but please excuse me if I advertise my project here.
A visual brainfuck interpreter online:
Github Page: web page
Github Repo: v-bf-web

Please provide a way to stop execution via the API

The API currently includes the function brainfuck_execute, but there is no way to stop the execution. This means that code that includes infinite loops will execute forever. Worse, if this API is used in code (i.e in a tool that interprets BF) this provides a plethora of opportunities for your application to crash.

Add automated test support

This issue is concerned with adding support for automated testing. Test automation provides the benefit of automatically checking for breaking changes when adding features or refactoring parts of the code. In addition, we can also add automated tests for parts of the code that are often forgotten when running manual tests.

I propose we use the builtin testing functionality of CMake - CTest - to drive our tests.

Needs license

Hey, wanting to use this for a project, could you throw a license in the repo? :)

Thanks

Add support for automatic packaging and releases

This issue is concerned with adding support for automatic packaging of the interpreter for various operating systems/distributions.

  • Create Release action that publishes artifacts in a Github Release
  • Explore packaging options via CPack

Support for Windows ? And other things...

Is Windows supported ? Can you create a compiler fonctionality with this command scheme : brainfuck <INPUT_FILE_PATH> -o <OUTPUT_FILE_PATH> ? And can you create a distribuable installer in .exe for Windows (if Windows is supported), .deb/.rpm for Linux and .dmg for macOS ?

Remove hard dependency on libedit

The project now requires the libedit library to be installed in order to build the command line interface.

We should have a build option to disable the interactive console, so people can build the command line interface without needing the libedit library if they are not interested in the interactive console.

Readme command-line options inconsistent

The readme for this project shows the usage guidelines ...

Usage

brainfuck [-ch] <filenames>
-e  run code directly
-h  show a help message.

... but is unclear as to whether -c or -e is actually the option for running code directly. This could be figured out really quickly by running the program, but I think it'd be beneficial from making the command-line options more unambiguous.

Allow for direct input

Currently, there’s brainfuck [file] for files, and brainfuck -i for interactive mode.

How about adding -c | --code which take direct input as strings?

E.g.

$ brainfuck -c '.+[.+]'

Espace sequences instead of cursor movement

I'm not sure if this intentional but the interpreter disallows basically any sort of cursor movement, like that with the arrow keys or emacs-like Ctrl-e/Ctrl-a type of stuff. When I try to to for example move my cursor to the right some escape sequence appears on the prompt instead. Any fix on my side or a rationale behind the choice to disable cursor movement ? (macOS 10.12 + iTerm)

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.