19 ms·
Do you have any good resources on how compliers actually work ? I imagine most of the work is in creating the complier,you could just fork the base language ad
by 999900000999 5y ago
Do you have any good resources on how compliers actually work ?
I imagine most of the work is in creating the complier,you could just fork the base language add enough features to it. This is a gross oversimplification, but I believe that was the original idea for c++, right?
- Someone 5y agoWho’s talking compiler? An interpreter typically is a bit easier. If you don’t want utmost speed, compilers can be easy, too, though. For version one, forget about what the textbooks say about separating tokenizing from parsing from optimizing from code generation, and just eagerly emit assembly instructions as soon as you reasonably can. Don’t worry about short or long branches, use only one or two registers, give each local its own place on the stack, even if they could use the same location, don’t keep intermediates in registers (so “a = a + 2 b; a = a + 1”* could compile to “load b, load 2, multiply, add a, store a, load a, load 1, add, store a”, without even attempting to remove the redundant “store a, load a” pair or without using inc instead of ”load 1; add” those are for version 2) etc. If you pick the right language, you can generate the code in a single pass, possibly with some after-the-fact patching to fill in jump targets. Pascal is famous for that (modules define types and functions before they are used, so when you encounter a function name, you can immediately check whether their arguments are of the correct types, and then generate the function call)