Professional development on Brainfuck

Do you want to drive your favorite computer science teacher crazy or be forever known as “special” among your colleagues immediately after your (immediate) dismissal?

Read below about the patented method.

You are observing a real Pandora's box, which the author has kindly opened for you.

You are witnessing the real thing. Pandora's boxwhich the author kindly opened for you.

What kind of crap is this?

Okay, let's say you don't have a direct relationship with the IT industry or aren't directly involved in software development.

Perhaps you are a beginner web developer who only knows how to do landing pages and business card sites (for now) – in other words, you have never heard of esoteric programming languages ​​and their brightest representative:

Brainfuck – one of esoteric programming languagesinvented Urban Muller (Ger. Urban Müller) in 1993is known for its minimalism. The name of the language can be translated into Russian as brain drainit is directly derived from the English expression brainfuck (brain – brain, fucktakeaway), i.e. mess around. The language has eight commands, each of which is written with one symbol. The source code of the program in Brainfuck is a sequence of these characters without any additional syntax.

I understand that it is difficult to immediately perceive the phrase “extreme minimalism» in relation to the programming language, so here is a small illustration:

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

Yes, this is the source code. It is also an extreme trash and horror, created specifically to blow the minds of programmers:

>++.

V cell 1 adding 2 to 70 and printing the ASCII code 72, i.e. the letter “N“.

>+.

V cell 2 adding 1 to 100 = 101, printing the letter “e»

+++++++..

in the same cell add 7 to 101 = 108, print “l» twice

+++.

in the same cell add 3 to 108 = 111, print “o»

So further and the like:

Brainfuck It is almost never used for practical programming (except for the works of individual enthusiasts), but is used mainly for puzzles and problems for competitions.

Here's a tutorial video for you to dive deeper:

Now (if you are a developer and are more or less savvy) – go back to the screenshot at the beginning of the article and think about it What It was the author who prepared it for you:)

Transpilers, interpreters and compilers

A moment of background before we dive into the hellish abyss of development, for the best at least some understanding of the topic.

Let's start with transpiler:

A source-to-source translator, source-to-source compiler (S2S compiler), transcompileror transpiler is a type of translator that takes the source code of a program written in a programming language as its input and produces an equivalent source code in the same or a different programming language. A source-to-source translator converts between programming languages ​​that operate at approximately the same level of abstractionwhile traditional compiler translates from a higher level programming language to a lower level programming language.

To put it simply, a transpiler is a special compiler that converts source code in one language into source code in another language.

It is very often used in practice, including in modern and extremely popular languages ​​such as Java or .NET.

Most often, this technique is used to pass off “wishful thinking” as reality; for example, generics and closures in Java are implemented using a transpiler.

Now about interpreter:

In computer sciencean interpreter is a computer program that directly executes instructions written in a programming or scripting languagewithout requiring them previously to have been compiled into a machine language program.

In essence, this is a kind of “guest worker from the software world” who himself does not know how to do anything. other than waving a broombut it executes sequential commands perfectly.

And finally, “his majesty” compiler:

In computinga compiler is a computer program that translates computer code written in one programming language (the source language) into another language (the target language). The name “compiler” is primarily used for programs that translate source code from a high-level programming language to a low-level programming language (eg assembly language, object codeor machine code) to create an executable program.[1][2]

Yes, it is “the very program with which other programs are created”, in the original sense of the word.

Almost any compiler is a very complex program, even just using which requires some training. And the task of creating a compiler from scratch is a subject for study in higher technical institutions and the lot of only experienced professionals.

Now combine in your imagination a crazy esoteric programming language and a bundle of a transpiler and compiler – for running and demonstrating.

Let me explain in a little more detail:

programming on Brainfuck itself insanely hard and you won't get anything further than “Hello, World” until you go crazybut if you use a transpiler, it becomes possible to write code in C (or a C-like language) and much more interesting things.

For example, you can take your homework Fibonacci numbers:

include "std.bfx"    

function main()
{
    prints("f[0] = ");
    let a = scand();

    prints("f[1] = ");
    let b = scand();

    prints("Up to n = ");
    let n = scand();

    prints("0: "); printd(a); endl();
    for (let i = 0; i != n - 1; ++i)
    {
        printd(i + 1); prints(": ");
        printd(b); endl();
        
        let tmp = b; 
        b += a;
        a = tmp;
    }

    printd(n); prints(": ");
    printd(b); endl();
}

And submit it in this form (fragment):

>>>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>[-]++++++++++++++++++++++++++++++++++++++++++++++++.>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++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>>+>+<<<<-]>>>>[<<<<+>>>>-]<[<[-]>[-]]<<<[>>>>>[-]+>[-]+>[-]>>[-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-]<<[-]>>>[-]<<<<<[>>+>>>+<<<<<-]>>>>>[<<<<<+>>>>>-]<<<[>[<<[-]+>>[-]]<[-]]>>>>[-]>[-]<<<<<<[>>>>>+>+<<<<<<-]>>>>>>[<<<<<<+>>>>>>-]>[-]+>[-]>[-]<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<[<[-]>[-]]<<<[>>>>>,>[-]>[-]<<[>+>+<<-]>>[<<+>>-]<<<<<<<[-]]>>[[-]]<<<<<<<<<[-]>[-]>>>>>>>>>>>>[<<<<<<<<<<<<<+>+>>>>>>>>>>>>-]<<<<<<<<<<<<[>>>>>>>>>>>>+<<<<<<<<<<<<-]<<<<<<[-]]>>[[-]]>>>>>[-]>>[-]>[-]<<<

The full version takes ~1.6Mb in one line, imagine how happy your favorite teacher will be with such an original work?

The main thing is that you are not found later.

Grimoire

Since programmers are mostly “special” guys, it turned out that a lot of different and interesting things have been implemented for the Brainfuck language. Below is a short overview of the most crazy tools that we will use in the name of the moon for the test project.

Bfpy

Author of this wonderful project declared a personal jihad against common sense:

BFPY is an alternative Python runtime that uses Brainfuck as a bytecode.

For those who haven't yet appreciated the full power of the quote above, I'll give you a small code example:

#!/usr/bin/env python3.4
# coding: utf-8

from bfpy.instruction import Instruction
from bfpy.bytecode import Bytecode
from bfpy.machine import Machine

def test(x,y):
    return x + y 

def main():
    bc = Bytecode.from_function(test, x=40,y=2)   #42
    print(bc)
    vm = Machine(bc)
    vm.run()
    print(vm.current)

if __name__ == '__main__':
    main()

Like this This looks like in work:

Now let's analyze the logic a little, for better understanding. This wonderful line generates the Brainfuck code from bytecode Python(!):

bc = Bytecode.from_function(test, x=40,y=2)   #42

What you see in the screenshot above, the following instruction prints:

print(bc)

Next, this block interprets the Brainfuck code and prints:

vm = Machine(bc)
vm.run()
print(vm.current)

All this joy fits perfectly into, for example, a Python script that works with neural networks, thereby adding a new dimension of meaning to the word “blackbox,” which ML developers love to use to cover up their failures.

Unfortunately this project is very far from completion:

For instance, BFPY can only translate arithmetic operations (addition, subtraction, multiplication, floor division and power). There is still a lot of features to implement for it to be a proper Python runtime alternative

So if you have an extra $1 million and hatred of humanity — you can invest in this wonderful project. The money will go to the author for treatment in the best madhouse.

Brainfix

Next project went a little further in his madnessso it is with its help that we will make a test application:

BrainFix is ​​a compiler/language that takes a C-style language (although the syntax slowly evolved to something very close to Javascript for some reason) and compiles this into BrainF*ckan esoteric programming language consisting of only 8 operations.

We take the source code and try to compile it:

git clone https://github.com/jorenheit/brainfix
cd brainfix
make

