3 ms·
Turning an expression tree into basic machine code is typically relatively straightforward if the target is stack-based. Basically you do a post-order traversal
by dukoid 4y ago
Turning an expression tree into basic machine code is typically relatively straightforward if the target is stack-based. Basically you do a post-order traversal. For leaf nodes, just push the value. For other nodes, obtain the arguments from the stack, perform the operation and push the result.
- evnix 4y agolet me teach you 4 years of physics and now you can build a rocket that goes to the moon. compiler books feel exactly like this, parsing something seems obvious but most compiler books devote 90% of their content to parsing, but how do you convert something from a class to binary code or how do you represent loops and modules, you don't even know where to begin. I figured out parsing pretty quickly and was able to build a working parser in a week, it took me a good few months to grasp converting that to x86 instructions.
- lloydatkinson 4y agoYes! Thank you! This is 100% what I mean. Especially higher level constructs like structs or classes as well. Emitting is for some reason even more of a dark art than parsing. How crazy is that?