🧠 Brain transplant: transpile Brainfuck code to Typst

I made a new package called Brain transplant, which transpiles Brainfuck code to Typst :tada:

Transpiles? Brainfuck? WTF?

Brainfuck is a minimalist esoteric programming language that consists of only eight simple instructions yet is Turing-complete (which essentially means everything computable by a fully-fledged language on a computer is in principle computable by it and vice versa).

A transpiler is a program that translates code given in a certain programming language to equivalent code in another language.

In our case the package takes code written in Brainfuck and translates it into Typst, which can then be displayed, modified or run (evaluated); for example:

#import "@preview/brain-transplant:0.1.0": *

#raw(brain-transplant("+++++++[->,.<]", input: "Hi Mom!", evaluate: true))

The output is:

Hi Mom!

If we don’t evaluate the code and for the sake of brevity don’t include safe-mode assert functions (#raw(brain-transplant("+++++++[->,.<]", input: "Hi Mom!", evaluate: false, unsafe: true))), the output is:

let mem = array(range(0, 30000)).map(a => 0)
let p = 0
let input = "Hi Mom!"
let input-pos = 0
mem.at(p) = calc.rem-euclid(mem.at(p) + 1, 256)
mem.at(p) = calc.rem-euclid(mem.at(p) + 1, 256)
mem.at(p) = calc.rem-euclid(mem.at(p) + 1, 256)
mem.at(p) = calc.rem-euclid(mem.at(p) + 1, 256)
mem.at(p) = calc.rem-euclid(mem.at(p) + 1, 256)
mem.at(p) = calc.rem-euclid(mem.at(p) + 1, 256)
mem.at(p) = calc.rem-euclid(mem.at(p) + 1, 256)
while (mem.at(p) != 0) {
mem.at(p) = calc.rem-euclid(mem.at(p) - 1, 256)
p += 1
mem.at(p) = calc.rem-euclid(str.to-unicode(input.at(input-pos)), 256)
input-pos += 1
str.from-unicode(calc.rem-euclid(mem.at(p), 256))
p -= 1
}

All this has no real practical use, but it’s very amusing (mindbogglingly so even :exploding_head:) if you’re into programming, minimalism, computer science, recreational and artistic use of math and technology, formal rules, meta and the like.

Have fun with using package, reading the manual (which provides introduction, usage information and mind-blowing Brainfuck examples), and eating brains! :zombie:

8 Likes