3 ms·
It's still a useful generic exercise for beginners. Although I recommend starting with a simpler syntax like sexpr, you can get more practice with less code. F
by weird_user 3y ago
It's still a useful generic exercise for beginners.
Although I recommend starting with a simpler syntax like sexpr, you can get more practice with less code.
For a deeper exercise, I recommend compiling to machine code instead of stopping at bytecode [1]; you can learn assembly along the way, which is more satisfying than compiling to C.
[1]: https://github.com/byo-books/pretty_laughable_lang/ https://github.com/byo-books/pretty_laughable_lang/
- orthoxerox 3y agoI agree that compiling to machine code is super satisfying, but there's a lot of upfront work you have to do before you can run your first "hello world": architectures, calling conventions, object file formats, linking. That's why I no longer object to compiler tutorials stopping at assembly or even C, as long as the phases not present in interpreter tutorials like Bob Nystrom's brilliant one are non-trivial: type checking, optimizations, lowering.
- samsquire 3y agoOne thing you can do is implement a JIT compiler. Here's Martin Jacob's code to execute arbitrary memory. If you malloc a byte array and insert opcodes directly at indexes, you can construct machine code and execute it. https://gist.github.com/martinjacobd/3ee56f3c7b7ce621034ec3ecbc8e13f1/fe7e78b89e1581ff15c4d2797662516f281a33bd https://gist.github.com/martinjacobd/3ee56f3c7b7ce621034ec3e... Since your C program is already in memory, you have access to the C standard library and don't have to worry about linking or object formats but you'll have to worry about parameter passing and FFI. My toy JIT compiler based on this idea is here https://github.com/samsquire/compiler https://github.com/samsquire/compiler but it is incomplete.
- grumpyprole 3y agoCompiling to LLVM IR is much easier than machine code and would still be satisfying I think.