Git Product home page Git Product logo

brainf--k.h's Introduction

brainfuck.h

A C header that allows building brainfuck programs within a C compiler.


As it turns out, the simplest brainfuck compiler is really just a C compiler (though its far from good at it).

Using the power of the C Preprocessor and very simple character substitution, any brainfuck program alongside this header can build as if it were C. Unlike other simple conversions, this one should be portable and not incur trivial errors such as buffer overflows, and can be configured through macros. Despite all that, you probably shouldn't use it.

The following substitutions are used to convert a source file:

bf bf.h
+ INC·
- DEC·
> IPT·
< DPT·
[ JNZ·
] JZR·
. PUT·
, GET·

The middle-dot character · represents whitespace. All programs must be terminated with an extra ·JZR instruction.

Usage

After a file is converted, the macros described in Implementation Details may be set (either with the -D[MACRO] compiler flag or in the file) and finally the brainfuck.h header file is included after the macros. Compile the converted source file as you would C to produce a binary.

This is not a header in the traditional sense (or in any sense), you never include it as part of anything but a converted brainfuck source file, and it contains entire blocks of code that don't compile to a translation unit.


A utility mkbfhsrc is included to automate the process of converting brainfuck source files to brainfuck.h-compatible ones.

Usage: mkbfhsrc [OPTIONS] [FILE]

  -c [BITS]	bit width of cells, leave unset to use all bits in the type
  -e [FORMAT]	EOF format (0: return 0, -1: return -1, 1: leave unchanged)
  -m [BITS]	bits of address space for memory (eg. 15 bits = 32768 cells)
  -o [FILE]	output file
  -t [TYPE]	C type for cells (eg. uint32_t, \"unsigned char\")

This utility does not check for errnoneous arguments or syntax errors
If no input or output file is specified, stdin and stdout are used respectively

Various test programs are included in the /tests directory. They must be built manually as I never got around to writing a build script.

Implementation Details

Various brainfuck interpreters over the years have implemented the language with minor variations. Various macros are provided to allow different conventions to be used. These macros must appear before the header is included.

MEMBITS

Bits of address space for memory (the cell count being 2 ^ MEMBITS), must be a positive integer.
Defaults to 15 (32,768 cells).

Memory will always wrap around when the pointer goes out of bounds (implemented as a circular buffer). This prevents buffer overflows, and cannot be configured.

CELLBITS

Bit width of cells, may be any arbitrary positive integer (though it must be smaller than sizeof(CELL_T)*8). Leave unset to use the full size of CELL_T.
Defaults to unset.

CELL_T

Type used to hold the cell. Must be equal to or larger in size than CELLBITS.
Defaults to uint8_t.

EOF_FORMAT

Convention to use when EOF is encountered on input. May be either:
0: set the cell to 0
-1: Set the cell to its maximum value (like (unsigned)~0)
1: leave the cell unchanged.
Defaults to 0.

Caveats

brainfuck.h serves mostly as an example of why macros are awful and how not to use the preprocessor.

Its largely a toy that I thought of and quickly put together in my free time, not realizing how disasterous it would eventually become. Macros used this way make it impossible to catch bugs and are a pain to configure, requiring extra burden to be placed on both the programmer and user.

brainf--k.h's People

Contributors

user21944 avatar

Stargazers

 avatar

Watchers

 avatar

brainf--k.h's Issues

You really missed out on making the C "pretty"

The last two use a C99 feature that was only added to GCC very recently; Clang has had it for a long while.

#include<stdio.h>
#define b while(*m) {
#define i {int (_c) = getchar(); if(_c!=EOF) *m=_c; }
#define o putchar(*m);
#define d --*m;
#define u ++*m;
#define l m-=1;
#define r m+=1;
#define e }
#define _ return 0;}
int mem[1048576];int main(){register int*m=mem;
u b r b l d b e r u b r u u u r b u u u u u u u u u u u r e b r e d b l
e r d e e u u u u u u u u u u l e r r r r r r d d d d o l l u u u o l d
o o u u u o l d o r r r o l l o u u u o d d d d d d o r d o l l u o l o _
#include<stdio.h>
#define mEEP while(*m) {
#define meep {int (_c) = getchar(); if(_c!=EOF) *m=_c; }
#define MEEP putchar(*m);
#define MeeP --*m;
#define mEEp ++*m;
#define Meep m-=1;
#define meeP m+=1;
#define MEEp }
#define _ return 0;}
int mem[1048576];int main(){register int*m=mem;
mEEp mEEP meeP mEEP Meep MeeP mEEP MEEp meeP mEEp mEEP meeP mEEp mEEp
mEEp meeP mEEP mEEp mEEp mEEp mEEp mEEp mEEp mEEp mEEp mEEp mEEp mEEp
meeP MEEp mEEP meeP MEEp MeeP mEEP Meep MEEp meeP MeeP MEEp MEEp mEEp
mEEp mEEp mEEp mEEp mEEp mEEp mEEp mEEp mEEp Meep MEEp meeP meeP meeP
meeP meeP meeP MeeP MeeP MeeP MeeP MEEP Meep Meep mEEp mEEp mEEp MEEP
Meep MeeP MEEP MEEP mEEp mEEp mEEp MEEP Meep MeeP MEEP meeP meeP meeP
MEEP Meep Meep MEEP mEEp mEEp mEEp MEEP MeeP MeeP MeeP MeeP MeeP MeeP
MEEP meeP MeeP MEEP Meep Meep mEEp MEEP Meep MEEP _
#include<stdio.h>
#define  while(*m) {
#define  {int (_c) = getchar(); if(_c!=EOF) *m=_c; }
#define  putchar(*m);
#define  --*m;
#define  ++*m;
#define  m-=1;
#define  m+=1;
#define  }
#define _ return 0;}
int mem[1048576];int main(){register int*m=mem;
                       
                       
                       
                       
            _
#include<stdio.h>
#define 🌜 while(*m) {
#define 🌝 {int (_c) = getchar(); if(_c!=EOF) *m=_c; }
#define 🌚 putchar(*m);
#define 👇 --*m;
#define 👆 ++*m;
#define 👈 m-=1;
#define 👉 m+=1;
#define 🌛 }
#define _ return 0;}
int mem[1048576];int main(){register int*m=mem;
👆 🌜 👉 🌜 👈 👇 🌜 🌛 👉 👆 🌜 👉 👆 👆 👆 👉 🌜 👆 👆 👆 👆 👆 👆 👆
👆 👆 👆 👆 👉 🌛 🌜 👉 🌛 👇 🌜 👈 🌛 👉 👇 🌛 🌛 👆 👆 👆 👆 👆 👆 👆
👆 👆 👆 👈 🌛 👉 👉 👉 👉 👉 👉 👇 👇 👇 👇 🌚 👈 👈 👆 👆 👆 🌚 👈 👇
🌚 🌚 👆 👆 👆 🌚 👈 👇 🌚 👉 👉 👉 🌚 👈 👈 🌚 👆 👆 👆 🌚 👇 👇 👇 👇
👇 👇 🌚 👉 👇 🌚 👈 👈 👆 🌚 👈 🌚 _

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.