Unfortunately, the following error will appear during assembly:

To fix it, just comment out the selection condition:

ifeq ($(GAMING_MODE_AVAILABLE),1)

The result should be like this:

CC=g++
CFLAGS= -c -O3 -Wall --std=c++2a -fmax-errors=2 #-Wfatal-errors
SOURCES=interpreter/bfint.cc interpreter/main.cc

OBJECTS=$(SOURCES:.cc=.o)
EXECUTABLE=bfint

all:	$(SOURCES) $(EXECUTABLE) 

#ifeq ($(GAMING_MODE_AVAILABLE),1)
#$(EXECUTABLE):$(OBJECTS)
#	$(CC) $(OBJECTS) -o ../$@ -lncurses
#
#.cc.o:
#	$(CC) $(CFLAGS) -DUSE_CURSES lt; -o $@ 
#
#else

$(EXECUTABLE):$(OBJECTS)
	$(CC) $(OBJECTS) -o ../$@

.cc.o:
	$(CC) $(CFLAGS) lt; -o $@ 
#endif

After editing, the assembly is completed successfully:

As a result of the build, a ready binary will appear in the root directory bfxwe launch:

Let's try to assemble a test example:

./bfx -o program.bf -O1 -I ./std -t int16 bfx_examples/hello.bfx

This is what the result looks like:

To check the correctness, insert the received Brainfuck code into online interpreter:

There's more many interesting examplesbut large programs will take a noticeably long time to compile into Brainfuck code, keep that in mind.

But this is far from the end.

BFC

How succinctly describes your project author: “an industrial-grade Brainfuck compiler” and he at the same time not lying at all:

bfc includes an extensive range of optimisations. This page discusses the techniques used, and gives examples of BF programs that are transformed in each case.

This thing is written in Rust, of course (did you really doubt it?) and now we will use it.

Let's take the source code:

git clone https://github.com/Wilfred/bfc.git

It is assembled with the following command:

RUSTFLAGS='-L /usr/local/lib' cargo build --release

Since this is on FreeBSD (did I forget to mention that?), you need to specify the path using the RUSTFLAGS environment variable /usr/local/libwhich for some reason is not taken into account by the cargo assembler. Also, the machine must have Rust itself and the 14th version of LLVM.

But now you also know how to use Rust on FreeBSD, I hope this knowledge will help you build a successful career in the IT industry and not set them on the path of extreme terrorism.

As a result of successful assembly in the catalog ./target/debug a ready to use binary will appear bfc:

So what are we going to do with the Brainfuck optimizing compiler?

Of course, use it for everything good and wonderful:

./target/release/bfc /opt/work/tmp/brainfix/program.bf 

This is the same “Hello world!” example generated above from C-like code that was turned into a real binary by a wonderful optimizing compiler:

Total

Let's repeat the whole chain again for better memorization:

C-like language code → Brainfix transpiler → Bfc compiler → ready binary

What is the difference from regular development:

You hand over calmly as a lab code on Brainfuck that will compile into an executable binary and fall under the term “source code”.

Of course, your favorite teacher will be absolutely delighted, trying to understand this code, and you will not even see any difficulties, since the real code with readable logic will remain at the transpiler level.

Isn't it fun?

Because of such cute pranks, my former teachers still remember me and turn pale when they hear my last name.

Well, if you are asked to write a test assignment, foolishly inserting the phrase “in any programming language you know” into the statement – now you know how and what to hand it in. You are unlikely to get the job, but you will have a lot of fun.

P.S.

This is a heavily censored version of the article, trashy original which is available on our blog.

Of course, all the information presented is provided for entertainment purposes only; you should not use such technologies in practice in real life.

But if it so happens that similar methods were used against your project or company, write and we will help.

0x08 Software

We are a small team of IT industry veterans, we create and refine a wide variety of software, our software automates business processes on three continents, in a wide variety of industries and conditions.

Bringing it to life long dead, we fix what never worked and we create impossible — then we talk about it in our articles.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *