Hacker News new | past | comments | ask | show | jobs | submit login
I wrote a Brainfuck to Go compiler (kabirgoel.com)
36 points by kabirgoel on June 7, 2022 | hide | past | favorite | 22 comments



Meanwhile, a Go to Brainfuck compiler remains uncharted territory.

(If you are genuinely curious, there is already a BASIC-to-Brainfuck compiler [1] and a C-to-Brainfuck compiler [2].)

[1] https://esolangs.org/wiki/BFBASIC

[2] https://esolangs.org/wiki/C2BF (and I think I've seen at least two more)


> a Go to Brainfuck compiler remains uncharted territory.

Not at all... Just compile your Go code to WebAssembly [1], then to C [2], and finally to BF [3].

[1] https://binx.io/2022/04/22/golang-webassembly/

[2] https://webassembly.github.io/wabt/doc/wasm2c.1.html

[3] https://esolangs.org/wiki/C2BF


The real brainfuck is in the toolchain.


I wonder how the code would look like if we compile the resulting BF code back to Go with the posted compiler.


You will probably want to go back to BF instead.


Related to this I used go to compile brainfuck to x86 assembly:

https://github.com/skx/bfcc

That was a fun project I challenged myself to complete in a weekend.


I might be wrong, but is this not a transpiler instead of a compiler?


Back in University, I had a Programming Languages professor who spent an entire lecture ranting about the term transpiler and how ridiculous he thought it was. Although I never shared his distaste for the term to the same degree, I anyways felt somewhat kindred with him on it. It's a useful term if you want to draw a distinction (for whatever reason), between moving from a human-readable language to a computer-readable one (compiling) vs moving from a human readable language to a different human readable one (transpiling), but I've never seen a reason to draw such a distinction.


Well technically you can read those computer-readable ones too. And even become somewhat proficient. It's just that it's "harder". And as a second trick, a computer can even "read" the human-readable languages as well :)

I don't know if there's any actual good distinction to draw that won't have problems. I'd be interested to listen to people theorize on what the distinction is.


Would LLVM IR be something you compile or transpile to?


It's compilation all the way down. I write C#, and it is compiled to CIL, which gets further compiled via the JIT into asm.

CIL, or JVM bytecode, or LLVM IR is just one more step in the compilation chain.


The word I'd use to describe a program that does what the OP does is indeed "transpiler", but it's not technically incorrect to call it a compiler.

The textbook definition of a compiler from A to B is any program that takes as input a program PA written in A and gives as output a program PB written in B, such that for all valid inputs I and modulo the undefined behaviors of A and B, it holds that PA(I) = PB(I).

So you could write a Java program that turns C++ into Python and it would technically qualify as a compiler.


For me, the word "transpiler" has the connotation that it would translate more or less directly from one language to another, without going very deep.

If the code is transformed fully down into intermediate representation (such as LLVM-IR or graph form), processed in that form and then transformed from that into a target language (be it machine language or something else), then it is "compilation".


Author here. You’re correct. I spent about 20 minutes mulling over whether to call it a transpiler for the purposes of the post, but "compiler" felt more appropriate. Technically, it could even be called a decompiler since it's going from low to high level. But ultimately even that's a compiler. :)


Well I must be wrong then, I was going to ask if it is not called a cross compiler? [1]

[1] https://en.wikipedia.org/wiki/Cross_compiler


... and wouldn't the most useful cross compiler to the world be COBOL to Python (or whatever)? "There are over 220 billion lines of COBOL in existence, a figure which equates to around 80% of the world's actively used code. There are estimated to be over a million COBOL programmers in the world today." [1]

[1] https://blog.codinghorror.com/cobol-everywhere-and-nowhere/


Neither the source nor the target language are JS, the project isn't webdev-adjacent, and the author isn't trying to make up a cool-sounding title for an early-computing magazine, so I'm not sure why it would be!


A transpiler is just a piece of software that translates code to code. A compiler is a transpiler that targets machine code and usually builds an executable through various pipelines, e.g. optimization passes etc.

Transpilers aren't limited to JS nor web dev. There are various other transpilers/compilers such as nimlangs compiler that can target C.


A compiler and a transpiler are the exact same thing, technically; the words differ in their connotations. And the communities you participate in definitely inform word choice.

(Parts of the Nim community are famously insistent that they have a compiler, not a transpiler!)


It's the other way around. A transpiler is a compiler that compiles code from one to another high level language. Compiler is the more general term here.

The distinction is almost arbitrary and rarely used until recently I think. It would be more useful if "transpiler" had some well defined property (like isomorphism) that distinguishes it from a compiler other than targeting a "high level" language. Are C, JVM bytecode, WASM, LLVM IR etc "high level" or not?

It gets even more confusing. In the JS world "transpiling" is for example used to describe the process of rewriting code from one version of the spec to another for compatibility reasons. What does that say about "npm fix" or "go fix"? Are those transpilers too? Does that mean they are compilers? What about a Lisp macro? An SQL query builder? Some of these do more involved things than what is called "transpiling" in the JS world.


I can never take Brainfuck seriously because it has such a juvenile name.


Fortunately, it's not really a language that wants you to take it seriously, either.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: