Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
A simple procedural macro that turns a Brainfuck program into Rust (github.com/huonw)
38 points by steveklabnik on May 9, 2014 | hide | past | favorite | 13 comments


If you find this interesting, you should check out RustLex, which makes writing a lexer in Rust incredibly simple through judicious use of Rust macros:

https://github.com/LeoTestard/rustlex


Does anyone have an example? I don't have any of the toolchain, and it seems like a lot to do to see a compiled example.


Well, you'd never see the compiled example. You'd compile the example in the README, and it'd give you a binary. It's a macro, not like CoffeeScript.

That said, you could view the expanded output if you were curious. You'd pass `--pretty expanded` to `rustc`. The pretty printer isn't great yet, though. I would run it for you and give you a gist, but I'm not at a computer with the toolchain either.


The example in the readme: http://ix.io/cdR

Emitted llvm ir doesn't look much more compact either: http://ix.io/cdS

Clearly there is still plenty of room for optimizing brainfuck compilers!


Yeah, the implementation is literally just a token-by-token transliteration into Rust, just hoping that LLVM makes it go fast (and it does go pretty fast, e.g. it's faster than bff4[1], which Esolangs[2] touts as the fastest interpreter). The Rust output & optimised IR clearly show there's room for improvement, though.

[1]: http://mazonka.com/brainf/

[2]: http://esolangs.org/wiki/Brainfuck_implementations#Optimizin...


Eh, compile with -O and let LLVM optimize it.


Is `rustc --opt-level 3 --emit ir` not right?


The optimization level is given to LLVM. The IR itself should be the same for all optimization levels AFAIK.


Almost all LLVM optimisations map IR to IR, and --emit=ir prints the final IR just before codegen (i.e. after optimisations happen).


Oh? I was under the impression it was the IR that was given to LLVM to begin with. Interesting.


Oh, thanks, I thought it compiled to rust code, I didn't realize it compiled to a binary.


Macros expand to Rust code, but not as a separate file or anything. It's a form of meta programming.


Amazing.




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

Search: