Coder Social home page Coder Social logo

redraiment / brainfuck Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 0.0 373 KB

Brainfuck Compiler and Interpreter with LLVM in C

License: GNU General Public License v3.0

C 73.81% Lex 1.43% Yacc 3.12% Dockerfile 1.85% C++ 6.75% CMake 11.61% Brainfuck 1.42%
brainfuck compiler interpreter llvm llvm-frontend c jit

brainfuck's People

Contributors

redraiment avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

brainfuck's Issues

Reduce the serial same commands

Merge the serial same commands to reduce instructions and enhance performances.

  • Reduce the serial + and - commands to one update instruction.
  • Reduce the serial > and < commands to one move instruction.

make error

Hello author:
when I use make command,it will be error.

user@user-Latitude-5430:~/brainfuck$ make
clang -c -I/usr/local/include  -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -o options.o options.c
clang -c -I/usr/local/include  -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -o engine.o engine.c
clang -c -I/usr/local/include  -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -o ast.o ast.c
/usr/bin/bison -d -o parser.c parser.y
/usr/bin/flex --header-file=scanner.h -o scanner.c scanner.l
clang -c -I/usr/local/include  -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -o parser.o parser.c
In file included from parser.y:3:
parser.y:12:3: error: unknown type name 'yytoken_kind_t'
  yytoken_kind_t token;
  ^
1 error generated.
make: *** [Makefile:35: parser.o] Error 1

Embed Brainfuck as DSL in Clojure with macro

Macro in Lisp is an amazing feature! It can be used for defining language syntax extensions, which is known as Domain Specific Languages (DSLs).

Try create a Clojure macro to embedded Brainfuck into Clojure.

Learn brainfuck specification

Resources

Key Information

  • Memory size: 30,000 bytes, and initialized to zero.
  • Data pointer initialized to point to the leftmost byte of the array.
  • Two streams of bytes for input and output.
  • Eight commands:
Character Meaning
> Increment the data pointer (to point to the next cell to the right).
< Decrement the data pointer (to point to the next cell to the left).
+ Increment (increase by one) the byte at the data pointer.
- Decrement (decrease by one) the byte at the data pointer.
. Output the byte at the data pointer.
, Accept 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, jump 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, jump it back to the command after the matching [ command.

Support shebang mode

Function Requirement

  • Support shebang mode like below:
#!/usr/bin/env brainfuck

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

Tech Requirement

  • Add new command # for single line comment.

Provide command line options to switch between compile mode and scripting mode

Function Description

Usage: brainfuck [Options] <source-file>

Options:

  • -c/--compile: Enable compile mode. Emit native object (.o).
  • -p/--preprocess: Enable preprocess mode. Emit LLVM representation (.ll).
  • -s/--script: [DEFAULT] Enable scripting mode. Parse and execute in memory.
  • -o/--output <output-file>: Write output to file.
  • -h/--help: Show this help and exit.

Tech requirement

  • Parsing command line options with GNU getopt library.

Support link mode

Function Requirement

  • Generate the executable file by link mode.

Tech Requirement

  • Use system() function to invoke ld command to link the object files.

Build a Brainfuck compile with LLVM C API

Almost of LLVM tutorials are using C++, however, I'd like to use C language to build the lightweight compiler.
Fortunately, the LLVM provides the C interface as well!

Set link mode as default

Change the default behavior same with clang:

  • Default to generate executable file.
  • Remove the link mod option.

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.