What are some well-known strategies for generating bytecode based on foundations from this anstract syntax tree (AST)?
Consider this expression 1 + 2 - 3 * 4 / 5 4/5 and its form AST:
bin_exp(-) bin_exp(+) num_exp(1) num_exp(2) bin_exp(/) bin_exp(*) num_exp(3) num_exp(4) num_exp(5)
I'm struggling to convert AST to the appropriate bytecode procedurally. So far, I have found only one article in which he only briefly talks about this. My interpretation of what he is trying to say ...
int ridx; // register index function visit_exp(exp) { switch (exp) { case bin_exp: visit_exp(exp.left); visit_exp(exp.right); printf("add %i, %i -> %i\n", ridx - 2, ridx - 1, ridx); // save ridx, as it contains the result break; case num_exp: printf("mov %i -> %i\n", ridx, exp.value); break; } }
Please give me a hand, thanks.
compiler-construction bytecode code-generation abstract-syntax-tree
ains
source share