1 then 1 else 0". I have to generate byte code for such expression. I have AST which looks like: --..." /> 1 then 1 else 0". I have to generate byte code for such expression. I have AST which looks like: --..." /> 1 then 1 else 0". I have to generate byte code for such expression. I have AST which looks like: --..."/>

visitors - dynamic change of traverse type which depends on node

77 views Asked by At

Let's consider such syntax: "case when a > 1 then 1 else 0". I have to generate byte code for such expression. I have AST which looks like:

       -----------------------case----------------
       |                       |                 |
       |                       |                 |
    whenthen-----           whenthen            else
       |        |              |                 |
       |        |              |                 |
      when    then           .....      Numerical expression
       |        |
       |Numerical expression
Boolean expression

As soon as I start writes on paper byte code for such expression, it' starts to be clear that code generation differs for arithmetical expressions and label and jump code generation. Traverse type for numerical or boolean expression is different than for case when syntax so I need to switch between traverse algorithm depends on currently visited node.

It really confusing me, how to implement visitor pattern that can change traverse type dynamically and do not move traversing to accept() methods. I would like to keep traverse algorithm decoupled from nodes. How such problem should be solved properly, how compiler-makers solve it. Is there anything I could use to make this problem easier to solve?

0

There are 0 answers