13 ms·
I wrote a Brainfuck to Go compiler
- AdvicePlz 4y agoI might be wrong, but is this not a transpiler instead of a compiler?
- qsort 4y agoThe 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.
- naniwaduni 4y agoNeither 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!
- c0balt 4y agoA 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.
- naniwaduni 4y agoA 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!)
- dgb23 4y agoIt'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.
- vsnf 4y agoBack 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.
- ethanwillis 4y agoWell 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.
- Findecanor 4y agoFor 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".
- stevenjgarner 4y agoWell 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 https://en.wikipedia.org/wiki/Cross_compiler
- stevenjgarner 4y ago... 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/ https://blog.codinghorror.com/cobol-everywhere-and-nowhere/
- kabirgoel 4y agoAuthor 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. :)
- lifthrasiir 4y agoMeanwhile, 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 https://esolangs.org/wiki/BFBASIC [2] https://esolangs.org/wiki/C2BF https://esolangs.org/wiki/C2BF (and I think I've seen at least two more)
- selcuka 4y ago> 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/ https://binx.io/2022/04/22/golang-webassembly/ [2] https://webassembly.github.io/wabt/doc/wasm2c.1.html https://webassembly.github.io/wabt/doc/wasm2c.1.html [3] https://esolangs.org/wiki/C2BF https://esolangs.org/wiki/C2BF
- scrame 4y agoThe real brainfuck is in the toolchain.
- panda88888 4y agoI wonder how the code would look like if we compile the resulting BF code back to Go with the posted compiler.
- selcuka 4y agoYou will probably want to go back to BF instead.
- stevekemp 4y agoRelated to this I used go to compile brainfuck to x86 assembly: https://github.com/skx/bfcc https://github.com/skx/bfcc That was a fun project I challenged myself to complete in a weekend.
- Hnrobert42 4y agoI can never take Brainfuck seriously because it has such a juvenile name.
- naniwaduni 4y agoFortunately, it's not really a language that wants you to take it seriously, either.